IPMIConfigurator.cpp 6.79 KB
Newer Older
1
2
3
4
5
6
7
8
/*
 * IPMIConfigurator.cpp
 *
 *  Created on: 26 Jan 2017
 *      Author: ottmi
 */

#include "IPMIConfigurator.h"
9

10
11
12
13
14
15
16
17
18
19
20
#include "IPMIHost.h"
#include "IPMISensor.h"

#include <boost/property_tree/info_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/optional.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>

namespace DCDB {

21
22
23
24
IPMIConfigurator::IPMIConfigurator() {
	_globalHost.sessionTimeout = 0;
	_globalHost.retransmissionTimeout = 0;
}
25

26
IPMIConfigurator::~IPMIConfigurator() {}
27

28
bool IPMIConfigurator::readConfig(std::string cfgPath) {
29
30
	Configurator::readConfig(cfgPath);

31
32
	boost::property_tree::iptree cfg;
	boost::property_tree::read_info(cfgPath, cfg);
33

34
35
	//read global variables (if present overwrite those from global.conf)
	BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
36
		if (boost::iequals(global.first, "SessionTimeout")) {
37
			_globalHost.sessionTimeout = stoi(global.second.data());
38
			LOG(debug) << "  SessionTimeout " << _globalHost.sessionTimeout;
39
40
		} else if (boost::iequals(global.first, "RetransmissionTimeout")) {
			_globalHost.retransmissionTimeout = stoi(global.second.data());
41
			LOG(debug) << "  RetransmissionTimeout " << _globalHost.retransmissionTimeout;
42
		} else if (boost::iequals(global.first, "mqttprefix")) {
43
			_mqttPrefix = global.second.data();
44
45
46
			if (_mqttPrefix[_mqttPrefix.length()-1] != '/') {
				_mqttPrefix.append("/");
			}
47
			LOG(debug) << "  Using own MQTT-Prefix " << _mqttPrefix;
48
49
50
51
		} else if (boost::iequals(global.first, "cacheInterval")) {
			_cacheInterval = stoul(global.second.data());
			LOG(debug) << "  Using own caching interval " << _cacheInterval << " [s]";
			_cacheInterval *= 1000;
52
53
		} else {
			LOG(error) << "  Value \"" << global.first << "\" not recognized. Omitting...";
54
		}
55
	}
56

57
58
	//read template sensors
	BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, cfg.get_child("templateSensors")) {
59
		if (boost::iequals(sensor.first, "sensor")) {
60
			LOG(debug) << "Template Sensor \"" << sensor.second.data() << "\"";
61
62
63
64
65
			if (!sensor.second.empty()) {
				DCDB::IPMISensor ipmiSensor(sensor.second.data());
				if(readSensor(ipmiSensor, sensor.second)) {
					_templateSensors.insert(sensorMap_t::value_type(ipmiSensor.getName(), ipmiSensor));
				} else {
66
					LOG(warning) << "Template sensor \"" << sensor.second.data() << "\" has bad values! Ignoring...";
67
68
69
				}
			}
		}
70
	}
71

72
	BOOST_FOREACH(boost::property_tree::iptree::value_type &host, cfg.get_child("hosts")) {
73
		if (boost::iequals(host.first, ("host"))) {
74
			LOG(debug) << "Host " << host.second.data();
75
76
77
			_hosts.push_back(DCDB::IPMIHost(host.second.data(),	_globalHost.retransmissionTimeout, _globalHost.sessionTimeout));
			DCDB::IPMIHost &ipmiHost = _hosts.back();
			ipmiHost.setMqttPrefix(_mqttPrefix);
78
			ipmiHost.setCache(_tempdir);
79
80
81
82
			if (!host.second.empty()) {
				BOOST_FOREACH(boost::property_tree::iptree::value_type &h, host.second) {
					if (boost::iequals(h.first, "username")) {
						ipmiHost.setUserName(h.second.data());
83
						LOG(debug) << "  Username " << ipmiHost.getUserName();
84
85
					} else if (boost::iequals(h.first, "password")) {
						ipmiHost.setPassword(h.second.data());
86
						LOG(debug) << "  Password " << ipmiHost.getPassword();
87
88
					} else if (boost::iequals(h.first, "mqttprefix")) {
						ipmiHost.setMqttPrefix(h.second.data());
89
						LOG(debug) << "  MQTTPrefix " << ipmiHost.getMqttPrefix();
90
91
92
					} else if (boost::iequals(h.first, "sensors")) {
						BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, h.second) {

93
							LOG(debug) << "Sensor \"" << sensor.second.data() << "\"";
94
							if (!sensor.second.empty()) {
95
96
								std::string name = host.second.data() + "_" + sensor.second.data();
								DCDB::IPMISensor* ipmiSensor = new DCDB::IPMISensor(name);
97
98
99
100

								//first check if default sensor is given
								boost::optional<boost::property_tree::iptree&> defaultC = sensor.second.get_child_optional("default");
								if(defaultC) {
101
									LOG(debug) << "  Using \"" << defaultC.get().data() << "\" as default.";
102
103
104
									sensorMap_t::iterator it = _templateSensors.find(defaultC.get().data());
									if(it != _templateSensors.end()) {
										*ipmiSensor = it->second;
105
										ipmiSensor->setName(name);
106
									} else {
107
										LOG(warning) << "  Template sensor \"" << defaultC.get().data() << "\" not found! Using standard values.";
108
109
110
111
112
113
114
115
116
									}
								}

								//set pointer to corresponding host
								ipmiSensor->setHost(&ipmiHost);
								//read remaining values
								if(readSensor(*ipmiSensor, sensor.second)) {
									_sensors.push_back(ipmiSensor);
								} else {
117
									LOG(warning) << "  Sensor \"" << sensor.second.data() << "\" has bad values! Ignoring..." << std::endl;
118
119
								}
							}
120
						}
121
122
123
124
125
					}
				}
			}
		}
	}
126
	return true;
127
128
129
130
131
132
133
134
135
136
137
138
139
140
}

bool IPMIConfigurator::readSensor(DCDB::IPMISensor& sensor, boost::property_tree::iptree& config) {
	BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
		if (boost::iequals(val.first, "interval")) {
			sensor.setInterval(stoull(val.second.data()));
		} else if (boost::iequals(val.first, "mqttsuffix")) {
			if (sensor.getHost() != NULL) {
				// only workaround so that every host can define own prefix
				// not guaranteed, but should actually work as long as prefix is defined before the sensors within a host
				if (sensor.getHost()->getMqttPrefix() != "") {
					sensor.setMqtt(sensor.getHost()->getMqttPrefix() + val.second.data());
				} else {
					sensor.setMqtt(_mqttPrefix + val.second.data());
141
142
				}
			}
143
144
145
146
147
148
149
150
151
152
		} else if (boost::iequals(val.first, "minValues")) {
			sensor.setMinValues(stoull(val.second.data()));
		} else if (boost::iequals(val.first, ("cmd"))) {
			sensor.setRawCmd(val.second.data());
		} else if (boost::iequals(val.first, "start")) {
			sensor.setStart(stoi(val.second.data()));
		} else if (boost::iequals(val.first, "stop")) {
			sensor.setStop(stoi(val.second.data()));
		} else if (boost::iequals(val.first, ("recordId"))) {
			sensor.setRecordId(stoul(val.second.data()));
153
154
		} else if (boost::iequals(val.first, ("factor"))) {
			sensor.setFactor(std::stod(val.second.data()));
155
156
157
		} else if (boost::iequals(val.first, "default")) {
			//avoid unnecessary "Value not recognized" message
		} else {
158
			LOG(warning) << "  Value \"" << val.first << "\" not recognized. Omitting...";
159
		}
160
161
	}

162
163
	sensor.setCacheInterval(_cacheInterval);

164
165
166
	LOG(debug) << "  MQTTSuffix:" << sensor.getMqtt();
	LOG(debug) << "  Interval : " << sensor.getInterval();
	LOG(debug) << "  minValues: " << sensor.getMinValues();
167
	LOG(debug) << "  factor   : " << sensor.getFactor();
168
	if (sensor.getRecordId() != 0) {
169
		LOG(debug) << "  RecordID : " << sensor.getRecordId();
170
	} else {
171
172
173
		LOG(debug) << "  Using raw command";
		LOG(debug) << "  Start    : " << unsigned(sensor.getStart());
		LOG(debug) << "  Stop     : " << unsigned(sensor.getStop());
174
175
176
	}
	return true;
}
177
178

} /* namespace DCDB */