Commit 3b75d64f authored by Micha Mueller's avatar Micha Mueller
Browse files

Start adaption of snmp plugin to sensorgroupsV2

parent ed673e9f
......@@ -10,10 +10,48 @@
SNMPConfigurator::SNMPConfigurator() {
/* Initialize SNMP library */
init_snmp("dcdbpusher_SNMPplugin");
_entityName = "connection";
_groupName = "group";
_baseName = "sensor";
}
SNMPConfigurator::~SNMPConfigurator() {}
void sensorBase(SNMPSensorBase& s, CFG_VAL config) {
//TODO sensor.setOID(sensor.getConnection()->getOIDPrefix() + "." + val.second.data());
ADD {
ATTRIBUTE("OID", setOID)
}
}
void sensorGroup(SNMPSensorGroup& s, CFG_VAL config) {
ADD {
//no group attributes currently
}
}
void sensorEntity(SNMPConnection& s, CFG_VAL config) {
ADD {
//ATTRIBUTE("Type", setType) //TODO would be relevant if we would support more than just agent mode...
//at the moment we just ignore it, as it can be only "Agent" anyways
ATTRIBUTE("Host", setHost)
ATTRIBUTE("Port", setPort)
ATTRIBUTE("Community", setSNMPCommunity)
ATTRIBUTE("OIDPrefix", setOIDPrefix)
ATTRIBUTE("Version", setVersion)
ATTRIBUTE("Username", setUsername)
ATTRIBUTE("SecLevel", setSecurityLevel)
ATTRIBUTE("AuthProto", setAuthProto)
ATTRIBUTE("PrivProto", setPrivProto)
ATTRIBUTE("AuthKey", setAuthKey)
ATTRIBUTE("PrivKey", setPrivKey)
//TODO MqttPart
}
}
bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
std::string mqttPartConnection;
......@@ -112,7 +150,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();
SNMPSingleSensor * snmpSensor = new SNMPSingleSensor(name);
SNMPSensorGroup * snmpSensor = new SNMPSensorGroup(name);
//first check if default sensor is given
boost::optional<boost::property_tree::iptree&> defaultC = c.second.get_child_optional("default");
......@@ -145,17 +183,3 @@ bool SNMPConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
}
return true;
}
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());
} else if (STRCMP(val, "OID")) {
sensor.setOID(sensor.getConnection()->getOIDPrefix() + "." + val.second.data());
}
}
LOG(debug) << " MQTT : " << sensor.getMqtt();
LOG(debug) << " OID : " << sensor.getOIDString();
return true;
}
......@@ -8,29 +8,25 @@
#ifndef SNMPCONFIGURATOR_H_
#define SNMPCONFIGURATOR_H_
#include "SNMPConnection.h"
#include "SNMPSingleSensor.h"
#include <list>
#include "../../includes/ConfiguratorTemplate.h"
class SNMPConfigurator : public ConfiguratorTemplate<SNMPSensorBase, SNMPSingleSensor> {
#include "SNMPConnection.h"
#include "SNMPSensorGroup.h"
typedef std::list<SNMPConnection> connectionList_t;
class SNMPConfigurator : public ConfiguratorTemplate<SNMPSensorBase, SNMPSensorGroup, SNMPConnection> {
public:
SNMPConfigurator();
virtual ~SNMPConfigurator();
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 derivedReadSensorBase(SNMPSensorBase& sensor, boost::property_tree::iptree& config) override;
/* Overwritten from ConfiguratorTemplate */
void sensorBase(SNMPSensorBase& s, CFG_VAL config) override;
void sensorGroup(SNMPSensorGroup& s, CFG_VAL config) override;
void sensorEntity(SNMPConnection& s, CFG_VAL config) override;
private:
connectionList_t _connections;
bool derivedReadConfig(boost::property_tree::iptree& cfg) override;
};
extern "C" ConfiguratorInterface* create() {
......
......@@ -19,13 +19,9 @@ public:
SNMPConnection();
virtual ~SNMPConnection();
void setSNMPCommunity(const std::string& community) {
_snmpCommunity = community;
}
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 . */
......@@ -36,96 +32,68 @@ public:
}
}
}
const std::string& getOIDPrefix() const {
return _oidPrefix;
}
void setUsername(const std::string& username) {
_username = username;
}
const std::string& getUsername() {
return _username;
}
void setAuthKey(const std::string& authKey) {
_authKey = authKey;
}
const std::string& getAuthKey() {
return _authKey;
}
void setPrivKey(const std::string& privKey) {
_privKey = privKey;
}
const std::string& getPrivKey() {
return _privKey;
}
void setAuthProto(oid* authProto) {
_authProto = authProto;
}
void setPrivProto(oid* privProto) {
_privProto = privProto;
}
void setAuthProtoLen(size_t authProtoLen) {
_authProtoLen = authProtoLen;
}
size_t getAuthProtoLen() {
return _authProtoLen;
}
void setPrivProtoLen(size_t privProtoLen) {
_privProtoLen = privProtoLen;
}
size_t getPrivProtoLen() {
return _privProtoLen;
}
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;
}
void setSecurityLevel(int securityLevel) {
_securityLevel = securityLevel;
void setUsername(const std::string& username) { _username = username; }
void setAuthKey(const std::string& authKey) { _authKey = authKey; }
void setPrivKey(const std::string& privKey) { _privKey = privKey; }
void setAuthProto(const std::string& authProto) {
if (authProto == "MD5") {
_authProto = usmHMACMD5AuthProtocol;
_authProtoLen = USM_AUTH_PROTO_MD5_LEN;
} else if (authProto == "SHA1") {
_authProto = usmHMACSHA1AuthProtocol;
_authProtoLen = USM_AUTH_PROTO_SHA_LEN;
}
}
int getSecurityLevel() {
return _securityLevel;
void setPrivProto(const std::string& privProto) {
if (privProto == "DES") {
_privProto = snmp_duplicate_objid(usmDESPrivProtocol, USM_PRIV_PROTO_DES_LEN);
_privProtoLen = USM_PRIV_PROTO_DES_LEN;
} else if (privProto == "AES") {
_privProto = snmp_duplicate_objid(usmAESPrivProtocol, USM_PRIV_PROTO_AES_LEN);
_privProtoLen = USM_PRIV_PROTO_AES_LEN;
}
}
void setVersion(long int version) {
_version = version;
void setHost(const std::string& host) { _host = host; }
void setPort(const std::string& port) { _port = stoi(port); }
void setSecurityLevel(const std::string& securityLevel) {
if (securityLevel == "noAuthNoPriv") {
_securityLevel = SNMP_SEC_LEVEL_NOAUTH;
} else if (securityLevel == "authNoPriv") {
_securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
} else if (securityLevel == "authPriv") {
_securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
}
}
long int getVersion() {
return _version;
void setVersion(const std::string& version) {
int vers = stoi(version);
switch(vers) {
case 1:
_version = SNMP_VERSION_1;
break;
case 2:
_version = SNMP_VERSION_2c;
break;
case 3:
_version = SNMP_VERSION_3;
break;
default:
_version = -1;
}
}
void initializeStrand(boost::asio::io_service& io);
boost::asio::io_service::strand* getStrand() const {
return _strand;
}
const std::string& getSNMPCommunity() const { return _snmpCommunity; }
const std::string& getOIDPrefix() const { return _oidPrefix; }
const std::string& getUsername() const { return _username; }
const std::string& getAuthKey() const { return _authKey; }
const std::string& getPrivKey() const { return _privKey; }
size_t getAuthProtoLen() const { return _authProtoLen; }
size_t getPrivProtoLen() const { return _privProtoLen; }
const std::string& getHost() const { return _host; }
int getPort() const { return _port; }
int getSecurityLevel() const { return _securityLevel; }
long int getVersion() const { return _version; }
boost::asio::io_service::strand* getStrand() const { return _strand; }
/**
* Initializes the connection. Must be called once before the connection can be used
......
......@@ -20,14 +20,11 @@ public:
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(const std::string& oid) {
size_t newoidnamelen = MAX_OID_LEN;
if (read_objid(oid.c_str(), _oid, &newoidnamelen)) {
_oidLen = newoidnamelen;
......@@ -37,10 +34,8 @@ public:
}
}
SNMPConnection* getConnection() const { return _connection; }
const oid* getOID() const { return _oid; }
size_t getOIDLen() const { return _oidLen; }
const oid* getOID() const { return _oid; }
std::string getOIDString() {
char buf[255];
int len = snprint_objid(buf, 255, _oid, _oidLen);
......@@ -54,7 +49,6 @@ public:
protected:
oid _oid[MAX_OID_LEN];
size_t _oidLen;
SNMPConnection* _connection;
};
......
/*
* SNMPSingleSensor.cpp
* SNMPSensorGroup.cpp
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
*/
#include "SNMPSingleSensor.h"
#include "timestamp.h"
#include "SNMPSensorGroup.h"
SNMPSingleSensor::SNMPSingleSensor(const std::string& name) :
SensorBase(name), SNMPSensorBase(name), SingleSensor(name) {}
SNMPSensorGroup::SNMPSensorGroup(const std::string& name) :
SensorGroupTemplate(name) {
_connection = nullptr;
}
SNMPSingleSensor::~SNMPSingleSensor() {}
SNMPSensorGroup::~SNMPSensorGroup() {}
void SNMPSingleSensor::init(boost::asio::io_service& io) {
void SNMPSensorGroup::init(boost::asio::io_service& io) {
SensorGroupTemplate::init(io);
if (_connection) {
_connection->initializeStrand(io);
} else {
LOG(error) << "No connection set for sensor " << _name << "! Cannot initialize sensor.";
LOG(error) << "No connection set for sensorgroup " << _groupName << "! Cannot initialize sensor.";
}
SingleSensor::init(io);
}
void SNMPSingleSensor::start() {
void SNMPSensorGroup::start() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensor " << _name << " already running.";
LOG(info) << "Sensorgroup " << _groupName << " already running.";
return;
}
if (_connection) {
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(_connection->getStrand()->wrap(std::bind(&SNMPSingleSensor::readAsync, this)));
LOG(info) << "Sensor " << _name << " started.";
_timer->async_wait(_connection->getStrand()->wrap(std::bind(&SNMPSensorGroup::readAsync, this)));
LOG(info) << "Sensorgroup " << _groupName << " started.";
} else {
LOG(error) << "No connection set for sensor " << _name << "! Cannot start polling.";
LOG(error) << "No connection set for sensorgroup " << _groupName << "! Cannot start polling.";
return;
}
}
void SNMPSingleSensor::stop() {
void SNMPSensorGroup::stop() {
_keepRunning = 0;
LOG(info) << "Sensor " << _name << " stopped.";
LOG(info) << "Sensorgroup " << _groupName << " stopped.";
}
void SNMPSingleSensor::read() {
void SNMPSensorGroup::read() {
reading_t reading;
reading.timestamp = getTimestamp();
try {
reading.value = _connection->issueGet(_oid, _oidLen);
storeReading(reading, _cacheIndex);
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
for(auto s : _sensors) {
try {
reading.value = _connection->issueGet(s->getOID(), s->getOIDLen());
} catch (const std::exception& e) {
LOG(error) << _groupName << "::" << s->getName() << " could not read value: " << e.what();
//dummy value
reading.value = 0;
}
#ifdef DEBUG
LOG(debug) << _name << ": \"" << reading.value << "\"";
LOG(debug) << _groupName << "::" << s->getName() << ": \"" << reading.value << "\"";
#endif
} catch (const std::exception& e) {
LOG(error) << _name << " could not read value: " << e.what();
//to keep the _cacheIndex uniform for all sensors store value in every case
s->storeReading(reading, _cacheIndex);
}
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
}
void SNMPSingleSensor::readAsync() {
void SNMPSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _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)));
_timer->async_wait(_connection->getStrand()->wrap(std::bind(&SNMPSensorGroup::readAsync, this)));
}
_pendingTasks--;
}
/*
* SNMPSingleSensor.h
* SNMPSensorGroup.h
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
*/
#ifndef SNMPSINGLESENSOR_H_
#define SNMPSINGLESENSOR_H_
#ifndef SNMPSENSORGROUP_H_
#define SNMPSENSORGROUP_H_
#include "SNMPSensorBase.h"
#include "../../includes/SingleSensor.h"
#include "../../includes/SensorGroupTemplate.h"
class SNMPSingleSensor : public SNMPSensorBase, public SingleSensor {
class SNMPSensorGroup : public SensorGroupTemplate<SNMPSensorBase> {
public:
SNMPSingleSensor(const std::string& name);
virtual ~SNMPSingleSensor();
SNMPSensorGroup(const std::string& name);
virtual ~SNMPSensorGroup();
void init(boost::asio::io_service& io) override;
void start() override;
void stop() override;
void setConnection(SNMPConnection* connection) { _connection = connection; }
SNMPConnection* getConnection() const { return _connection; }
private:
void read() override;
void readAsync() override;
SNMPConnection* _connection;
};
#endif /* SNMPSINGLESENSOR_H_ */
#endif /* SNMPSENSORGROUP_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