configuration.cpp 5.34 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// 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.cacheInterval = 900;
    _global.logLevelFile = boost::log::trivial::trace;
    _global.logLevelCmd = boost::log::trivial::info;

    _global.cassandraSettings.address = string(CASSANDRAHOST) + ":" + string(CASSANDRAPORT);
    _global.cassandraSettings.username = "";
    _global.cassandraSettings.password = "";
    _global.cassandraSettings.ttl = 0;
}

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) {
        cout << "Error when reading collectagent.conf: " << e.what() << endl;
        //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, "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.cacheInterval = stoul(global.second.data());
        } else if (boost::iequals(global.first, "verbosity")) {
            _global.logLevelFile = translateLogLevel(stoi(global.second.data()));
        } else if (boost::iequals(global.first, "daemonize")) {
            _global.daemonize = global.second.data() == "true";
        } else if (boost::iequals(global.first, "statistics")) {
            _global.statistics = global.second.data() == "true";
        } else {
            cout << "  Value \"" << global.first << "\" not recognized. Omitting" << endl;
            //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 {
            cout << "  Value \"" << global.first << "\" not recognized. Omitting" << endl;
            //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 {
            cout << "  Value \"" << global.first << "\" not recognized. Omitting" << endl;
            //LOG(warning) << "  Value \"" << global.first << "\" not recognized. Omitting";
        }
    }

    return true;
}

boost::log::trivial::severity_level Configuration::translateLogLevel(int logLevel) {
    switch (logLevel) {
        case 0:
            return boost::log::trivial::fatal;
            break;
        case 1:
            return boost::log::trivial::error;
            break;
        case 2:
            return boost::log::trivial::warning;
            break;
        case 3:
            return boost::log::trivial::info;
            break;
        case 4:
            return boost::log::trivial::debug;
            break;
        case 5:
            return boost::log::trivial::trace;
            break;
        default:
            return boost::log::trivial::info;
            break;
    }
}

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