Commit f00724d9 authored by Michael Ott's avatar Michael Ott
Browse files

Move configuration parser to new class Configuration. Add configuration...

Move configuration parser to new class Configuration. Add configuration parameters for IPMI session timeout and retransmission timeout.
parent a8a717b2
/*
* Configuration.cpp
*
* Created on: 26 Jan 2017
* Author: ottmi
*/
#include "Configuration.h"
#include <boost/property_tree/info_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/optional.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include "IPMIHost.h"
#include "IPMISensor.h"
namespace DCDB {
Configuration::Configuration(std::string cfgFile) {
_global.threads = 1;
_global.sessionTimeout = 0;
_global.retransmissionTimeout = 0;
boost::property_tree::iptree cfg;
boost::property_tree::read_info(cfgFile, cfg);
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
std::cout << global.first << " " << global.second.data() << std::endl;
if (boost::iequals(global.first, "threads")) {
_global.threads = stoi(global.second.data());
} else if (boost::iequals(global.first, "SessionTimeout")) {
_global.sessionTimeout = stoi(global.second.data());
} else if(boost::iequals(global.first, "RetransmissionTimeout")) {
_global.retransmissionTimeout = stoi(global.second.data());
}
}
BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, cfg.get_child("sensors")) {
if (boost::iequals(sensor.first, "sensor")) {
std::cout << "Sensor " << sensor.second.data() << std::endl;
if (!sensor.second.empty()) {
DCDB::IPMISensor ipmiSensor(sensor.second.data());
if (boost::iequals(sensor.second.get<std::string>("type"), "raw")) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &s, sensor.second) {
std::cout << " " << s.first << " " << s.second.data() << std::endl;
if (boost::iequals(s.first, ("cmd"))) {
ipmiSensor.setRawCmd(s.second.data());
} else if (boost::iequals(s.first, "freq")) {
ipmiSensor.setFreq(stoi(s.second.data()));
} else if (boost::iequals(s.first, "start")) {
ipmiSensor.setStart(stoi(s.second.data()));
} else if (boost::iequals(s.first, "stop")) {
ipmiSensor.setStop(stoi(s.second.data()));
} else if (boost::iequals(s.first, "mqttsuffix")) {
ipmiSensor.setMqttSuffix(s.second.data());
}
}
}
_sensors.insert(sensorMap_t::value_type(ipmiSensor.getName(), ipmiSensor));
}
}
}
BOOST_FOREACH(boost::property_tree::iptree::value_type &host, cfg.get_child("hosts")) {
if (boost::iequals(host.first, ("host"))) {
std::cout << "Host " << host.second.data() << std::endl;
_hosts.push_back(DCDB::IPMIHost(host.second.data(), *this));
DCDB::IPMIHost &ipmiHost = _hosts.back();
if (!host.second.empty()) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &h, host.second) {
std::cout << " " << h.first << " " << h.second.data() << std::endl;
if (boost::iequals(h.first, "username")) {
ipmiHost.setUserName(h.second.data());
} else if (boost::iequals(h.first, "password")) {
ipmiHost.setPassword(h.second.data());
} else if (boost::iequals(h.first, "sensors")) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &s, h.second) {
std::cout << " " << s.first << " " << s.second.data() << std::endl;
sensorMap_t::iterator it = _sensors.find(s.second.data());
if (it != _sensors.end()) {
DCDB::IPMISensor sensor = it->second;
boost::optional< boost::property_tree::iptree& > mqtt = s.second.get_child_optional("mqttsuffix");
if (mqtt) {
sensor.setMqttSuffix(mqtt.get().data());
}
ipmiHost.addSensor(sensor);
}
}
} else if (boost::iequals(h.first, "mqttprefix")) {
ipmiHost.setMqttPrefix(h.second.data());
}
}
}
}
}
}
Configuration::~Configuration() {
// TODO Auto-generated destructor stub
}
} /* namespace DCDB */
/*
* Configuration.h
*
* Created on: 26 Jan 2017
* Author: ottmi
*/
#ifndef CONFIGURATION_H_
#define CONFIGURATION_H_
#include <boost/property_tree/ptree.hpp>
#include <string>
#include <map>
#include <list>
namespace DCDB {
class IPMIHost;
class IPMISensor;
class Configuration {
typedef std::list<DCDB::IPMIHost> hostList_t;
typedef std::map<std::string, DCDB::IPMISensor> sensorMap_t;
typedef struct {
uint32_t threads;
uint32_t sessionTimeout;
uint32_t retransmissionTimeout;
} global_t;
public:
Configuration(std::string cfgFile);
virtual ~Configuration();
sensorMap_t _sensors;
hostList_t _hosts;
global_t _global;
};
} /* namespace DCDB */
#endif /* CONFIGURATION_H_ */
......@@ -21,13 +21,15 @@
namespace DCDB {
IPMIHost::IPMIHost(const std::string hostName) {
IPMIHost::IPMIHost(const std::string& hostName, const Configuration& cfg) {
_ipmiCtx = NULL;
_hostName = hostName;
_userName = std::string("admin");
_password = std::string("admin");
_auth = IPMI_AUTHENTICATION_TYPE_MD5;
_priv = IPMI_PRIVILEGE_LEVEL_ADMIN;
_retransmissionTimeout = cfg._global.retransmissionTimeout;
_sessionTimeout = cfg._global.sessionTimeout;
}
IPMIHost::~IPMIHost() {
......@@ -41,8 +43,7 @@ namespace DCDB {
int workaround_flags = 0;
int flags = 0;
/* if (ipmi_ctx_open_outofband(_ipmiCtx, _hostName.c_str(), _userName.c_str(), _password.c_str(), _auth, _priv, 0, 0, workaround_flags, flags) < 0) {*/
if (ipmi_ctx_open_outofband_2_0(_ipmiCtx, _hostName.c_str(), _userName.c_str(), _password.c_str(), NULL, 0, _priv, 3, 0, 0, workaround_flags, flags) < 0) {
if (ipmi_ctx_open_outofband(_ipmiCtx, _hostName.c_str(), _userName.c_str(), _password.c_str(), _auth, _priv, _sessionTimeout, _retransmissionTimeout, workaround_flags, flags) < 0) {
printf("ipmi_ctx_open_outofband: %s\n", ipmi_ctx_errormsg(_ipmiCtx));
ipmi_ctx_close(_ipmiCtx);
ipmi_ctx_destroy(_ipmiCtx);
......
......@@ -12,12 +12,13 @@
#include <list>
#include <freeipmi/freeipmi.h>
#include "IPMISensor.h"
#include "Configuration.h"
namespace DCDB {
class IPMIHost {
public:
IPMIHost(const std::string hostName);
IPMIHost(const std::string& hostName, const Configuration& cfg);
virtual ~IPMIHost();
int connect();
......@@ -93,6 +94,9 @@ namespace DCDB {
uint8_t _priv;
std::string _mqttPrefix;
std::list<DCDB::IPMISensor> _sensors;
uint32_t _sessionTimeout;
uint32_t _retransmissionTimeout;
};
} /* namespace DCDB */
......
......@@ -10,7 +10,7 @@ DISTFILES_HASHES = freeipmi-1.5.5.tar.gz|b8abfefee0b757f351d8fab777e3c1bb
include $(DCDBCOREPATH)/common.mk
CXXFLAGS = -O2 -g -Wall $(CXX11FLAGS) -I$(DCDBDEPLOYPATH)/include/ -I$(DCDBBASEPATH)/include -I../deps/freeipmi-$(FREEIPMI_VERSION)/libfreeipmi/include -I../deps/freeipmi-$(FREEIPMI_VERSION)/libfreeipmi/api
OBJS = ipmipusher.o IPMISensor.o IPMIHost.o
OBJS = ipmipusher.o IPMISensor.o IPMIHost.o Configuration.o
LIBS = -L$(DCDBDEPLOYPATH)/lib/ -lfreeipmi -lboost_thread -lboost_regex -lboost_system -lboost_date_time
TARGET = ipmipusher
......
global {
threads 16
sessiontimeout 500
retransmissiontimeout 200
}
sensors {
......
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/optional.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <string>
#include <map>
#include <list>
#include <exception>
#include <iostream>
#include <unistd.h>
#include <thread>
#include "IPMIHost.h"
#include "IPMISensor.h"
std::list<DCDB::IPMIHost> hosts;
typedef std::map<std::string, DCDB::IPMISensor> sensorMap_t;
sensorMap_t sensors;
int numOfThreads = 1;
boost::asio::io_service io;
boost::thread_group threads;
int parseConfig(std::string cfgFile) {
boost::property_tree::iptree cfg;
boost::property_tree::read_info(cfgFile, cfg);
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
std::cout << global.first << " " << global.second.data() << std::endl;
if (boost::iequals(global.first, "threads")) {
numOfThreads = stoi(global.second.data());
}
}
BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, cfg.get_child("sensors")) {
if (boost::iequals(sensor.first, "sensor")) {
std::cout << "Sensor " << sensor.second.data() << std::endl;
if (!sensor.second.empty()) {
DCDB::IPMISensor ipmiSensor(sensor.second.data());
if (boost::iequals(sensor.second.get<std::string>("type"), "raw")) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &s, sensor.second) {
std::cout << " " << s.first << " " << s.second.data() << std::endl;
if (boost::iequals(s.first, ("cmd"))) {
ipmiSensor.setRawCmd(s.second.data());
} else if (boost::iequals(s.first, "freq")) {
ipmiSensor.setFreq(stoi(s.second.data()));
} else if (boost::iequals(s.first, "start")) {
ipmiSensor.setStart(stoi(s.second.data()));
} else if (boost::iequals(s.first, "stop")) {
ipmiSensor.setStop(stoi(s.second.data()));
} else if (boost::iequals(s.first, "mqttsuffix")) {
ipmiSensor.setMqttSuffix(s.second.data());
}
}
}
sensors.insert(sensorMap_t::value_type(ipmiSensor.getName(), ipmiSensor));
}
}
}
BOOST_FOREACH(boost::property_tree::iptree::value_type &host, cfg.get_child("hosts")) {
if (boost::iequals(host.first, ("host"))) {
std::cout << "Host " << host.second.data() << std::endl;
hosts.push_back(DCDB::IPMIHost(host.second.data()));
DCDB::IPMIHost &ipmiHost = hosts.back();
if (!host.second.empty()) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &h, host.second) {
std::cout << " " << h.first << " " << h.second.data() << std::endl;
if (boost::iequals(h.first, "username")) {
ipmiHost.setUserName(h.second.data());
} else if (boost::iequals(h.first, "password")) {
ipmiHost.setPassword(h.second.data());
} else if (boost::iequals(h.first, "sensors")) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &s, h.second) {
std::cout << " " << s.first << " " << s.second.data() << std::endl;
sensorMap_t::iterator it = sensors.find(s.second.data());
if (it != sensors.end()) {
DCDB::IPMISensor sensor = it->second;
boost::optional< boost::property_tree::iptree& > mqtt = s.second.get_child_optional("mqttsuffix");
if (mqtt) {
sensor.setMqttSuffix(mqtt.get().data());
}
ipmiHost.addSensor(sensor);
}
}
} else if (boost::iequals(h.first, "mqttprefix")) {
ipmiHost.setMqttPrefix(h.second.data());
}
}
}
}
}
return 0;
}
#include "Configuration.h"
int main (int argc, char **argv) {
if (argc <= 1) {
......@@ -104,24 +17,21 @@ int main (int argc, char **argv) {
return 1;
}
parseConfig(argv[1]);
boost::asio::io_service io;
boost::thread_group threads;
DCDB::Configuration cfg(argv[1]);
BOOST_FOREACH(DCDB::IPMIHost &host, hosts) {
BOOST_FOREACH(DCDB::IPMIHost &host, cfg._hosts) {
std::cout << "Host " << host.getHostName() << std::endl;
BOOST_FOREACH(DCDB::IPMISensor& sensor, host.getSensors()) {
sensor.startPolling(io);
//t.async_wait(boost::bind(runTask, boost::asio::placeholders::error, &t, &sensor));
}
}
for (std::size_t i = 0; i < numOfThreads; i++) {
for (std::size_t i = 0; i < cfg._global.threads; i++) {
threads.create_thread(boost::bind(&boost::asio::io_service::run, &io));
}
threads.join_all();
/*
BOOST_FOREACH(DCDB::IPMIHost &host, hosts) {
host.disconnect();
}
*/
return 0;
}
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