Ensured that config reload will re-init all helper classes.
* @param argv[] Pointer to command line argument array given to main.
* @return 0 if all is fine, -1 if not.
*/
-int Config::parse_cmd_line(int argc, char *argv[])
+int Config::parse_cmd_line(int argc, char *argv[], HTTPHelper::Ptr http_help)
{
try
{
if ( VariablesMap.count("dns_cache_ttl") )
dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
- Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
+ Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl,http_help);
if ( service )
{
ServiceHolder->add_service(service);
* @param password Password.
* @return A pointer to the created Service object.
*/
-Service::Ptr Config::create_service(const string &protocol,const string &hostname, const string &login, const string &password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl)
+Service::Ptr Config::create_service(const string &protocol,const string &hostname, const string &login, const string &password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl, const HTTPHelper::Ptr http_help)
{
if(protocol == "dhs")
{
- Service::Ptr service_dhs(new DHS(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl));
+ Service::Ptr service_dhs(new DHS(protocol,hostname,login,password,Log,http_help,update_interval,max_updates_within_interval,dns_cache_ttl));
return service_dhs;
}
else if(protocol == "ods")
* @param full_filename Filename of the service config file to load.
* @return 0 if all is fine, -1 otherwise.
*/
-int Config::load_service_config_file(const string& full_filename)
+int Config::load_service_config_file(const string& full_filename, const HTTPHelper::Ptr http_help)
{
Log->print_load_service_conf(full_filename);
if ( vm.count("dns_cache_ttl") )
dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
- Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
+ Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl,http_help);
if ( service )
{
ServiceHolder->add_service(service);
* @param config_path The path to the config directory.
* @return 0 if all is fine, -1 otherwise
*/
-int Config::load_config_from_files()
+int Config::load_config_from_files(const HTTPHelper::Ptr http_help)
{
fs::path full_config_path = fs::path(ConfigPath);
else if ( boost::regex_search( actual_file,expr ) )
{
string full_filename = dir_itr->path().string();
- if ( load_service_config_file(full_filename) != 0 )
+ if ( load_service_config_file(full_filename,http_help) != 0 )
return -1;
}
}
#include "logger.h"
#include "serviceholder.h"
+#include "httphelper.h"
#include <boost/program_options.hpp>
#include <boost/shared_ptr.hpp>
std::string ExternalWarningLog;
int ExternalWarningLevel;
- Service::Ptr create_service(const std::string &protocol,const std::string &hostname, const std::string &login, const std::string &password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl);
+ Service::Ptr create_service(const std::string &protocol,const std::string &hostname, const std::string &login, const std::string &password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl, const HTTPHelper::Ptr http_help);
int load_main_config_file(const std::string& full_filename);
- int load_service_config_file(const std::string& full_filename);
+ int load_service_config_file(const std::string& full_filename, const HTTPHelper::Ptr http_help);
public:
~Config();
- int parse_cmd_line(int argc, char *argv[]);
+ int parse_cmd_line(int argc, char *argv[], const HTTPHelper::Ptr http_help);
- int load_config_from_files();
+ int load_config_from_files(const HTTPHelper::Ptr http_help);
Options_descriptionPtr get_opt_desc_cmd() const;
* @param _login The login name.
* @param _password The corresponding password.
*/
-DHS::DHS(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const Logger::Ptr& _logger, const int _update_interval, const int _max_updates_within_interval, const int _dns_cache_ttl)
+DHS::DHS(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const Logger::Ptr& _logger, const HTTPHelper::Ptr& _http_help, const int _update_interval, const int _max_updates_within_interval, const int _dns_cache_ttl)
{
if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config)
set_update_interval(0); // use default protocol value
set_login(_login);
set_password(_password);
set_logger(_logger);
+
+ HTTPHelp = _http_help;
}
*/
int DHS::perform_update(const std::string& ip)
{
+
+
return 0;
}
template<class Archive>
void serialize(Archive & ar, const unsigned int version);
+ HTTPHelper::Ptr HTTPHelp;
+
public:
typedef boost::shared_ptr<DHS> Ptr;
DHS();
- DHS(const std::string& _protocol, const std::string& _hostname, const std::string& _login, const std::string& _password, const Logger::Ptr& _logger, const int _update_interval, const int _max_updates_within_interval, const int dns_cache_ttl);
+ DHS(const std::string& _protocol, const std::string& _hostname, const std::string& _login, const std::string& _password, const Logger::Ptr& _logger, const HTTPHelper::Ptr& _http_help, const int _update_interval, const int _max_updates_within_interval, const int dns_cache_ttl);
~DHS();
--- /dev/null
+/** @file
+ * @brief HTTPHelper class implementation. This class represents a Helper to perform HTTP operations easily.
+ *
+ *
+ *
+ * @copyright Intra2net AG
+ * @license GPLv2
+*/
+
+#include "httphelper.h"
+
+HTTPHelper::HTTPHelper()
+ : Proxy("")
+ , ProxyPort(0)
+ , Log(new Logger)
+{
+}
+
+HTTPHelper::HTTPHelper(Logger::Ptr _log, string _proxy, int _proxy_port)
+{
+ Log = _log;
+ Proxy = _proxy;
+ ProxyPort = _proxy_port;
+ CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
+}
+
+
+HTTPHelper::~HTTPHelper()
+{
+}
+
+
+
+/**
+ * Perform a HTTP GET operation
+ * @param url URL for HTTP GET operation
+ * @return The data GET from the URL
+ */
+string HTTPHelper::http_get(const string& url)
+{
+ int curl_err_code;
+
+ set_curl_url(url);
+
+ if ( (curl_err_code = curl_easy_perform(CurlEasyHandle) ) != 0 )
+ {
+ // TODO: Logging errors
+ return "";
+ }
+ // Operation performed without any problems so we can return the CurlWritedataBuff
+ return CurlWritedataBuff;
+}
+
+
+/**
+ * Initialized curl easy handle with a few options.
+ * @param curl_writedata_buff Reference to a string wich will be filled with the curl result
+ * @param curl_err_buff A pointer to an char array which will be filled with error message.
+ * @return A pointer to the easy curl handle.
+ */
+CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) const
+{
+ CURL *curl_easy_handle = curl_easy_init();
+
+ curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
+ curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
+ curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
+ curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
+ curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
+ curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
+ curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
+
+ if ( !Proxy.empty() )
+ {
+ curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
+ curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
+ }
+
+ return curl_easy_handle;
+}
+
+
+/**
+ * Sets a url to the easy curl handle
+ * @param url The url to set.
+ */
+void HTTPHelper::set_curl_url(const string& url)
+{
+ curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
+}
+
+
+/**
+ * Callback Function is called every time CURL is receiving data from HTTPS-Server and will copy all received Data to the given stream pointer
+ * @param inBuffer Pointer to input.
+ * @param size How many mem blocks are received
+ * @param nmemb size of each memblock
+ * @param outBuffer Pointer to output stream.
+ * @return The size received.
+ */
+int HTTPHelper::http_receive( char *inBuffer, size_t size, size_t nmemb, string *outBuffer )
+{
+ outBuffer->append(inBuffer);
+ return (size*nmemb);
+}
+
--- /dev/null
+/** @file
+ * @brief HTTPHelper class header. This class represents a Helper to perform HTTP operations easily.
+ *
+ *
+ *
+ * @copyright Intra2net AG
+ * @license GPLv2
+*/
+
+#ifndef HTTPHELPER_H
+#define HTTPHELPER_H
+
+#include "logger.h"
+
+#include <boost/shared_ptr.hpp>
+#include <curl/curl.h>
+
+
+class HTTPHelper
+{
+private:
+
+ std::string Proxy;
+ int ProxyPort;
+ CURL* CurlEasyHandle;
+ std::string CurlWritedataBuff;
+ char* CurlErrBuff;
+
+ Logger::Ptr Log;
+
+public:
+
+ typedef boost::shared_ptr<HTTPHelper> Ptr;
+
+ HTTPHelper();
+
+ HTTPHelper(Logger::Ptr _log, std::string _proxy, int _proxy_port);
+
+ ~HTTPHelper();
+
+ CURL* init_curl(std::string& curl_writedata_buff, char* curl_err_buff) const;
+
+
+ std::string http_get(const std::string& url);
+
+ void set_curl_url(const std::string& url);
+
+ // libcurl is a C library, so we have to make the callback member function static :-(
+ static int http_receive(char *inBuffer, size_t size, size_t nmemb, std::string *outBuffer);
+
+};
+
+#endif
#include <boost/shared_ptr.hpp>
#include <curl/curl.h>
-/**
- @author Bjoern Sikora <bjoern.sikora@intra2net.com>
-*/
+
class IPHelper
{
private:
{
string external = ExternalWarningLog;
external.append(" ");
+ external.append("\"");
external.append(msg);
+ external.append("\"");
int ret_val = system(external.c_str());
}
}
if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
{
ostringstream msg;
- msg << "This hosts' IP could not be determined through any configured webcheck url." << endl;
+ msg << "IP-Address of this host could not be determined through any configured webcheck url." << endl;
log_warning(msg.str(),level);
}
}
*/
void Logger::print_regex_ip_not_found(const string& data) const
{
- int level = 0;
+ int level = 1;
if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
{
ostringstream msg;
*/
void Logger::print_update_not_allowed(const int current_time, const int old_time, const int MaxUpdatesWithinInterval, const string& service) const
{
- int level = 0;
+ int level = 1;
if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) )
{
ostringstream msg;
#include "serviceholder.cpp"
#include "updater.cpp"
#include "iphelper.cpp"
+#include "httphelper.cpp"
#include "serializeservicecontainer.cpp"
updater = _updater;
_updater.reset();
- // load the cmd options
- if ( updater->init_config_from_cmd(argc,argv) != 0 )
- return -1;
-
- // load the config and service files
- if ( updater->init_config_from_files() != 0 )
+ // load config and initialize helper classes
+ if ( updater->load_config(argc,argv) != 0 )
return -1;
// open pidfile and check for running process
if ( init_signals() != 0)
return -1;
- // init the ip_helper
- if ( updater->init_ip_helper() != 0 )
- return -1;
-
// init daemon_mode if enabled
if ( init_daemon_mode(updater->get_config()->get_daemon_mode()) != 0 )
return -1;
}while ( updater->get_config()->get_daemon_mode() == 1 );
-
// Serialize services to save their actual state.
if ( shutdown() != 0 )
return -1;
}
+
+/**
+ * Load config and initialize helper classes.
+ * @param argc Number of arguments in array
+ * @param argv Array with cmd options
+ */
+int Updater::load_config(int argc, char *argv[])
+{
+ // load the cmd options
+ if ( init_config_from_cmd(argc,argv) != 0 )
+ return -1;
+
+ // load the config and service files
+ if ( init_config_from_files() != 0 )
+ return -1;
+
+ // init all helper classes
+ if ( init_helper_classes() != 0 )
+ return -1;
+
+ return 0;
+}
+
+
+/**
+ * Reloading the config. Delete all Service objects and then init new Service objects from config files.
+ */
+int Updater::reload_config()
+{
+ // serialize all service objects
+ if ( ServiceHolder->serialize_services() != 0 )
+ return -1;
+
+ // delete all service objects
+ ServiceHolder->delete_services();
+
+ // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
+ Conf->delete_variables_map();
+
+ // load only config files
+ if ( init_config_from_files() != 0 )
+ return -1;
+
+ // init all helper classes
+ if ( init_helper_classes() != 0 )
+ return -1;
+
+ return 0;
+}
+
+
/**
* Parse the command line arguments and initialize corresponding options.
* @param argc Number command line arguments.
int Updater::init_config_from_cmd(int argc, char *argv[])
{
// Load the command line parameters
- if( Conf->parse_cmd_line( argc, argv) != 0)
+ if( Conf->parse_cmd_line( argc, argv, HTTPHelp) != 0)
return -1;
// If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
int Updater::init_config_from_files()
{
// Load the main and service config files in config path
- if ( Conf->load_config_from_files() != 0 )
+ if ( Conf->load_config_from_files(HTTPHelp) != 0 )
return -1;
// Re-init log facility, perhaps new config file options for logger are set.
/**
- * Getter for member Config.
- * @return Member Config.
+ * Init all Helper classes
+ * @return
*/
-Config::Ptr Updater::get_config() const
+int Updater::init_helper_classes()
{
- return Conf;
-}
+ // Initialize IPHelper
+ if ( init_ip_helper() != 0 )
+ return -1;
+ // Initialize HTTPHelper
+ if ( init_http_helper() != 0 )
+ return -1;
-/**
- * Getter for member Logger.
- * @return Member Logger.
- */
-Logger::Ptr Updater::get_logger() const
-{
- return Log;
+ return 0;
}
/**
- * Reloading the config. Delete all Service objects and then init new Service objects from config files.
+ * Init the IPHelp member with needed values.
+ * @return 0 if all is fine, -1 otherwise.
*/
-int Updater::reload_config()
+int Updater::init_http_helper()
{
- // serialize all service objects
- if ( ServiceHolder->serialize_services() != 0 )
- return -1;
+ // initialize IPHelper
+ HTTPHelper::Ptr _httphelp(new HTTPHelper(Log,Conf->get_proxy(),Conf->get_proxy_port()));
+ HTTPHelp = _httphelp;
+ _httphelp.reset();
- // delete all service objects
- ServiceHolder->delete_services();
+ return 0;
+}
- // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
- Conf->delete_variables_map();
- // load only config files
- if ( init_config_from_files() != 0 )
- return -1;
+/**
+ * Getter for member Config.
+ * @return Member Config.
+ */
+Config::Ptr Updater::get_config() const
+{
+ return Conf;
+}
- return 0;
+
+/**
+ * Getter for member Logger.
+ * @return Member Logger.
+ */
+Logger::Ptr Updater::get_logger() const
+{
+ return Log;
}
#include "config.h"
#include "iphelper.h"
+#include "httphelper.h"
#include "logger.h"
#include "serviceholder.h"
Config::Ptr Conf;
IPHelper::Ptr IPHelp;
+ HTTPHelper::Ptr HTTPHelp;
Logger::Ptr Log;
Serviceholder::Ptr ServiceHolder;
Serviceholder::Ptr get_service_holder() const;
+ int load_config(int argc, char *argv[]);
+
int reload_config();
+ int init_helper_classes();
+
void init_log_facility();
int init_ip_helper();
+
+ int init_http_helper();
};
#endif