SNMPSensorBase.h 1.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * SNMPSensorBase.h
 *
 *  Created on: 13.08.2018
 *      Author: Micha Mueller
 */

#ifndef SNMP_SNMPSENSORBASE_H_
#define SNMP_SNMPSENSORBASE_H_

#include "../../headers/SensorBase.h"

#include "SNMPConnection.h"

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>

class SNMPSensorBase : virtual public SensorBase {
public:
	SNMPSensorBase(const std::string& name) :
21
22
		SensorBase(name),
		_oidLen(0) {
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
		memset(_oid, 0x0, sizeof(oid) * MAX_OID_LEN);
		_connection = NULL;
	}

	virtual ~SNMPSensorBase() {}

	void setConnection(SNMPConnection* connection) { _connection = connection; }

	void setOID(std::string oid) {
		size_t newoidnamelen = MAX_OID_LEN;
		if (read_objid(oid.c_str(), _oid, &newoidnamelen)) {
			_oidLen = newoidnamelen;
		} else {
			_oidLen = 0;
			//LOG(error) << _name << ": Cannot convert OID string: " << oid;
		}
	}

	SNMPConnection*	getConnection()	const { return _connection;	}

	const oid* 		getOID()		const { return _oid; }

	std::string getOIDString() {
		char buf[255];
		int len = snprint_objid(buf, 255, _oid, _oidLen);
		if (len == -1) {
			//LOG(error) << _name << ": getOIDString buffer not large enough!";
			return std::string("");
		}
		return std::string(buf, len);
	}

protected:
	oid		_oid[MAX_OID_LEN];
	size_t	_oidLen;
	SNMPConnection* _connection;

};

#endif /* SNMP_SNMPSENSORBASE_H_ */