MSRConfigurator.cpp 8.89 KB
Newer Older
1
2
3
4
/*
 * MSRConfigurator.cpp
 *
 *  Created on: 28.01.2019
5
 *      Author: Carla Guillen
6
7
8
 */

#include "MSRConfigurator.h"
lu43jih's avatar
lu43jih committed
9
#include <iomanip>
10
11
12
13
14
15
16
17
18
19

MSRConfigurator::MSRConfigurator() {
	_groupName = "group";
	_baseName = "sensor";
}

MSRConfigurator::~MSRConfigurator() {}

void MSRConfigurator::sensorBase(MSRSensorBase& s, CFG_VAL config) {
	ADD {
20
		if (boost::iequals(val.first, "metric")){
lu43jih's avatar
lu43jih committed
21
22
23
			//TODO try catch...
			uint64_t metric = std::stoull(val.second.data(),nullptr,16);
			s.setMetric(metric);
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
		}
	}
}

void MSRConfigurator::sensorGroup(MSRSensorGroup& s, CFG_VAL config) {
	ADD {
		if (boost::iequals(val.first, "cpus")){
			std::set<int> cpus = ConfiguratorTemplate::parseCpuString(val.second.data());
			for(int cpu: cpus){
				s.addCpu(static_cast<unsigned int>(cpu));
			}
		}
	}
}

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 * Custom readConfig, as MSR has to copy sensors for each CPU
 */
bool MSRConfigurator::readConfig(std::string cfgPath) {
    _cfgPath = cfgPath;

    boost::property_tree::iptree cfg;
    boost::property_tree::read_info(cfgPath, cfg);

    //read global variables (if present overwrite those from global.conf)
    readGlobal(cfg);

    //read groups and templates for groups
    BOOST_FOREACH(boost::property_tree::iptree::value_type &val, cfg) {
        //template group
        if (boost::iequals(val.first, "template_" + _groupName)) {
            LOG(debug) << "Template " << _groupName << " \"" << val.second.data() << "\"";
            if (!val.second.empty()) {
                MSRSensorGroup* group = new MSRSensorGroup(val.second.data());
                if (readSensorGroup(*group, val.second, true)) {
                    auto ret = _templateSensorGroups.insert(std::pair<std::string, MSRSensorGroup*>(val.second.data(), group));
                    if(!ret.second) {
                        LOG(warning) << "Template " << _groupName << " " << val.second.data() << " already exists! Omitting...";
                        delete group;
                    }
                } else {
                    LOG(warning) << "Template " << _groupName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
                    delete group;
                }
            }
        //template base
        } else if (boost::iequals(val.first, "template_" + _baseName)) {
            LOG(debug) << "Template " << _baseName << " \"" << val.second.data() << "\"";
            if (!val.second.empty()) {
                MSRSensorBase* base = new MSRSensorBase(val.second.data());
                if (readSensorBase(*base, val.second, true)) {
                    auto ret = _templateSensorBases.insert(std::pair<std::string, MSRSensorBase*>(val.second.data(), base));
                    if(!ret.second) {
                        LOG(warning) << "Template " << _baseName << " " << val.second.data() << " already exists! Omitting...";
                        delete base;
                    }
                } else {
                    LOG(warning) << "Template " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
                    delete base;
                }
            }
        //template single sensor
        } else if (boost::iequals(val.first, "template_single_" + _baseName)) {
            LOG(debug) << "Template single " << _baseName << " \"" << val.second.data() << "\"";
            if (!val.second.empty()) {
                MSRSensorGroup* group = new MSRSensorGroup(val.second.data());
                if (readSensorGroup(*group, val.second, true)) {
                    //group which consists of only one sensor
                    SB_Ptr sensor = std::make_shared<MSRSensorBase>(val.second.data());
                    if (readSensorBase(*sensor, val.second, true)) {
                        group->pushBackSensor(sensor);
                        auto ret = _templateSensorGroups.insert(std::pair<std::string, MSRSensorGroup*>(val.second.data(), group));
                        if(!ret.second) {
                            LOG(warning) << "Template single " << _baseName << " " << val.second.data() << " already exists! Omitting...";
                            delete group;
                        }
                    } else {
                        LOG(warning) << "Template single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
                        delete group;
                    }
                } else {
                    LOG(warning) << "Template single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
                    delete group;
                }
            }
        //group
        } else if (boost::iequals(val.first, _groupName)) {
            LOG(debug) << _groupName << " \"" << val.second.data() << "\"";
            if (!val.second.empty()) {
                SG_Ptr group = std::make_shared<MSRSensorGroup>(val.second.data());
                if (readSensorGroup(*group, val.second)) {
                    customizeAndStore(group);
                } else {
                    LOG(warning) << _groupName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
                }
            }
        //single sensor
        } else if (boost::iequals(val.first, "single_" + _baseName)) {
            LOG(debug) << "Single " << _baseName << " \"" << val.second.data() << "\"";
            if (!val.second.empty()) {
                SG_Ptr group = std::make_shared<MSRSensorGroup>(val.second.data());
                if (readSensorGroup(*group, val.second)) {
                    //group which consists of only one sensor
                    SB_Ptr sensor;
                    //perhaps one sensor is already present because it was copied from the template group
                    if (group->getSensors().size() != 0) {
                        sensor = std::dynamic_pointer_cast<MSRSensorBase>(group->getSensors()[0]);
                        //check if cast was successful (sensor != nullptr)
                        if (sensor) {
                            sensor->setName(val.second.data());
                            if (readSensorBase(*sensor, val.second)) {
                                customizeAndStore(group);
                            } else {
                                LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
                            }
                        } else {
                            LOG(warning) << "Single " << _baseName << " " << val.second.data() << " had a type mismatch when casting! Omitting";
                        }
                    } else {
                        sensor = std::make_shared<MSRSensorBase>(val.second.data());
                        if (readSensorBase(*sensor, val.second)) {
                            group->pushBackSensor(sensor);
                            customizeAndStore(group);
                        } else {
                            LOG(warning) << "Single " << _baseName << " " << val.second.data() << " could not be read! Omitting";
                        }
                    }
                } else {
                    LOG(warning) << "Single " << _baseName << " \"" << val.second.data() << "\" has bad values! Ignoring...";
                }
            }
        } else if( !boost::iequals(val.first, "global") ) {
            LOG(error) << "\"" << val.first << "\": unknown construct!";
            return false;
        }
    }
    //read of config finished. Now we build the mqtt-topic for every sensor
    constructSensorNames();
    for(const auto& g : _sensorGroups) {
        for(const auto& s : g->getSensors()) {
            s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt());
            LOG(debug) << g->getGroupName() << "::" << s->getName() << " using MQTT-topic \"" << s->getMqtt() << "\"";
        }
    }
    return true;
}

void MSRConfigurator::customizeAndStore(SG_Ptr g) {
  bool begin = true;

  std::vector<SB_Ptr> original;

  for(auto cpu : g->getCpus()) {
      if (begin){
          for(auto s : g->getSensors()) {
              SB_Ptr sensor = std::dynamic_pointer_cast<MSRSensorBase>(s);
              sensor->setCpu(cpu);
              s->setName(s->getName(), cpu);
              auto size = s->getMqtt().size();
              s->setMqtt(formatMqttCPU("XX", cpu) + s->getMqtt().substr(size-2));
              original.push_back(sensor);
          }
          begin = false;
      } else {
          for (auto s: original) {
              auto s_otherCPUs = std::make_shared<MSRSensorBase>(s->getName());
lu43jih's avatar
lu43jih committed
190
191
192
193
194
	      std::size_t found = s->getName().find_first_of(".");
	      if( found != std::string::npos){
		found++; //to skip the point
		s_otherCPUs->setName(s->getName().substr(found), cpu);
	      }
195
196
197
198
199
200
201
202
203
204
205
              s_otherCPUs->setCpu(cpu);
              s_otherCPUs->setMetric(s->getMetric());
              auto size = s->getMqtt().size();
              s_otherCPUs->setMqtt(formatMqttCPU("XX", cpu) + s->getMqtt().substr(size-2));
              g->pushBackSensor(s_otherCPUs);
          }
      }
  }
  storeSensorGroup(g);
}