configuration.cpp 4.74 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
//
// 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;
Alessio Netti's avatar
Alessio Netti committed
24
    _global.cleaningInterval = 86400;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    _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) {
Alessio Netti's avatar
Logging    
Alessio Netti committed
46
        LOG(error) << "Error when reading collectagent.conf: " << e.what();
47
48
49
50
51
52
53
        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();
Alessio Netti's avatar
Alessio Netti committed
54
55
        } else if (boost::iequals(global.first, "cleaningInterval")) {
            _global.cleaningInterval = stoul(global.second.data());
56
57
58
59
60
61
62
        } 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")) {
Alessio Netti's avatar
Logging    
Alessio Netti committed
63
64
65
66
67
68
            _global.logLevelFile = DCDBLog::translateLogLevel(stoi(global.second.data()));
        } else if (boost::iequals(global.first, "tempDir")) {
            _global.tempDir = global.second.data();
            if (_global.tempDir[_global.tempDir.length()-1] != '/') {
                _global.tempDir.append("/");
            }
69
70
71
72
73
        } 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 {
Alessio Netti's avatar
Logging    
Alessio Netti committed
74
            LOG(warning) << "  Value \"" << global.first << "\" not recognized. Omitting";
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        }
    }

    //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 {
Alessio Netti's avatar
Logging    
Alessio Netti committed
89
            LOG(warning) << "  Value \"" << global.first << "\" not recognized. Omitting";
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
        }
    }

    //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
106
            LOG(warning) << "  Value \"" << global.first << "\" not recognized. Omitting";
107
108
109
110
111
112
113
114
115
        }
    }

    return true;
}

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