diff --git a/generate_agent.sh b/generate_agent.sh index bce1f96f1dad9dbce74bb486d198626ce5690a39..ff84715e597b2a8a25a729edbbe0420a8234a907 100755 --- a/generate_agent.sh +++ b/generate_agent.sh @@ -101,7 +101,7 @@ tmp=$(cat ../../../conf/proxy-conf.json) printf "$tmp\n" $host $port $usr $passwd > proxy.json tmp=$(cat ../../../conf/datasid-conf.json) -printf "$tmp\n" $idPoint $project /opt/agentC3SL/conf/proxy-conf.json $version $webService > datasid-conf.json +printf "$tmp\n" $idPoint $project conf/proxy-conf.json $version $webService > datasid-conf.json cd ../../.. diff --git a/include/agent/agent.h b/include/agent/agent.h index e370ba638608476d3c5becadc5f8b148a6709e79..462bb235cae9fa0997aecdeca30260a782c63919 100644 --- a/include/agent/agent.h +++ b/include/agent/agent.h @@ -14,14 +14,16 @@ * @brief This file has the Agent class and in it is saved info about the current agent, * like absolute path, current versio and so on. * The Agent class has 8 attributes: - * - confDir - Absolute path from conf directory (/opt/agentc3sl/conf) - * - configFileName - Absolute path from config file (/opt/agentc3sl/conf/datasid-conf.json) - * - dirInstall - Absolute path from local to install agent (/opt/agentc3sl) - * - logFile - Absolute path from log file (/opt/agentc3sl/log/datasid-agent.log) - * - 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 - * - runName Name of agent .run file + * + * - pathConfDir - Absolute path from conf directory (/opt/agentc3sl/conf) + * - pathConfigFile - Absolute path from config file (/opt/agentc3sl/conf/datasid-conf.json) + * - pathDirInstall - Absolute path from local to install agent (/opt/agentc3sl) + * - pathLogDir - Absolute path from log file (/opt/agentc3sl/log/datasid-agent.log) + * - prefix - Absolute path from agent - get current local (path) of agent + * - installerName Name of (agent.run) or (agent.exe) file + * - *connection Pointer from Connection object (make get or post request with or without proxy) + * - *conf Pointer from Conf object + * - *proxy Pointer from Proxy object * <br><br> */ @@ -38,9 +40,9 @@ class Agent{ std::string pathLogFile; /*!< Absolute path from log file */ std::string prefix; /*!< Absolute path from agent */ std::string installerName; /*!< Name of agent installer file */ - Connection* connection; - Conf* conf; - Proxy* proxy; + Connection* connection; /*!< make get or post request with or without proxy */ + Conf* conf; /*!< agent config */ + Proxy* proxy; /*!< proxy config */ public: // We can't return fstream type diff --git a/include/agent/connection.h b/include/agent/connection.h index bd1953cb5689d4046a75a758639c1169bd3e9347..b127f3155d08e192da63c552c354f15749c80bae 100644 --- a/include/agent/connection.h +++ b/include/agent/connection.h @@ -11,10 +11,10 @@ #include <json/json.h> /** - * @file curl.h - * @brief This file has Curl class and the purpose is to make a get request to web service and download the agent zip file + * @file connection.h + * @brief This file has Curl class and the purpose is to make a get or post request to web service and download the agent zip file * - Obs - * - Using libcurl to make a get request (curl/curl.h) + * - Using libcurl to make a get or post request (curl/curl.h) */ class Agent; // forward declarations @@ -33,7 +33,7 @@ class Connection{ int sendJsonByProxy(Agent*, std::string, int); /** Default callback function that is necessary to make a curl request. - * @brief Obs: + * @brief get agent binary (agent.run or agent.exe) * - Parameters from this method is define by curl lib (curl/curl.h); */ static size_t callBackDownload(void *ptr, size_t size, size_t nmemb,void* userdata) { @@ -46,6 +46,10 @@ class Connection{ return written; } + /** Default callback function that is necessary to make a curl request. + * @brief get version agent or filename installer + * - Parameters from this method is define by curl lib (curl/curl.h); + */ static size_t callback(void *contents, size_t size, size_t nmemb, void *userp) { ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; diff --git a/include/agent/get.h b/include/agent/get.h index 4c87e8a5d549d816c814a0c68e449bd100d8cfd4..b7d5ffc088487094db983485dede9d98eb053549 100644 --- a/include/agent/get.h +++ b/include/agent/get.h @@ -25,5 +25,5 @@ int check_update(Agent*); bool create_directories(std::string); bool compare_version(Agent*, std::string); bool download(Agent*, std::string, int); -void execute_agent(boost::filesystem::path); +void execute_agent(Agent*, boost::filesystem::path); bool get_agent_installer_name(Agent*, std::string*, int); diff --git a/src/agent/agent.cpp b/src/agent/agent.cpp index 4028ab0a2369657a5aa6f132a60b9816d28f02f3..e10b5bcf2e86265a697be1ad5f8c50ba323dff36 100644 --- a/src/agent/agent.cpp +++ b/src/agent/agent.cpp @@ -26,7 +26,6 @@ void Agent::setPathConfDir(std::string dir) { this->pathConfDir = this->prefix + dir; } - /** Set the absolute path of config filename */ void Agent::setPathConfigFile(std::string filename) { this->pathConfigFile = this->prefix + filename; @@ -65,14 +64,17 @@ void Agent::setInstallerName(std::string i) { this->installerName = i; } +/** Set connection object */ void Agent::setConnection(Connection* c) { this->connection = c; } +/** Set conf object */ void Agent::setConf(Conf* conf) { this->conf = conf; } +/** Set proxy object */ void Agent::setProxy(Proxy* proxy) { this->proxy = proxy; } @@ -109,14 +111,17 @@ std::string Agent::getInstallerName() const { return this->installerName; } +/** Return connection object pointer */ Connection* Agent::getConnection() const { return this->connection; } +/** Return conf object pointer */ Conf* Agent::getConf() const { return this->conf; } +/** Return proxy object pointer */ Proxy* Agent::getProxy() const { return this->proxy; } @@ -132,13 +137,16 @@ void Agent::prefixISet() { * @brief This file has the implementation from Agent's method and in it is saved info about the current agent, * like absolute path, current version and so on. * The Agent class has 8 attributes: - * - confDir - Absolute path from conf directory (/opt/agentc3sl/conf) - * - configFileName - Absolute path from config file (/opt/agentc3sl/conf/datasid-conf.json) - * - dirInstall - Absolute path from local to install agent (/opt/agentc3sl) - * - logFile - Absolute path from log file (/opt/agentc3sl/log/datasid-agent.log) - * - 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 + * + * - pathConfDir - Absolute path from conf directory (/opt/agentc3sl/conf) + * - pathConfigFile - Absolute path from config file (/opt/agentc3sl/conf/datasid-conf.json) + * - pathDirInstall - Absolute path from local to install agent (/opt/agentc3sl) + * - pathLogDir - Absolute path from log file (/opt/agentc3sl/log/datasid-agent.log) + * - prefix - Absolute path from agent - get current local (path) of agent + * - version - Current agent version * - installerName Name of agent installer + * - *connection Pointer from Connection object (make get or post request with or without proxy) + * - *conf Pointer from Conf object + * - *proxy Pointer from Proxy object * <br><br> */ diff --git a/src/agent/connection.cpp b/src/agent/connection.cpp index 4b458cab3828c08e29b85dd5fc17c6962d3b01b7..89dab77f46318219ab4f365129ae979388949133 100644 --- a/src/agent/connection.cpp +++ b/src/agent/connection.cpp @@ -27,10 +27,9 @@ * - Not downloading file from https protocol; * - Is a get request. * -* @param url references to webService -* @param agent object agent -* @param data json object to save success, error and warning messages -* @param logFile references from logfile to save success, error and warning messages +* @param url references to webService +* @param agent object agent +* @param option use proxy or not (option == 0 - use proxy) */ bool Connection::downloadAgent(std::string url, Agent* agent, int option) { curl = curl_easy_init(); @@ -121,7 +120,7 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) { fclose(fp); // run agent.run on linux - execute_agent(file_installer); + execute_agent(agent, file_installer); // remove agent tmp directory boost::uintmax_t res = boost::filesystem::remove_all(tmp_dir_agent); @@ -139,6 +138,14 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) { return true; } +/** Set curl default options +* @brief Obs: +* - Is a get request. +* +* @param url references to webService +* @param agent object agent +* @param version pointer variable thats receive version or filename from web service. +*/ void Connection::setCurlOption(std::string url, Agent* agent, std::string* out) { std::string::size_type sz; // NOLINT [runtime/int] @@ -167,6 +174,14 @@ void Connection::setCurlOption(std::string url, Agent* agent, curl_easy_cleanup(curl); } +/** Contact webservice server using proxy to get version. +* @brief Obs: +* - Is a get request. +* +* @param url references to webService +* @param agent object agent +* @param version pointer variable thats receive version from web service; +*/ bool Connection::getVersionByProxy(Agent* agent, std::string *version, std::string url) { struct curl_slist *headers = NULL; @@ -185,6 +200,14 @@ bool Connection::getVersionByProxy(Agent* agent, std::string *version, return false; } +/** Contact webservice server using proxy to get installer name. +* @brief Obs: +* - Is a get request. +* +* @param url references to webService +* @param agent object agent +* @param version pointer variable thats receive file name from web service; +*/ int Connection::getInstallerNameByProxy(std::string url, Agent* agent, std::string* filename) { curl = curl_easy_init(); @@ -192,6 +215,14 @@ int Connection::getInstallerNameByProxy(std::string url, Agent* agent, return httpCode; } +/** Contact webservice server using proxy to send json (inventory, net_usage or user_history) +* @brief Obs: +* - Is a post request. +* +* @param agent object agent +* @param json references to json +* @param opt opt == 0 (send inventory) | opt == 2 (send net bandwidth) | otherwise (send user history) +*/ int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) { std::string::size_type sz; // NOLINT [runtime/int] long port; // NOLINT [runtime/int] @@ -238,15 +269,15 @@ int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) { curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); curl_easy_cleanup(curl); - return httpCode; + return this->httpCode; } /** - * @file curl.cpp - * @brief This file has implementation from Curl's method and the purpose is to make a get request to web + * @file connection.cpp + * @brief This file has implementation from Curl's method and the purpose is to make a get or post request to web * service and download the agent zip file * - Obs - * - Using libcurl to make a get request (curl/curl.h) + * - Using libcurl to make a get or post request (curl/curl.h) */ diff --git a/src/agent/get.cpp b/src/agent/get.cpp index 1f934ac0a2e185f975d1800ae62f7f32887ac2d3..b3047e3a043448c34575f659c4ad5c5a2fe82e5e 100644 --- a/src/agent/get.cpp +++ b/src/agent/get.cpp @@ -27,7 +27,7 @@ * * Get agent agent version from webService. Compare to cliente version; * <br> - * Case if needed, making download from agent zipfile. + * Case if needed, making download from agent (.run) or (.exe)). */ int check_update(Agent* agent) { @@ -199,19 +199,24 @@ bool create_directories(std::string dir) { } /** - * Giving execute permission to agent.run and run it. + * Giving execute permission to agent.run and run it or run agent.exe */ -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()); - try { +void execute_agent(Agent* agent, boost::filesystem::path tmp) { + try { + #ifdef __linux__ + std::string chmod = "chmod 777 " + std::string(tmp.c_str()); system(chmod.c_str()); exec(tmp.c_str()); - } catch (std::string err) { - // something - } - #endif + #else + std::string cmd = tmp.string() + + std::string(" /SILENT /VERYSILENT"); + exec(cmd.c_str()); + #endif + } catch (std::string err) { + agent->logFile << __DATE__ + std::string(" - ERROR: Failed to ") + + std::string("execute (agent.run) or (agent.exe) update.") + << std::endl; + } } diff --git a/src/agent/get_disks_info.cpp b/src/agent/get_disks_info.cpp index cbcd4ae89357c8da58f5c2418b97410845152e41..d18d936adb5a70cb5edaea17513c90f467ccbf5e 100644 --- a/src/agent/get_disks_info.cpp +++ b/src/agent/get_disks_info.cpp @@ -101,6 +101,7 @@ void get_scsi_disks(std::list<disk_t>& disks) { // NOLINT(runtime/references) udev_unref(udev); } +/** Get the disk used on Linux machine */ int get_disk_used() { try { std::string cmd = exec("df -h . | awk '{print $3}' | sed 1d"); @@ -138,6 +139,7 @@ void get_scsi_disks(std::list<disk_t>& disks) { // NOLINT(runtime/references) } } +/** Get the disk used on Windows machine */ int get_disk_used() { try { std::string free_space, total_size, free_output, total_output; @@ -176,7 +178,7 @@ int get_disk_used() { #endif -/** Get disks info (Model and size) */ +/** Get disks info (Model, size and total disk used) */ void get_disks_info(std::list<disk_t>& disks) { // NOLINT(runtime/references) get_scsi_disks(disks); diff --git a/src/agent/get_distro.cpp b/src/agent/get_distro.cpp index 75c09d5ff8d269d5b0eea1d0cd773d277efd577d..479bdf73dc2b9d348efc50637ddfed050946f816 100644 --- a/src/agent/get_distro.cpp +++ b/src/agent/get_distro.cpp @@ -27,6 +27,7 @@ * - Debian * - Linux Comunicações * - Linux Educacional + * - Windows distro * - Get distro info also from * - Fedora info at /etc/os-release file * - OpenSUSE info at /etc/redhat-release file; diff --git a/src/agent/get_memory_size.cpp b/src/agent/get_memory_size.cpp index 2626c6150bfec21a3237a6426e70ff6b0dd0e8be..99a31cf69249f3cf86c9ccd0eef800bcb177e70e 100644 --- a/src/agent/get_memory_size.cpp +++ b/src/agent/get_memory_size.cpp @@ -20,7 +20,7 @@ */ #include <agent/get_memory_size.h> -/** Get memory size info from /proc/meminfo file +/** Get memory size. */ int get_memory_size() { std::string memsize; @@ -72,5 +72,5 @@ int get_memory_size() { /** * @file get_memory_size.cpp - * @brief Get memory size info from /proc/meminfo file + * @brief Get memory size info from /proc/meminfo file on linux machine. On windows, use wmic command. */ diff --git a/src/agent/get_processor_model.cpp b/src/agent/get_processor_model.cpp index cb8b050dee0770b77f108b5a9ee899b7ff8e4ffe..49f18950bdfc42578d75cdc167b8c189886e28f4 100644 --- a/src/agent/get_processor_model.cpp +++ b/src/agent/get_processor_model.cpp @@ -20,7 +20,7 @@ */ #include <agent/get_processor_model.h> -/** Get processor model info from /proc/cpuinfo file */ +/** Get processor model info */ std::string get_processor_model() { std::string line; #ifdef __unix__ @@ -55,5 +55,5 @@ std::string get_processor_model() { } /** @file get_processor_model.cpp - * @brief Case if linux OS, get processor model info from /proc/cpuinfo file + * @brief Case if linux OS, get processor model info from /proc/cpuinfo file. On windows, use wmic command. */ diff --git a/src/agent/main.cpp b/src/agent/main.cpp index bd26082b6cb66cdd389c7be8392777407eed2302..107e70f703f8ad3d383771a6460166ccf6f2f3ea 100644 --- a/src/agent/main.cpp +++ b/src/agent/main.cpp @@ -144,6 +144,11 @@ int parse_command_line(int ac, char* av[], int *increment, int *lim, return(0); } + +/** Read config file (datasid-conf.json and proxy-conf.json) and parse it to respective objects + * - Options: + * - *agent - pointer to agent object + */ bool read_config_files(Agent* agent) { /* Set conf's file */ agent->setPathConfDir("/conf"); diff --git a/src/agent/network_bandwidth.cpp b/src/agent/network_bandwidth.cpp index f95a6f1bfb74820eecc236ae134c00420c8bf558..681ba0d7acf727fa499279e4c8bd5ab68bbac05c 100644 --- a/src/agent/network_bandwidth.cpp +++ b/src/agent/network_bandwidth.cpp @@ -34,6 +34,9 @@ extern std::string path_to_agent_conf; * - Packets recived * - Bytes transmited * - Packets transmited + * - date collected + * - time collected + * - id Point * . * Linux has a file (/proc/net/dev) that contain info about network bandwidth * <br> diff --git a/src/agent/parse_config_file.cpp b/src/agent/parse_config_file.cpp index b0601b4574578fe15af4f98cb9ac1de8050062f0..394bcc63dab02e0398a120db8adb8e2ac2675d5d 100644 --- a/src/agent/parse_config_file.cpp +++ b/src/agent/parse_config_file.cpp @@ -54,7 +54,6 @@ void Conf::setProject(std::string p) { this->project = p; } - /** Return id Point */ int Conf::getIdPoint() const { return this->idPoint;