Commit 9d4d273f authored by Micha Mueller's avatar Micha Mueller
Browse files

Refactor IPMI plugin: make config more consistent with the other plugins

parent cf5fa04d
globalHost {
sessiontimeout 500
retransmissiontimeout 200
mqttprefix /AABBAABBAABBAACCDDCCDDCC
global {
sessiontimeout 500
retransmissiontimeout 200
mqttprefix /AABBAABBAABBAACCDDCCDDCC
}
templateSensors {
sensor energy {
type raw
freq 1000
cmd "0x00 0x2e 0x81 0x4d 0x4f 0x00 0x00 0x01 0x82 0x00 0x08"
start 5
stop 12
mqttsuffix 99887766
}
sensor energy {
; type raw
interval 1000
cmd "0x00 0x2e 0x81 0x4d 0x4f 0x00 0x00 0x01 0x82 0x00 0x08"
start 5
stop 12
mqttsuffix 99887766
}
}
hosts {
host localhost {
username "USERID"
password "PASSW0RD"
mqttprefix "/00/11/2233445566/778899AABB"
sensors {
sensor energy {
MQTTsuffix 11223344
}
}
}
; host test {
; username "USERID"
; password "PASSWORD"
; }
host localhost {
username "USERID"
password "PASSW0RD"
mqttprefix "/00/11/2233445566/778899AABB"
sensors {
sensor energySens {
default energy
MQTTsuffix 11223344
}
sensor recordSens {
recordId 4321
mqttsuffix 11223345
}
}
}
; host test {
; username "USERID"
; password "PASSWORD"
; }
}
......@@ -17,111 +17,144 @@
namespace DCDB {
IPMIConfigurator::IPMIConfigurator() {
_globalHost.sessionTimeout = 0;
_globalHost.retransmissionTimeout = 0;
}
IPMIConfigurator::IPMIConfigurator() {
_globalHost.sessionTimeout = 0;
_globalHost.retransmissionTimeout = 0;
}
IPMIConfigurator::~IPMIConfigurator() {
//TODO probably should create sensors with new
//TODO test IPMI at least
/*for (auto s : _sensors) {
delete s;
}*/
}
IPMIConfigurator::~IPMIConfigurator() {
for (auto s : _sensors) {
delete s;
}
}
std::vector<Sensor*>& IPMIConfigurator::readConfig(std::string cfgPath) {
boost::property_tree::iptree cfg;
boost::property_tree::read_info(cfgPath, cfg);
std::vector<Sensor*>& IPMIConfigurator::readConfig(std::string cfgPath) {
boost::property_tree::iptree cfg;
boost::property_tree::read_info(cfgPath, cfg);
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("globalHost")) {
//read global variables (if present overwrite those from global.conf)
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, "SessionTimeout")) {
_globalHost.sessionTimeout = stoi(global.second.data());
} else if(boost::iequals(global.first, "RetransmissionTimeout")) {
_globalHost.retransmissionTimeout = stoi(global.second.data());
} else if(boost::iequals(global.first, "mqttprefix")) {
_globalHost.sessionTimeout = stoi(global.second.data());
} else if (boost::iequals(global.first, "RetransmissionTimeout")) {
_globalHost.retransmissionTimeout = stoi(global.second.data());
} else if (boost::iequals(global.first, "mqttprefix")) {
_mqttPrefix = global.second.data();
}
}
}
BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, cfg.get_child("templateSensors")) {
//read template sensors
BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, cfg.get_child("templateSensors")) {
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.setInterval(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.setMqtt(s.second.data());
std::cout << "Template Sensor \"" << sensor.second.data() << "\"" << std::endl;
if (!sensor.second.empty()) {
DCDB::IPMISensor ipmiSensor(sensor.second.data());
if(readSensor(ipmiSensor, sensor.second)) {
_templateSensors.insert(sensorMap_t::value_type(ipmiSensor.getName(), ipmiSensor));
} else {
std::cout << "Template sensor \"" << sensor.second.data() << "\" has bad values! Ignoring..." << std::endl;
}
}
} else if (boost::iequals(sensor.second.get<std::string>("type"), "recordId")) {
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, ("id"))) {
ipmiSensor.setRecordId(stoul(s.second.data()));
} else if (boost::iequals(s.first, "freq")) {
ipmiSensor.setInterval(stoi(s.second.data()));
} else if (boost::iequals(s.first, "mqttsuffix")) {
ipmiSensor.setMqtt(s.second.data());
}
}
}
_templateSensors.insert(sensorMap_t::value_type(ipmiSensor.getName(), ipmiSensor));
}
}
}
}
BOOST_FOREACH(boost::property_tree::iptree::value_type &host, cfg.get_child("hosts")) {
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(), _globalHost.retransmissionTimeout, _globalHost.sessionTimeout));
DCDB::IPMIHost &ipmiHost = _hosts.back();
ipmiHost.setMqttPrefix(_mqttPrefix);
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 = _templateSensors.find(s.second.data());
if (it != _templateSensors.end()) {
DCDB::IPMISensor sensor = it->second;
boost::optional< boost::property_tree::iptree& > mqtt = s.second.get_child_optional("mqttsuffix");
if (mqtt) {
sensor.setMqtt(mqtt.get().data());
std::cout << "Host " << host.second.data() << std::endl;
_hosts.push_back(DCDB::IPMIHost(host.second.data(), _globalHost.retransmissionTimeout, _globalHost.sessionTimeout));
DCDB::IPMIHost &ipmiHost = _hosts.back();
ipmiHost.setMqttPrefix(_mqttPrefix);
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, "mqttprefix")) {
ipmiHost.setMqttPrefix(h.second.data());
} else if (boost::iequals(h.first, "sensors")) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, h.second) {
std::cout << "Sensor \"" << sensor.second.data() << "\"" << std::endl;
if (!sensor.second.empty()) {
DCDB::IPMISensor* ipmiSensor = new DCDB::IPMISensor(sensor.second.data());
//first check if default sensor is given
boost::optional<boost::property_tree::iptree&> defaultC = sensor.second.get_child_optional("default");
if(defaultC) {
std::cout << " Using \"" << defaultC.get().data() << "\" as default." << std::endl;
sensorMap_t::iterator it = _templateSensors.find(defaultC.get().data());
if(it != _templateSensors.end()) {
*ipmiSensor = it->second;
ipmiSensor->setName(sensor.second.data());
} else {
std::cout << " Template sensor \"" << defaultC.get().data() << "\" not found! Using standard values." << std::endl;
}
}
//set pointer to corresponding host
ipmiSensor->setHost(&ipmiHost);
//read remaining values
if(readSensor(*ipmiSensor, sensor.second)) {
_sensors.push_back(ipmiSensor);
} else {
std::cout << " Sensor \"" << sensor.second.data() << "\" has bad values! Ignoring..." << std::endl;
}
}
}
ipmiHost.addSensor(sensor);
}
}
} else if (boost::iequals(h.first, "mqttprefix")) {
ipmiHost.setMqttPrefix(h.second.data());
}
}
}
}
}
return _sensors;
}
bool IPMIConfigurator::readSensor(DCDB::IPMISensor& sensor, boost::property_tree::iptree& config) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (boost::iequals(val.first, "interval")) {
sensor.setInterval(stoull(val.second.data()));
} else if (boost::iequals(val.first, "mqttsuffix")) {
if (sensor.getHost() != NULL) {
// only workaround so that every host can define own prefix
// not guaranteed, but should actually work as long as prefix is defined before the sensors within a host
if (sensor.getHost()->getMqttPrefix() != "") {
sensor.setMqtt(sensor.getHost()->getMqttPrefix() + val.second.data());
} else {
sensor.setMqtt(_mqttPrefix + val.second.data());
}
}
}
} else if (boost::iequals(val.first, "minValues")) {
sensor.setMinValues(stoull(val.second.data()));
} else if (boost::iequals(val.first, ("cmd"))) {
sensor.setRawCmd(val.second.data());
} else if (boost::iequals(val.first, "start")) {
sensor.setStart(stoi(val.second.data()));
} else if (boost::iequals(val.first, "stop")) {
sensor.setStop(stoi(val.second.data()));
} else if (boost::iequals(val.first, ("recordId"))) {
sensor.setRecordId(stoul(val.second.data()));
} else if (boost::iequals(val.first, "default")) {
//avoid unnecessary "Value not recognized" message
} else {
std::cout << " Value \"" << val.first
<< "\" not recognized. Omitting..." << std::endl;
}
}
for (auto& h : _hosts) {
for (auto& s : h.getSensors()) {
s.setMqtt(std::string(h.getMqttPrefix()+s.getMqtt()));
_sensors.push_back(&s);
}
}
return _sensors;
}
}
std::cout << " MQTTSuffix:" << sensor.getMqtt() << std::endl;
std::cout << " Interval : " << sensor.getInterval() << std::endl;
std::cout << " minValues: " << sensor.getMinValues() << std::endl;
if (sensor.getRecordId() != 0) {
std::cout << " RecordID : " << sensor.getRecordId() << std::endl;
} else {
std::cout << " Using raw command" << std::endl;
std::cout << " Start : " << unsigned(sensor.getStart()) << std::endl;
std::cout << " Stop : " << unsigned(sensor.getStop()) << std::endl;
}
return true;
}
} /* namespace DCDB */
......@@ -36,6 +36,13 @@ namespace DCDB {
std::vector<Sensor*>& readConfig(std::string cfgPath);
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(IPMISensor& sensor, boost::property_tree::iptree& config);
std::vector<Sensor*> _sensors;
sensorMap_t _templateSensors;
hostList_t _hosts;
......
......@@ -23,8 +23,7 @@
namespace DCDB {
IPMIHost::IPMIHost(const std::string& hostName, uint32_t retransmissionTimeout,
uint32_t sessionTimeout) {
IPMIHost::IPMIHost(const std::string& hostName, uint32_t retransmissionTimeout, uint32_t sessionTimeout) {
_ipmiCtx = NULL;
_sdrCtx = NULL;
_sensorReadCtx = NULL;
......@@ -33,6 +32,7 @@ IPMIHost::IPMIHost(const std::string& hostName, uint32_t retransmissionTimeout,
_password = std::string("admin");
_auth = IPMI_AUTHENTICATION_TYPE_MD5;
_priv = IPMI_PRIVILEGE_LEVEL_ADMIN;
_mqttPrefix = "";
_retransmissionTimeout = retransmissionTimeout;
_sessionTimeout = sessionTimeout;
_strand = NULL;
......@@ -229,13 +229,6 @@ void IPMIHost::increaseErrorCount() {
}
}
void IPMIHost::addSensor(const DCDB::IPMISensor& sensor) {
_sensors.push_back(sensor);
_sensors.back().setHost(this);
std::cerr << "IPMIHost::addSensor host=" << this << " sensor="
<< &_sensors.back() << std::endl;
}
void IPMIHost::initalizeStrand(boost::asio::io_service& io) {
if (!_strand) {
_strand = new boost::asio::io_service::strand(io);
......
......@@ -71,9 +71,9 @@ namespace DCDB {
void addSensor(const DCDB::IPMISensor& sensor);
std::list<DCDB::IPMISensor>& getSensors() {
/* std::list<DCDB::IPMISensor>& getSensors() {
return _sensors;
}
}*/
const std::string& getMqttPrefix() const {
return _mqttPrefix;
......@@ -123,7 +123,6 @@ namespace DCDB {
uint8_t _auth;
uint8_t _priv;
std::string _mqttPrefix;
std::list<DCDB::IPMISensor> _sensors;
uint32_t _sessionTimeout;
uint32_t _retransmissionTimeout;
boost::asio::io_service::strand* _strand;
......
......@@ -26,6 +26,10 @@ namespace DCDB {
_recordId = recordId;
}
uint16_t getRecordId() const {
return _recordId;
}
uint8_t getStart() const {
return _start;
}
......
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