diff --git a/conf/datasid-conf.json b/conf/datasid-conf.json index 53fe43f6af284f318429ef66a7b67801ef45c27b..9f2e72fb8219069289033e1e9be45e0305ec2b9c 100644 --- a/conf/datasid-conf.json +++ b/conf/datasid-conf.json @@ -4,4 +4,4 @@ "webService": "%s", "version": "%s", "project" : "%s" -} +} \ No newline at end of file diff --git a/include/agent/agent.h b/include/agent/agent.h index c27f676583dfa050a2ffdb2c247854ab17123a97..290d82904f5bca346e21136d57d2107bd5615ce9 100644 --- a/include/agent/agent.h +++ b/include/agent/agent.h @@ -1,6 +1,8 @@ #pragma once #include <iostream> #include <fstream> +#include <agent/parse_config_file.h> +#include <agent/parse_proxy_file.h> #include <boost/filesystem.hpp> #include <boost/filesystem/path.hpp> #include <boost/regex.hpp> @@ -22,33 +24,42 @@ * <br><br> */ +class Conf; // forward declarations +class Proxy; // forward declarations + class Agent{ private: - std::string confDir; /*!< Absolute path from conf directory */ - std::string configFile; /*!< Absolute path from config file */ - std::string dirInstall; /*!< Absolute path from local to install agent */ - std::string logDir; /*!< Absolute path from log directory */ - std::string logFile; /*!< Absolute path from log file */ + std::string pathConfDir; /*!< Absolute path from conf directory */ + std::string pathConfigFile; /*!< Absolute path from config file */ + std::string pathDirInstall; /*!< Absolute path from local to install agent */ + std::string pathLogDir; /*!< Absolute path from log directory */ + std::string pathLogFile; /*!< Absolute path from log file */ std::string message; /*!< Success, warning and error messages to be setted in logfile */ std::string prefix; /*!< Absolute path from agent */ - std::string runName; /*!< Name of agent .run file */ + std::string installerName; /*!< Name of agent installer file */ + Conf* conf; + Proxy* proxy; public: - void setConfDir(std::string); - void setConfigFile(std::string); - void setLogDir(std::string); - void setLogFile(std::string); + void setPathConfDir(std::string); + void setPathConfigFile(std::string); + void setPathLogDir(std::string); + void setPathLogFile(std::string); void setMessage(std::string); void setPrefix(std::string, int); - void setRunName(std::string); + void setInstallerName(std::string); + void setConf(Conf*); + void setProxy(Proxy*); - std::string getConfDir() const; - std::string getConfigFile() const; - std::string getLogDir() const; - std::string getLogFile() const; + std::string getPathConfDir() const; + std::string getPathConfigFile() const; + std::string getPathLogDir() const; + std::string getPathLogFile() const; std::string getMessage() const; std::string getPrefix() const; - std::string getRunName() const; + std::string getInstallerName() const; + Conf* getConf() const; + Proxy* getProxy() const; void prefixISet(); }; \ No newline at end of file diff --git a/include/agent/common.h b/include/agent/common.h index 0be2e483b9c7b2e906ed4b501f4ace6e3bb0a39e..0a35622a959df86374a9323f7fbf0ae43b72151c 100644 --- a/include/agent/common.h +++ b/include/agent/common.h @@ -16,7 +16,7 @@ /** * To make parsing from config and proxy file. */ -bool common(std::fstream*, Agent*, Conf*, Proxy*); +bool common(std::fstream*, Agent*); /** * Check if a file exist. diff --git a/include/agent/get.h b/include/agent/get.h index 02c3d0727f3ba744758446e6afe105db046e1875..6504c017295a2b823667bd6bf4cc5cfb39258dcc 100644 --- a/include/agent/get.h +++ b/include/agent/get.h @@ -4,12 +4,12 @@ #include <stdlib.h> #include <cpr/cpr.h> #include <json/json.h> -#include <agent/agent.h> #include <agent/common.h> #include <agent/curl.h> -#include <agent/parse_config_file.h> +#include <agent/helper.h> #include <agent/parse_proxy_file.h> #include <agent/rotate_log_file.h> +#include <boost/regex.hpp> #ifdef WIN32 #include <windows.h> @@ -21,8 +21,9 @@ const std::string url = "http://localhost:3001"; * @brief Check case if has a new agent version available */ -int check_update(); +int check_update(Agent*, std::fstream*); bool create_directories(std::string); -bool compare_version(Agent*, Conf*, std::string, std::fstream*); -bool download(Conf, Agent*, std::fstream*); +bool compare_version(Agent*, std::string, std::fstream*); +bool download(Agent*, std::fstream*, std::string); void execute_agent(boost::filesystem::path); +bool get_agent_installer_name(Conf*, std::string*); diff --git a/include/agent/get_disks_info.h b/include/agent/get_disks_info.h index a7816f30df7129722bda4b9d087e5d427942ece9..e6220b40bf26111bd0885c2f921bc212e9cd23a4 100644 --- a/include/agent/get_disks_info.h +++ b/include/agent/get_disks_info.h @@ -7,9 +7,11 @@ #endif #include <iostream> #include <fstream> +#include <math.h> #include <list> #include <string> #include <agent/helper.h> +#include <boost/regex.hpp> /** * @file get_disks_info.h @@ -26,4 +28,4 @@ typedef struct disk disk_t; void get_scsi_disks(std::list<disk_t>&); void get_disks_info(std::list<disk_t>&); -std::string get_disk_used(); \ No newline at end of file +int get_disk_used(); \ No newline at end of file diff --git a/include/agent/main.h b/include/agent/main.h index 3fd10b025535a78c0a4ed8b546ba186b817c4308..15b85cbdbe130451a96a9df08ec70e65b1040dab 100644 --- a/include/agent/main.h +++ b/include/agent/main.h @@ -3,6 +3,13 @@ #include <fstream> #include <iostream> #include <string> +#include <agent/agent.h> +#include <agent/agent_uninstall.h> +#include <agent/common.h> +#include <agent/get.h> +#include <agent/parse_config_file.h> +#include <agent/parse_proxy_file.h> +#include <agent/post.h> #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/date_time/posix_time/posix_time.hpp> @@ -10,9 +17,6 @@ #include <boost/program_options/variables_map.hpp> #include <boost/filesystem/fstream.hpp> #include <cpr/cpr.h> -#include <agent/get.h> -#include <agent/post.h> -#include <agent/agent_uninstall.h> namespace pt = boost::posix_time; namespace po = boost::program_options; @@ -21,6 +25,26 @@ namespace pholders = boost::asio::placeholders; namespace sys = boost::system; namespace fs = boost::filesystem; +#ifdef __linux__ + std::string path_to_agent_tmp = // NOLINT(runtime/string) + "/opt/agentC3SL/tmp/"; // NOLINT(runtime/string) + std::string path_to_agent = "/opt/"; // NOLINT(runtime/string) + //std::string path_to_agent_conf = // NOLINT(runtime/string) + //"/opt/agentC3SL/conf/"; // NOLINT(runtime/string) + std::string path_to_agent_conf = "/home/lais/Imagens/agent/conf/"; +#else + // Temporary... Define agent path + std::string path_to_agent_tmp = "C:\\agentC3SL\\tmp\\"; + std::string path_to_agent = "C:\\agentC3SL\\"; + std::string path_to_agent_conf = // NOLINT(runtime/string) + "C:\\agentC3SL\\conf\\"; // NOLINT(runtime/string) +#endif + +bool agent_send_inventory = false; +bool agent_send_net_band = false; +bool agent_send_user_hist = false; +bool agent_send_database = false; + /** * @file main.h * @brief Run agent to collect inventory, network bandwidth and user history to send c3sl web service; Also check case if has a new agent version available @@ -42,8 +66,10 @@ int main(int, char**); */ int parse_command_line(int, char*, int*, int*, bool*); +bool read_config_files(Agent*, std::fstream*); + /** Run agent */ -int run_agent(const sys::error_code& /*e*/, pt::ptime, bool); +int run_agent(const sys::error_code& /*e*/, pt::ptime, bool, Agent*); /** Set the first date and time when agent was run */ void set_begin(pt::ptime&); diff --git a/include/agent/network_bandwidth.h b/include/agent/network_bandwidth.h index 8036f4dc14cdb53da91554849cdc83d5d1a73f14..a85f18c91c78cbe4689a7443a1b016918edd4788 100644 --- a/include/agent/network_bandwidth.h +++ b/include/agent/network_bandwidth.h @@ -5,6 +5,8 @@ #include <stdlib.h> #include <json/json.h> #include <agent/get_macaddr.h> +#include <agent/get_date.h> +#include <agent/get_time.h> #include <agent/helper.h> #include <boost/date_time/posix_time/posix_time.hpp> diff --git a/include/agent/parse_config_file.h b/include/agent/parse_config_file.h index 28bbe2713dc34122a013f547078d2c6726155aa0..987a5f87eb6be4c626b3c392721073fcdf419e4a 100644 --- a/include/agent/parse_config_file.h +++ b/include/agent/parse_config_file.h @@ -11,33 +11,35 @@ * @brief This file has a Conf class - The main purpose is get config file content and make parse to set in a conf object */ +class Agent; + class Conf{ private: int idPoint; - std::string proxyConf; /*!< Absolute path from proxy file. */ + std::string pathProxyFile; /*!< Absolute path from proxy file. */ std::string webService; /*!< url from web service. */ std::string version; /*!< Current agent version */ std::string project; public: void setIdPoint(int); - void setProxyConf(std::string); + void setPathProxyFile(std::string); void setWebService(std::string); void setVersion(std::string); void setProject(std::string); int getIdPoint() const; - std::string getProxyConf() const; + std::string getPathProxyFile() const; std::string getWebService() const; std::string getVersion() const; std::string getProject() const; }; -bool parse_config_file(Agent*, Conf*, std::fstream*); +bool parse_config_file(Agent*, std::fstream*); /** * @file parse_config_file.h * @brief This file has a Conf class - The main purpose is get config file content and make parse to set in a conf object */ -bool set_conf_object(Conf*, Agent*, std::map<std::string, std::string>*); +bool set_conf_object(Agent*, std::map<std::string, std::string>*); diff --git a/include/agent/parse_proxy_file.h b/include/agent/parse_proxy_file.h index 9dc0992d5952ed603115894f5c40080d66b4ca13..9af65c0903452b2efc91aeae85b7727f98953914 100644 --- a/include/agent/parse_proxy_file.h +++ b/include/agent/parse_proxy_file.h @@ -1,6 +1,5 @@ #pragma once #include <map> -#include <agent/agent.h> #include <agent/parse_config_file.h> #include <agent/parse.h> #include <json/json.h> @@ -10,6 +9,7 @@ * @brief This file has the Proxy class - The main purpose is get proxy file content and * make parse to set in a proxy object */ +class Agent; class Proxy{ private: @@ -34,11 +34,11 @@ class Proxy{ bool proxyFileIsSet() const; }; -bool parse_proxy_file(Conf*, Proxy*, std::fstream*, Agent*); +bool parse_proxy_file(Agent*, std::fstream*); /** * @file parse_proxy_file.h * @brief This file has the Proxy class - The main purpose is get proxy file content and * make parse to set in a proxy object */ -bool set_proxy_object(Proxy*, std::map<std::string, std::string>*); +bool set_proxy_object(Agent*, std::map<std::string, std::string>*); diff --git a/include/agent/post.h b/include/agent/post.h index 7c4648d0897f28a5b2372bd61322c5da89f7e591..3acab3d47070b8b0f29e9126cc5e055b9c172a22 100644 --- a/include/agent/post.h +++ b/include/agent/post.h @@ -6,13 +6,11 @@ #include <agent/user_history.h> #include <agent/network_bandwidth.h> -const std::string base_url = "http://localhost:3001"; - /** * @file post.h * @brief Collect data from hardware and send to c3sl web service */ -int send_inventory(bool); -int send_net_bandwidth(bool); -int send_user_history(bool); +int send_inventory(bool, std::string); +int send_net_bandwidth(bool, std::string); +int send_user_history(bool, std::string); diff --git a/modules/boost.cmake b/modules/boost.cmake index 50045d355bd7b310d481506880b1f0041e528684..b4f16ecea5424153c86dd6140d1aca8b7adfd26f 100644 --- a/modules/boost.cmake +++ b/modules/boost.cmake @@ -17,10 +17,10 @@ if (NOT Boost) SET (PROGRAM_OPTIONS_LIB ${BOOST_BUILD}/lib/libboost_program_options.a) else () SET (Boost_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/Boost/src/Boost/) - SET (FILESYSTEM_LIB ${BOOST_BUILD}/lib/libboost_filesystem-vc141-mt-gd-1_65_1.lib) - SET (SYSTEM_LIB ${BOOST_BUILD}/lib/libboost_system-vc141-mt-gd-1_65_1.lib) - SET (REGEX_LIB ${BOOST_BUILD}/lib/libboost_regex-vc141-mt-gd-1_65_1.lib) - SET (PROGRAM_OPTIONS_LIB ${BOOST_BUILD}/lib/libboost_program_options-vc141-mt-gd-1_65_1.lib) + SET (FILESYSTEM_LIB ${BOOST_BUILD}/lib/libboost_filesystem-vc141-mt-gd-1_66.lib) + SET (SYSTEM_LIB ${BOOST_BUILD}/lib/libboost_system-vc141-mt-gd-1_66.lib) + SET (REGEX_LIB ${BOOST_BUILD}/lib/libboost_regex-vc141-mt-gd-1_66.lib) + SET (PROGRAM_OPTIONS_LIB ${BOOST_BUILD}/lib/libboost_program_options-vc141-mt-gd-1_66.lib) endif () if (UNIX) diff --git a/src/agent/agent.cpp b/src/agent/agent.cpp index d834ba9f9aa6e4339153cd09199a4cf88269406b..8b7f896e8e83d924c2be84929febdc494e21f136 100644 --- a/src/agent/agent.cpp +++ b/src/agent/agent.cpp @@ -22,27 +22,27 @@ #include <string> /** Set the absolute path from conf directory */ -void Agent::setConfDir(std::string dir) { - this->confDir = this->prefix + dir; +void Agent::setPathConfDir(std::string dir) { + this->pathConfDir = this->prefix + dir; } /** Set the absolute path of config filename */ -void Agent::setConfigFile(std::string filename) { - this->configFile = this->prefix + filename; +void Agent::setPathConfigFile(std::string filename) { + this->pathConfigFile = this->prefix + filename; } /** Set the absolute path of log directory */ -void Agent::setLogDir(std::string logDir) { - this->logDir = this->prefix + logDir; +void Agent::setPathLogDir(std::string logDir) { + this->pathLogDir = this->prefix + logDir; } /** Set the absolute path of log file * @param logDir absolute path from log directory * @param file name of logfile */ -void Agent::setLogFile(std::string file) { - this->logFile = this->logDir + file; +void Agent::setPathLogFile(std::string file) { + this->pathLogFile = this->prefix + file; } /** Set the log message @@ -70,29 +70,37 @@ void Agent::setPrefix(std::string p, int tot) { this->prefix = p.substr(0, i); } -/** Set the name to agent .run file */ -void Agent::setRunName(std::string r) { - this->runName = r; +/** Set the name to agent installer (.run (linux) or .exe (windows)) file */ +void Agent::setInstallerName(std::string i) { + this->installerName = i; +} + +void Agent::setConf(Conf* conf) { + this->conf = conf; +} + +void Agent::setProxy(Proxy* proxy) { + this->proxy = proxy; } /** Return the absolute path from conf directory */ -std::string Agent::getConfDir() const { - return this->confDir; +std::string Agent::getPathConfDir() const { + return this->pathConfDir; } /** Return the absolute path from default config filename */ -std::string Agent::getConfigFile() const { - return this->configFile; +std::string Agent::getPathConfigFile() const { + return this->pathConfigFile; } /** Return the absolute path from logfile */ -std::string Agent::getLogDir() const { - return this->logDir; +std::string Agent::getPathLogDir() const { + return this->pathLogDir; } /** Return the absolute path from logfile */ -std::string Agent::getLogFile() const { - return this->logFile; +std::string Agent::getPathLogFile() const { + return this->pathLogFile; } /** Return the success, error or warning message */ @@ -105,9 +113,19 @@ std::string Agent::getPrefix() const { return this->prefix; } -/** Return .run filename downloaded from webService when update is needed */ -std::string Agent::getRunName() const { - return this->runName; +/** Return (.exe) or (.run) filename downloaded from + * webService when update is needed + */ +std::string Agent::getInstallerName() const { + return this->installerName; +} + +Conf* Agent::getConf() const { + return this->conf; +} + +Proxy* Agent::getProxy() const { + return this->proxy; } /** Check if the absolute path of agent was setted */ @@ -128,6 +146,6 @@ void Agent::prefixISet() { * - message - Success, warning and error messages to be setted in logfile * - prefix - Absolute path from agent - get current local (path) of agent * - version - Current agent version - * - binName Name of agent .run file + * - installerName Name of agent installer * <br><br> */ diff --git a/src/agent/common.cpp b/src/agent/common.cpp index 2f31662be0a30468573b8465c1bb48680b816aa7..cab79e66f0809d671b56842299a7fbd2dd125754 100644 --- a/src/agent/common.cpp +++ b/src/agent/common.cpp @@ -43,45 +43,43 @@ bool file_exist(const std::string& file) { /** * To make parsing from config and proxy file. */ -bool common(std::fstream* logFile, Agent* agent, Conf* conf, - Proxy* proxy) { +bool common(std::fstream* logFile, Agent* agent) { try { agent->prefixISet(); } catch (const char* error) { - agent->setMessage(__DATE__ + *error); - *logFile << agent->getMessage() << std::endl; + *logFile << __DATE__ + *error << std::endl; return false; } /* Read config file and declare only valid variables */ - - if (!file_exist(agent->getConfigFile())) { - agent->setMessage(__DATE__ + - std::string(" - ERROR: Config file not found.")); - *logFile << agent->getMessage() << std::endl; + if (!file_exist(agent->getPathConfigFile())) { + *logFile << __DATE__ + + std::string(" - ERROR: Config file not found.") << std::endl; return false; } else { - if (!parse_config_file(agent, conf, logFile)) { + if (!parse_config_file(agent, logFile)) { + *logFile << __DATE__ + + std::string(" - ERROR: parse config file.") << std::endl; return false; } } /* Read proxy file and declare only valid variables */ - - if (!file_exist(conf->getProxyConf())) { - agent->setMessage(__DATE__ + - std::string(" - WARNING: Proxy file not found.")); - *logFile << agent->getMessage() << std::endl; + if (!file_exist(agent->getConf()->getPathProxyFile())) { + *logFile << __DATE__ + + std::string(" - WARNING: Proxy file not found.") << std::endl; } else { - if (!parse_proxy_file(conf, proxy, logFile, agent)) { + if (!parse_proxy_file(agent , logFile)) { + *logFile << __DATE__ + + std::string(" - ERROR: parse proxy file.") << std::endl; return false; } } - if (conf->getWebService().size() == 0) { - agent->setMessage(__DATE__ + - std::string(" - ERROR: Web service url not configured.")); - *logFile << agent->getMessage() << std::endl; + if (agent->getConf()->getWebService().size() == 0) { + *logFile << __DATE__ + + std::string(" - ERROR: Web service url not configured.") + << std::endl; return false; } diff --git a/src/agent/curl.cpp b/src/agent/curl.cpp index ec1cba6e9388dc956f2f8791d719beba52846b39..c9fb80bc192a88e0ff5795f9bea370d306725181 100644 --- a/src/agent/curl.cpp +++ b/src/agent/curl.cpp @@ -57,14 +57,14 @@ bool Curl::downloadAgent(std::string url, std::fstream* logFile, return false; } - /* Create the agent tmp .run file into tmp_dir_agent */ #ifdef __unix__ - agent->setRunName("agent.run"); + agent->setInstallerName("agent.run"); #else - agent->setRunName("agent."); + agent->setInstallerName("agent.exe"); #endif - boost::filesystem::path file_installer(tmp_dir_agent / agent->getRunName()); + /* Create the agent tmp (.run) or (.exe) file into tmp_dir_agent */ + boost::filesystem::path file_installer(tmp_dir_agent / agent->getInstallerName()); // NOLINT [whitespace/line_length] #ifdef __unix__ FILE* fp = fopen(file_installer.c_str(), "wb"); diff --git a/src/agent/get.cpp b/src/agent/get.cpp index 6378992d25bce06a93571c8172fbf4cdbdf647a7..4de5c0bd2d3a6eba623846e3a98f7ca0b91be59c 100644 --- a/src/agent/get.cpp +++ b/src/agent/get.cpp @@ -30,109 +30,94 @@ * Case if needed, making download from agent zipfile. */ -int check_update() { - Agent agent; - Conf conf; - Proxy proxy; - Rotate rotate; +int check_update(Agent* agent, std::fstream* logFile) { std::string version; + Json::Reader reader; + Json::Value version_json; + Json::FastWriter fastWriter; + + /* Contact webservice server to get client version. */ + auto p = cpr::Get(cpr::Url{agent->getConf()->getWebService() + + "/api/agent/version"}, + cpr::Header{{"content-type", "application/json"}}); + + /* Check error with connection */ + if (p.status_code == 502) + *logFile << __DATE__ << "- ERROR: The proxy server received an" + << " invalid response from server." << std::endl; + + // p.text - response from api + reader.parse(p.text, version_json, false); + version = fastWriter.write(version_json["version"]); + + boost::regex re("(\")|(\n)"); + version = boost::regex_replace(version, re, ""); + + // Case if not connected before, try it with proxy + if (p.status_code != 200) { + // try send via proxy + auto p = cpr::Get(cpr::Url{agent->getConf()->getWebService() + "/api"}, + cpr::Proxies{{"http", agent->getProxy()->getHost()}}, + cpr::Parameters{{"agent", "version"}}, + cpr::Header{{"content-type", "application/json"}}); + + /* Check error with connection */ + if (p.status_code == 502) + *logFile << __DATE__ << " - ERROR: The proxy server received an" + << "invalid response from server." << std::endl; + + reader.parse(p.text, version_json, false); + version = fastWriter.write(version_json["version"]); + version = boost::regex_replace(version, re, ""); - /* Set agent prefix */ - agent.setPrefix(__FILE__, 3); - - /* Set conf's file */ - agent.setConfDir("/conf"); - agent.setConfigFile("/conf/datasid-conf.json"); - agent.setLogDir("/log/"); - create_directories(agent.getLogDir()); - agent.setLogFile("datasid-agent.log"); - - std::fstream logFile(agent.getLogFile().c_str(), std::fstream::app); - - /* run common.cpp to make parse from config and proxy file */ - if (common(&logFile, &agent, &conf, &proxy)) { - rotate_log_file(&agent); - logFile << __DATE__ << " - Agent started." << std::endl; - logFile << __DATE__ << " - Start of update process execution." - << std::endl; - /* If proxy is setted */ - - if (proxy.proxyFileIsSet()) { - auto p = cpr::Get(cpr::Url{conf.getWebService() + "/api/v1/get"}, - cpr::Proxies{{"http", proxy.getHost()}}, - cpr::Parameters{{"agent", "version"}}, - cpr::Header{{"content-type", "application/json"}}); - - /* Check error with connection */ - if (p.status_code == 502) { - agent.setMessage(__DATE__ + - std::string(" - ERROR: The proxy server received an") + - std::string(" invalid response from server.")); - logFile << agent.getMessage() << std::endl; - } - - version = p.header["version"]; - - } else { - /* Contact webservice server to get client version. */ - auto p = cpr::Get(cpr::Url{conf.getWebService() + "/api/v1/get"}, - cpr::Parameters{{"agent", "version"}}, - cpr::Header{{"content-type", "application/json"}}); - - /* Check error with connection */ - if (p.status_code == 502) { - agent.setMessage(__DATE__ + - std::string(" - ERROR: The proxy server received an") + - std::string(" invalid response from server.")); - logFile << agent.getMessage() << std::endl; - } + } - version = p.header["version"]; - } + // Case if none of them get rigth, set log file + if (p.status_code != 200) { + *logFile << __DATE__ << " - ERROR: Try to connect with server" + << std::endl; + return false; + } - if (conf.getVersion() == "0.0.0") { - // open version.json file and update to current version - Json::Value config; - Json::Reader reader; - std::string path = agent.getConfigFile(); - std::ifstream version_file(path.c_str(), std::ofstream::binary); - if (version_file.is_open()) { - reader.parse(version_file, config , false); - version_file.close(); - config["version"] = version; - std::string idPoint = config["idPoint"].asString(); - std::string proxyConf = config["proxyConf"].asString(); - std::string webService = config["webService"].asString(); - std::ofstream file(agent.getConfigFile(), - std::ios::trunc); - if (file.is_open()) { - file << config; - file.close(); - } + if (agent->getConf()->getVersion() == "0.0.0") { + // open version.json file and update to current version + Json::Value config; + Json::Reader reader; + std::string path = agent->getPathConfigFile(); + std::ifstream version_file(path.c_str(), std::ofstream::binary); + if (version_file.is_open()) { + reader.parse(version_file, config , false); + version_file.close(); + config["version"] = version; + std::string idPoint = config["idPoint"].asString(); + std::string proxyConf = config["proxyConf"].asString(); + std::string webService = config["webService"].asString(); + std::ofstream file(agent->getPathConfigFile(), + std::ios::trunc); + if (file.is_open()) { + file << config; + file.close(); } } + } - /* Case version are different, then make the download */ - if (compare_version(&agent, &conf, version, &logFile)) { - /* Case not download, return false */ - if (download(conf, &agent, &logFile)) { - agent.setMessage(__DATE__ + - std::string(" - SUCCESS: Update process terminated ") + - std::string("successfully.")); - logFile << agent.getMessage() << std::endl; - } else { - agent.setMessage(__DATE__ + - std::string(" - ERROR: Failed to update agent.")); - logFile << agent.getMessage() << std::endl; + /* Case version are different, then make the download */ + if (compare_version(agent, version, logFile)) { + /* Case not download, return false */ + std::string filename; + if (get_agent_installer_name(agent->getConf(), &filename)) { + if (download(agent, logFile, filename)) { + *logFile << __DATE__ << " - SUCCESS: Update process " << + "terminated successfully." << std::endl; + logFile->close(); + return true; } - } else { - agent.setMessage(__DATE__ + - std::string(" - ERROR: Failed to update agent.")); - logFile << agent.getMessage() << std::endl; } - logFile.close(); + *logFile << __DATE__ << " - ERROR: Failed to update agent." + << std::endl; } - return(0); + logFile->close(); + return false; } /** @@ -141,23 +126,48 @@ int check_update() { * <br> * Otherwise, return false. */ -bool compare_version(Agent *agent, Conf *conf, std::string version, +bool compare_version(Agent *agent, std::string version, std::fstream* logFile) { if (!strcmp(version.c_str(), "")) { - agent->setMessage(__DATE__ + - std::string(" - ERROR: Server returned an empty version string.")); - *logFile << agent->getMessage() << std::endl; + *logFile << __DATE__ + std::string(" - ERROR: Server returned an") + + std::string(" empty version string.") << std::endl; + return false; + } else if (strcmp(version.c_str(), agent->getConf()->getVersion().c_str())) { // NOLINT [whitespace/line_length] + *logFile << __DATE__ + + std::string(" - WARNING: Client version out-of-date.") + + std::string(" Update client.") << std::endl; + return true; + } else { + *logFile << __DATE__ + + std::string(" - WARNING: Client is already updated.")<< std::endl; return false; - } else if (strcmp(version.c_str(), conf->getVersion().c_str())) { - agent->setMessage(__DATE__ + - std::string(" - WARNING: Client version out-of-date.") + - std::string(" Update client.")); - *logFile << agent->getMessage() << std::endl; + } +} + +/* Contact webservice server to request a agent installer. */ +bool get_agent_installer_name(Conf* conf, std::string* filename) { + Json::FastWriter fastWriter; + Json::Value filename_json; + Json::Reader reader; + + #ifdef __linux__ + std::string os = "linux"; + #else + std::string os = "windows"; + #endif + + auto p = cpr::Get(cpr::Url{conf->getWebService() + "/api/agent/install/" + os + "/" + std::to_string(2)}, // NOLINT [whitespace/line_length] + cpr::Header{{"content-type", "application/json"}}); + + if (p.status_code == 200) { + // p.text - response from api + reader.parse(p.text, filename_json, false); + *filename = fastWriter.write(filename_json["filename"]); + + boost::regex re("(\")|(\n)"); + *filename = boost::regex_replace(*filename, re, ""); return true; } else { - agent->setMessage(__DATE__ + - std::string(" - WARNING: Client is already updated.")); - *logFile << agent->getMessage() << std::endl; return false; } } @@ -166,10 +176,12 @@ bool compare_version(Agent *agent, Conf *conf, std::string version, * To make agent download. * Curl library is used to make a webService request to get agent zipfile. */ -bool download(Conf conf, Agent* agent, std::fstream* logFile) { +bool download(Agent* agent, std::fstream* logFile, std::string filename) { Curl curl; - if (curl.downloadAgent(conf.getWebService(), - logFile, agent)) + std::string web_service = agent->getConf()->getWebService() + + std::string("/install/") + + std::string(filename); + if (curl.downloadAgent(web_service, logFile, agent)) return true; return false; } @@ -187,15 +199,18 @@ bool create_directories(std::string dir) { } /** - * Giving execute permission to agent-0.run and run it. + * Giving execute permission to agent.run and run it. */ void execute_agent(boost::filesystem::path tmp) { #ifdef __linux__ // Execute permission to agent-0.run std::string chmod = "chmod 777 " + std::string(tmp.c_str()); - system(chmod.c_str()); - // Run agent-0.run - system(tmp.c_str()); + try { + system(chmod.c_str()); + exec(tmp.c_str()); + } catch (std::string err) { + // something + } #endif } diff --git a/src/agent/get_disks_info.cpp b/src/agent/get_disks_info.cpp index 084580fd201700dafacfcfdf99c823b192d9168e..cbcd4ae89357c8da58f5c2418b97410845152e41 100644 --- a/src/agent/get_disks_info.cpp +++ b/src/agent/get_disks_info.cpp @@ -101,10 +101,15 @@ void get_scsi_disks(std::list<disk_t>& disks) { // NOLINT(runtime/references) udev_unref(udev); } -std::string get_disk_used() { +int get_disk_used() { try { std::string cmd = exec("df -h . | awk '{print $3}' | sed 1d"); - return cmd.substr(0, cmd.find_last_of("G")); + cmd = cmd.substr(0, cmd.find_last_of("G")); + + // replace comma to dot + boost::regex re("(,)"); + cmd = boost::regex_replace(cmd, re, "."); + return round(std::stod(cmd)); } catch (std::string err) { throw std::string("Failed to get disk used space from disk1"); } @@ -133,7 +138,7 @@ void get_scsi_disks(std::list<disk_t>& disks) { // NOLINT(runtime/references) } } -std::string get_disk_used() { +int get_disk_used() { try { std::string free_space, total_size, free_output, total_output; std::size_t found; @@ -159,7 +164,10 @@ std::string get_disk_used() { long long res_free = free / (1024*1024*1024); // NOLINT(runtime/int) long long disk_used = res_total - res_free; // NOLINT(runtime/int) - return std::to_string(disk_used); + // replace comma to dot + boost::regex re("(,)"); + total_output = boost::regex_replace(std::to_string(disk_used), re, "."); + return round(std::stod(total_output)); } catch (std::string err) { throw std::string("Failed to get disk used space from disk1"); } diff --git a/src/agent/get_machine_type.cpp b/src/agent/get_machine_type.cpp index fc74e7de344bfbde2af273f05afde44722d2841f..ecdd9534092e1947849f78d4f3dff3fb77c9b0ac 100644 --- a/src/agent/get_machine_type.cpp +++ b/src/agent/get_machine_type.cpp @@ -73,7 +73,6 @@ std::string get_machine_type() { } freeaddrinfo(info); - return "client"; #endif } diff --git a/src/agent/inventory.cpp b/src/agent/inventory.cpp index 0ea3a7dcca7ec2d99e5e3a4ca42bb8558d7f75b5..6c57660ab412918f56ac70118b7e241c3baa0d67 100644 --- a/src/agent/inventory.cpp +++ b/src/agent/inventory.cpp @@ -52,6 +52,7 @@ Json::Value get_inventory() { uname(&sysinfo); #endif + // change path (current) agent_conf.open(path_to_agent_conf + "datasid-conf.json", std::ifstream::binary); reader.parse(agent_conf, conf , false); diff --git a/src/agent/main.cpp b/src/agent/main.cpp index 91df0f04184f3c86afa471e816bbfca2627d1135..30b8da6a173928500ec7db8eb2717851eefde9b6 100644 --- a/src/agent/main.cpp +++ b/src/agent/main.cpp @@ -22,40 +22,26 @@ #include <string> #include <iostream> -#ifdef __linux__ - std::string path_to_agent_tmp = // NOLINT(runtime/string) - "/opt/agentC3SL/tmp/"; // NOLINT(runtime/string) - std::string path_to_agent = "/opt/"; // NOLINT(runtime/string) - std::string path_to_agent_conf = // NOLINT(runtime/string) - "/opt/agentC3SL/conf/"; // NOLINT(runtime/string) -#else - // Temporary... Define agent path - std::string path_to_agent_tmp = "C:\\agentC3SL\\tmp\\"; - std::string path_to_agent = "C:\\agentC3SL\\"; - std::string path_to_agent_conf = // NOLINT(runtime/string) - "C:\\agentC3SL\\conf\\"; // NOLINT(runtime/string) -#endif - -bool agent_send_inventory = false; -bool agent_send_net_band = false; -bool agent_send_user_hist = false; -bool agent_send_database = false; - /** Run agent */ -int run_agent(const sys::error_code& /*e*/, pt::ptime begin, bool print ) { +int run_agent(const sys::error_code& /*e*/, pt::ptime begin, + bool print, Agent* agent, std::fstream* logFile) { pt::ptime now; now = pt::second_clock::local_time(); pt::time_duration t3 = now - begin; + bool parse = read_config_files(agent, logFile); if (agent_send_net_band && (t3.minutes() % 5 == 0 )) { - send_net_bandwidth(print); + if (parse) + send_net_bandwidth(print, agent->getConf()->getWebService()); } if ((t3.hours() % 24 == 0) && (t3.minutes() %60 == 0 )) { - check_update(); - if (agent_send_inventory) { - send_inventory(print); - } - if (agent_send_user_hist) { - send_user_history(print); + // make parse from config files and check update + if (parse) { + if (agent_send_inventory) { + send_inventory(print, agent->getConf()->getWebService()); + } + if (agent_send_user_hist) { + send_user_history(print, agent->getConf()->getWebService()); + } } } return(0); @@ -158,12 +144,46 @@ int parse_command_line(int ac, char* av[], int *increment, int *lim, return(0); } +bool read_config_files(Agent* agent, std::fstream* logFile) { + /* Set conf's file */ + agent->setPathConfDir("/conf"); + agent->setPathConfigFile("/conf/datasid-conf.json"); + agent->setPathLogDir("/log"); + create_directories(agent->getPathLogDir()); + agent->setPathLogFile("datasid-agent.log"); + + /* run common.cpp to make parse from config and proxy file */ + if (common(logFile, agent)) { + rotate_log_file(agent); + *logFile << __DATE__ << " - Agent started." << std::endl; + *logFile << __DATE__ << " - Start of update process execution." + << std::endl; + + check_update(agent, logFile); + return (true); + } + + return (false); +} + /** Run agent only one time per day. * - Check case if needed to update agent * - Send inventory to web service * - Send network bandwidth to web service */ int main(int argc, char* argv[]) { + Agent agent; + Conf conf; + Proxy proxy; + Rotate rotate; + agent.setConf(&conf); + agent.setProxy(&proxy); + + /* Set agent prefix (agent folder) */ + agent.setPrefix(__FILE__, 3); + agent.setPathLogFile("/log/datasid-agent.log"); + std::fstream logFile(agent.getPathLogFile().c_str(), std::fstream::app); + int increment = 0; int lim = 1; bool print; @@ -175,18 +195,24 @@ int main(int argc, char* argv[]) { config_agent(); set_begin(begin); - check_update(); - if (agent_send_inventory) { - send_inventory(print); - } - if (agent_send_net_band) { - send_net_bandwidth(print); - } - if (agent_send_user_hist) { - send_user_history(print); - } - if (agent_send_database) { - // get database for PInSIS + /* make parse from config files. Case if ok, then send + * web service base url to send inventory, net bandwidth + * and user history + */ + if (read_config_files(&agent, &logFile)) { + + if (agent_send_inventory) { + send_inventory(print, agent.getConf()->getWebService()); + } + if (agent_send_net_band) { + send_net_bandwidth(print, agent.getConf()->getWebService()); + } + if (agent_send_user_hist) { + send_user_history(print, agent.getConf()->getWebService()); + } + if (agent_send_database) { + // get database for PInSIS + } } if (lim == 0) { @@ -197,7 +223,7 @@ int main(int argc, char* argv[]) { while (i < lim) { asio::io_service io; asio::deadline_timer t(io, pt::seconds(60)); - t.async_wait(boost::bind(run_agent, pholders::error, begin, print)); + t.async_wait(boost::bind(run_agent, pholders::error, begin, print, &agent, &logFile)); // NOLINT [whitespace/line_length] io.run(); i += increment; } diff --git a/src/agent/network_bandwidth.cpp b/src/agent/network_bandwidth.cpp index 2aca3ce0ce75d55d70d58c44eb43571752722e86..f95a6f1bfb74820eecc236ae134c00420c8bf558 100644 --- a/src/agent/network_bandwidth.cpp +++ b/src/agent/network_bandwidth.cpp @@ -92,7 +92,7 @@ Json::Value get_net_bandwidth() { b_tr_allt = net_band_all_time["data_net"]["bytes_transmitted"].asLargestUInt(); pack_tr_allt = - net_band_all_time["data_net"]["packets_tranmitted"].asLargestUInt(); + net_band_all_time["data_net"]["packets_transmitted"].asLargestUInt(); } @@ -176,7 +176,7 @@ Json::Value get_net_bandwidth() { net_band_all_time["data_net"]["bytes_received"] = b_rec; net_band_all_time["data_net"]["packets_received"] = pack_rec; net_band_all_time["data_net"]["bytes_transmitted"] = b_tr; - net_band_all_time["data_net"]["packets_tranmitted"] = pack_tr; + net_band_all_time["data_net"]["packets_transmitted"] = pack_tr; ofs << net_band_all_time; ofs.close(); @@ -186,9 +186,6 @@ Json::Value get_net_bandwidth() { time = pt::second_clock::local_time(); time_file << time; time_file.close(); - time_file.open(path_to_agent_tmp + "time_now.txt", std::ios::in); - getline(time_file, str); - time_file.close(); // get id_point and mac_address net_band["data_net"]["mac_address"] = get_macaddr(); @@ -201,8 +198,9 @@ Json::Value get_net_bandwidth() { net_band["data_net"]["bytes_received"] = b_rec - b_rec_allt; net_band["data_net"]["packets_received"] = pack_rec - pack_rec_allt; net_band["data_net"]["bytes_transmitted"] = b_tr - b_tr_allt; - net_band["data_net"]["packets_tranmitted"] = pack_tr - pack_tr_allt; - net_band["data_net"]["collect_time"] = str; + net_band["data_net"]["packets_transmitted"] = pack_tr - pack_tr_allt; + net_band["data_net"]["collect_time"] = get_time(); + net_band["data_net"]["collect_date"] = get_date(); return (net_band); diff --git a/src/agent/parse_config_file.cpp b/src/agent/parse_config_file.cpp index 019b8750e6bc51f6f2686b42f308a76bffda06d0..eb10617aff6172fa78320beb91924c8a80b05c82 100644 --- a/src/agent/parse_config_file.cpp +++ b/src/agent/parse_config_file.cpp @@ -34,8 +34,8 @@ void Conf::setIdPoint(int i) { } /** Set absolute path from proxyfile */ -void Conf::setProxyConf(std::string p) { - this->proxyConf = p; +void Conf::setPathProxyFile(std::string p) { + this->pathProxyFile = p; } /** Set web service url */ @@ -54,14 +54,15 @@ void Conf::setProject(std::string p) { this->project = p; } + /** Return id Point */ int Conf::getIdPoint() const { return this->idPoint; } /** Return absolute path from proxyfile */ -std::string Conf::getProxyConf() const { - return this->proxyConf; +std::string Conf::getPathProxyFile() const { + return this->pathProxyFile; } /** Return url from web service */ @@ -82,8 +83,8 @@ std::string Conf::getProject() const { /** * After parse the config file, get the values from map variable and set at conf object */ -bool set_conf_object(Conf* conf, Agent* agent, std::map<std::string, - std::string>* content) { +bool set_conf_object(Agent* agent, std::map<std::string, + std::string>* content) { if (content->count("PROXYCONF") == 0 || content->count("WEBSERVICE") == 0|| content->count("IDPOINT") == 0 || @@ -94,31 +95,30 @@ bool set_conf_object(Conf* conf, Agent* agent, std::map<std::string, /* * agent.getPrefix() contains the absolute path from agent */ - std::string proxyConf = agent->getPrefix() + std::string("/") + + std::string pathProxyFile = agent->getPrefix() + std::string("/") + (*content)["PROXYCONF"]; std::string webService = (*content)["WEBSERVICE"]; std::string version = (*content)["VERSION"]; std::string project = (*content)["PROJECT"]; int idPoint = atoi((*content)["IDPOINT"].c_str()); - conf->setProxyConf(proxyConf); - conf->setWebService(webService); - conf->setIdPoint(idPoint); - conf->setProject(project); - conf->setVersion(version); + agent->getConf()->setPathProxyFile(pathProxyFile); + agent->getConf()->setWebService(webService); + agent->getConf()->setIdPoint(idPoint); + agent->getConf()->setProject(project); + agent->getConf()->setVersion(version); return true; } /** Read config file and make the parse to conf object */ -bool parse_config_file(Agent* agent, Conf* conf, std::fstream* logFile) { +bool parse_config_file(Agent* agent, std::fstream* logFile) { std::map<std::string, std::string> content; - if (read_file(agent->getConfigFile(), &content)) { - if (!set_conf_object(conf, agent, &content)) { - agent->setMessage(__DATE__ + - std::string(" - ERROR: to parse variables from config file.")); - *logFile << agent->getMessage() << std::endl; + if (read_file(agent->getPathConfigFile(), &content)) { + if (!set_conf_object(agent, &content)) { + *logFile << __DATE__ + std::string(" - ERROR: to parse") + + std::string(" variables from config file.") << std::endl; return false; } } diff --git a/src/agent/parse_proxy_file.cpp b/src/agent/parse_proxy_file.cpp index 9e0ffa1cb0664ae035c240a6f9f10c5f7a68ccdc..e9c48e4d422de3e2dc7f452eb5d35e37d3051a5a 100644 --- a/src/agent/parse_proxy_file.cpp +++ b/src/agent/parse_proxy_file.cpp @@ -76,7 +76,7 @@ bool Proxy::proxyFileIsSet() const { /** Get map variable and set the values at proxy object * After parse the proxy file, get the values from map variable and set at proxy object */ -bool set_proxy_object(Proxy* proxy, std::map<std::string, +bool set_proxy_object(Agent* agent, std::map<std::string, std::string>* content) { if (content->count("HOST") == 0 || content->count("PORT") == 0 || @@ -84,21 +84,20 @@ bool set_proxy_object(Proxy* proxy, std::map<std::string, content->count("PASSWORD") == 0) return false; - proxy->setHost((*content)["HOST"]); - proxy->setPort((*content)["PORT"]); - proxy->setUser((*content)["USR"]); - proxy->setPassword((*content)["PASSWORD"]); + agent->getProxy()->setHost((*content)["HOST"]); + agent->getProxy()->setPort((*content)["PORT"]); + agent->getProxy()->setUser((*content)["USR"]); + agent->getProxy()->setPassword((*content)["PASSWORD"]); return true; } /** Read proxy file and make the parse to proxy object */ -bool parse_proxy_file(Conf* conf, Proxy* proxy, std::fstream* logFile, - Agent* agent) { +bool parse_proxy_file(Agent* agent, std::fstream* logFile) { std::map<std::string, std::string> content; - if (read_file(conf->getProxyConf(), &content)) { - if (!set_proxy_object(proxy, &content)) { + if (read_file(agent->getConf()->getPathProxyFile(), &content)) { + if (!set_proxy_object(agent, &content)) { agent->setMessage(__DATE__ + std::string(" - ERROR: to parse variables from proxy file.")); *logFile << agent->getMessage() << std::endl; @@ -114,13 +113,13 @@ bool parse_proxy_file(Conf* conf, Proxy* proxy, std::fstream* logFile, std::string find = ""; #endif - std::cout << "'" << find << "'" << std::endl; - - if (proxy->getHost() == find && proxy->getPort() == find - && proxy->getUser() == find && proxy->getPassword() == find) - proxy->setProxyFile(false); + if (agent->getProxy()->getHost() == find && + agent->getProxy()->getPort() == find + && agent->getProxy()->getUser() == find && + agent->getProxy()->getPassword() == find) + agent->getProxy()->setProxyFile(false); else - proxy->setProxyFile(true); + agent->getProxy()->setProxyFile(true); return true; } diff --git a/src/agent/post.cpp b/src/agent/post.cpp index 4143327d64993700d82362c22eda148d744ac574..c5ff982e2d1a5bcfe95c020a5bea862b9ad3ad24 100644 --- a/src/agent/post.cpp +++ b/src/agent/post.cpp @@ -29,7 +29,7 @@ * - Send inventory json object; * - Cpr wrapper library - to make request to webService; */ -int send_inventory(bool print) { +int send_inventory(bool print, std::string base_url) { Json::Value inventory = get_inventory(); Json::FastWriter fastWriter; @@ -41,14 +41,12 @@ int send_inventory(bool print) { std::cout << "Empty inventory object" << std::endl; } - auto p = cpr::Post(cpr::Url{base_url + "/api/v1/collect"}, - cpr::Parameters{{"type", "inventory"}}, + auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/inventory"}, cpr::Body{fastWriter.write(inventory)}, cpr::Header{{"content-type", "application/json"}}); - if (p.status_code == 200) { + if (p.status_code == 200) return 1; - } return 0; } @@ -61,7 +59,7 @@ int send_inventory(bool print) { * - Send net json object; * - Cpr wrapper library - to make request to webService; */ -int send_net_bandwidth(bool print) { +int send_net_bandwidth(bool print, std::string base_url) { Json::Value net = get_net_bandwidth(); Json::FastWriter fastWriter; @@ -73,14 +71,12 @@ int send_net_bandwidth(bool print) { std::cout << "Empty network_bandwidth object" << std::endl; } - auto p = cpr::Post(cpr::Url{base_url + "/api/v1/collect"}, - cpr::Parameters{{"type", "network_bandwidth"}}, + auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/net_usage"}, cpr::Body{fastWriter.write(net)}, cpr::Header{{"content-type", "application/json"}}); - if (p.status_code == 200) { + if (p.status_code == 200) return 1; - } return 0; } @@ -93,7 +89,7 @@ int send_net_bandwidth(bool print) { * - Send u_h json object; * - Cpr wrapper library - to make request to webService; */ -int send_user_history(bool print) { +int send_user_history(bool print, std::string base_url) { Json::Value u_h; Json::FastWriter fastWriter; @@ -105,7 +101,7 @@ int send_user_history(bool print) { std::cout << "Empty user_history object" << std::endl; } - auto p = cpr::Post(cpr::Url{base_url + "/api/v1/collect"}, + auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/user_history"}, cpr::Parameters{{"type", "user_history"}}, cpr::Body{fastWriter.write(u_h)}, cpr::Header{{"content-type", "application/json"}}); diff --git a/src/agent/rotate_log_file.cpp b/src/agent/rotate_log_file.cpp index dde789a4c1a04bdf7605f0c0a2b00b77cbfafa27..aa0b7805d8a332dd576166042bf398d01c84e9b8 100644 --- a/src/agent/rotate_log_file.cpp +++ b/src/agent/rotate_log_file.cpp @@ -36,7 +36,7 @@ void Rotate::setNumberOfFiles() { /** Get the size of a logfile and set the attribute sizeOfLogfile */ void Rotate::setSizeOfLogfile(Agent* agent) { - std::fstream file((agent->getLogFile()).c_str(), std::fstream::app); + std::fstream file((agent->getPathLogFile()).c_str(), std::fstream::app); this->sizeOfLogfile = file.tellg(); // NOLINT [runtime/int] file.close(); } @@ -119,7 +119,7 @@ void Rotate::renameFile(Agent* agent) { */ void rotate_log_file(Agent* agent) { Rotate rotate; - rotate.setLogfileName(agent->getLogFile()); + rotate.setLogfileName(agent->getPathLogFile()); rotate.setSizeOfLogfile(agent); rotate.setLogDir(agent->getPrefix() + std::string("/log")); rotate.setNumberOfFiles();