SNMPSensorBase.h 1.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * SNMPSensorBase.h
 *
 *  Created on: 13.08.2018
 *      Author: Micha Mueller
 */

#ifndef SNMP_SNMPSENSORBASE_H_
#define SNMP_SNMPSENSORBASE_H_

#include "SNMPConnection.h"

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
15
#include "../../includes/SensorBase.h"
16

17
class SNMPSensorBase : public SensorBase {
18
19
public:
	SNMPSensorBase(const std::string& name) :
20
21
		SensorBase(name),
		_oidLen(0) {
22
23
24
		memset(_oid, 0x0, sizeof(oid) * MAX_OID_LEN);
	}

25
26
27
28
29
30
31
	SNMPSensorBase(const SNMPSensorBase& other) :
	  SensorBase(other),
	  _oidLen(other._oidLen) {
	  memset(_oid, 0x0, sizeof(oid) * MAX_OID_LEN);
	  memcpy(_oid, other._oid, other._oidLen);
	}

32
33
	virtual ~SNMPSensorBase() {}

34
35
36
37
38
39
40
41
42
	SNMPSensorBase operator=(const SNMPSensorBase& other) {
	  SensorBase::operator=(other);
	  memset(_oid, 0x0, sizeof(oid) * MAX_OID_LEN);
      memcpy(_oid, other._oid, _oidLen);
      _oidLen = other._oidLen;

      return *this;
	}

43
	void setOID(const std::string& oid) {
44
45
46
47
48
49
50
51
52
		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;
		}
	}

53
54
55
	size_t				getOIDLen()	const { return _oidLen; }
	const oid* const	getOID()	const { return _oid; }
	std::string 		getOIDString() {
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
		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;

};

#endif /* SNMP_SNMPSENSORBASE_H_ */