Commit d094ba1a authored by Micha Mueller's avatar Micha Mueller
Browse files

Adapt SNMP plugin to new class structure

parent 4e2daf27
......@@ -87,5 +87,5 @@ libdcdbplugin_pdu.$(LIBEXT): src/Sensor.o src/sensors/pdu/PDUSensor.o src/sensor
libdcdbplugin_bacnet.$(LIBEXT): src/Sensor.o src/sensors/bacnet/BACnetSensor.o src/sensors/bacnet/BACnetClient.o src/sensors/bacnet/BACnetConfigurator.o
$(CXX) $(LIBFLAGS)$@ -o $@ $^ -L$(DCDBDEPLOYPATH)/lib/ -lboost_log -lboost_system -lbacnet
libdcdbplugin_snmp.$(LIBEXT): src/Sensor.o src/sensors/snmp/SNMPSensor.o src/sensors/snmp/SNMPConnection.o src/sensors/snmp/SNMPConfigurator.o
libdcdbplugin_snmp.$(LIBEXT): src/sensors/snmp/SNMPSingleSensor.o src/sensors/snmp/SNMPConnection.o src/sensors/snmp/SNMPConfigurator.o
$(CXX) $(LIBFLAGS)$@ -o $@ $^ -L$(DCDBDEPLOYPATH)/lib/ -lboost_log -lboost_system -lnetsnmp -lnetsnmpagent
......@@ -183,7 +183,7 @@ protected:
* @return True on success, false otherwise
*/
bool readSingleSensor(S& sensor, boost::property_tree::iptree& config) {
return (readSensorBase(sensor, config) | readSensorInterface(sensor, config));
return (readSensorBase(sensor, config) & readSensorInterface(sensor, config));
}
/**
......
......@@ -5,12 +5,12 @@
* Author: Micha Mueller
*/
#include "../../headers/SensorBase.h"
#include <limits.h>
#ifndef PERFEVENT_PERFSENSORBASE_H_
#define PERFEVENT_PERFSENSORBASE_H_
#include "../../headers/SensorBase.h"
#include <limits.h>
class PerfSensorBase : virtual public SensorBase {
public:
PerfSensorBase(const std::string& name) :
......
......@@ -20,7 +20,6 @@ class PerfeventConfigurator : public ConfiguratorTemplate<PerfSensorBase, PerfSi
typedef std::map<std::string, unsigned int> enumMap_t;
public:
PerfeventConfigurator();
virtual ~PerfeventConfigurator();
......
......@@ -112,7 +112,7 @@ bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
LOG(debug) << "Sensor \"" << c.second.data() << "\"";
if (!c.second.empty()) {
std::string name = connection.second.data() + "_" + c.second.data();
SNMPSensor * snmpSensor = new SNMPSensor(name);
SNMPSingleSensor * snmpSensor = new SNMPSingleSensor(name);
//first check if default sensor is given
boost::optional<boost::property_tree::iptree&> defaultC = c.second.get_child_optional("default");
......@@ -131,7 +131,7 @@ bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
snmpSensor->setMqtt(_mqttPrefix + mqttPartConnection);
//read remaining values
if(readSensor(*snmpSensor, c.second)) {
if(readSensorBase(*snmpSensor, c.second)) {
_sensors.push_back(snmpSensor);
} else {
LOG(warning) << " Sensor \"" << c.second.data() << "\" has bad values! Ignoring...";
......@@ -146,7 +146,7 @@ bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
return true;
}
bool SNMPConfigurator::derivedReadSensor(SNMPSensor& sensor, boost::property_tree::iptree& config) {
bool SNMPConfigurator::derivedReadSensorBase(SNMPSensorBase& sensor, boost::property_tree::iptree& config) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (STRCMP(val, "mqttsuffix")) {
sensor.setMqtt(sensor.getMqtt() + val.second.data());
......
......@@ -8,14 +8,14 @@
#ifndef SNMPCONFIGURATOR_H_
#define SNMPCONFIGURATOR_H_
#include "SNMPSensor.h"
#include "SNMPConnection.h"
#include <list>
#include "../../ConfiguratorTemplate.h"
#include "SNMPSingleSensor.h"
class SNMPConfigurator : public ConfiguratorTemplate<SNMPSensor> {
class SNMPConfigurator : public ConfiguratorTemplate<SNMPSensorBase, SNMPSingleSensor> {
typedef std::list<SNMPConnection> connectionList_t;
......@@ -26,8 +26,8 @@ public:
protected:
bool derivedReadConfig(boost::property_tree::iptree& cfg) override;
void derivedReReadConfig() override { _connections.clear(); }
void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override {/*nothing to overwrite*/}
bool derivedReadSensor(SNMPSensor& sensor, boost::property_tree::iptree& config) override;
void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override { /*nothing to overwrite*/ }
bool derivedReadSensorBase(SNMPSensorBase& sensor, boost::property_tree::iptree& config) override;
private:
connectionList_t _connections;
......
/*
* SNMPSensor.h
* SNMPSensorBase.h
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
* Created on: 13.08.2018
* Author: Micha Mueller
*/
#ifndef SNMPSENSOR_H_
#define SNMPSENSOR_H_
#ifndef SNMP_SNMPSENSORBASE_H_
#define SNMP_SNMPSENSORBASE_H_
#include "../../headers/SensorBase.h"
#include "../../Sensor.h"
#include "SNMPConnection.h"
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
class SNMPSensor :public Sensor {
class SNMPSensorBase : virtual public SensorBase {
public:
SNMPSensor(const std::string& name);
virtual ~SNMPSensor();
SNMPSensorBase(const std::string& name) :
SensorBase(name), _oidLen(0) {
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 {
LOG(error) << _name << ": Cannot convert OID string: " << oid;
_oidLen = 0;
//LOG(error) << _name << ": Cannot convert OID string: " << oid;
}
}
const oid* getOID() const {
return _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!";
//LOG(error) << _name << ": getOIDString buffer not large enough!";
return std::string("");
}
return std::string(buf, len);
}
void setConnection(SNMPConnection* connection) {
_connection = connection;
}
SNMPConnection* getConnection() const {
return _connection;
}
void init(boost::asio::io_service& io);
void read();
void readAsync();
void startPolling();
void stopPolling();
private:
protected:
oid _oid[MAX_OID_LEN];
size_t _oidLen;
SNMPConnection* _connection;
};
#endif /* SNMPSENSOR_H_ */
#endif /* SNMP_SNMPSENSORBASE_H_ */
/*
* SNMPSensor.cpp
* SNMPSingleSensor.cpp
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
*/
#include "SNMPSensor.h"
#include "SNMPSingleSensor.h"
#include "timestamp.h"
SNMPSensor::SNMPSensor(const std::string& name) :
Sensor(name) {
memset(_oid, 0x0, sizeof(oid) * MAX_OID_LEN);
_oidLen = 0;
_connection = NULL;
}
SNMPSingleSensor::SNMPSingleSensor(const std::string& name) :
SensorBase(name), SNMPSensorBase(name), SingleSensor(name) {}
SNMPSensor::~SNMPSensor() {
// Nothing to do
}
SNMPSingleSensor::~SNMPSingleSensor() {}
void SNMPSensor::read() {
reading_t reading;
reading.timestamp = getTimestamp();
try {
reading.value = _connection->issueGet(_oid, _oidLen);
storeReading(reading);
#ifdef DEBUG
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
} catch (const std::exception& e) {
LOG(error) << _name << " could not read value: " << e.what();
}
}
void SNMPSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
_timer->async_wait(_connection->getStrand()->wrap(std::bind(&SNMPSensor::readAsync, this)));
}
_pendingTasks--;
}
void SNMPSensor::init(boost::asio::io_service& io) {
void SNMPSingleSensor::init(boost::asio::io_service& io) {
if (_connection) {
_connection->initializeStrand(io);
} else {
LOG(error) << "No connection set for sensor " << _name << "! Cannot initialize sensor.";
}
Sensor::init(io);
SingleSensor::init(io);
}
void SNMPSensor::startPolling() {
void SNMPSingleSensor::start() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensor " << _name << " already running.";
......@@ -65,7 +33,7 @@ void SNMPSensor::startPolling() {
if (_connection) {
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(_connection->getStrand()->wrap(std::bind(&SNMPSensor::readAsync, this)));
_timer->async_wait(_connection->getStrand()->wrap(std::bind(&SNMPSingleSensor::readAsync, this)));
LOG(info) << "Sensor " << _name << " started.";
} else {
LOG(error) << "No connection set for sensor " << _name << "! Cannot start polling.";
......@@ -73,7 +41,35 @@ void SNMPSensor::startPolling() {
}
}
void SNMPSensor::stopPolling() {
void SNMPSingleSensor::stop() {
_keepRunning = 0;
LOG(info) << "Sensor " << _name << " stopped.";
}
void SNMPSingleSensor::read() {
reading_t reading;
reading.timestamp = getTimestamp();
try {
reading.value = _connection->issueGet(_oid, _oidLen);
storeReading(reading, _cacheIndex);
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
#ifdef DEBUG
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
} catch (const std::exception& e) {
LOG(error) << _name << " could not read value: " << e.what();
}
}
void SNMPSingleSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
_timer->async_wait(_connection->getStrand()->wrap(std::bind(&SNMPSingleSensor::readAsync, this)));
}
_pendingTasks--;
}
/*
* SNMPSingleSensor.h
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
*/
#ifndef SNMPSINGLESENSOR_H_
#define SNMPSINGLESENSOR_H_
#include "SNMPSensorBase.h"
#include "../../headers/SingleSensor.h"
class SNMPSingleSensor :public SNMPSensorBase, public SingleSensor {
public:
SNMPSingleSensor(const std::string& name);
virtual ~SNMPSingleSensor();
void init(boost::asio::io_service& io) override;
void start() override;
void stop() override;
private:
void read() override;
void readAsync() override;
private:
};
#endif /* SNMPSINGLESENSOR_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment