8#include <kea_version.h>
29#include <sys/resource.h>
38const uint32_t MAX_LEASE_ERRORS = 100;
52 : protocol_version_(0), verbose_(false), config_file_(
""), previous_file_(
""),
53 copy_file_(
""), output_file_(
""), finish_file_(
""), pid_file_(
"") {
61 bool do_rotate =
true;
79 startLogger(test_mode);
94 int existing = pid_file.
check();
96 if ((existing != 0) && (existing != getpid())) {
112 static_cast<void>(setpriority(PRIO_PROCESS, 0, 4));
122 if (!lf_finish.
exists()) {
129 processLeases<Lease4, CSVLeaseFile4, Lease4Storage>();
131 processLeases<Lease6, CSVLeaseFile6, Lease6Storage>();
133 }
catch (
const std::exception& proc_ex) {
170 while ((ch = getopt(argc, argv,
":46dhvVWp:x:i:o:c:f:")) != -1) {
174 protocol_version_ = 4;
179 protocol_version_ = 6;
184 std::cout << getVersion(
false) << std::endl;
189 std::cout << getVersion(
true) << std::endl;
204 if (optarg == NULL) {
212 if (optarg == NULL) {
215 previous_file_ = optarg;
220 if (optarg == NULL) {
228 if (optarg == NULL) {
231 output_file_ = optarg;
236 if (optarg == NULL) {
239 finish_file_ = optarg;
244 if (optarg == NULL) {
247 config_file_ = optarg;
275 if (protocol_version_ == 0) {
279 if (pid_file_.empty()) {
283 if (previous_file_.empty()) {
287 if (copy_file_.empty()) {
291 if (output_file_.empty()) {
295 if (finish_file_.empty()) {
299 if (config_file_.empty()) {
305 std::cout <<
"Protocol version: DHCPv" << protocol_version_ << std::endl
306 <<
"Previous or ex lease file: " << previous_file_ << std::endl
307 <<
"Copy lease file: " << copy_file_ << std::endl
308 <<
"Output lease file: " << output_file_ << std::endl
309 <<
"Finish file: " << finish_file_ << std::endl
310 <<
"Config file: " << config_file_ << std::endl
311 <<
"PID file: " << pid_file_ << std::endl
317LFCController::usage(
const std::string& text) {
319 std::cerr <<
"Usage error: " << text << std::endl;
323 <<
" [-4|-6] -p file -x file -i file -o file -f file -c file" << std::endl
324 <<
" -4 or -6 clean a set of v4 or v6 lease files" << std::endl
325 <<
" -p <file>: PID file" << std::endl
326 <<
" -x <file>: previous or ex lease file" << std::endl
327 <<
" -i <file>: copy of lease file" << std::endl
328 <<
" -o <file>: output lease file" << std::endl
329 <<
" -f <file>: finish file" << std::endl
330 <<
" -c <file>: configuration file" << std::endl
331 <<
" -v: print version number and exit" << std::endl
332 <<
" -V: print extended version information and exit" << std::endl
333 <<
" -d: optional, verbose output " << std::endl
334 <<
" -h: print this message " << std::endl
339LFCController::getVersion(
const bool extended)
const{
340 std::stringstream version_stream;
342 version_stream << VERSION;
344 std::string db_version;
345 if (protocol_version_ == 4) {
347 }
else if (protocol_version_ == 6) {
351 version_stream <<
" (" << SOURCE_OF_INSTALLATION <<
")";
352 if (!db_version.empty()) {
353 db_version =
"backend: " + db_version;
354 version_stream << std::endl << db_version;
358 return (version_stream.str());
361template<
typename LeaseObjectType,
typename LeaseFileType,
typename StorageType>
363LFCController::processLeases()
const {
368 if (lf_prev.exists()) {
375 if (lf_copy.exists()) {
386 .arg(lf_prev.getReadLeases() + lf_copy.getReadLeases())
387 .arg(lf_prev.getReads() + lf_copy.getReads())
388 .arg(lf_prev.getReadErrs() + lf_copy.getReadErrs());
391 .arg(lf_output.getWriteLeases())
392 .arg(lf_output.getWrites())
393 .arg(lf_output.getWriteErrs());
397 isc_throw(RunTimeFail,
"Unable to move output (" << output_file_
398 <<
") to complete (" << finish_file_
399 <<
") error: " << strerror(errno));
409 << previous_file_ <<
"' error: " << strerror(errno));
416 << copy_file_ <<
"' error: " << strerror(errno));
420 if (rename(finish_file_.c_str(), previous_file_.c_str()) != 0) {
422 <<
") to previous (" << previous_file_
423 <<
") error: " << strerror(errno));
428LFCController::startLogger(
const bool test_mode)
const {
454 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.