Commit 2088e1d7 authored by Michael Ott's avatar Michael Ott

Allow for actual configuration files to be specified on command line, not just...

Allow for actual configuration files to be specified on command line, not just configuration directories. Add exception handling to main of pusher, collectagent, grafanaserver
parent 66a9aac0
......@@ -46,14 +46,13 @@ void AnalyticsController::stop() {
_initialized = false;
}
bool AnalyticsController::initialize(Configuration& settings, const string& configPath) {
bool AnalyticsController::initialize(Configuration& settings) {
_settings = settings;
_configPath = configPath;
_navigator = make_shared<SensorNavigator>();
// A sensor navigator is only built if operator plugins are expected to be instantiated
QueryEngine &_queryEngine = QueryEngine::getInstance();
if(_manager->probe(_configPath, "collectagent.conf")) {
if(_manager->probe(settings.cfgFilePath, settings.cfgFileName)) {
vector<string> topics;
for(const auto& kv : _metadataStore->getMap())
if(kv.second.isValid())
......@@ -77,7 +76,7 @@ bool AnalyticsController::initialize(Configuration& settings, const string& conf
//TODO: find a better solution to disable the SensorBase default cache
_settings.pluginSettings.cacheInterval = 0;
if(!_manager->load(_configPath, "collectagent.conf", _settings.pluginSettings)) {
if(!_manager->load(_settings.cfgFilePath, _settings.cfgFileName, _settings.pluginSettings)) {
LOG(fatal) << "Failed to load data analytics manager!";
return false;
}
......
......@@ -108,7 +108,7 @@ public:
* @param configPath Path to the configuration files for the data analytics framework
* @return True if successful, false otherwise
*/
bool initialize(Configuration& settings, const string& configPath);
bool initialize(Configuration& settings);
/**
* @brief Sets the cache to be used for sensors
......@@ -222,7 +222,6 @@ private:
shared_ptr<OperatorManager> _manager;
// Misc configuration attributes
Configuration _settings;
string _configPath;
// Readings counter
uint64_t _readingCtr;
......
......@@ -529,7 +529,7 @@ void usage() {
012345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
cout << "Usage:" << endl;
cout << " collectagent [-d] [-s] [-x] [-a] [-m<host>] [-c<host>] [-u<username>] [-p<password>] [-t<ttl>] [-v<verbosity>] <path/to/configfiles/>" << endl;
cout << " collectagent [-d] [-s] [-x] [-a] [-m<host>] [-c<host>] [-u<username>] [-p<password>] [-t<ttl>] [-v<verbosity>] <config>" << endl;
cout << " collectagent -h" << endl;
cout << endl;
......@@ -557,7 +557,7 @@ int main(int argc, char* const argv[]) {
// Checking if path to config is supplied
if (argc <= 1) {
cout << "Please specify a path to the config-directory" << endl << endl;
cout << "Please specify a path to the config-directory or a config-file" << endl << endl;
usage();
exit(EXIT_FAILURE);
}
......@@ -584,10 +584,7 @@ int main(int argc, char* const argv[]) {
auto cmdSink = setupCmdLogger();
Configuration config(argv[argc - 1], "collectagent.conf");
if( !config.readConfig() ) {
LOG(fatal) << "Failed to read global configuration!";
exit(EXIT_FAILURE);
}
config.readConfig();
// References to shorten access to config parameters
Configuration& settings = config;
......@@ -739,7 +736,7 @@ int main(int argc, char* const argv[]) {
queryEngine.setGroupQueryCallback(sensorGroupQueryCallback);
queryEngine.setMetadataQueryCallback(metadataQueryCallback);
queryEngine.setJobQueryCallback(jobQueryCallback);
if(!analyticsController->initialize(settings, argv[argc - 1]))
if(!analyticsController->initialize(settings))
return EXIT_FAILURE;
LOG_LEVEL vLogLevel = settings.validateConfig ? LOG_LEVEL::info : LOG_LEVEL::debug;
......@@ -876,6 +873,10 @@ int main(int argc, char* const argv[]) {
delete metadataStore;
LOG(info) << "Collect Agent closed. Bye bye...";
}
catch (const std::runtime_error& e) {
LOG(fatal) << e.what();
return EXIT_FAILURE;
}
catch (const exception& e) {
LOG(fatal) << "Exception: " << e.what();
abrt(EXIT_FAILURE, INTERR);
......
......@@ -171,7 +171,7 @@ public:
*
* @return True if successful, false otherwise
*/
bool readConfig();
void readConfig();
/**
* @brief Reads user credentials from the config file and accordingly adds
......@@ -192,7 +192,9 @@ public:
analyticsSettings_t analyticsSettings;
serverSettings_t restAPISettings;
pluginSettings_t pluginSettings;
std::string cfgFilePath;
std::string cfgFileName;
protected:
/**
* @brief Virtual method to integrate additional configuration blocks
......@@ -215,10 +217,6 @@ protected:
*/
virtual bool readAdditionalValues(boost::property_tree::iptree::value_type &global) { return false; }
// Path and name of the configuration file
std::string _cfgFilePath;
std::string _cfgFileName;
// Logger object
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
};
......
......@@ -27,25 +27,35 @@
#include "globalconfiguration.h"
#include "RESTHttpsServer.h"
#include "boost/filesystem.hpp"
GlobalConfiguration::GlobalConfiguration(const std::string& cfgFilePath, const std::string& cfgFileName) {
_cfgFileName = cfgFileName;
_cfgFilePath = cfgFilePath;
if (_cfgFilePath[_cfgFilePath.length()-1] != '/')
_cfgFilePath.append("/");
boost::filesystem::path p(cfgFilePath);
if (boost::filesystem::exists(p)) {
if (boost::filesystem::is_directory(p)) {
this->cfgFileName = cfgFileName;
this->cfgFilePath = cfgFilePath;
} else {
this->cfgFileName = p.filename().native();
this->cfgFilePath = p.parent_path().native();
}
if (this->cfgFilePath[this->cfgFilePath.length()-1] != '/')
this->cfgFilePath.append("/");
} else {
throw std::runtime_error(cfgFilePath + " does not exist");
}
}
bool GlobalConfiguration::readConfig() {
void GlobalConfiguration::readConfig() {
// Open file
std::string globalConfig = _cfgFilePath;
globalConfig.append(_cfgFileName);
std::string globalConfig = cfgFilePath;
globalConfig.append(cfgFileName);
boost::property_tree::iptree cfg;
// Parse to property_tree
try {
boost::property_tree::read_info(globalConfig, cfg);
} catch (boost::property_tree::info_parser_error& e) {
LOG(error) << "Error when reading " << _cfgFileName << ": " << e.what();
return false;
throw std::runtime_error("Error when parsing " + globalConfig + ": " + e.what());
}
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("global")) {
......@@ -119,20 +129,19 @@ bool GlobalConfiguration::readConfig() {
}
readAdditionalBlocks(cfg);
return true;
}
bool GlobalConfiguration::readRestAPIUsers(RESTHttpsServer* server) {
//open file
std::string globalConfig = _cfgFilePath;
globalConfig.append(_cfgFileName);
std::string globalConfig = cfgFilePath;
globalConfig.append(cfgFileName);
boost::property_tree::iptree cfg;
//parse to property_tree
try {
boost::property_tree::read_info(globalConfig, cfg);
} catch (boost::property_tree::info_parser_error& e) {
LOG(error) << "Error when reading users from " << _cfgFileName << ":" << e.what();
LOG(error) << "Error when reading users from " << cfgFileName << ":" << e.what();
return false;
}
......
......@@ -53,18 +53,18 @@ bool Configuration::readAdditionalValues(boost::property_tree::iptree::value_typ
}
bool Configuration::readPlugins(PluginManager &pluginManager) {
std::string globalConfig = _cfgFilePath;
globalConfig.append(_cfgFileName);
std::string globalConfig = cfgFilePath;
globalConfig.append(cfgFileName);
boost::property_tree::iptree cfg;
try {
boost::property_tree::read_info(globalConfig, cfg);
} catch (boost::property_tree::info_parser_error &e) {
LOG(error) << "Error when reading plugins from " << _cfgFileName << ": " << e.what();
LOG(error) << "Error while parsing plugins from " << globalConfig << ": " << e.what();
return false;
}
pluginManager.setCfgFilePath(_cfgFilePath);
pluginManager.setCfgFilePath(cfgFilePath);
//read plugins
BOOST_FOREACH (boost::property_tree::iptree::value_type &plugin, cfg.get_child("plugins")) {
if (boost::iequals(plugin.first, "plugin")) {
......
This diff is collapsed.
This diff is collapsed.
......@@ -23,6 +23,7 @@ LIBS = -L../lib \
-lboost_log \
-lboost_regex \
-lboost_log_setup \
-lboost_filesystem \
-lcrypto \
-lssl
......
Markdown is supported
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