SNMPConnection.h 1.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * SNMPConnection.h
 *
 *  Created on: 05.07.2018
 *      Author: Axel Auweter (original), Micha Mueller
 */

#ifndef SNMPCONNECTION_H_
#define SNMPCONNECTION_H_

#include "../../Logging.h"

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
Micha Mueller's avatar
Micha Mueller committed
15
#include <boost/asio.hpp>
16
17
18
19
20
21
22

class SNMPConnection {

public:
	SNMPConnection();
	virtual ~SNMPConnection();

Micha Mueller's avatar
Micha Mueller committed
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
	void setSNMPCommunity(const std::string& community) {
		_snmpCommunity = community;
	}

	const std::string& getSNMPCommunity() const {
		return _snmpCommunity;
	}

	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);
			}
		}
	}

	const std::string& getOIDPrefix() const {
		return _oidPrefix;
	}

	void setHost(const std::string& host) {
		_host = host;
	}

	const std::string& getHost() const {
		return _host;
	}

	void setPort(int port) {
		_port = port;
	}

	int getPort() const {
		return _port;
	}

61
62
63
64
65
66
	void initializeStrand(boost::asio::io_service& io);

	boost::asio::io_service::strand* getStrand() const {
		return _strand;
	}

Micha Mueller's avatar
Micha Mueller committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
	/**
	 * Initializes the connection. Must be called once before the connection can be used
	 */
	void init();

	/**
	 * 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
	 */
	uint64_t issueGet(oid* OID, size_t OIDLen);

82
private:
Micha Mueller's avatar
Micha Mueller committed
83
84
85
86
87
88
	std::string _snmpCommunity;
	std::string _oidPrefix;
	std::string	_host;
	int			_port;
	struct snmp_session	_snmpSession;

89
90
91
92
93
	boost::asio::io_service::strand*	_strand;
	boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
};

#endif /* SNMPCONNECTION_H_ */