SNMPConfigurator.cpp 5.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * SNMPConfigurator.cpp
 *
 *  Created on: 05.07.2018
 *      Author: Axel Auweter (original), Micha Mueller
 */

#include "SNMPConfigurator.h"

#include <boost/foreach.hpp>
#include <boost/property_tree/info_parser.hpp>
Micha Mueller's avatar
Micha Mueller committed
12
#include <boost/algorithm/string.hpp>
13
14

SNMPConfigurator::SNMPConfigurator() {
Micha Mueller's avatar
Micha Mueller committed
15
16
	/* Initialize SNMP library */
	init_snmp("dcdbpusher_SNMPplugin");
17
18
}

Micha Mueller's avatar
Micha Mueller committed
19
SNMPConfigurator::~SNMPConfigurator() {}
20
21

bool SNMPConfigurator::readConfig(std::string cfgPath) {
Micha Mueller's avatar
Micha Mueller committed
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
	Configurator::readConfig(cfgPath);
	boost::property_tree::iptree cfg;
	boost::property_tree::read_info(cfgPath, cfg);

	std::string mqttPartConnection;

	//read global variables (if present overwrite those from global.conf)
	BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
		if (boost::iequals(global.first, "mqttprefix")) {
			_mqttPrefix = global.second.data();
			if (_mqttPrefix[_mqttPrefix.length()-1] != '/') {
				_mqttPrefix.append("/");
			}
			LOG(debug) << "  Using own MQTT-Prefix " << _mqttPrefix;
		} else if (boost::iequals(global.first, "cacheInterval")) {
			_cacheInterval = stoul(global.second.data());
			LOG(debug) << "  Using own caching interval " << _cacheInterval << " [s]";
			_cacheInterval *= 1000;
		} else {
			LOG(error) << "  Value \"" << global.first << "\" not recognized. Omitting...";
		}
	}

	//read template sensors
	BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, cfg.get_child("templates")) {
		if (boost::iequals(sensor.first, "sensor")) {
			LOG(debug) << "Template Sensor \"" << sensor.second.data() << "\"";
			if (!sensor.second.empty()) {
				SNMPSensor snmpSensor(sensor.second.data());
				if(readSensor(snmpSensor, sensor.second)) {
					_templateSensors.insert(sensorMap_t::value_type(snmpSensor.getName(), snmpSensor));
				} else {
					LOG(warning) << "Template sensor \"" << sensor.second.data() << "\" has bad values! Ignoring...";
				}
			}
		}
	}

	BOOST_FOREACH(boost::property_tree::iptree::value_type &connection, cfg.get_child("connections")) {
		if (boost::iequals(connection.first, ("connection"))) {
			LOG(debug) << "Connection " << connection.second.data();
			_connections.push_back(SNMPConnection());
			SNMPConnection &conn = _connections.back();

			if (!connection.second.empty()) {
				BOOST_FOREACH(boost::property_tree::iptree::value_type &c, connection.second) {
					if (boost::iequals(c.first, "Type")) {
						//TODO	would be relevant if we would support more than just agent mode...
						//		at the moment we just ignore it, as it can be only "Agent" anyways
					} else if (boost::iequals(c.first, "Host")) {
						conn.setHost(c.second.data());
					} else if (boost::iequals(c.first, "Port")) {
						conn.setPort(stoi(c.second.data()));
					} else if (boost::iequals(c.first, "Community")) {
						conn.setSNMPCommunity(c.second.data());
					} else if (boost::iequals(c.first, "OIDPrefix")) {
						conn.setOIDPrefix(c.second.data());
					} else if (boost::iequals(c.first, "mqttPart")) {
						mqttPartConnection = c.second.data();
						if (mqttPartConnection[mqttPartConnection.length()-1] != '/') {
							mqttPartConnection.append("/");
						}
					} else if (boost::iequals(c.first, "sensor")) {
						LOG(debug) << "Sensor \"" << c.second.data() << "\"";
						if (!c.second.empty()) {
							std::string name = connection.second.data() + "_" + c.second.data();
							SNMPSensor * snmpSensor = new SNMPSensor(name);

							//first check if default sensor is given
							boost::optional<boost::property_tree::iptree&> defaultC = c.second.get_child_optional("default");
							if(defaultC) {
								LOG(debug) << "  Using \"" << defaultC.get().data() << "\" as default.";
								sensorMap_t::iterator it = _templateSensors.find(defaultC.get().data());
								if(it != _templateSensors.end()) {
									*snmpSensor = it->second;
									snmpSensor->setName(name);
								} else {
									LOG(warning) << "  Template sensor \"" << defaultC.get().data() << "\" not found! Using standard values.";
								}
							}

							snmpSensor->setMqtt(_mqttPrefix + mqttPartConnection);
							snmpSensor->setConnection(&conn);

							//read remaining values
							if(readSensor(*snmpSensor, c.second)) {
								_sensors.push_back(snmpSensor);
							} else {
								LOG(warning) << "  Sensor \"" << c.second.data() << "\" has bad values! Ignoring..." << std::endl;
							}
						}
					}
				}
			}
			conn.init();
		}
	}
119
120
121
122
	return true;
}

bool SNMPConfigurator::readSensor(SNMPSensor& sensor, boost::property_tree::iptree& config) {
Micha Mueller's avatar
Micha Mueller committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	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")) {
			sensor.setMqtt(sensor.getMqtt() + val.second.data());
		} else if (boost::iequals(val.first, "minValues")) {
			sensor.setMinValues(stoull(val.second.data()));
		} else if (boost::iequals(val.first, "OID")) {
			sensor.setOID(sensor.getConnection()->getOIDPrefix() + "." + val.second.data());
		} else if (boost::iequals(val.first, "default")) {
			//avoid unnecessary "Value not recognized" message
		} else {
			LOG(warning) << "  Value \"" << val.first << "\" not recognized. Omitting...";
		}
	}
	sensor.setCacheInterval(_cacheInterval);

	LOG(debug) << "  MQTT     : " << sensor.getMqtt();
	LOG(debug) << "  Interval : " << sensor.getInterval();
	LOG(debug) << "  minValues: " << sensor.getMinValues();
	LOG(debug) << "  OID      : " << sensor.getOIDString();
144
145
	return true;
}