configuration.cpp 6.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
// 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;
23
    _global.threads = 24;
Alessio Netti's avatar
Alessio Netti committed
24
    _global.cleaningInterval = 86400;
25
26
27
    _global.logLevelFile = boost::log::trivial::trace;
    _global.logLevelCmd = boost::log::trivial::info;

28
29
30
31
32
    _global.pluginSettings.cacheInterval = 900000;
    _global.pluginSettings.tempdir = "./";
    _global.pluginSettings.sensorPattern = "";
    _global.pluginSettings.mqttPrefix = "";

33
34
35
36
    _global.cassandraSettings.address = string(CASSANDRAHOST) + ":" + string(CASSANDRAPORT);
    _global.cassandraSettings.username = "";
    _global.cassandraSettings.password = "";
    _global.cassandraSettings.ttl = 0;
Alessio Netti's avatar
Alessio Netti committed
37
38
39
40
41
42
    _global.cassandraSettings.numThreadsIo = 1;
    _global.cassandraSettings.queueSizeIo = 4096;
    _global.cassandraSettings.coreConnPerHost = 1;
    _global.cassandraSettings.maxConnPerHost = 2;
    _global.cassandraSettings.maxConcRequests = 100;
    _global.cassandraSettings.debugLog = false;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
}

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) {
Alessio Netti's avatar
Logging    
Alessio Netti committed
57
        LOG(error) << "Error when reading collectagent.conf: " << e.what();
58
59
60
61
62
63
64
        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();
65
66
        } else if (boost::iequals(global.first, "hierarchy")) {
            _global.hierarchy = global.second.data();
Alessio Netti's avatar
Alessio Netti committed
67
68
        } else if (boost::iequals(global.first, "cleaningInterval")) {
            _global.cleaningInterval = stoul(global.second.data());
69
70
71
72
73
        } 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")) {
74
            _global.pluginSettings.cacheInterval = stoul(global.second.data());
75
        } else if (boost::iequals(global.first, "verbosity")) {
76
            _global.logLevelFile = translateLogLevel(stoi(global.second.data()));
77
78
        } else if (boost::iequals(global.first, "threads")) {
            _global.threads = stoi(global.second.data());
Alessio Netti's avatar
Logging    
Alessio Netti committed
79
        } else if (boost::iequals(global.first, "tempDir")) {
80
81
82
83
84
85
86
87
            _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("/");
Alessio Netti's avatar
Logging    
Alessio Netti committed
88
            }
89
90
        } else if (boost::iequals(global.first, "sensorpattern")) {
            _global.pluginSettings.sensorPattern = global.second.data();
91
        } else if (boost::iequals(global.first, "daemonize")) {
Alessio Netti's avatar
Alessio Netti committed
92
            _global.daemonize = to_bool(global.second.data());
93
        } else if (boost::iequals(global.first, "statistics")) {
Alessio Netti's avatar
Alessio Netti committed
94
            _global.statistics = to_bool(global.second.data());
95
        } else {
Alessio Netti's avatar
Logging    
Alessio Netti committed
96
            LOG(warning) << "  Value \"" << global.first << "\" not recognized. Omitting";
97
98
99
100
101
102
103
104
105
106
107
108
109
        }
    }

    //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());
Alessio Netti's avatar
Alessio Netti committed
110
111
112
113
114
115
116
117
118
119
120
        } 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")) {
Alessio Netti's avatar
Alessio Netti committed
121
            _global.cassandraSettings.debugLog = to_bool(global.second.data());
122
        } else {
Alessio Netti's avatar
Logging    
Alessio Netti committed
123
            LOG(warning) << "  Value \"" << global.first << "\" not recognized. Omitting";
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
        }
    }

    //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 {
Alessio Netti's avatar
Logging    
Alessio Netti committed
140
            LOG(warning) << "  Value \"" << global.first << "\" not recognized. Omitting";
141
142
143
144
145
146
147
148
149
        }
    }

    return true;
}

globalCA_t& Configuration::getGlobal() {
    return _global;
}