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 @@
* Configuration.cpp
*
* Created on: 13.12.2017
* Author: Micha Mueller
* Author: Michael Ott (original), Micha Mueller
*/
#include "Configuration.h"
......@@ -29,10 +29,53 @@ Configuration::Configuration(const std::string& cfgFilePath) :
_global.brokerPort = 1883;
_global.mqttPrefix = "";
_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() {
//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() {
......@@ -71,52 +114,62 @@ bool Configuration::read() {
//dl-code based on http://tldp.org/HOWTO/C++-dlopen/thesolution.html
std::string sysfsConfig = _cfgFilePath;
sysfsConfig.append("sysfs.conf");
ifstream sysfs(sysfsConfig.c_str());
if (sysfs.good()) {
//build expted path to sysfs.conf
std::string sysfsConfigFile = _cfgFilePath;
sysfsConfigFile.append("sysfs.conf");
ifstream sysfsStream(sysfsConfigFile.c_str());
if (sysfsStream.good()) {
//sysfs.conf exists --> open sysfs.so and read config
cout << "sysfs.conf found" << endl;
void* sysfs = dlopen("./sysfs.so", RTLD_NOW);
if(!sysfs) {
sysfs.DL = dlopen("./libsysfs.so", RTLD_NOW);
if(!sysfs.DL) {
cerr << "Cannot load sysfs-library: " << dlerror() << endl;
return false;
}
//reset errors
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();
if (dlsym_error) {
cerr << "Cannot load symbol create for sysfs: " << dlsym_error << endl;
return false;
}
destroy_t* sysfsDestructor = (destroy_t*) dlsym(sysfs, "destroy");
sysfs.destroy = (destroy_t*) dlsym(sysfs.DL, "destroy");
dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol destroy for sysfs: " << dlsym_error << endl;
return false;
}
Configurator* sysfsConfigurator = sysfsCreator();
std::vector<Sensor>& sysfsSensors = sysfsConfigurator->readConfig(sysfsConfig);
sysfs.configurator = sysfs.create();
//read in config
std::vector<Sensor*>& sysfsSensors = sysfs.configurator->readConfig(sysfsConfigFile);
for(auto& s : sysfsSensors) {
if(checkMqtt(s.getMqtt())) {
_sensors.push_back(&s);
//save only pointers to the sensors; copy-constructing an dynamically loaded object may lead to segfaults
//but before storing them, check if an MQTT-suffix was assigned twice
for(auto s : sysfsSensors) {
if(checkMqtt(s->getMqtt())) {
_sensors.push_back(s);
} else {
cerr << "MQTT-Suffix used twice! Aborting" << endl;
}
}
} else {
cout << "[INFO] sysfs.conf not found at \"" << sysfsConfigFile << "\". Omitting..." << endl;
}
std::string perfeventConfig = _cfgFilePath;
perfeventConfig.append("perfevent.conf");
ifstream perfevent(perfeventConfig.c_str());
if (perfevent.good()) {
std::string perfeventConfigFile = _cfgFilePath;
perfeventConfigFile.append("perfevent.conf");
ifstream perfeventStream(perfeventConfigFile.c_str());
if (perfeventStream.good()) {
cout << "perfevent.conf found" << endl;
//perfevent.conf exists. Open perfevent.so
//TODO check if mqtt used twice!
} else {
cout << "[INFO] perfevent.conf not found. Omitting..." << endl;
}
//TODO check for more config-files
......@@ -147,3 +200,11 @@ bool Configuration::checkMqtt(const std::string& mqtt) {
}
return true;
}
global_t Configuration::getGlobal() const {
return _global;
}
sensorVector_t& Configuration::getSensors() {
return _sensors;
}
......@@ -2,7 +2,7 @@
* Configuration.h
*
* Created on: 13.12.2017
* Author: Micha Mueller
* Author: Michael Ott (original), Micha Mueller
*/
#ifndef CONFIGURATION_H_
......@@ -11,6 +11,7 @@
#include <vector>
#include <set>
#include "Configurator.h"
#include "Sensor.h"
#include <boost/property_tree/ptree.hpp>
......@@ -27,6 +28,13 @@ typedef struct {
class Configuration {
typedef struct {
void* DL;
Configurator* configurator;
create_t* create;
destroy_t* destroy;
} dl_t;
public:
/**
* Create new Configuration. Sets global config file to read from to cfgFile.
......@@ -37,7 +45,7 @@ public:
virtual ~Configuration();
/**
* Reads configuration from _cfgFile.
* Reads configuration from global.conf (located at _cfgFilePath).
* Reads and sets global values as well as all sensors.
* Detects which sensor types are required and dynamically opens required libraries.
*
......@@ -61,13 +69,8 @@ public:
*/
bool checkMqtt(const std::string& mqtt);
global_t getGlobal() const {
return _global;
}
sensorVector_t& getSensors() {
return _sensors;
}
global_t getGlobal() const;
sensorVector_t& getSensors();
private:
......@@ -75,6 +78,11 @@ private:
global_t _global;
sensorVector_t _sensors;
mqttSet_t _mqttSuffixes;
dl_t sysfs;
dl_t perfevent;
dl_t ipmi;
dl_t snmp;
};
#endif /* CONFIGURATION_H_ */
......@@ -8,14 +8,16 @@
#ifndef SRC_CONFIGURATOR_H_
#define SRC_CONFIGURATOR_H_
#include "Configuration.h"
#include "Sensor.h"
#include <vector>
#include <string>
class Configurator {
public:
Configurator() {}
virtual ~Configurator() {}
virtual void readConfig(std::string cfgPath) = 0;
virtual std::vector<Sensor*>& readConfig(std::string cfgPath) = 0;
};
typedef Configurator* create_t();
......
......@@ -2,7 +2,7 @@
* MQTTPusher.cpp
*
* Created on: 13.12.2017
* Author: Micha Mueller
* Author: Michael Ott (original), Micha Mueller
*/
#include "MQTTPusher.h"
......
......@@ -2,7 +2,7 @@
* MQTTPusher.h
*
* Created on: 13.12.2017
* Author: Micha Mueller
* Author: Michael Ott (original), Micha Mueller
*/
#ifndef MQTTPUSHER_H_
......
......@@ -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 = -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
$(TARGET): $(OBJS)
......
//================================================================================
// Name : dcdbpusher.cpp
// Author : Micha Mueller
// Author : Michael Ott (original), Micha Mueller
// Copyright : Leibniz Supercomputing Centre
// Description : Main functions for the DCDB MQTT Pusher
//================================================================================
......
......@@ -7,7 +7,6 @@
#include "SysfsConfigurator.h"
#include <iostream>
#include "SysfsSensor.h"
#include <boost/foreach.hpp>
#include <boost/property_tree/info_parser.hpp>
......@@ -20,10 +19,12 @@ 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::read_info(cfgPath, cfg);
......@@ -44,7 +45,7 @@ std::vector<SysfsSensor>& SysfsConfigurator::readConfig(std::string cfgPath) {
if (boost::iequals(sensor.first, "sensor")) {
cout << "Sensor \"" << sensor.second.data() << "\"" << endl;
if (!sensor.second.empty()) {
SysfsSensor sysfsSensor(sensor.second.data());
SysfsSensor* sysfsSensor = new SysfsSensor(sensor.second.data());
//first check if default sensor is given
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) {
cout << " Using \"" << defaultS.get().data() << "\" as default." << endl;
sensorMap_t::iterator it = _templateSensors.find(defaultS.get().data());
if(it != _templateSensors.end()) {
sysfsSensor = it->second;
sysfsSensor.setName(sensor.second.data());
*sysfsSensor = it->second;
sysfsSensor->setName(sensor.second.data());
} else {
cout << "Template sensor \"" << defaultS.get().data() << "\" not found! Using standard values." << endl;
}
}
//read remaining values
readSensor(sysfsSensor, sensor.second);
readSensor(*sysfsSensor, sensor.second);
_sensors.push_back(sysfsSensor);
}
}
......@@ -72,7 +73,7 @@ void SysfsConfigurator::readSensor(SysfsSensor& sensor, boost::property_tree::ip
if (boost::iequals(s.first, ("path"))) {
sensor.setPath(s.second.data());
} 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")) {
sensor.setMqtt(s.second.data());
} else if (boost::iequals(s.first, "minValues")) {
......
/*
* SYSFSConfiguration.h
* SysfsConfigurator.h
*
* Created on: 18.11.2017
* Author: Micha Mueller
*/
#ifndef SYSFSCONFIGURATION_H_
#define SYSFSCONFIGURATION_H_
#ifndef SYSFSCONFIGURATOR_H_
#define SYSFSCONFIGURATOR_H_
#include "SysfsSensor.h"
#include "../../Configurator.h"
#include "../../Configuration.h"
#include <string>
#include <vector>
#include <map>
#include <boost/property_tree/ptree.hpp>
class SysfsConfigurator : public Configurator{
typedef std::map<std::string, SysfsSensor> sysfsSensorMap_t;
typedef std::map<std::string, SysfsSensor> sensorMap_t;
public:
......@@ -29,7 +30,7 @@ public:
* @param cfgPath Path + name of the config-file
* @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:
/**
......@@ -40,8 +41,8 @@ private:
*/
void readSensor(SysfsSensor& sensor, boost::property_tree::iptree& config);
std::vector<SysfsSensor> _sensors;
sysfsSensorMap_t _templateSensors;
std::vector<Sensor*> _sensors;
sensorMap_t _templateSensors;
};
extern "C" Configurator* create() {
......@@ -52,4 +53,4 @@ extern "C" void destroy(Configurator* c) {
delete c;
}
#endif /* SYSFSCONFIGURATION_H_ */
#endif /* SYSFSCONFIGURATOR_H_ */
......@@ -2,7 +2,7 @@
* SYSFSSensor.cpp
*
* Created on: 18.11.2017
* Author: Micha Mueller
* Author: Michael Ott (original), Micha Mueller
*/
#include "SysfsSensor.h"
......
......@@ -2,7 +2,7 @@
* SYSFSSensor.h
*
* Created on: 18.11.2017
* Author: Micha Mueller
* Author: Michael Ott (original), Micha Mueller
*/
#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