lfc_controller.cc 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2015  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15
#include <lfc/lfc_controller.h>
16
#include <lfc/lfc_log.h>
17
#include <util/pid_file.h>
18
#include <exceptions/exceptions.h>
19 20 21 22 23
#include <dhcpsrv/csv_lease_file4.h>
#include <dhcpsrv/csv_lease_file6.h>
#include <dhcpsrv/memfile_lease_storage.h>
#include <dhcpsrv/lease_mgr.h>
#include <dhcpsrv/lease_file_loader.h>
24 25
#include <log/logger_manager.h>
#include <log/logger_name.h>
Shawn Routhier's avatar
Shawn Routhier committed
26
#include <config.h>
27

28
#include <iostream>
29 30
#include <sstream>
#include <unistd.h>
31
#include <stdlib.h>
32
#include <cerrno>
33 34

using namespace std;
35
using namespace isc::util;
36
using namespace isc::dhcp;
37
using namespace isc::log;
38

39 40 41 42 43
namespace {
/// @brief Maximum number of errors to allow when reading leases from the file.
const uint32_t MAX_LEASE_ERRORS = 100;
}; // namespace anonymous

44 45 46 47 48
namespace isc {
namespace lfc {

/// @brief Defines the application name, it may be used to locate
/// configuration data and appears in log statements.
49
const char* LFCController::lfc_app_name_ = "DhcpLFC";
50 51

/// @brief Defines the executable name.
52
const char* LFCController::lfc_bin_name_ = "kea-lfc";
53

54
LFCController::LFCController()
55
    : protocol_version_(0), verbose_(false), config_file_(""), previous_file_(""),
Shawn Routhier's avatar
Shawn Routhier committed
56
      copy_file_(""), output_file_(""), finish_file_(""), pid_file_("") {
57 58
}

59
LFCController::~LFCController() {
60 61 62
}

void
63
LFCController::launch(int argc, char* argv[], const bool test_mode) {
64
    bool do_rotate = true;
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79
    // It would be nice to set up the logger as the first step
    // in the process, but we don't know where to send logging
    // info until after we have parsed our arguments.  As we
    // don't currently log anything when trying to parse the
    // arguments we do the parse before the logging setup.  If
    // we do decide to log something then the code will need
    // to move around a bit.

    try {
        parseArgs(argc, argv);
    } catch (const InvalidUsage& ex) {
        usage(ex.what());
        throw;  // rethrow it
    }
80 81 82 83 84 85 86 87 88
    // If we are running in test mode use the environment variables
    // else use our defaults
    if (test_mode) {
        initLogger();
    }
    else {
        OutputOption option;
        LoggerManager manager;

89
        initLogger(lfc_app_name_, INFO, 0, NULL, false);
90

91 92 93 94 95
        // Prepare the objects to define the logging specification
        LoggerSpecification spec(getRootLoggerName(),
                                 keaLoggerSeverity(INFO),
                                 keaLoggerDbglevel(0));

Shawn Routhier's avatar
Shawn Routhier committed
96 97 98 99 100 101 102 103 104
        // If we are running in verbose (debugging) mode
        // we send the output to the console, otherwise
        // by default we send it to the SYSLOG
        if (verbose_) {
            option.destination = OutputOption::DEST_CONSOLE;
        } else {
            option.destination = OutputOption::DEST_SYSLOG;

        }
105 106 107 108 109 110 111 112

        // ... and set the destination
        spec.addOutputOption(option);

        manager.process(spec);
    }

    LOG_INFO(lfc_logger, LFC_START);
113

114 115
    // verify we are the only instance
    PIDFile pid_file(pid_file_);
116 117

    try {
118
        if (pid_file.check()) {
119
            // Already running instance, bail out
120
            LOG_FATAL(lfc_logger, LFC_RUNNING);
121 122
            return;
        }
123

124
        // create the pid file for this instance
125 126
        pid_file.write();
    } catch (const PIDFileError& pid_ex) {
127
        LOG_FATAL(lfc_logger, LFC_FAIL_PID_CREATE).arg(pid_ex.what());
128 129 130
        return;
    }

131 132 133 134 135
    // If we don't have a finish file do the processing.  We
    // don't know the exact type of the finish file here but
    // all we care about is if it exists so that's okay
    CSVFile lf_finish(getFinishFile());
    if (!lf_finish.exists()) {
136 137 138
        LOG_INFO(lfc_logger, LFC_PROCESSING)
          .arg(previous_file_)
          .arg(copy_file_);
139 140

        try {
141
            if (getProtocolVersion() == 4) {
142 143 144 145 146 147
                processLeases<Lease4, CSVLeaseFile4, Lease4Storage>();
            } else {
                processLeases<Lease6, CSVLeaseFile6, Lease6Storage>();
            }
        } catch (const isc::Exception& proc_ex) {
            // We don't want to do the cleanup but do want to get rid of the pid
148
            do_rotate = false;
149
            LOG_FATAL(lfc_logger, LFC_FAIL_PROCESS).arg(proc_ex.what());
150
        }
151
    }
152

153
    // If do_rotate is true We either already had a finish file or
154 155
    // were able to create one.  We now want to do the file cleanup,
    // we don't want to return after the catch as we
156
    // still need to cleanup the pid file
157
    if (do_rotate) {
158
        LOG_INFO(lfc_logger, LFC_ROTATING);
159

160
        try {
161
            fileRotate();
162
        } catch (const RunTimeFail& run_ex) {
163
          LOG_FATAL(lfc_logger, LFC_FAIL_ROTATE).arg(run_ex.what());
164
        }
165
    }
166 167

    // delete the pid file for this instance
168 169 170
    try {
        pid_file.deleteFile();
    } catch (const PIDFileError& pid_ex) {
171
          LOG_FATAL(lfc_logger, LFC_FAIL_PID_DEL).arg(pid_ex.what());
172
    }
173

174
    LOG_INFO(lfc_logger, LFC_TERMINATE);
175 176 177
}

void
178
LFCController::parseArgs(int argc, char* argv[]) {
179 180
    int ch;

181 182
    opterr = 0;
    optind = 1;
183
    while ((ch = getopt(argc, argv, ":46dvVp:x:i:o:c:f:")) != -1) {
184
        switch (ch) {
Shawn Routhier's avatar
Shawn Routhier committed
185
        case '4':
186
            // Process DHCPv4 lease files.
187
            protocol_version_ = 4;
188 189
            break;

Shawn Routhier's avatar
Shawn Routhier committed
190
        case '6':
191
            // Process DHCPv6 lease files.
192
            protocol_version_ = 6;
193 194
            break;

Shawn Routhier's avatar
Shawn Routhier committed
195
        case 'v':
196
            // Print just Kea vesion and exit.
Shawn Routhier's avatar
Shawn Routhier committed
197
            std::cout << getVersion(false) << std::endl;
198 199
            exit(EXIT_SUCCESS);

Shawn Routhier's avatar
Shawn Routhier committed
200
        case 'V':
201
            // Print extended  Kea vesion and exit.
Shawn Routhier's avatar
Shawn Routhier committed
202
            std::cout << getVersion(true) << std::endl;
203 204
            exit(EXIT_SUCCESS);

Shawn Routhier's avatar
Shawn Routhier committed
205 206 207 208 209 210
        case 'd':
            // Verbose output.
            verbose_ = true;
            break;

        case 'p':
211 212 213 214 215 216 217 218 219
            // PID file name.
            if (optarg == NULL) {
                isc_throw(InvalidUsage, "PID file name missing");
            }
            pid_file_ = optarg;
            break;

        case 'x':
            // Previous (or ex) file name.
Shawn Routhier's avatar
Shawn Routhier committed
220
            if (optarg == NULL) {
221
                isc_throw(InvalidUsage, "Previous (ex) file name missing");
Shawn Routhier's avatar
Shawn Routhier committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
            }
            previous_file_ = optarg;
            break;

        case 'i':
            // Copy file name.
            if (optarg == NULL) {
                isc_throw(InvalidUsage, "Copy file name missing");
            }
            copy_file_ = optarg;
            break;

        case 'o':
            // Output file name.
            if (optarg == NULL) {
                isc_throw(InvalidUsage, "Output file name missing");
            }
            output_file_ = optarg;
            break;

        case 'f':
243
            // Finish file name.
Shawn Routhier's avatar
Shawn Routhier committed
244 245 246 247 248 249 250
            if (optarg == NULL) {
                isc_throw(InvalidUsage, "Finish file name missing");
            }
            finish_file_ = optarg;
            break;

        case 'c':
251
            // Configuration file name
Shawn Routhier's avatar
Shawn Routhier committed
252 253 254 255 256 257
            if (optarg == NULL) {
                isc_throw(InvalidUsage, "Configuration file name missing");
            }
            config_file_ = optarg;
            break;

258
        case 'h':
Shawn Routhier's avatar
Shawn Routhier committed
259
            usage("");
260 261 262 263 264 265 266 267 268
            exit(EXIT_SUCCESS);

        case '?':
            // Unknown argument
            isc_throw(InvalidUsage, "Unknown argument");

        case ':':
            // Missing option argument
            isc_throw(InvalidUsage, "Missing option argument");
269 270

        default:
271 272 273
            // I don't think we should get here as the unknown arguments
            // and missing options cases should cover everything else
            isc_throw(InvalidUsage, "Invalid command line");
Shawn Routhier's avatar
Shawn Routhier committed
274
        }
275 276 277 278 279 280 281
    }

    // Check for extraneous parameters.
    if (argc > optind) {
        isc_throw(InvalidUsage, "Extraneous parameters.");
    }

282
    if (protocol_version_ == 0) {
283 284 285
        isc_throw(InvalidUsage, "DHCP version required");
    }

286 287 288 289
    if (pid_file_.empty()) {
        isc_throw(InvalidUsage, "PID file not specified");
    }

290
    if (previous_file_.empty()) {
Shawn Routhier's avatar
Shawn Routhier committed
291
        isc_throw(InvalidUsage, "Previous file not specified");
292 293 294
    }

    if (copy_file_.empty()) {
Shawn Routhier's avatar
Shawn Routhier committed
295
        isc_throw(InvalidUsage, "Copy file not specified");
296 297 298
    }

    if (output_file_.empty()) {
Shawn Routhier's avatar
Shawn Routhier committed
299
        isc_throw(InvalidUsage, "Output file not specified");
300 301
    }

302
    if (finish_file_.empty()) {
Shawn Routhier's avatar
Shawn Routhier committed
303
        isc_throw(InvalidUsage, "Finish file not specified");
304 305
    }

306
    if (config_file_.empty()) {
Shawn Routhier's avatar
Shawn Routhier committed
307
        isc_throw(InvalidUsage, "Config file not specified");
308 309 310
    }

    // If verbose is set echo the input information
311
    if (verbose_) {
312
        std::cout << "Protocol version:    DHCPv" << protocol_version_ << std::endl
313 314 315 316 317 318 319
                  << "Previous or ex lease file: " << previous_file_ << std::endl
                  << "Copy lease file:           " << copy_file_ << std::endl
                  << "Output lease file:         " << output_file_ << std::endl
                  << "Finish file:               " << finish_file_ << std::endl
                  << "Config file:               " << config_file_ << std::endl
                  << "PID file:                  " << pid_file_ << std::endl
                  << std::endl;
320 321 322 323
    }
}

void
324
LFCController::usage(const std::string& text) {
325
    if (!text.empty()) {
326 327 328 329
        std::cerr << "Usage error: " << text << std::endl;
    }

    std::cerr << "Usage: " << lfc_bin_name_ << std::endl
330
              << " [-4|-6] -p file -x file -i file -o file -f file -c file" << std::endl
331
              << "   -4 or -6 clean a set of v4 or v6 lease files" << std::endl
332 333
              << "   -p <file>: PID file" << std::endl
              << "   -x <file>: previous or ex lease file" << std::endl
334 335
              << "   -i <file>: copy of lease file" << std::endl
              << "   -o <file>: output lease file" << std::endl
336
              << "   -f <file>: finish file" << std::endl
337 338 339 340
              << "   -c <file>: configuration file" << std::endl
              << "   -v: print version number and exit" << std::endl
              << "   -V: print extended version inforamtion and exit" << std::endl
              << "   -d: optional, verbose output " << std::endl
341
              << "   -h: print this message " << std::endl
342 343 344
              << std::endl;
}

345
std::string
346 347
LFCController::getVersion(const bool extended) const{
    std::stringstream version_stream;
348

349
    version_stream << VERSION;
350
    if (extended) {
351
        version_stream << std::endl << EXTENDED_VERSION;
352 353
    }

354
    return (version_stream.str());
355 356
}

357
template<typename LeaseObjectType, typename LeaseFileType, typename StorageType>
358 359
void
LFCController::processLeases() const {
360 361 362
    StorageType storage;

    // If a previous file exists read the entries into storage
363
    LeaseFileType lf_prev(getPreviousFile());
364 365 366
    if (lf_prev.exists()) {
        LeaseFileLoader::load<LeaseObjectType>(lf_prev, storage,
                                               MAX_LEASE_ERRORS);
367 368
    }

369
    // Follow that with the copy of the current lease file
370
    LeaseFileType lf_copy(getCopyFile());
371 372 373
    if (lf_copy.exists()) {
        LeaseFileLoader::load<LeaseObjectType>(lf_copy, storage,
                                               MAX_LEASE_ERRORS);
374 375 376
    }

    // Write the result out to the output file
377
    LeaseFileType lf_output(getOutputFile());
378
    LeaseFileLoader::write<LeaseObjectType>(lf_output, storage);
379

380
    // If desired log the stats
381
    LOG_INFO(lfc_logger, LFC_READ_STATS)
382 383 384 385
      .arg(lf_prev.getReadLeases() + lf_copy.getReadLeases())
      .arg(lf_prev.getReads() + lf_copy.getReads())
      .arg(lf_prev.getReadErrs() + lf_copy.getReadErrs());

386
    LOG_INFO(lfc_logger, LFC_WRITE_STATS)
387 388 389 390
      .arg(lf_output.getWriteLeases())
      .arg(lf_output.getWrites())
      .arg(lf_output.getWriteErrs());

391
    // Once we've finished the output file move it to the complete file
392
    if (rename(getOutputFile().c_str(), getFinishFile().c_str()) != 0) {
393 394 395
        isc_throw(RunTimeFail, "Unable to move output (" << output_file_
                  << ") to complete (" << finish_file_
                  << ") error: " << strerror(errno));
396
    }
397 398 399
}

void
400
LFCController::fileRotate() const {
401
    // Remove the old previous file
402
    if ((remove(getPreviousFile().c_str()) != 0) &&
403 404 405 406 407 408
        (errno != ENOENT)) {
        isc_throw(RunTimeFail, "Unable to delete previous file '"
                  << previous_file_ << "' error: " << strerror(errno));
    }

    // Remove the copy file
409
    if ((remove(getCopyFile().c_str()) != 0) &&
410 411 412 413 414 415
        (errno != ENOENT)) {
        isc_throw(RunTimeFail, "Unable to delete copy file '"
                  << copy_file_ << "' error: " << strerror(errno));
    }

    // Rename the finish file to be the previous file
416
    if (rename(finish_file_.c_str(), previous_file_.c_str()) != 0) {
417 418 419
        isc_throw(RunTimeFail, "Unable to move finish (" << finish_file_
                  << ") to previous (" << previous_file_
                  << ") error: " << strerror(errno));
420
    }
421
}
422 423
}; // namespace isc::lfc
}; // namespace isc