// // Created by Netti, Alessio on 30.11.18. // #include "configuration.h" using namespace std; Configuration::Configuration(const std::string& cfgFilePath) : _cfgFilePath(cfgFilePath) { if (_cfgFilePath[_cfgFilePath.length()-1] != '/') { _cfgFilePath.append("/"); } //set default values for global variables _global.daemonize = false; _global.statistics = false; _global.mqttListenAddress = string(LISTENHOST) + ":" + string(LISTENPORT); _global.restListenAddress = string(RESTAPIHOST) + ":" + string(RESTAPIPORT); _global.messageThreads = 128; _global.messageSlots = 16; _global.threads = 24; _global.cleaningInterval = 86400; _global.logLevelFile = boost::log::trivial::trace; _global.logLevelCmd = boost::log::trivial::info; _global.pluginSettings.cacheInterval = 900000; _global.pluginSettings.tempdir = "./"; _global.pluginSettings.sensorPattern = ""; _global.pluginSettings.mqttPrefix = ""; _global.cassandraSettings.address = string(CASSANDRAHOST) + ":" + string(CASSANDRAPORT); _global.cassandraSettings.username = ""; _global.cassandraSettings.password = ""; _global.cassandraSettings.ttl = 0; _global.cassandraSettings.numThreadsIo = 1; _global.cassandraSettings.queueSizeIo = 4096; _global.cassandraSettings.coreConnPerHost = 1; _global.cassandraSettings.maxConnPerHost = 2; _global.cassandraSettings.maxConcRequests = 100; _global.cassandraSettings.debugLog = false; } Configuration::~Configuration() {} bool Configuration::readGlobal() { //open file std::string globalConfig = _cfgFilePath; globalConfig.append("collectagent.conf"); boost::property_tree::iptree cfg; //parse to property_tree try { boost::property_tree::read_info(globalConfig, cfg); } catch (boost::property_tree::info_parser_error& e) { LOG(error) << "Error when reading collectagent.conf: " << e.what(); return false; } //read global struct BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) { if (boost::iequals(global.first, "mqttListenAddress")) { _global.mqttListenAddress = global.second.data(); } else if (boost::iequals(global.first, "hierarchy")) { _global.hierarchy = global.second.data(); } else if (boost::iequals(global.first, "cleaningInterval")) { _global.cleaningInterval = stoul(global.second.data()); } else if (boost::iequals(global.first, "messageThreads")) { _global.messageThreads = stoul(global.second.data()); } else if (boost::iequals(global.first, "messageSlots")) { _global.messageSlots = stoul(global.second.data()); } else if (boost::iequals(global.first, "cacheInterval")) { _global.pluginSettings.cacheInterval = stoul(global.second.data()) * 1000; } else if (boost::iequals(global.first, "verbosity")) { _global.logLevelFile = translateLogLevel(stoi(global.second.data())); } else if (boost::iequals(global.first, "threads")) { _global.threads = stoi(global.second.data()); } else if (boost::iequals(global.first, "tempDir")) { _global.pluginSettings.tempdir = global.second.data(); if (_global.pluginSettings.tempdir[_global.pluginSettings.tempdir.length()-1] != '/') { _global.pluginSettings.tempdir.append("/"); } } else if (boost::iequals(global.first, "mqttprefix")) { _global.pluginSettings.mqttPrefix = global.second.data(); if (_global.pluginSettings.mqttPrefix[_global.pluginSettings.mqttPrefix.length()-1] != '/') { _global.pluginSettings.mqttPrefix.append("/"); } } else if (boost::iequals(global.first, "sensorpattern")) { _global.pluginSettings.sensorPattern = global.second.data(); } else if (boost::iequals(global.first, "daemonize")) { _global.daemonize = to_bool(global.second.data()); } else if (boost::iequals(global.first, "statistics")) { _global.statistics = to_bool(global.second.data()); } else { LOG(warning) << " Value \"" << global.first << "\" not recognized. Omitting"; } } //read cassandra struct BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("cassandra")) { if (boost::iequals(global.first, "address")) { _global.cassandraSettings.address = global.second.data(); } else if (boost::iequals(global.first, "username")) { _global.cassandraSettings.username = global.second.data(); } else if (boost::iequals(global.first, "password")) { _global.cassandraSettings.password = global.second.data(); } else if (boost::iequals(global.first, "ttl")) { _global.cassandraSettings.ttl = stoul(global.second.data()); } else if (boost::iequals(global.first, "numThreadsIo")) { _global.cassandraSettings.numThreadsIo = stoul(global.second.data()); } else if (boost::iequals(global.first, "queueSizeIo")) { _global.cassandraSettings.queueSizeIo = stoul(global.second.data()); } else if (boost::iequals(global.first, "coreConnPerHost")) { _global.cassandraSettings.coreConnPerHost = stoul(global.second.data()); } else if (boost::iequals(global.first, "maxConnPerHost")) { _global.cassandraSettings.maxConnPerHost = stoul(global.second.data()); } else if (boost::iequals(global.first, "maxConcRequests")) { _global.cassandraSettings.maxConcRequests = stoul(global.second.data()); } else if (boost::iequals(global.first, "debugLog")) { _global.cassandraSettings.debugLog = to_bool(global.second.data()); } else { LOG(warning) << " Value \"" << global.first << "\" not recognized. Omitting"; } } //read restAPI struct BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("restAPI")) { if (boost::iequals(global.first, "address")) { _global.restListenAddress = global.second.data(); //} else if (boost::iequals(global.first, "certificate")) { // _global.restAPISettings.certificate = global.second.data(); //} else if (boost::iequals(global.first, "privateKey")) { // _global.restAPISettings.privateKey = global.second.data(); //} else if (boost::iequals(global.first, "dhFile")) { // _global.restAPISettings.dhFile = global.second.data(); //} else if (boost::iequals(global.first, "authkey")) { // //Avoid unnecessary "Value not recognized" message } else { LOG(warning) << " Value \"" << global.first << "\" not recognized. Omitting"; } } return true; } globalCA_t& Configuration::getGlobal() { return _global; }