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 ...@@ -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 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 $(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 $(CXX) $(LIBFLAGS)$@ -o $@ $^ -L$(DCDBDEPLOYPATH)/lib/ -lboost_log -lboost_system -lnetsnmp -lnetsnmpagent
...@@ -183,7 +183,7 @@ protected: ...@@ -183,7 +183,7 @@ protected:
* @return True on success, false otherwise * @return True on success, false otherwise
*/ */
bool readSingleSensor(S& sensor, boost::property_tree::iptree& config) { 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 @@ ...@@ -5,12 +5,12 @@
* Author: Micha Mueller * Author: Micha Mueller
*/ */
#include "../../headers/SensorBase.h"
#include <limits.h>
#ifndef PERFEVENT_PERFSENSORBASE_H_ #ifndef PERFEVENT_PERFSENSORBASE_H_
#define PERFEVENT_PERFSENSORBASE_H_ #define PERFEVENT_PERFSENSORBASE_H_
#include "../../headers/SensorBase.h"
#include <limits.h>
class PerfSensorBase : virtual public SensorBase { class PerfSensorBase : virtual public SensorBase {
public: public:
PerfSensorBase(const std::string& name) : PerfSensorBase(const std::string& name) :
......
...@@ -20,7 +20,6 @@ class PerfeventConfigurator : public ConfiguratorTemplate<PerfSensorBase, PerfSi ...@@ -20,7 +20,6 @@ class PerfeventConfigurator : public ConfiguratorTemplate<PerfSensorBase, PerfSi
typedef std::map<std::string, unsigned int> enumMap_t; typedef std::map<std::string, unsigned int> enumMap_t;
public: public:
PerfeventConfigurator(); PerfeventConfigurator();
virtual ~PerfeventConfigurator(); virtual ~PerfeventConfigurator();
......
...@@ -112,7 +112,7 @@ bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) { ...@@ -112,7 +112,7 @@ bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
LOG(debug) << "Sensor \"" << c.second.data() << "\""; LOG(debug) << "Sensor \"" << c.second.data() << "\"";
if (!c.second.empty()) { if (!c.second.empty()) {
std::string name = connection.second.data() + "_" + c.second.data(); 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 //first check if default sensor is given
boost::optional<boost::property_tree::iptree&> defaultC = c.second.get_child_optional("default"); 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) { ...@@ -131,7 +131,7 @@ bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
snmpSensor->setMqtt(_mqttPrefix + mqttPartConnection); snmpSensor->setMqtt(_mqttPrefix + mqttPartConnection);
//read remaining values //read remaining values
if(readSensor(*snmpSensor, c.second)) { if(readSensorBase(*snmpSensor, c.second)) {
_sensors.push_back(snmpSensor); _sensors.push_back(snmpSensor);
} else { } else {
LOG(warning) << " Sensor \"" << c.second.data() << "\" has bad values! Ignoring..."; LOG(warning) << " Sensor \"" << c.second.data() << "\" has bad values! Ignoring...";
...@@ -146,7 +146,7 @@ bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) { ...@@ -146,7 +146,7 @@ bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
return true; 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) { BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (STRCMP(val, "mqttsuffix")) { if (STRCMP(val, "mqttsuffix")) {
sensor.setMqtt(sensor.getMqtt() + val.second.data()); sensor.setMqtt(sensor.getMqtt() + val.second.data());
......
...@@ -8,14 +8,14 @@ ...@@ -8,14 +8,14 @@
#ifndef SNMPCONFIGURATOR_H_ #ifndef SNMPCONFIGURATOR_H_
#define SNMPCONFIGURATOR_H_ #define SNMPCONFIGURATOR_H_
#include "SNMPSensor.h"
#include "SNMPConnection.h" #include "SNMPConnection.h"
#include <list> #include <list>
#include "../../ConfiguratorTemplate.h" #include "../../ConfiguratorTemplate.h"
#include "SNMPSingleSensor.h"
class SNMPConfigurator : public ConfiguratorTemplate<SNMPSensor> { class SNMPConfigurator : public ConfiguratorTemplate<SNMPSensorBase, SNMPSingleSensor> {
typedef std::list<SNMPConnection> connectionList_t; typedef std::list<SNMPConnection> connectionList_t;
...@@ -26,8 +26,8 @@ public: ...@@ -26,8 +26,8 @@ public:
protected: protected:
bool derivedReadConfig(boost::property_tree::iptree& cfg) override; bool derivedReadConfig(boost::property_tree::iptree& cfg) override;
void derivedReReadConfig() override { _connections.clear(); } void derivedReReadConfig() override { _connections.clear(); }
void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override {/*nothing to overwrite*/} void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override { /*nothing to overwrite*/ }
bool derivedReadSensor(SNMPSensor& sensor, boost::property_tree::iptree& config) override; bool derivedReadSensorBase(SNMPSensorBase& sensor, boost::property_tree::iptree& config) override;
private: private:
connectionList_t _connections; connectionList_t _connections;
......
/* /*
* SNMPSensor.h * SNMPSensorBase.h
* *
* Created on: 05.07.2018 * Created on: 13.08.2018
* Author: Axel Auweter (original), Micha Mueller * Author: Micha Mueller
*/ */
#ifndef SNMPSENSOR_H_ #ifndef SNMP_SNMPSENSORBASE_H_
#define SNMPSENSOR_H_ #define SNMP_SNMPSENSORBASE_H_
#include "../../headers/SensorBase.h"
#include "../../Sensor.h"
#include "SNMPConnection.h" #include "SNMPConnection.h"
#include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h> #include <net-snmp/net-snmp-includes.h>
class SNMPSensor :public Sensor { class SNMPSensorBase : virtual public SensorBase {
public: public:
SNMPSensor(const std::string& name); SNMPSensorBase(const std::string& name) :
virtual ~SNMPSensor(); 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) { void setOID(std::string oid) {
size_t newoidnamelen = MAX_OID_LEN; size_t newoidnamelen = MAX_OID_LEN;
if (read_objid(oid.c_str(), _oid, &newoidnamelen)) { if (read_objid(oid.c_str(), _oid, &newoidnamelen)) {
_oidLen = newoidnamelen; _oidLen = newoidnamelen;
} else { } else {
LOG(error) << _name << ": Cannot convert OID string: " << oid; _oidLen = 0;
//LOG(error) << _name << ": Cannot convert OID string: " << oid;
} }
} }
const oid* getOID() const { SNMPConnection* getConnection() const { return _connection; }
return _oid;
} const oid* getOID() const { return _oid; }
std::string getOIDString() { std::string getOIDString() {
char buf[255]; char buf[255];
int len = snprint_objid(buf, 255, _oid, _oidLen); int len = snprint_objid(buf, 255, _oid, _oidLen);
if (len == -1) { 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("");
} }
return std::string(buf, len); return std::string(buf, len);
} }
void setConnection(SNMPConnection* connection) { protected:
_connection = connection;
}
SNMPConnection* getConnection() const {
return _connection;
}
void init(boost::asio::io_service& io);
void read();
void readAsync();
void startPolling();
void stopPolling();
private:
oid _oid[MAX_OID_LEN]; oid _oid[MAX_OID_LEN];
size_t _oidLen; size_t _oidLen;
SNMPConnection* _connection; SNMPConnection* _connection;
}; };
#endif /* SNMPSENSOR_H_ */ #endif /* SNMP_SNMPSENSORBASE_H_ */
/* /*
* SNMPSensor.cpp * SNMPSingleSensor.cpp
* *
* Created on: 05.07.2018 * Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller * Author: Axel Auweter (original), Micha Mueller
*/ */
#include "SNMPSensor.h" #include "SNMPSingleSensor.h"
#include "timestamp.h" #include "timestamp.h"
SNMPSensor::SNMPSensor(const std::string& name) : SNMPSingleSensor::SNMPSingleSensor(const std::string& name) :
Sensor(name) { SensorBase(name), SNMPSensorBase(name), SingleSensor(name) {}
memset(_oid, 0x0, sizeof(oid) * MAX_OID_LEN);
_oidLen = 0;
_connection = NULL;
}
SNMPSensor::~SNMPSensor() { SNMPSingleSensor::~SNMPSingleSensor() {}
// Nothing to do
}
void SNMPSensor::read() { void SNMPSingleSensor::init(boost::asio::io_service& io) {
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) {
if (_connection) { if (_connection) {
_connection->initializeStrand(io); _connection->initializeStrand(io);
} else { } else {
LOG(error) << "No connection set for sensor " << _name << "! Cannot initialize sensor."; 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) { if (_keepRunning) {
//we have been started already //we have been started already
LOG(info) << "Sensor " << _name << " already running."; LOG(info) << "Sensor " << _name << " already running.";
...@@ -65,7 +33,7 @@ void SNMPSensor::startPolling() { ...@@ -65,7 +33,7 @@ void SNMPSensor::startPolling() {
if (_connection) { if (_connection) {
_keepRunning = 1; _keepRunning = 1;
_pendingTasks++; _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."; LOG(info) << "Sensor " << _name << " started.";
} else { } else {
LOG(error) << "No connection set for sensor " << _name << "! Cannot start polling."; LOG(error) << "No connection set for sensor " << _name << "! Cannot start polling.";
...@@ -73,7 +41,35 @@ void SNMPSensor::startPolling() { ...@@ -73,7 +41,35 @@ void SNMPSensor::startPolling() {
} }
} }
void SNMPSensor::stopPolling() { void SNMPSingleSensor::stop() {
_keepRunning = 0; _keepRunning = 0;
LOG(info) << "Sensor " << _name << " stopped."; 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_ */
Supports Markdown
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