Commit 5bc0d229 authored by Micha Mueller's avatar Micha Mueller
Browse files

Add global option to specify a writeable tempdir

parent bff17558
......@@ -30,6 +30,7 @@ Configuration::Configuration(const std::string& cfgFilePath) :
_global.brokerPort = 1883;
_global.mqttPrefix = "";
_global.threads = 1;
_global.tempdir = "./";
_global.logLevelFile = boost::log::trivial::trace;
_global.logLevelCmd = boost::log::trivial::info;
}
......@@ -62,18 +63,23 @@ bool Configuration::readGlobal() {
//read global variables
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
if(boost::iequals(global.first, "mqttBroker")) {
if (boost::iequals(global.first, "mqttBroker")) {
_global.brokerHost = global.second.data();
size_t pos = _global.brokerHost.find(":");
if (pos != string::npos) {
_global.brokerPort = stoi(_global.brokerHost.substr(pos+1));
_global.brokerHost.erase(pos);
}
} else if(boost::iequals(global.first, "mqttprefix")) {
} else if (boost::iequals(global.first, "mqttprefix")) {
_global.mqttPrefix = global.second.data();
if (_global.mqttPrefix[_global.mqttPrefix.length()-1] != '/') {
_global.mqttPrefix.append("/");
}
} else if (boost::iequals(global.first, "tempdir")) {
_global.tempdir = global.second.data();
if (_global.tempdir[_global.tempdir.length()-1] != '/') {
_global.tempdir.append("/");
}
} else if (boost::iequals(global.first, "threads")) {
_global.threads = stoi(global.second.data());
} else if (boost::iequals(global.first, "daemonize")) {
......@@ -166,7 +172,7 @@ bool Configuration::read() {
dynLib.configurator = dynLib.create();
//set prefix to global prefix (may be overwritten)
dynLib.configurator->setMqttPrefix(_global.mqttPrefix);
dynLib.configurator->setGlobalSettings(_global);
//read in config
std::vector<Sensor*>& sensors = dynLib.configurator->readConfig(pluginConfig);
......
......@@ -20,16 +20,6 @@
typedef std::vector<Sensor*> sensorVector_t;
typedef std::set<std::string> mqttSet_t;
typedef struct {
int daemonize;
int brokerPort;
std::string brokerHost;
std::string mqttPrefix;
uint32_t threads;
boost::log::trivial::severity_level logLevelFile;
boost::log::trivial::severity_level logLevelCmd;
} global_t;
/**
* Class responsible of reading the global configuration as well as loading and invoking required dynamic libraries.
*/
......
......@@ -13,6 +13,17 @@
#include <vector>
#include <string>
typedef struct {
int daemonize;
int brokerPort;
std::string brokerHost;
std::string mqttPrefix;
std::string tempdir;
uint32_t threads;
boost::log::trivial::severity_level logLevelFile;
boost::log::trivial::severity_level logLevelCmd;
} global_t;
/**
* Abstract base class, which defines the interface for the configurators in the shared dynamic libraries.
*/
......@@ -28,8 +39,8 @@ public:
*/
virtual std::vector<Sensor*>& readConfig(std::string cfgPath) = 0;
void setMqttPrefix(const std::string& mqttPrefix) {
_mqttPrefix = mqttPrefix;
virtual void setGlobalSettings(const global_t& globalSettings) {
_mqttPrefix = globalSettings.mqttPrefix;
}
protected:
......
......@@ -76,6 +76,7 @@ void printSyntax()
cout << " -p<port> MQTT broker port" << endl;
cout << " -m<string> MQTT topic prefix" << endl;
cout << " -t<number> Thread count" << endl;
cout << " -w<path> Writable directory for temporal files" << endl;
cout << endl;
cout << " -d Daemonize" << endl;
cout << " -v Set verbosity of output." << endl
......@@ -92,7 +93,7 @@ int main(int argc, char** argv) {
}
//define allowed command-line options once
const char opts[] = "b:p:m:t:v:dh";
const char opts[] = "b:p:m:t:v:w:dh";
//check if help flag specified
char c;
......@@ -112,22 +113,6 @@ int main(int argc, char** argv) {
//init LOGGING
boost::log::add_common_attributes();
//TODO what to do to avoid writing to the same (growing to infinity) log-file forever?
//set up logger to file
auto fileSink = boost::log::add_file_log(
boost::log::keywords::file_name = "dcdb.log", //
//boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0), //Throws bad year-exception for no obious reason
boost::log::keywords::format = //("[%TimeStamp%] [%ThreadID%] [%Severity%]: %Message%") //whyever %TimeStamp% throws a bad year-exception >:(
( boost::log::expressions::stream
<< boost::log::expressions::attr< unsigned int >("LineID")
<< " [" << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S") << "]"
<< " " << boost::log::expressions::attr<boost::log::attributes::current_thread_id::value_type >("ThreadID")
<< " <" << boost::log::trivial::severity << ">"
<< ": " << boost::log::expressions::smessage
),
boost::log::keywords::filter = (boost::log::trivial::severity >= boost::log::trivial::trace),
boost::log::keywords::auto_flush = true
);
//set up logger to command line
auto cmdSink = boost::log::add_console_log(
std::cout,
......@@ -141,7 +126,7 @@ int main(int argc, char** argv) {
//get logger instance
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
//finished logging startup
//finished logging startup for the moment (file log added later)
boost::asio::io_service io;
boost::thread_group threads;
......@@ -183,6 +168,12 @@ int main(int argc, char** argv) {
case 'd':
globalSettings.daemonize = 1;
break;
case 'w':
globalSettings.tempdir = optarg;
if (globalSettings.tempdir[globalSettings.tempdir.length()-1] != '/') {
globalSettings.tempdir.append("/");
}
break;
case 'h':
printSyntax();
return 1;
......@@ -195,10 +186,30 @@ int main(int argc, char** argv) {
//propagate overwritten values back to cfg
cfg.setGlobal(globalSettings);
//TODO what to do to avoid writing to the same (growing to infinity) log-file forever?
//we now should now where the writeable tempdir is
//set up logger to file
auto fileSink = boost::log::add_file_log(
boost::log::keywords::file_name = globalSettings.tempdir + "dcdb.log", //
//boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0), //Throws bad year-exception for no obious reason
boost::log::keywords::format = //("[%TimeStamp%] [%ThreadID%] [%Severity%]: %Message%") //whyever %TimeStamp% throws a bad year-exception >:(
( boost::log::expressions::stream
<< boost::log::expressions::attr< unsigned int >("LineID")
<< " [" << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S") << "]"
<< " " << boost::log::expressions::attr<boost::log::attributes::current_thread_id::value_type >("ThreadID")
<< " <" << boost::log::trivial::severity << ">"
<< ": " << boost::log::expressions::smessage
),
boost::log::keywords::filter = (boost::log::trivial::severity >= boost::log::trivial::trace),
boost::log::keywords::auto_flush = true
);
//severity level may be overwritten (per option or config-file) --> set it according to globalSettings
fileSink->set_filter(boost::log::trivial::severity >= globalSettings.logLevelFile);
cmdSink->set_filter(boost::log::trivial::severity >= globalSettings.logLevelCmd);
LOG(info) << "Logging setup complete";
//Read in rest of configuration. Also creates all sensors
if(!cfg.read()) {
LOG(fatal) << "Failed to read configuration!";
......@@ -216,6 +227,7 @@ int main(int argc, char** argv) {
} else {
LOG(info) << " Daemonize: Disabled";
}
LOG(info) << " Write-Dir: " << globalSettings.tempdir;
//Init all sensors
LOG(info) << "Starting sensors...";
......
......@@ -69,6 +69,7 @@ std::vector<Sensor*>& IPMIConfigurator::readConfig(std::string cfgPath) {
_hosts.push_back(DCDB::IPMIHost(host.second.data(), _globalHost.retransmissionTimeout, _globalHost.sessionTimeout));
DCDB::IPMIHost &ipmiHost = _hosts.back();
ipmiHost.setMqttPrefix(_mqttPrefix);
ipmiHost.setCache(_tempdir);
if (!host.second.empty()) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &h, host.second) {
if (boost::iequals(h.first, "username")) {
......
......@@ -35,6 +35,12 @@ namespace DCDB {
std::vector<Sensor*>& readConfig(std::string cfgPath);
//Overwrite from Configurator
virtual void setGlobalSettings(const global_t& globalSettings) {
Configurator::setGlobalSettings(globalSettings);
_tempdir = globalSettings.tempdir;
}
private:
/**
* Set the variables of sensor according to the values specified in config.
......@@ -43,6 +49,7 @@ namespace DCDB {
*/
bool readSensor(IPMISensor& sensor, boost::property_tree::iptree& config);
std::string _tempdir;
std::vector<Sensor*> _sensors;
sensorMap_t _templateSensors;
hostList_t _hosts;
......
......@@ -30,6 +30,7 @@ IPMIHost::IPMIHost(const std::string& hostName, uint32_t retransmissionTimeout,
_hostName = hostName;
_userName = std::string("admin");
_password = std::string("admin");
_cache = "";
_auth = IPMI_AUTHENTICATION_TYPE_MD5;
_priv = IPMI_PRIVILEGE_LEVEL_ADMIN;
_mqttPrefix = "";
......@@ -86,15 +87,13 @@ void IPMIHost::createSdrCache() {
}
}
std::string cacheName = ".ipmiPluginSdrCache." + _hostName;
_sensorReadCtx = ipmi_sensor_read_ctx_create(_ipmiCtx);
_sdrCtx = ipmi_sdr_ctx_create();
std::string errorMsg;
if (ipmi_sdr_cache_create(_sdrCtx, _ipmiCtx, cacheName.c_str(), IPMI_SDR_CACHE_CREATE_FLAGS_DEFAULT, NULL, NULL) < 0) {
if (ipmi_sdr_cache_create(_sdrCtx, _ipmiCtx, _cache.c_str(), IPMI_SDR_CACHE_CREATE_FLAGS_DEFAULT, NULL, NULL) < 0) {
errorMsg = ipmi_sdr_ctx_errormsg(_sdrCtx);
} else if (ipmi_sdr_cache_open(_sdrCtx, _ipmiCtx, cacheName.c_str()) < 0) {
} else if (ipmi_sdr_cache_open(_sdrCtx, _ipmiCtx, _cache.c_str()) < 0) {
errorMsg = ipmi_sdr_ctx_errormsg(_sdrCtx);
} else {
return;
......@@ -107,8 +106,7 @@ void IPMIHost::createSdrCache() {
void IPMIHost::destroySdrCache() {
if (_sdrCtx) {
ipmi_sdr_cache_close(_sdrCtx);
std::string cacheName = ".ipmiPluginSdrCache." + _hostName;
ipmi_sdr_cache_delete(_sdrCtx, cacheName.c_str());
ipmi_sdr_cache_delete(_sdrCtx, _cache.c_str());
ipmi_sdr_ctx_destroy(_sdrCtx);
_sdrCtx = NULL;
}
......
......@@ -53,6 +53,14 @@ namespace DCDB {
_password = password;
}
const std::string& getCache() const {
return _cache;
}
void setCache(const std::string& cacheDir) {
_cache = cacheDir + ".ipmiPluginSdrCache." + _hostName;
}
uint8_t getPriv() const {
return _priv;
}
......@@ -114,6 +122,7 @@ namespace DCDB {
std::string _hostName;
std::string _userName;
std::string _password;
std::string _cache;
uint8_t _auth;
uint8_t _priv;
std::string _mqttPrefix;
......
......@@ -47,9 +47,7 @@ namespace DCDB {
}
}
catch (const std::exception& e) {
#ifdef DEBUG
LOG(debug) << _host->getHostName() << "::" << _name << " " << _mqtt << " => " << e.what();
#endif
LOG(error) << _host->getHostName() << "::" << _name << " " << _mqtt << " => " << e.what();
return;
}
#ifdef DEBUG
......
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