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

sysfs-functionality is now available as dynamic library.

Finished rework of sysfs-files.
Adapted and improved various other involved classes.
TODOs remaining:
-adapting Makefile for automated compilation of librarys
-solving runtime errors when trying to run dcdbpusher with sysfs
parent 49c79894
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* Configuration.cpp * Configuration.cpp
* *
* Created on: 13.12.2017 * Created on: 13.12.2017
* Author: Micha Mueller * Author: Michael Ott (original), Micha Mueller
*/ */
#include "Configuration.h" #include "Configuration.h"
...@@ -29,10 +29,53 @@ Configuration::Configuration(const std::string& cfgFilePath) : ...@@ -29,10 +29,53 @@ Configuration::Configuration(const std::string& cfgFilePath) :
_global.brokerPort = 1883; _global.brokerPort = 1883;
_global.mqttPrefix = ""; _global.mqttPrefix = "";
_global.threads = 1; _global.threads = 1;
//initialize pointers to NULL
sysfs.DL = NULL;
sysfs.configurator = NULL;
perfevent.DL = NULL;
perfevent.configurator = NULL;
ipmi.DL = NULL;
ipmi.configurator = NULL;
snmp.DL = NULL;
snmp.configurator = NULL;
} }
Configuration::~Configuration() { Configuration::~Configuration() {
//TODO destroy dl's //close sysfs
if(sysfs.configurator) {
sysfs.destroy(sysfs.configurator);
}
if(sysfs.DL) {
dlclose(sysfs.DL);
}
//close perfevent
if(perfevent.configurator) {
perfevent.destroy(perfevent.configurator);
}
if(perfevent.DL) {
dlclose(perfevent.DL);
}
//close ipmi
if(ipmi.configurator) {
ipmi.destroy(ipmi.configurator);
}
if(ipmi.DL) {
dlclose(ipmi.DL);
}
//close snmp
if(snmp.configurator) {
snmp.destroy(snmp.configurator);
}
if(snmp.DL) {
dlclose(snmp.DL);
}
} }
bool Configuration::read() { bool Configuration::read() {
...@@ -71,52 +114,62 @@ bool Configuration::read() { ...@@ -71,52 +114,62 @@ bool Configuration::read() {
//dl-code based on http://tldp.org/HOWTO/C++-dlopen/thesolution.html //dl-code based on http://tldp.org/HOWTO/C++-dlopen/thesolution.html
std::string sysfsConfig = _cfgFilePath; //build expted path to sysfs.conf
sysfsConfig.append("sysfs.conf"); std::string sysfsConfigFile = _cfgFilePath;
ifstream sysfs(sysfsConfig.c_str()); sysfsConfigFile.append("sysfs.conf");
if (sysfs.good()) { ifstream sysfsStream(sysfsConfigFile.c_str());
if (sysfsStream.good()) {
//sysfs.conf exists --> open sysfs.so and read config
cout << "sysfs.conf found" << endl; cout << "sysfs.conf found" << endl;
void* sysfs = dlopen("./sysfs.so", RTLD_NOW); sysfs.DL = dlopen("./libsysfs.so", RTLD_NOW);
if(!sysfs) { if(!sysfs.DL) {
cerr << "Cannot load sysfs-library: " << dlerror() << endl; cerr << "Cannot load sysfs-library: " << dlerror() << endl;
return false; return false;
} }
//reset errors //reset errors
dlerror(); dlerror();
create_t* sysfsCreator = (create_t*) dlsym(sysfs, "create"); //set sysfs-struct
sysfs.create = (create_t*) dlsym(sysfs.DL, "create");
const char* dlsym_error = dlerror(); const char* dlsym_error = dlerror();
if (dlsym_error) { if (dlsym_error) {
cerr << "Cannot load symbol create for sysfs: " << dlsym_error << endl; cerr << "Cannot load symbol create for sysfs: " << dlsym_error << endl;
return false; return false;
} }
destroy_t* sysfsDestructor = (destroy_t*) dlsym(sysfs, "destroy"); sysfs.destroy = (destroy_t*) dlsym(sysfs.DL, "destroy");
dlsym_error = dlerror(); dlsym_error = dlerror();
if (dlsym_error) { if (dlsym_error) {
cerr << "Cannot load symbol destroy for sysfs: " << dlsym_error << endl; cerr << "Cannot load symbol destroy for sysfs: " << dlsym_error << endl;
return false; return false;
} }
Configurator* sysfsConfigurator = sysfsCreator(); sysfs.configurator = sysfs.create();
std::vector<Sensor>& sysfsSensors = sysfsConfigurator->readConfig(sysfsConfig); //read in config
std::vector<Sensor*>& sysfsSensors = sysfs.configurator->readConfig(sysfsConfigFile);
for(auto& s : sysfsSensors) { //save only pointers to the sensors; copy-constructing an dynamically loaded object may lead to segfaults
if(checkMqtt(s.getMqtt())) { //but before storing them, check if an MQTT-suffix was assigned twice
_sensors.push_back(&s); for(auto s : sysfsSensors) {
if(checkMqtt(s->getMqtt())) {
_sensors.push_back(s);
} else { } else {
cerr << "MQTT-Suffix used twice! Aborting" << endl; cerr << "MQTT-Suffix used twice! Aborting" << endl;
} }
} }
} else {
cout << "[INFO] sysfs.conf not found at \"" << sysfsConfigFile << "\". Omitting..." << endl;
} }
std::string perfeventConfig = _cfgFilePath; std::string perfeventConfigFile = _cfgFilePath;
perfeventConfig.append("perfevent.conf"); perfeventConfigFile.append("perfevent.conf");
ifstream perfevent(perfeventConfig.c_str()); ifstream perfeventStream(perfeventConfigFile.c_str());
if (perfevent.good()) { if (perfeventStream.good()) {
cout << "perfevent.conf found" << endl; cout << "perfevent.conf found" << endl;
//perfevent.conf exists. Open perfevent.so //perfevent.conf exists. Open perfevent.so
//TODO check if mqtt used twice! //TODO check if mqtt used twice!
} else {
cout << "[INFO] perfevent.conf not found. Omitting..." << endl;
} }
//TODO check for more config-files //TODO check for more config-files
...@@ -147,3 +200,11 @@ bool Configuration::checkMqtt(const std::string& mqtt) { ...@@ -147,3 +200,11 @@ bool Configuration::checkMqtt(const std::string& mqtt) {
} }
return true; return true;
} }
global_t Configuration::getGlobal() const {
return _global;
}
sensorVector_t& Configuration::getSensors() {
return _sensors;
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* Configuration.h * Configuration.h
* *
* Created on: 13.12.2017 * Created on: 13.12.2017
* Author: Micha Mueller * Author: Michael Ott (original), Micha Mueller
*/ */
#ifndef CONFIGURATION_H_ #ifndef CONFIGURATION_H_
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <vector> #include <vector>
#include <set> #include <set>
#include "Configurator.h"
#include "Sensor.h" #include "Sensor.h"
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
...@@ -27,6 +28,13 @@ typedef struct { ...@@ -27,6 +28,13 @@ typedef struct {
class Configuration { class Configuration {
typedef struct {
void* DL;
Configurator* configurator;
create_t* create;
destroy_t* destroy;
} dl_t;
public: public:
/** /**
* Create new Configuration. Sets global config file to read from to cfgFile. * Create new Configuration. Sets global config file to read from to cfgFile.
...@@ -37,7 +45,7 @@ public: ...@@ -37,7 +45,7 @@ public:
virtual ~Configuration(); virtual ~Configuration();
/** /**
* Reads configuration from _cfgFile. * Reads configuration from global.conf (located at _cfgFilePath).
* Reads and sets global values as well as all sensors. * Reads and sets global values as well as all sensors.
* Detects which sensor types are required and dynamically opens required libraries. * Detects which sensor types are required and dynamically opens required libraries.
* *
...@@ -61,13 +69,8 @@ public: ...@@ -61,13 +69,8 @@ public:
*/ */
bool checkMqtt(const std::string& mqtt); bool checkMqtt(const std::string& mqtt);
global_t getGlobal() const { global_t getGlobal() const;
return _global; sensorVector_t& getSensors();
}
sensorVector_t& getSensors() {
return _sensors;
}
private: private:
...@@ -75,6 +78,11 @@ private: ...@@ -75,6 +78,11 @@ private:
global_t _global; global_t _global;
sensorVector_t _sensors; sensorVector_t _sensors;
mqttSet_t _mqttSuffixes; mqttSet_t _mqttSuffixes;
dl_t sysfs;
dl_t perfevent;
dl_t ipmi;
dl_t snmp;
}; };
#endif /* CONFIGURATION_H_ */ #endif /* CONFIGURATION_H_ */
...@@ -8,14 +8,16 @@ ...@@ -8,14 +8,16 @@
#ifndef SRC_CONFIGURATOR_H_ #ifndef SRC_CONFIGURATOR_H_
#define SRC_CONFIGURATOR_H_ #define SRC_CONFIGURATOR_H_
#include "Configuration.h" #include "Sensor.h"
#include <vector>
#include <string>
class Configurator { class Configurator {
public: public:
Configurator() {} Configurator() {}
virtual ~Configurator() {} virtual ~Configurator() {}
virtual void readConfig(std::string cfgPath) = 0; virtual std::vector<Sensor*>& readConfig(std::string cfgPath) = 0;
}; };
typedef Configurator* create_t(); typedef Configurator* create_t();
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* MQTTPusher.cpp * MQTTPusher.cpp
* *
* Created on: 13.12.2017 * Created on: 13.12.2017
* Author: Micha Mueller * Author: Michael Ott (original), Micha Mueller
*/ */
#include "MQTTPusher.h" #include "MQTTPusher.h"
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* MQTTPusher.h * MQTTPusher.h
* *
* Created on: 13.12.2017 * Created on: 13.12.2017
* Author: Micha Mueller * Author: Michael Ott (original), Micha Mueller
*/ */
#ifndef MQTTPUSHER_H_ #ifndef MQTTPUSHER_H_
......
...@@ -7,7 +7,7 @@ include $(realpath $(dir $(lastword $(MAKEFILE_LIST)))/../..)/dcdb/common.mk ...@@ -7,7 +7,7 @@ include $(realpath $(dir $(lastword $(MAKEFILE_LIST)))/../..)/dcdb/common.mk
CXXFLAGS = -std=c++0x -O2 -g -Wall -Werror -Wno-unused-local-typedefs -Wno-unknown-warning-option -Wno-deprecated-declarations -I$(DCDBDEPLOYPATH)/include -I$(DCDBBASEPATH)/include CXXFLAGS = -std=c++0x -O2 -g -Wall -Werror -Wno-unused-local-typedefs -Wno-unknown-warning-option -Wno-deprecated-declarations -I$(DCDBDEPLOYPATH)/include -I$(DCDBBASEPATH)/include
CXXFLAGS = -O2 -g -Wall -Wno-unused-function $(CXX11FLAGS) -I$(DCDBBASEPATH)/dcdb/include -I$(DCDBDEPLOYPATH)/include CXXFLAGS = -O2 -g -Wall -Wno-unused-function $(CXX11FLAGS) -I$(DCDBBASEPATH)/dcdb/include -I$(DCDBDEPLOYPATH)/include
LIBS = -L../deps/mosquitto_build/lib -L$(DCDBDEPLOYPATH)/lib/ -lmosquitto -lboost_system -lboost_thread LIBS = -L../deps/mosquitto_build/lib -L$(DCDBDEPLOYPATH)/lib/ -ldl -lmosquitto -lboost_system -lboost_thread
OBJS = dcdbpusher.o Configuration.o Sensor.o MQTTPusher.o OBJS = dcdbpusher.o Configuration.o Sensor.o MQTTPusher.o
$(TARGET): $(OBJS) $(TARGET): $(OBJS)
......
//================================================================================ //================================================================================
// Name : dcdbpusher.cpp // Name : dcdbpusher.cpp
// Author : Micha Mueller // Author : Michael Ott (original), Micha Mueller
// Copyright : Leibniz Supercomputing Centre // Copyright : Leibniz Supercomputing Centre
// Description : Main functions for the DCDB MQTT Pusher // Description : Main functions for the DCDB MQTT Pusher
//================================================================================ //================================================================================
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "SysfsConfigurator.h" #include "SysfsConfigurator.h"
#include <iostream> #include <iostream>
#include "SysfsSensor.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/property_tree/info_parser.hpp> #include <boost/property_tree/info_parser.hpp>
...@@ -20,10 +19,12 @@ SysfsConfigurator::SysfsConfigurator() { ...@@ -20,10 +19,12 @@ SysfsConfigurator::SysfsConfigurator() {
} }
SysfsConfigurator::~SysfsConfigurator() { SysfsConfigurator::~SysfsConfigurator() {
// TODO Auto-generated destructor stub for(auto s : _sensors) {
delete s;
}
} }
std::vector<SysfsSensor>& SysfsConfigurator::readConfig(std::string cfgPath) { std::vector<Sensor*>& SysfsConfigurator::readConfig(std::string cfgPath) {
boost::property_tree::iptree cfg; boost::property_tree::iptree cfg;
boost::property_tree::read_info(cfgPath, cfg); boost::property_tree::read_info(cfgPath, cfg);
...@@ -44,7 +45,7 @@ std::vector<SysfsSensor>& SysfsConfigurator::readConfig(std::string cfgPath) { ...@@ -44,7 +45,7 @@ std::vector<SysfsSensor>& SysfsConfigurator::readConfig(std::string cfgPath) {
if (boost::iequals(sensor.first, "sensor")) { if (boost::iequals(sensor.first, "sensor")) {
cout << "Sensor \"" << sensor.second.data() << "\"" << endl; cout << "Sensor \"" << sensor.second.data() << "\"" << endl;
if (!sensor.second.empty()) { if (!sensor.second.empty()) {
SysfsSensor sysfsSensor(sensor.second.data()); SysfsSensor* sysfsSensor = new SysfsSensor(sensor.second.data());
//first check if default sensor is given //first check if default sensor is given
boost::optional<boost::property_tree::iptree&> defaultS = sensor.second.get_child_optional("default"); boost::optional<boost::property_tree::iptree&> defaultS = sensor.second.get_child_optional("default");
...@@ -52,14 +53,14 @@ std::vector<SysfsSensor>& SysfsConfigurator::readConfig(std::string cfgPath) { ...@@ -52,14 +53,14 @@ std::vector<SysfsSensor>& SysfsConfigurator::readConfig(std::string cfgPath) {
cout << " Using \"" << defaultS.get().data() << "\" as default." << endl; cout << " Using \"" << defaultS.get().data() << "\" as default." << endl;
sensorMap_t::iterator it = _templateSensors.find(defaultS.get().data()); sensorMap_t::iterator it = _templateSensors.find(defaultS.get().data());
if(it != _templateSensors.end()) { if(it != _templateSensors.end()) {
sysfsSensor = it->second; *sysfsSensor = it->second;
sysfsSensor.setName(sensor.second.data()); sysfsSensor->setName(sensor.second.data());
} else { } else {
cout << "Template sensor \"" << defaultS.get().data() << "\" not found! Using standard values." << endl; cout << "Template sensor \"" << defaultS.get().data() << "\" not found! Using standard values." << endl;
} }
} }
//read remaining values //read remaining values
readSensor(sysfsSensor, sensor.second); readSensor(*sysfsSensor, sensor.second);
_sensors.push_back(sysfsSensor); _sensors.push_back(sysfsSensor);
} }
} }
...@@ -72,7 +73,7 @@ void SysfsConfigurator::readSensor(SysfsSensor& sensor, boost::property_tree::ip ...@@ -72,7 +73,7 @@ void SysfsConfigurator::readSensor(SysfsSensor& sensor, boost::property_tree::ip
if (boost::iequals(s.first, ("path"))) { if (boost::iequals(s.first, ("path"))) {
sensor.setPath(s.second.data()); sensor.setPath(s.second.data());
} else if (boost::iequals(s.first, "interval")) { } else if (boost::iequals(s.first, "interval")) {
sensor.setInterval(stoi(s.second.data())); sensor.setInterval(stoull(s.second.data()));
} else if (boost::iequals(s.first, "mqttsuffix")) { } else if (boost::iequals(s.first, "mqttsuffix")) {
sensor.setMqtt(s.second.data()); sensor.setMqtt(s.second.data());
} else if (boost::iequals(s.first, "minValues")) { } else if (boost::iequals(s.first, "minValues")) {
......
/* /*
* SYSFSConfiguration.h * SysfsConfigurator.h
* *
* Created on: 18.11.2017 * Created on: 18.11.2017
* Author: Micha Mueller * Author: Micha Mueller
*/ */
#ifndef SYSFSCONFIGURATION_H_ #ifndef SYSFSCONFIGURATOR_H_
#define SYSFSCONFIGURATION_H_ #define SYSFSCONFIGURATOR_H_
#include "SysfsSensor.h" #include "SysfsSensor.h"
#include "../../Configurator.h" #include "../../Configurator.h"
#include "../../Configuration.h" #include "../../Configuration.h"
#include <string>
#include <vector> #include <vector>
#include <map> #include <map>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
class SysfsConfigurator : public Configurator{ class SysfsConfigurator : public Configurator{
typedef std::map<std::string, SysfsSensor> sysfsSensorMap_t; typedef std::map<std::string, SysfsSensor> sensorMap_t;
public: public:
...@@ -29,7 +30,7 @@ public: ...@@ -29,7 +30,7 @@ public:
* @param cfgPath Path + name of the config-file * @param cfgPath Path + name of the config-file
* @param config Configuration where to store sensor-pointers and check for double used MQTT-suffixes * @param config Configuration where to store sensor-pointers and check for double used MQTT-suffixes
*/ */
std::vector<SysfsSensor>& readConfig(std::string cfgPath); std::vector<Sensor*>& readConfig(std::string cfgPath);
private: private:
/** /**
...@@ -40,8 +41,8 @@ private: ...@@ -40,8 +41,8 @@ private:
*/ */
void readSensor(SysfsSensor& sensor, boost::property_tree::iptree& config); void readSensor(SysfsSensor& sensor, boost::property_tree::iptree& config);
std::vector<SysfsSensor> _sensors; std::vector<Sensor*> _sensors;
sysfsSensorMap_t _templateSensors; sensorMap_t _templateSensors;
}; };
extern "C" Configurator* create() { extern "C" Configurator* create() {
...@@ -52,4 +53,4 @@ extern "C" void destroy(Configurator* c) { ...@@ -52,4 +53,4 @@ extern "C" void destroy(Configurator* c) {
delete c; delete c;
} }
#endif /* SYSFSCONFIGURATION_H_ */ #endif /* SYSFSCONFIGURATOR_H_ */
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* SYSFSSensor.cpp * SYSFSSensor.cpp
* *
* Created on: 18.11.2017 * Created on: 18.11.2017
* Author: Micha Mueller * Author: Michael Ott (original), Micha Mueller
*/ */
#include "SysfsSensor.h" #include "SysfsSensor.h"
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* SYSFSSensor.h * SYSFSSensor.h
* *
* Created on: 18.11.2017 * Created on: 18.11.2017
* Author: Micha Mueller * Author: Michael Ott (original), Micha Mueller
*/ */
#ifndef SYSFSSENSOR_H_ #ifndef SYSFSSENSOR_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