Commit 6eacc129 authored by Micha Mueller's avatar Micha Mueller
Browse files

Add code skeleton for upcoming SNMP plugin

parent 2e77b9be
/*
* SNMPConfigurator.cpp
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
*/
#include "SNMPConfigurator.h"
#include <boost/foreach.hpp>
#include <boost/property_tree/info_parser.hpp>
SNMPConfigurator::SNMPConfigurator() {
// TODO Auto-generated constructor stub
}
SNMPConfigurator::~SNMPConfigurator() {
// TODO Auto-generated destructor stub
}
bool SNMPConfigurator::readConfig(std::string cfgPath) {
//TODO
return true;
}
bool SNMPConfigurator::readSensor(SNMPSensor& sensor, boost::property_tree::iptree& config) {
//TODO
return true;
}
/*
* SNMPConfigurator.h
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
*/
#ifndef SNMPCONFIGURATOR_H_
#define SNMPCONFIGURATOR_H_
#include "../../Configurator.h"
#include "SNMPSensor.h"
#include "SNMPConnection.h"
#include <string>
#include <vector>
#include <map>
#include <boost/property_tree/ptree.hpp>
class SNMPConfigurator : public Configurator {
typedef std::map<std::string, SNMPSensor> sensorMap_t;
public:
SNMPConfigurator();
virtual ~SNMPConfigurator();
/**
* Read in the configuration for snmp-connections and sensors.
* @param cfgPath Path + name of the config-file
* @return true on success, false otherwise
*/
bool readConfig(std::string cfgPath);
//Overwrite from Configurator
bool reReadConfig() {
//TODO
_templateSensors.clear();
return Configurator::reReadConfig();
}
private:
/**
* Set the variables of sensor according to the values specified in config.
* @param sensor The sensor to be configured
* @param config A property(sub)tree containing the values
*/
bool readSensor(SNMPSensor& sensor, boost::property_tree::iptree& config);
sensorMap_t _templateSensors;
};
extern "C" Configurator* create() {
return new SNMPConfigurator;
}
extern "C" void destroy(Configurator* c) {
delete c;
}
#endif /* SNMPCONFIGURATOR_H_ */
/*
* SNMPConnection.cpp
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
*/
#include "SNMPConnection.h"
SNMPConnection::SNMPConnection() {
// TODO Auto-generated constructor stub
}
SNMPConnection::~SNMPConnection() {
// TODO Auto-generated destructor stub
}
void SNMPConnection::initializeStrand(boost::asio::io_service& io) {
if (!_strand) {
_strand = new boost::asio::io_service::strand(io);
}
}
/*
* SNMPConnection.h
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
*/
#ifndef SNMPCONNECTION_H_
#define SNMPCONNECTION_H_
#include "../../Logging.h"
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
class SNMPConnection {
public:
SNMPConnection();
virtual ~SNMPConnection();
void initializeStrand(boost::asio::io_service& io);
boost::asio::io_service::strand* getStrand() const {
return _strand;
}
private:
boost::asio::io_service::strand* _strand;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
};
#endif /* SNMPCONNECTION_H_ */
/*
* SNMPSensor.cpp
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
*/
#include "SNMPSensor.h"
#include "timestamp.h"
SNMPSensor::SNMPSensor(const std::string& name) :
Sensor(name) {
// TODO Auto-generated constructor stub
_connection = NULL;
}
SNMPSensor::~SNMPSensor() {
// TODO Auto-generated destructor stub
}
void SNMPSensor::read() {
reading_t reading;
char buf[1024];
reading.timestamp = getTimestamp();
//TODO
#ifdef DEBUG
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
}
storeReading(reading);
}
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));
_timer->async_wait(_connection->getStrand()->wrap(std::bind(&SNMPSensor::readAsync, this)));
}
}
void SNMPSensor::init(boost::asio::io_service& io) {
if (_connection) {
_connection->initializeStrand(io);
} else {
LOG(error) << "No host set for sensor " << _name << "! Cannot initialize sensor.";
}
Sensor::init(io);
}
void SNMPSensor::startPolling() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensor " << _name << " already running.";
return;
}
//TODO
if (_connection) {
_keepRunning = 1;
_timer->async_wait(_connection->getStrand()->wrap(std::bind(&SNMPSensor::readAsync, this)));
LOG(info) << "Sensor " << _name << " started.";
} else {
LOG(error) << "No connection set for sensor " << _name << "! Cannot start polling.";
return;
}
}
void SNMPSensor::stopPolling() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
//wait until read() finished before closing _file
_timer->wait();
//TODO
LOG(info) << "Sensor " << _name << " stopped.";
}
/*
* SNMPSensor.h
*
* Created on: 05.07.2018
* Author: Axel Auweter (original), Micha Mueller
*/
#ifndef SNMPSENSOR_H_
#define SNMPSENSOR_H_
#include "../../Sensor.h"
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
class SNMPSensor :public Sensor {
public:
SNMPSensor(const std::string& name);
virtual ~SNMPSensor();
void init(boost::asio::io_service& io);
void read();
void readAsync();
void startPolling();
void stopPolling();
private:
oid _oidName[MAX_OID_LEN];
size_t _oidNameLen;
SNMPConnection* _connection;
};
#endif /* SNMPSENSOR_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