8#include <kea_version.h>
37const uint32_t MAX_LEASE_ERRORS = 100;
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);
93 int existing = pid_file.
check();
95 if ((existing != 0) && (existing != getpid())) {
114 if (!lf_finish.
exists()) {
121 processLeases<Lease4, CSVLeaseFile4, Lease4Storage>();
123 processLeases<Lease6, CSVLeaseFile6, Lease6Storage>();
125 }
catch (
const std::exception& proc_ex) {
162 while ((ch = getopt(argc, argv,
":46dhvVWp:x:i:o:c:f:")) != -1) {
166 protocol_version_ = 4;
171 protocol_version_ = 6;
176 std::cout << getVersion(
false) << std::endl;
181 std::cout << getVersion(
true) << std::endl;
196 if (optarg == NULL) {
204 if (optarg == NULL) {
207 previous_file_ = optarg;
212 if (optarg == NULL) {
220 if (optarg == NULL) {
223 output_file_ = optarg;
228 if (optarg == NULL) {
231 finish_file_ = optarg;
236 if (optarg == NULL) {
239 config_file_ = optarg;
267 if (protocol_version_ == 0) {
271 if (pid_file_.empty()) {
275 if (previous_file_.empty()) {
279 if (copy_file_.empty()) {
283 if (output_file_.empty()) {
287 if (finish_file_.empty()) {
291 if (config_file_.empty()) {
297 std::cout <<
"Protocol version: DHCPv" << protocol_version_ << std::endl
298 <<
"Previous or ex lease file: " << previous_file_ << std::endl
299 <<
"Copy lease file: " << copy_file_ << std::endl
300 <<
"Output lease file: " << output_file_ << std::endl
301 <<
"Finish file: " << finish_file_ << std::endl
302 <<
"Config file: " << config_file_ << std::endl
303 <<
"PID file: " << pid_file_ << std::endl
309LFCController::usage(
const std::string& text) {
311 std::cerr <<
"Usage error: " << text << std::endl;
315 <<
" [-4|-6] -p file -x file -i file -o file -f file -c file" << std::endl
316 <<
" -4 or -6 clean a set of v4 or v6 lease files" << std::endl
317 <<
" -p <file>: PID file" << std::endl
318 <<
" -x <file>: previous or ex lease file" << std::endl
319 <<
" -i <file>: copy of lease file" << std::endl
320 <<
" -o <file>: output lease file" << std::endl
321 <<
" -f <file>: finish file" << std::endl
322 <<
" -c <file>: configuration file" << std::endl
323 <<
" -v: print version number and exit" << std::endl
324 <<
" -V: print extended version information and exit" << std::endl
325 <<
" -d: optional, verbose output " << std::endl
326 <<
" -h: print this message " << std::endl
331LFCController::getVersion(
const bool extended)
const{
332 std::stringstream version_stream;
334 version_stream << VERSION;
336 std::string db_version;
337 if (protocol_version_ == 4) {
339 }
else if (protocol_version_ == 6) {
343 version_stream <<
" (" << EXTENDED_VERSION <<
")";
344 if (!db_version.empty()) {
345 db_version =
"backend: " + db_version;
346 version_stream << std::endl << db_version;
350 return (version_stream.str());
353template<
typename LeaseObjectType,
typename LeaseFileType,
typename StorageType>
355LFCController::processLeases()
const {
360 if (lf_prev.exists()) {
367 if (lf_copy.exists()) {
378 .arg(lf_prev.getReadLeases() + lf_copy.getReadLeases())
379 .arg(lf_prev.getReads() + lf_copy.getReads())
380 .arg(lf_prev.getReadErrs() + lf_copy.getReadErrs());
383 .arg(lf_output.getWriteLeases())
384 .arg(lf_output.getWrites())
385 .arg(lf_output.getWriteErrs());
389 isc_throw(RunTimeFail,
"Unable to move output (" << output_file_
390 <<
") to complete (" << finish_file_
391 <<
") error: " << strerror(errno));
401 << previous_file_ <<
"' error: " << strerror(errno));
408 << copy_file_ <<
"' error: " << strerror(errno));
412 if (rename(finish_file_.c_str(), previous_file_.c_str()) != 0) {
414 <<
") to previous (" << previous_file_
415 <<
") error: " << strerror(errno));
420LFCController::startLogger(
const bool test_mode)
const {
446 spec.addOutputOption(option);
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when an unexpected error condition occurs.
static void write(LeaseFileType &lease_file, const StorageType &storage)
Write leases from the storage into a lease file.
static void load(LeaseFileType &lease_file, StorageType &storage, const uint32_t max_errors=0, const bool close_file_on_exit=true)
Load leases from the lease file into the specified storage.
static std::string getDBVersionInternal(Universe const &u)
Local version of getDBVersion() class method.
Exception thrown when the command line is invalid.
void fileRotate() const
Rotate files.
void launch(int argc, char *argv[], const bool test_mode)
Acts as the primary entry point to start execution of the process.
int getProtocolVersion() const
Gets the protocol version of the leases files.
LFCController()
Constructor.
std::string getFinishFile() const
Gets the finish file name.
~LFCController()
Destructor.
std::string getCopyFile() const
Gets the copy file name.
static const char * lfc_bin_name_
Defines the executable name, by convention this should match the executable name.
void parseArgs(int argc, char *argv[])
Process the command line arguments.
static const char * lfc_app_name_
Defines the application name, it may be used to locate configuration data and appears in log statemen...
std::string getPreviousFile() const
Gets the previous file name.
std::string getOutputFile() const
Gets the output file name.
Exceptions thrown when a method is unable to manipulate (remove or rename) a file.
void process(T start, T finish)
Process Specifications.
Provides input/output access to CSV files.
bool exists() const
Checks if the CSV file exists and can be opened for reading.
Exception thrown when an error occurs during PID file processing.
Class to help with processing PID files.
void write(int) const
Write the PID to the file.
void deleteFile() const
Delete the PID file.
std::string getLockname() const
Returns the path to the lock file.
int check() const
Read the PID in from the file and check it.
RAII device to handle a lock file to avoid race conditions.
bool isLocked()
Return the lock status.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
An abstract API for lease database.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
#define LOG_FATAL(LOGGER, MESSAGE)
Macro to conveniently test fatal output and log it.
std::string getConfigReport()
const isc::log::MessageID LFC_FAIL_PROCESS
const isc::log::MessageID LFC_START
const isc::log::MessageID LFC_TERMINATE
const isc::log::MessageID LFC_FAIL_PID_DEL
const isc::log::MessageID LFC_FAIL_ROTATE
const isc::log::MessageID LFC_RUNNING
const isc::log::MessageID LFC_WRITE_STATS
const isc::log::MessageID LFC_FAIL_PID_CREATE
isc::log::Logger lfc_logger("DhcpLFC")
Defines the logger used within LFC.
const isc::log::MessageID LFC_PROCESSING
const isc::log::MessageID LFC_ROTATING
const isc::log::MessageID LFC_READ_STATS
const std::string & getRootLoggerName()
Get root logger name.
void initLogger(const string &root, isc::log::Severity severity, int dbglevel, const char *file, bool buffer)
Run-time initialization.
int keaLoggerDbglevel(int defdbglevel)
Obtains logging debug level from KEA_LOGGER_DBGLEVEL.
isc::log::Severity keaLoggerSeverity(isc::log::Severity defseverity)
Obtains logging severity from KEA_LOGGER_SEVERITY.
Defines the logger used by the top-level component of kea-lfc.
Destination destination
Members.