Commit 4361e22d authored by Alessio Netti's avatar Alessio Netti
Browse files

Merge remote-tracking branch 'origin/master'

parents 4da0788a 9f1bfba9
......@@ -137,7 +137,7 @@ public:
LOG(debug) << "Template " << _groupName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
SGroup* group = new SGroup(val.second.data());
if (readSensorGroup(*group, val.second)) {
if (readSensorGroup(*group, val.second, true)) {
auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(val.second.data(), group));
if(!ret.second) {
LOG(warning) << "Template " << _groupName << " " << val.second.data() << " already exists! Omitting...";
......@@ -153,7 +153,7 @@ public:
LOG(debug) << "Template " << _baseName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
SBase* base = new SBase(val.second.data());
if (readSensorBase(*base, val.second)) {
if (readSensorBase(*base, val.second, true)) {
auto ret = _templateSensorBases.insert(std::pair<std::string, SBase*>(val.second.data(), base));
if(!ret.second) {
LOG(warning) << "Template " << _baseName << " " << val.second.data() << " already exists! Omitting...";
......@@ -169,10 +169,10 @@ public:
LOG(debug) << "Template single " << _baseName << " \"" << val.second.data() << "\"";
if (!val.second.empty()) {
SGroup* group = new SGroup(val.second.data());
if (readSensorGroup(*group, val.second)) {
if (readSensorGroup(*group, val.second, true)) {
//group which consists of only one sensor
SB_Ptr sensor = std::make_shared<SBase>(val.second.data());
if (readSensorBase(*sensor, val.second)) {
if (readSensorBase(*sensor, val.second, true)) {
group->pushBackSensor(sensor);
auto ret = _templateSensorGroups.insert(std::pair<std::string, SGroup*>(val.second.data(), group));
if(!ret.second) {
......@@ -340,22 +340,25 @@ protected:
*
* @param sBase The sensor base for which to set the values
* @param config A boost property (sub-)tree containing the sensor values
* @param isTemplate Are we parsing a template or a regular sensor?
*
* @return True on success, false otherwise
*/
bool readSensorBase(SBase& sBase, CFG_VAL config) {
bool readSensorBase(SBase& sBase, CFG_VAL config, bool isTemplate=false) {
sBase.setCacheInterval(_cacheInterval);
boost::optional<boost::property_tree::iptree&> def = config.get_child_optional("default");
if(def) {
//we copy all values from default (including copy constructing its sensors)
//if own sensors are specified they are appended
LOG(debug) << " Using \"" << def.get().data() << "\" as default.";
auto it = _templateSensorBases.find(def.get().data());
if(it != _templateSensorBases.end()) {
sBase = *(it->second);
sBase.setName(config.data());
} else {
LOG(warning) << "Template " << _baseName << "\" " << def.get().data() << "\" not found! Using standard values.";
if (!isTemplate) {
boost::optional<boost::property_tree::iptree&> def = config.get_child_optional("default");
if(def) {
//we copy all values from default (including copy constructing its sensors)
//if own sensors are specified they are appended
LOG(debug) << " Using \"" << def.get().data() << "\" as default.";
auto it = _templateSensorBases.find(def.get().data());
if(it != _templateSensorBases.end()) {
sBase = *(it->second);
sBase.setName(config.data());
} else {
LOG(warning) << "Template " << _baseName << "\" " << def.get().data() << "\" not found! Using standard values.";
}
}
}
......@@ -387,22 +390,25 @@ protected:
*
* @param sGroup The sensor group for which to set the values
* @param config A boost property (sub-)tree containing the sensor values
* @param isTemplate Is this a template or a regular group?
*
* @return True on success, false otherwise
*/
bool readSensorGroup(SGroup& sGroup, CFG_VAL config) {
//first check if default group is given
boost::optional<boost::property_tree::iptree&> def = config.get_child_optional("default");
if(def) {
//we copy all values from default (including copy constructing its sensors)
//if own sensors are specified they are appended
LOG(debug) << " Using \"" << def.get().data() << "\" as default.";
auto it = _templateSensorGroups.find(def.get().data());
if(it != _templateSensorGroups.end()) {
sGroup = *(it->second);
sGroup.setGroupName(config.data());
} else {
LOG(warning) << "Template " << _groupName << "\" " << def.get().data() << "\" not found! Using standard values.";
bool readSensorGroup(SGroup& sGroup, CFG_VAL config, bool isTemplate=false) {
//first check if default group is given, unless we are reading a template already
if (!isTemplate) {
boost::optional<boost::property_tree::iptree&> def = config.get_child_optional("default");
if(def) {
//we copy all values from default (including copy constructing its sensors)
//if own sensors are specified they are appended
LOG(debug) << " Using \"" << def.get().data() << "\" as default.";
auto it = _templateSensorGroups.find(def.get().data());
if(it != _templateSensorGroups.end()) {
sGroup = *(it->second);
sGroup.setGroupName(config.data());
} else {
LOG(warning) << "Template " << _groupName << "\" " << def.get().data() << "\" not found! Using standard values.";
}
}
}
......@@ -421,11 +427,13 @@ protected:
sGroup.setSync(true);
}
} else if (boost::iequals(val.first, _baseName)) {
LOG(debug) << " " << _baseName << " " << val.second.data();
if (!isTemplate) {
LOG(debug) << " " << _baseName << " " << val.second.data();
}
SB_Ptr sensor = std::make_shared<SBase>(val.second.data());
if (readSensorBase(*sensor, val.second)) {
if (readSensorBase(*sensor, val.second, isTemplate)) {
sGroup.pushBackSensor(sensor);
} else {
} else if (!isTemplate) {
LOG(warning) << _baseName << " " << sGroup.getGroupName() << "::" << sensor->getName() << " could not be read! Omitting";
}
}
......
......@@ -20,7 +20,7 @@ SNMPConfigurator::~SNMPConfigurator() {}
void SNMPConfigurator::sensorBase(SNMPSensorBase& s, CFG_VAL config) {
ADD {
ATTRIBUTE("OID", setOID);
ATTRIBUTE("OIDSuffix", setOIDSuffix);
}
}
......
......@@ -178,3 +178,38 @@ int64_t SNMPConnection::issueGet(const oid* const OID, size_t OIDLen) {
snmp_close(ss);
return ret;
}
void SNMPConnection::printConfig() {
LOG(debug) << " Host: " << getHost();
LOG(debug) << " Port: " << getPort();
LOG(debug) << " OIDPrefix: " << getOIDPrefix();
std::string version("unknown");
switch(getVersion()) {
case SNMP_VERSION_1:
version = "1";
break;
case SNMP_VERSION_2c:
version = "2c";
break;
case SNMP_VERSION_3:
version = "3";
break;
}
LOG(debug) << " Version: " << version;
if (getVersion() < SNMP_VERSION_3) {
LOG(debug) << " Community: " << getSNMPCommunity();
} else {
LOG(debug) << " Username: " << getUsername();
LOG(debug) << " SecLevel: " << getSecurityLevelString();
if (_securityLevel != SNMP_SEC_LEVEL_NOAUTH) {
LOG(debug) << " AuthProto: " << getAuthProtoString();
LOG(debug) << " AuthKey: " << getAuthKey();
}
if (_securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
LOG(debug) << " PrivProto: " << getPrivProtoString();
LOG(debug) << " PrivKey: " << getPrivKey();
}
}
LOG(debug) << " MqttPart: " << getMqttPart();
}
......@@ -11,8 +11,17 @@
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>
#include "../../includes/Logging.h"
#define AUTH_MD5 "MD5"
#define AUTH_SHA1 "SHA1"
#define PRIV_DES "DES"
#define PRIV_AES "AES"
#define SEC_NOAUTHNOPRIV "noAuthNoPriv"
#define SEC_AUTHNOPRIV "authNoPriv"
#define SEC_AUTHPRIV "authPriv"
class SNMPConnection {
public:
......@@ -40,19 +49,19 @@ public:
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") {
if (boost::iequals(authProto, AUTH_MD5)) {
_authProto = usmHMACMD5AuthProtocol;
_authProtoLen = USM_AUTH_PROTO_MD5_LEN;
} else if (authProto == "SHA1") {
} else if (boost::iequals(authProto, AUTH_SHA1)) {
_authProto = usmHMACSHA1AuthProtocol;
_authProtoLen = USM_AUTH_PROTO_SHA_LEN;
}
}
void setPrivProto(const std::string& privProto) {
if (privProto == "DES") {
if (boost::iequals(privProto, PRIV_DES)) {
_privProto = snmp_duplicate_objid(usmDESPrivProtocol, USM_PRIV_PROTO_DES_LEN);
_privProtoLen = USM_PRIV_PROTO_DES_LEN;
} else if (privProto == "AES") {
} else if (boost::iequals(privProto, PRIV_AES)) {
_privProto = snmp_duplicate_objid(usmAESPrivProtocol, USM_PRIV_PROTO_AES_LEN);
_privProtoLen = USM_PRIV_PROTO_AES_LEN;
}
......@@ -60,11 +69,11 @@ public:
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") {
if (boost::iequals(securityLevel, SEC_NOAUTHNOPRIV)) {
_securityLevel = SNMP_SEC_LEVEL_NOAUTH;
} else if (securityLevel == "authNoPriv") {
} else if (boost::iequals(securityLevel, SEC_AUTHNOPRIV)) {
_securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
} else if (securityLevel == "authPriv") {
} else if (boost::iequals(securityLevel, SEC_AUTHPRIV)) {
_securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
}
}
......@@ -92,13 +101,46 @@ public:
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; }
uint64_t getAuthProto() const { return *_authProto; }
size_t getAuthProtoLen() const { return _authProtoLen; }
const std::string getAuthProtoString() const {
if (_authProto == usmHMACMD5AuthProtocol) {
return std::string(AUTH_MD5);
} else if (_authProto == usmHMACSHA1AuthProtocol) {
return std::string(AUTH_SHA1);
} else {
return std::string("unknown");
}
}
uint64_t getPrivProto() const { return *_privProto; }
size_t getPrivProtoLen() const { return _privProtoLen; }
const std::string getPrivProtoString() const {
if (std::memcmp(_privProto, usmDESPrivProtocol, _privProtoLen*sizeof(oid)) == 0) {
return std::string(PRIV_DES);
} else if (std::memcmp(_privProto, usmAESPrivProtocol, _privProtoLen*sizeof(oid)) == 0) {
return std::string(PRIV_AES);
} else {
return std::string("unknown");
}
}
const std::string& getHost() const { return _host; }
int getPort() const { return _port; }
int getSecurityLevel() const { return _securityLevel; }
long int getVersion() const { return _version; }
int getPort() const { return _port; }
int getSecurityLevel() const { return _securityLevel; }
const std::string getSecurityLevelString() const {
if (_securityLevel == SNMP_SEC_LEVEL_NOAUTH) {
return std::string(SEC_NOAUTHNOPRIV);
} else if (_securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV) {
return std::string(SEC_AUTHNOPRIV);
} else if (_securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
return std::string(SEC_AUTHPRIV);
} else {
return std::string("unknown");
}
}
long int getVersion() const { return _version; }
boost::asio::io_service::strand* getStrand() const { return _strand; }
void printConfig();
/**
* Initializes the connection. Must be called once before the connection can be used
......
......@@ -13,6 +13,8 @@
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include "../../includes/SensorBase.h"
#include "../../includes/Logging.h"
class SNMPSensorBase : public SensorBase {
public:
......@@ -40,6 +42,10 @@ public:
return *this;
}
void setOIDSuffix(const std::string& suffix) {
_oidSuffix = suffix;
}
void setOID(const std::string& oid) {
size_t newoidnamelen = MAX_OID_LEN;
if (read_objid(oid.c_str(), _oid, &newoidnamelen)) {
......@@ -50,9 +56,9 @@ public:
}
}
size_t getOIDLen() const { return _oidLen; }
size_t getOIDLen() const { return _oidLen; }
const oid* const getOID() const { return _oid; }
std::string getOIDString() {
const std::string getOIDString() {
char buf[255];
int len = snprint_objid(buf, 255, _oid, _oidLen);
if (len == -1) {
......@@ -61,11 +67,25 @@ public:
}
return std::string(buf, len);
}
const std::string getOIDSuffix() const { return _oidSuffix; }
void printConfig() {
LOG(debug) << " Sensor " << getName() << ":";
LOG(debug) << " mqtt: " << getMqtt();
LOG(debug) << " skipConstVal: " << getSkipConstVal();
LOG(debug) << " delta: " << isDelta();
LOG(debug) << " sink: " << getSinkPath();
LOG(debug) << " subSampling: " << getSubsampling();
LOG(debug) << " OID: " << getOIDString();
}
protected:
oid _oid[MAX_OID_LEN];
size_t _oidLen;
std::string _oidSuffix;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
};
#endif /* SNMP_SNMPSENSORBASE_H_ */
......@@ -27,6 +27,8 @@ SNMPSensorGroup& SNMPSensorGroup::operator=(const SNMPSensorGroup& other) {
void SNMPSensorGroup::init(boost::asio::io_service& io) {
SensorGroupTemplate::init(io);
printConfig();
if (_connection) {
_connection->initializeStrand(io);
} else {
......@@ -57,6 +59,17 @@ void SNMPSensorGroup::stop() {
LOG(info) << "Sensorgroup " << _groupName << " stopped.";
}
void SNMPSensorGroup::printConfig() {
LOG(debug) << "Sensorgroup " << _groupName << ":";
LOG(debug) << " Connection: ";
getConnection()->printConfig();
for(const auto& s : _sensors) {
s->printConfig();
}
}
void SNMPSensorGroup::read() {
reading_t reading;
reading.timestamp = getTimestamp();
......
......@@ -22,10 +22,11 @@ public:
void init(boost::asio::io_service& io) override;
void start() override;
void stop() override;
void printConfig();
void setOIDPrefix(const std::string& oidPrefix) {
for(auto s : _sensors) {
s->setOID(oidPrefix + s->getOIDString());
s->setOID(oidPrefix + s->getOIDSuffix());
}
}
void setConnection(SNMPConnection* connection) { _connection = connection; }
......
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