8 #include <kea_version.h> 37 const uint32_t MAX_LEASE_ERRORS = 100;
48 const char* LFCController::lfc_app_name_ =
"DhcpLFC";
51 const char* LFCController::lfc_bin_name_ =
"kea-lfc";
53 LFCController::LFCController()
54 : protocol_version_(0), verbose_(false), config_file_(
""), previous_file_(
""),
55 copy_file_(
""), output_file_(
""), finish_file_(
""), pid_file_(
"") {
63 bool do_rotate =
true;
81 startLogger(test_mode);
89 if (pid_file.
check()) {
106 if (!lf_finish.
exists()) {
113 processLeases<Lease4, CSVLeaseFile4, Lease4Storage>();
115 processLeases<Lease6, CSVLeaseFile6, Lease6Storage>();
117 }
catch (
const std::exception& proc_ex) {
154 while ((ch = getopt(argc, argv,
":46dhvVWp:x:i:o:c:f:")) != -1) {
158 protocol_version_ = 4;
163 protocol_version_ = 6;
168 std::cout << getVersion(
false) << std::endl;
173 std::cout << getVersion(
true) << std::endl;
188 if (optarg == NULL) {
196 if (optarg == NULL) {
199 previous_file_ = optarg;
204 if (optarg == NULL) {
212 if (optarg == NULL) {
215 output_file_ = optarg;
220 if (optarg == NULL) {
223 finish_file_ = optarg;
228 if (optarg == NULL) {
231 config_file_ = optarg;
259 if (protocol_version_ == 0) {
263 if (pid_file_.empty()) {
267 if (previous_file_.empty()) {
271 if (copy_file_.empty()) {
275 if (output_file_.empty()) {
279 if (finish_file_.empty()) {
283 if (config_file_.empty()) {
289 std::cout <<
"Protocol version: DHCPv" << protocol_version_ << std::endl
290 <<
"Previous or ex lease file: " << previous_file_ << std::endl
291 <<
"Copy lease file: " << copy_file_ << std::endl
292 <<
"Output lease file: " << output_file_ << std::endl
293 <<
"Finish file: " << finish_file_ << std::endl
294 <<
"Config file: " << config_file_ << std::endl
295 <<
"PID file: " << pid_file_ << std::endl
301 LFCController::usage(
const std::string& text) {
303 std::cerr <<
"Usage error: " << text << std::endl;
307 <<
" [-4|-6] -p file -x file -i file -o file -f file -c file" << std::endl
308 <<
" -4 or -6 clean a set of v4 or v6 lease files" << std::endl
309 <<
" -p <file>: PID file" << std::endl
310 <<
" -x <file>: previous or ex lease file" << std::endl
311 <<
" -i <file>: copy of lease file" << std::endl
312 <<
" -o <file>: output lease file" << std::endl
313 <<
" -f <file>: finish file" << std::endl
314 <<
" -c <file>: configuration file" << std::endl
315 <<
" -v: print version number and exit" << std::endl
316 <<
" -V: print extended version information and exit" << std::endl
317 <<
" -d: optional, verbose output " << std::endl
318 <<
" -h: print this message " << std::endl
323 LFCController::getVersion(
const bool extended)
const{
324 std::stringstream version_stream;
326 version_stream << VERSION;
328 std::string db_version;
329 if (protocol_version_ == 4) {
330 db_version = Memfile_LeaseMgr::getDBVersion(Memfile_LeaseMgr::V4);
331 }
else if (protocol_version_ == 6) {
332 db_version = Memfile_LeaseMgr::getDBVersion(Memfile_LeaseMgr::V6);
334 if (!db_version.empty()) {
335 db_version =
"database: " + db_version;
337 version_stream << std::endl
338 << EXTENDED_VERSION << std::endl
342 return (version_stream.str());
345 template<
typename LeaseObjectType,
typename LeaseFileType,
typename StorageType>
347 LFCController::processLeases()
const {
352 if (lf_prev.exists()) {
353 LeaseFileLoader::load<LeaseObjectType>(lf_prev, storage,
359 if (lf_copy.exists()) {
360 LeaseFileLoader::load<LeaseObjectType>(lf_copy, storage,
366 LeaseFileLoader::write<LeaseObjectType>(lf_output, storage);
370 .arg(lf_prev.getReadLeases() + lf_copy.getReadLeases())
371 .arg(lf_prev.getReads() + lf_copy.getReads())
372 .arg(lf_prev.getReadErrs() + lf_copy.getReadErrs());
375 .arg(lf_output.getWriteLeases())
376 .arg(lf_output.getWrites())
377 .arg(lf_output.getWriteErrs());
382 <<
") to complete (" << finish_file_
383 <<
") error: " << strerror(errno));
393 << previous_file_ <<
"' error: " << strerror(errno));
400 << copy_file_ <<
"' error: " << strerror(errno));
404 if (rename(finish_file_.c_str(), previous_file_.c_str()) != 0) {
406 <<
") to previous (" << previous_file_
407 <<
") error: " << strerror(errno));
412 LFCController::startLogger(
const bool test_mode)
const {
const isc::log::MessageID LFC_FAIL_PID_DEL
const std::string & getRootLoggerName()
Get root logger name.
bool exists() const
Checks if the CSV file exists and can be opened for reading.
void addOutputOption(const OutputOption &option)
Add output option.
isc::log::Logger lfc_logger("DhcpLFC")
Defines the logger used within LFC.
Exception thrown when an error occurs during PID file processing.
~LFCController()
Destructor.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
isc::log::Severity keaLoggerSeverity(isc::log::Severity defseverity)
Obtains logging severity from KEA_LOGGER_SEVERITY.
void process(T start, T finish)
Process Specifications.
const isc::log::MessageID LFC_RUNNING
An abstract API for lease database.
int keaLoggerDbglevel(int defdbglevel)
Obtains logging debug level from KEA_LOGGER_DBGLEVEL.
const isc::log::MessageID LFC_FAIL_PID_CREATE
std::string getOutputFile() const
Gets the output file name.
std::string getFinishFile() const
Gets the finish file name.
Class to help with processing PID files.
void deleteFile() const
Delete the PID file.
Exceptions thrown when a method is unable to manipulate (remove or rename) a file.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
const isc::log::MessageID LFC_FAIL_PROCESS
void launch(int argc, char *argv[], const bool test_mode)
Acts as the primary entry point to start execution of the process.
const isc::log::MessageID LFC_START
const isc::log::MessageID LFC_READ_STATS
int getProtocolVersion() const
Gets the protocol version of the leases files.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void parseArgs(int argc, char *argv[])
Process the command line arguments.
const isc::log::MessageID LFC_PROCESSING
static const char * lfc_bin_name_
Defines the executable name, by convention this should match the executable name. ...
Destination destination
Members.
const isc::log::MessageID LFC_FAIL_ROTATE
void write(int) const
Write the PID to the file.
static const char * lfc_app_name_
Defines the application name, it may be used to locate configuration data and appears in log statemen...
Exception thrown when the command line is invalid.
Defines the logger used by the top-level component of kea-lfc.
const isc::log::MessageID LFC_TERMINATE
const isc::log::MessageID LFC_ROTATING
const char *const config_report[]
void fileRotate() const
Rotate files.
std::string getPreviousFile() const
Gets the previous file name.
std::string getCopyFile() const
Gets the copy file name.
Provides input/output access to CSV files.
const char *const * lfc_config_report
std::string getConfigReport()
#define LOG_FATAL(LOGGER, MESSAGE)
Macro to conveniently test fatal output and log it.
void initLogger(const string &root, isc::log::Severity severity, int dbglevel, const char *file, bool buffer)
Run-time initialization.
const isc::log::MessageID LFC_WRITE_STATS
int check() const
Read the PID in from the file and check it.