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

#include "SNMPConfigurator.h"

SNMPConfigurator::SNMPConfigurator() {
Micha Mueller's avatar
Micha Mueller committed
11
12
	/* Initialize SNMP library */
	init_snmp("dcdbpusher_SNMPplugin");
13
14
}

Micha Mueller's avatar
Micha Mueller committed
15
SNMPConfigurator::~SNMPConfigurator() {}
16

17
bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
Micha Mueller's avatar
Micha Mueller committed
18
19
20
	std::string mqttPartConnection;

	BOOST_FOREACH(boost::property_tree::iptree::value_type &connection, cfg.get_child("connections")) {
21
		if (STRCMP(connection, ("connection"))) {
Micha Mueller's avatar
Micha Mueller committed
22
23
24
25
26
27
			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) {
28
					if (STRCMP(c, "Type")) {
Micha Mueller's avatar
Micha Mueller committed
29
30
						//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
31
					} else if (STRCMP(c, "Host")) {
Micha Mueller's avatar
Micha Mueller committed
32
						conn.setHost(c.second.data());
Micha Mueller's avatar
Micha Mueller committed
33
						LOG(debug) << "  Host " << conn.getHost();
34
					} else if (STRCMP(c, "Port")) {
Micha Mueller's avatar
Micha Mueller committed
35
						conn.setPort(stoi(c.second.data()));
Micha Mueller's avatar
Micha Mueller committed
36
						LOG(debug) << "  Port " << conn.getPort();
37
					} else if (STRCMP(c, "Community")) {
Micha Mueller's avatar
Micha Mueller committed
38
						conn.setSNMPCommunity(c.second.data());
Micha Mueller's avatar
Micha Mueller committed
39
						LOG(debug) << "  Community " << conn.getSNMPCommunity();
40
					} else if (STRCMP(c, "OIDPrefix")) {
Micha Mueller's avatar
Micha Mueller committed
41
						conn.setOIDPrefix(c.second.data());
Micha Mueller's avatar
Micha Mueller committed
42
						LOG(debug) << "  OID-prefix " << conn.getOIDPrefix();
43
					} else if (STRCMP(c, "Version")) {
Micha Mueller's avatar
Micha Mueller committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
						int version = stoi(c.second.data());
						switch (version) {
						case 1:
							conn.setVersion(SNMP_VERSION_1);
							LOG(debug) << "  Version 1";
							break;
						case 2:
							conn.setVersion(SNMP_VERSION_2c);
							LOG(debug) << "  Version 2c";
							break;
						case 3:
							conn.setVersion(SNMP_VERSION_3);
							LOG(debug) << "  Version 3";
							break;
						default:
							conn.setVersion(-1);
							LOG(warning) << "Invalid SNMP Version " << version << "!";
						}
62
					} else if (STRCMP(c, "Username")) {
Micha Mueller's avatar
Micha Mueller committed
63
64
						conn.setUsername(c.second.data());
						LOG(debug) << "  Username " << conn.getUsername();
65
					} else if (STRCMP(c, "SecLevel")) {
Micha Mueller's avatar
Micha Mueller committed
66
67
68
69
70
71
72
73
74
75
76
						std::string secLvl = c.second.data();
						if (boost::iequals(secLvl, "noAuthNoPriv")) {
							conn.setSecurityLevel(SNMP_SEC_LEVEL_NOAUTH);
							LOG(debug) << "  Security Level NoAuthNoPriv";
						} else if (boost::iequals(secLvl, "authNoPriv")) {
							conn.setSecurityLevel(SNMP_SEC_LEVEL_AUTHNOPRIV);
							LOG(debug) << "  Security Level AuthNoPriv";
						} else if (boost::iequals(secLvl, "authPriv")) {
							conn.setSecurityLevel(SNMP_SEC_LEVEL_AUTHPRIV);
							LOG(debug) << "  Security Level AuthPriv";
						}
77
					} else if (STRCMP(c, "AuthProto")) {
Micha Mueller's avatar
Micha Mueller committed
78
79
80
81
82
83
84
85
86
87
						std::string authProto = c.second.data();
						if (boost::iequals(authProto, "MD5")) {
							conn.setAuthProto(usmHMACMD5AuthProtocol);
							conn.setAuthProtoLen(USM_AUTH_PROTO_MD5_LEN);
							LOG(debug) << "  Authentication protocol MD5";
						} else if (boost::iequals(authProto, "SHA1")) {
							conn.setAuthProto(usmHMACSHA1AuthProtocol);
							conn.setAuthProtoLen(USM_AUTH_PROTO_SHA_LEN);
							LOG(debug) << "  Authentication protocol SHA1";
						}
88
					} else if (STRCMP(c, "PrivProto")) {
Micha Mueller's avatar
Micha Mueller committed
89
90
91
92
93
94
95
96
97
98
						std::string privProto = c.second.data();
						if (boost::iequals(privProto, "DES")) {
							conn.setPrivProto(snmp_duplicate_objid(usmDESPrivProtocol, USM_PRIV_PROTO_DES_LEN));
							conn.setPrivProtoLen(USM_PRIV_PROTO_DES_LEN);
							LOG(debug) << "  Privacy protocol DES";
						} else if (boost::iequals(privProto, "AES")) {
							conn.setPrivProto(snmp_duplicate_objid(usmAESPrivProtocol, USM_PRIV_PROTO_AES_LEN));
							conn.setPrivProtoLen(USM_PRIV_PROTO_AES_LEN);
							LOG(debug) << "  Privacy protocol AES";
						}
99
					} else if (STRCMP(c, "AuthKey")) {
Micha Mueller's avatar
Micha Mueller committed
100
101
						conn.setAuthKey(c.second.data());
						LOG(debug) << "  Authkey " << conn.getAuthKey();
102
					} else if (STRCMP(c, "PrivKey")) {
Micha Mueller's avatar
Micha Mueller committed
103
104
						conn.setPrivKey(c.second.data());
						LOG(debug) << "  Privkey " << conn.getPrivKey();
105
					} else if (STRCMP(c, "mqttPart")) {
Micha Mueller's avatar
Micha Mueller committed
106
107
108
109
						mqttPartConnection = c.second.data();
						if (mqttPartConnection[mqttPartConnection.length()-1] != '/') {
							mqttPartConnection.append("/");
						}
Micha Mueller's avatar
Micha Mueller committed
110
						LOG(debug) << "  MQTTPart " << mqttPartConnection;
111
					} else if (STRCMP(c, "sensor")) {
Micha Mueller's avatar
Micha Mueller committed
112
113
114
						LOG(debug) << "Sensor \"" << c.second.data() << "\"";
						if (!c.second.empty()) {
							std::string name = connection.second.data() + "_" + c.second.data();
115
							SNMPSingleSensor * snmpSensor = new SNMPSingleSensor(name);
Micha Mueller's avatar
Micha Mueller committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

							//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->setConnection(&conn);
131
							snmpSensor->setMqtt(_mqttPrefix + mqttPartConnection);
Micha Mueller's avatar
Micha Mueller committed
132
133

							//read remaining values
134
							if(readSensorBase(*snmpSensor, c.second)) {
Micha Mueller's avatar
Micha Mueller committed
135
136
								_sensors.push_back(snmpSensor);
							} else {
137
								LOG(warning) << "  Sensor \"" << c.second.data() << "\" has bad values! Ignoring...";
Micha Mueller's avatar
Micha Mueller committed
138
139
140
141
142
143
144
145
							}
						}
					}
				}
			}
			conn.init();
		}
	}
146
147
148
	return true;
}

149
bool SNMPConfigurator::derivedReadSensorBase(SNMPSensorBase& sensor, boost::property_tree::iptree& config) {
Micha Mueller's avatar
Micha Mueller committed
150
	BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
151
		if (STRCMP(val, "mqttsuffix")) {
Micha Mueller's avatar
Micha Mueller committed
152
			sensor.setMqtt(sensor.getMqtt() + val.second.data());
153
		} else if (STRCMP(val, "OID")) {
Micha Mueller's avatar
Micha Mueller committed
154
155
156
157
158
159
			sensor.setOID(sensor.getConnection()->getOIDPrefix() + "." + val.second.data());
		}
	}

	LOG(debug) << "  MQTT     : " << sensor.getMqtt();
	LOG(debug) << "  OID      : " << sensor.getOIDString();
160
161
	return true;
}