8 #include <kea_version.h> 37 const uint32_t MAX_LEASE_ERRORS = 100;
45 const char* LFCController::lfc_app_name_ =
"DhcpLFC";
48 const char* LFCController::lfc_bin_name_ =
"kea-lfc";
50 LFCController::LFCController()
51 : protocol_version_(0), verbose_(false), config_file_(
""), previous_file_(
""),
52 copy_file_(
""), output_file_(
""), finish_file_(
""), pid_file_(
"") {
60 bool do_rotate =
true;
78 startLogger(test_mode);
86 if (pid_file.
check()) {
103 if (!lf_finish.
exists()) {
110 processLeases<Lease4, CSVLeaseFile4, Lease4Storage>();
112 processLeases<Lease6, CSVLeaseFile6, Lease6Storage>();
114 }
catch (
const std::exception& proc_ex) {
151 while ((ch = getopt(argc, argv,
":46dhvVWp:x:i:o:c:f:")) != -1) {
155 protocol_version_ = 4;
160 protocol_version_ = 6;
165 std::cout << getVersion(
false) << std::endl;
170 std::cout << getVersion(
true) << std::endl;
185 if (optarg == NULL) {
193 if (optarg == NULL) {
196 previous_file_ = optarg;
201 if (optarg == NULL) {
209 if (optarg == NULL) {
212 output_file_ = optarg;
217 if (optarg == NULL) {
220 finish_file_ = optarg;
225 if (optarg == NULL) {
228 config_file_ = optarg;
256 if (protocol_version_ == 0) {
260 if (pid_file_.empty()) {
264 if (previous_file_.empty()) {
268 if (copy_file_.empty()) {
272 if (output_file_.empty()) {
276 if (finish_file_.empty()) {
280 if (config_file_.empty()) {
286 std::cout <<
"Protocol version: DHCPv" << protocol_version_ << std::endl
287 <<
"Previous or ex lease file: " << previous_file_ << std::endl
288 <<
"Copy lease file: " << copy_file_ << std::endl
289 <<
"Output lease file: " << output_file_ << std::endl
290 <<
"Finish file: " << finish_file_ << std::endl
291 <<
"Config file: " << config_file_ << std::endl
292 <<
"PID file: " << pid_file_ << std::endl
298 LFCController::usage(
const std::string& text) {
300 std::cerr <<
"Usage error: " << text << std::endl;
304 <<
" [-4|-6] -p file -x file -i file -o file -f file -c file" << std::endl
305 <<
" -4 or -6 clean a set of v4 or v6 lease files" << std::endl
306 <<
" -p <file>: PID file" << std::endl
307 <<
" -x <file>: previous or ex lease file" << std::endl
308 <<
" -i <file>: copy of lease file" << std::endl
309 <<
" -o <file>: output lease file" << std::endl
310 <<
" -f <file>: finish file" << std::endl
311 <<
" -c <file>: configuration file" << std::endl
312 <<
" -v: print version number and exit" << std::endl
313 <<
" -V: print extended version information and exit" << std::endl
314 <<
" -d: optional, verbose output " << std::endl
315 <<
" -h: print this message " << std::endl
320 LFCController::getVersion(
const bool extended)
const{
321 std::stringstream version_stream;
323 version_stream << VERSION;
325 std::string db_version;
326 if (protocol_version_ == 4) {
327 db_version = Memfile_LeaseMgr::getDBVersion(Memfile_LeaseMgr::V4);
328 }
else if (protocol_version_ == 6) {
329 db_version = Memfile_LeaseMgr::getDBVersion(Memfile_LeaseMgr::V6);
331 if (!db_version.empty()) {
332 db_version =
"database: " + db_version;
334 version_stream << std::endl
335 << EXTENDED_VERSION << std::endl
339 return (version_stream.str());
342 template<
typename LeaseObjectType,
typename LeaseFileType,
typename StorageType>
344 LFCController::processLeases()
const {
349 if (lf_prev.exists()) {
350 LeaseFileLoader::load<LeaseObjectType>(lf_prev, storage,
356 if (lf_copy.exists()) {
357 LeaseFileLoader::load<LeaseObjectType>(lf_copy, storage,
363 LeaseFileLoader::write<LeaseObjectType>(lf_output, storage);
367 .arg(lf_prev.getReadLeases() + lf_copy.getReadLeases())
368 .arg(lf_prev.getReads() + lf_copy.getReads())
369 .arg(lf_prev.getReadErrs() + lf_copy.getReadErrs());
372 .arg(lf_output.getWriteLeases())
373 .arg(lf_output.getWrites())
374 .arg(lf_output.getWriteErrs());
379 <<
") to complete (" << finish_file_
380 <<
") error: " << strerror(errno));
390 << previous_file_ <<
"' error: " << strerror(errno));
397 << copy_file_ <<
"' error: " << strerror(errno));
401 if (rename(finish_file_.c_str(), previous_file_.c_str()) != 0) {
403 <<
") to previous (" << previous_file_
404 <<
") error: " << strerror(errno));
409 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
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.
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.