SNMPConnection.h 6.83 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
24
25
//================================================================================
// Name        : SNMPConnection.h
// Author      : Axel Auweter, Micha Mueller
// Copyright   : Leibniz Supercomputing Centre
// Description : Header file for SNMPConnection class.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2018-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================
26
27
28
29

#ifndef SNMPCONNECTION_H_
#define SNMPCONNECTION_H_

30
31
#include "../../includes/EntityInterface.h"

32
33
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
34
#include <boost/algorithm/string.hpp>
35

36
37
38
39
40
41
42
43
#define AUTH_MD5  "MD5"
#define AUTH_SHA1 "SHA1"
#define PRIV_DES  "DES"
#define PRIV_AES  "AES"
#define SEC_NOAUTHNOPRIV "noAuthNoPriv"
#define SEC_AUTHNOPRIV   "authNoPriv"
#define SEC_AUTHPRIV     "authPriv"

44
45
46
47
48
/**
 * @brief This class handles a SNMP connection.
 *
 * @ingroup snmp
 */
49
class SNMPConnection : public EntityInterface {
50
51

public:
52
53
	SNMPConnection(const std::string& name = "SNMPConn");
	SNMPConnection(const SNMPConnection& other);
54
	virtual ~SNMPConnection();
55
	SNMPConnection& operator=(const SNMPConnection& other);
56

57
	void setSNMPCommunity(const std::string& community) { _snmpCommunity = community; }
Micha Mueller's avatar
Micha Mueller committed
58
59
60
61
62
63
64
65
66
	void setOIDPrefix(const std::string& oidPrefix) {
		/* Ensure that the oidPrefix does not contain a trailing . */
		_oidPrefix = oidPrefix;
		if (_oidPrefix.length() > 0) {
			if (_oidPrefix.at(_oidPrefix.length() - 1) == '.') {
				_oidPrefix.resize(_oidPrefix.size() - 1);
			}
		}
	}
67
68
69
70
	void setUsername(const std::string& username)	{ _username = username; }
	void setAuthKey(const std::string& authKey)		{ _authKey = authKey; }
	void setPrivKey(const std::string& privKey)		{ _privKey = privKey; }
	void setAuthProto(const std::string& authProto)	{
71
		if (boost::iequals(authProto, AUTH_MD5)) {
72
73
			_authProto = usmHMACMD5AuthProtocol;
			_authProtoLen = USM_AUTH_PROTO_MD5_LEN;
74
		} else if (boost::iequals(authProto, AUTH_SHA1)) {
75
76
77
			_authProto = usmHMACSHA1AuthProtocol;
			_authProtoLen = USM_AUTH_PROTO_SHA_LEN;
		}
Micha Mueller's avatar
Micha Mueller committed
78
	}
79
	void setPrivProto(const std::string& privProto)	{
80
		if (boost::iequals(privProto, PRIV_DES)) {
81
82
			_privProto = snmp_duplicate_objid(usmDESPrivProtocol, USM_PRIV_PROTO_DES_LEN);
			_privProtoLen = USM_PRIV_PROTO_DES_LEN;
83
		} else if (boost::iequals(privProto, PRIV_AES)) {
84
85
86
			_privProto = snmp_duplicate_objid(usmAESPrivProtocol, USM_PRIV_PROTO_AES_LEN);
			_privProtoLen = USM_PRIV_PROTO_AES_LEN;
		}
Micha Mueller's avatar
Micha Mueller committed
87
	}
88
	void setHost(const std::string& host)			{ _name = host; }
89
90
	void setPort(const std::string& port)			{ _port = stoi(port); }
	void setSecurityLevel(const std::string& securityLevel) {
91
		if (boost::iequals(securityLevel, SEC_NOAUTHNOPRIV)) {
92
			_securityLevel = SNMP_SEC_LEVEL_NOAUTH;
93
		} else if (boost::iequals(securityLevel, SEC_AUTHNOPRIV)) {
94
			_securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
95
		} else if (boost::iequals(securityLevel, SEC_AUTHPRIV)) {
96
97
			_securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
		}
Micha Mueller's avatar
Micha Mueller committed
98
	}
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	void setVersion(const std::string& version)		{
		int vers = stoi(version);
		switch(vers) {
		case 1:
			_version = SNMP_VERSION_1;
			break;
		case 2:
			_version = SNMP_VERSION_2c;
			break;
		case 3:
			_version = SNMP_VERSION_3;
			break;
		default:
			_version = -1;
		}
Micha Mueller's avatar
Micha Mueller committed
114
	}
115

116
117
118
119
120
	const std::string& getSNMPCommunity()	const { return _snmpCommunity; }
	const std::string& getOIDPrefix() 		const { return _oidPrefix; }
	const std::string& getUsername()		const { return _username; }
	const std::string& getAuthKey()			const { return _authKey; }
	const std::string& getPrivKey()			const { return _privKey; }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
	uint64_t getAuthProto()				const { return *_authProto; }
	size_t getAuthProtoLen()			const { return _authProtoLen; }
	const std::string getAuthProtoString()		const {
		if (_authProto == usmHMACMD5AuthProtocol) {
			return std::string(AUTH_MD5);
		} else if (_authProto == usmHMACSHA1AuthProtocol) {
			return std::string(AUTH_SHA1);
		} else {
			return std::string("unknown");
		}
	}
	uint64_t getPrivProto()				const { return *_privProto; }
	size_t getPrivProtoLen()			const { return _privProtoLen; }
	const std::string getPrivProtoString()		const {
		if (std::memcmp(_privProto, usmDESPrivProtocol, _privProtoLen*sizeof(oid)) == 0) {
			return std::string(PRIV_DES);
		} else if (std::memcmp(_privProto, usmAESPrivProtocol, _privProtoLen*sizeof(oid)) == 0) {
			return std::string(PRIV_AES);
		} else {
			return std::string("unknown");
		}
	}
143
	const std::string& getHost()			const { return _name; }
144
145
146
147
148
149
150
151
152
153
154
155
156
	int getPort()					const { return _port; }
	int getSecurityLevel()				const { return _securityLevel; }
	const std::string getSecurityLevelString()		const {
		if (_securityLevel == SNMP_SEC_LEVEL_NOAUTH) {
			return std::string(SEC_NOAUTHNOPRIV);
		} else if (_securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV) {
			return std::string(SEC_AUTHNOPRIV);
		} else if (_securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
			return std::string(SEC_AUTHPRIV);
		} else {
			return std::string("unknown");
		}
	}
157
	long int getVersion() const { return _version; }
158

159
	void printEntityConfig(LOG_LEVEL ll) final override;
160

Micha Mueller's avatar
Micha Mueller committed
161
162
163
	/**
	 * Initializes the connection. Must be called once before the connection can be used
	 */
164
	void execOnInit() final override;
Micha Mueller's avatar
Micha Mueller committed
165

166
167
168
169
	/**
	 *  Open SNMP Session. Must be called once before issuing a get() call.
	 */
	bool open();
170

171
172
173
174
175
	/**
	 *  Close SNMP Session.
	 */
	void close();

Micha Mueller's avatar
Micha Mueller committed
176
177
178
179
180
181
182
183
	/**
	 * Issues a get request to _host for the value specified by the OID.
	 *
	 * @param OID		Pointer to the OID buffer which identifies the value to request
	 * @param OIDLen	Lenght of the OID buffer
	 *
	 * @return		The requested value
	 */
184
	int64_t get(const oid* const OID, size_t OIDLen);
Micha Mueller's avatar
Micha Mueller committed
185

186
private:
Micha Mueller's avatar
Micha Mueller committed
187
188
	bool		_isInitialized;

Micha Mueller's avatar
Micha Mueller committed
189
190
	std::string _snmpCommunity;
	std::string _oidPrefix;
Micha Mueller's avatar
Micha Mueller committed
191
192
193
194
195
196
197
	std::string _username;
	std::string _authKey;
	std::string	_privKey;
	oid*		_authProto;
	oid*		_privProto;
	size_t		_authProtoLen;
	size_t		_privProtoLen;
Micha Mueller's avatar
Micha Mueller committed
198
	int			_port;
Micha Mueller's avatar
Micha Mueller committed
199
200
	int			_securityLevel;
	long int	_version;
Micha Mueller's avatar
Micha Mueller committed
201
	struct snmp_session	_snmpSession;
202
	void*       _snmpSessp;
203
204
205
};

#endif /* SNMPCONNECTION_H_ */