diff --git a/conf/datasid-conf.json b/conf/datasid-conf.json index 53fe43f6af284f318429ef66a7b67801ef45c27b..72b10693cac74af328fd71bc62627139966518e3 100644 --- a/conf/datasid-conf.json +++ b/conf/datasid-conf.json @@ -1,7 +1,7 @@ { - "idPoint" : "%d", - "proxyConf" : "%s", - "webService": "%s", - "version": "%s", - "project" : "%s" -} + "idPoint" : "22", + "project" : "simmc", + "proxyConf" : "conf/proxy-conf.json", + "version" : "1.0.0.0", + "webService" : "http://localhost:3000/" +} \ No newline at end of file diff --git a/include/agent/get.h b/include/agent/get.h index 02c3d0727f3ba744758446e6afe105db046e1875..f3971e4411da6c0941ea33b7957eb8ca6cf8a3fd 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> @@ -24,5 +24,6 @@ const std::string url = "http://localhost:3001"; int check_update(); bool create_directories(std::string); bool compare_version(Agent*, Conf*, std::string, std::fstream*); -bool download(Conf, Agent*, std::fstream*); +bool download(Conf, 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..2068fea20a756731759831265297645bc368479d 100644 --- a/include/agent/main.h +++ b/include/agent/main.h @@ -3,6 +3,11 @@ #include <fstream> #include <iostream> #include <string> +#include <agent/agent.h> +#include <agent/parse_config_file.h> +#include <agent/get.h> +#include <agent/post.h> +#include <agent/agent_uninstall.h> #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/date_time/posix_time/posix_time.hpp> @@ -10,9 +15,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 +23,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 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/post.h b/include/agent/post.h index 7c4648d0897f28a5b2372bd61322c5da89f7e591..3126c35b7bac0c4461ced132c7f82f0dac8cea89 100644 --- a/include/agent/post.h +++ b/include/agent/post.h @@ -6,7 +6,7 @@ #include <agent/user_history.h> #include <agent/network_bandwidth.h> -const std::string base_url = "http://localhost:3001"; +const std::string base_url = "http://localhost:3000"; /** * @file post.h diff --git a/src/agent/curl.cpp b/src/agent/curl.cpp index ec1cba6e9388dc956f2f8791d719beba52846b39..d1d1e0503f59abe0022bde8cc0fd841b093faa33 100644 --- a/src/agent/curl.cpp +++ b/src/agent/curl.cpp @@ -61,7 +61,7 @@ bool Curl::downloadAgent(std::string url, std::fstream* logFile, #ifdef __unix__ agent->setRunName("agent.run"); #else - agent->setRunName("agent."); + agent->setRunName("agent.exe"); #endif boost::filesystem::path file_installer(tmp_dir_agent / agent->getRunName()); @@ -116,6 +116,8 @@ bool Curl::downloadAgent(std::string url, std::fstream* logFile, *logFile << agent->getMessage() << std::endl; } + std::cout << "http code = " << httpCode << std::endl; + if (response) { agent->setMessage(__DATE__ + std::string(" - ERROR: Failed ") + std::string("to download the agent.")); diff --git a/src/agent/get.cpp b/src/agent/get.cpp index 6378992d25bce06a93571c8172fbf4cdbdf647a7..4ab2dfc6a91ef1cb2bfe865df59f24de09d9aaf5 100644 --- a/src/agent/get.cpp +++ b/src/agent/get.cpp @@ -36,6 +36,9 @@ int check_update() { Proxy proxy; Rotate rotate; std::string version; + Json::Reader reader; + Json::Value version_json; + Json::FastWriter fastWriter; /* Set agent prefix */ agent.setPrefix(__FILE__, 3); @@ -49,47 +52,54 @@ int check_update() { 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"}}, + /* Contact webservice server to get client version. */ + auto p = cpr::Get(cpr::Url{conf.getWebService() + "api/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; - } + /* 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"]); - version = p.header["version"]; + boost::regex re("(\")|(\n)"); + version = boost::regex_replace(version, re, ""); - } 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"}}); + // Case if not connected before, try it with proxy + if (p.status_code != 200) { + // try send via proxy + auto p = cpr::Get(cpr::Url{conf.getWebService() + "/api"}, + 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; - } + 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, ""); - 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; + if (conf.getVersion() == "0.0.0") { // open version.json file and update to current version Json::Value config; @@ -115,20 +125,18 @@ int check_update() { /* 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; + std::string filename; + if (get_agent_installer_name(conf, &filename)) { + if (download(conf, &agent, &logFile, filename)) { + logFile << __DATE__ << " - SUCCESS: Update process " << + "terminated successfully." << std::endl; + logFile.close(); + return (0); + } } - } else { - agent.setMessage(__DATE__ + - std::string(" - ERROR: Failed to update agent.")); - logFile << agent.getMessage() << std::endl; + + logFile << __DATE__ << " - ERROR: Failed to update agent." + << std::endl; } logFile.close(); } @@ -162,14 +170,48 @@ bool compare_version(Agent *agent, Conf *conf, std::string version, } } +/* 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, ""); + + std::cout << "response = " << p.text << std::endl; + return true; + } else { + return false; + } +} + /** * 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(Conf conf, Agent* agent, std::fstream* logFile, + std::string filename) { Curl curl; - if (curl.downloadAgent(conf.getWebService(), - logFile, agent)) + std::string web_service = conf.getWebService() + + std::string("install/") + + std::string(filename); + + std::cout << "webservice = " << web_service << std::endl; + if (curl.downloadAgent(web_service, logFile, agent)) return true; return false; } @@ -187,15 +229,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..0044603d33333ffb56f23bf998af3ca6c38b299f 100644 --- a/src/agent/get_disks_info.cpp +++ b/src/agent/get_disks_info.cpp @@ -101,10 +101,16 @@ 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, "."); + size_t sz; + return round(std::stod(cmd)); } 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..46295e261a478f870a6b4e193445a1e7845eff41 100644 --- a/src/agent/get_machine_type.cpp +++ b/src/agent/get_machine_type.cpp @@ -73,7 +73,7 @@ std::string get_machine_type() { } freeaddrinfo(info); - + std::cout << "chegou aqui \n"; 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..cae6c90750de5d731eacfc2ddcda16d3b4ae8d53 100644 --- a/src/agent/main.cpp +++ b/src/agent/main.cpp @@ -22,25 +22,6 @@ #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 ) { pt::ptime now; 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/post.cpp b/src/agent/post.cpp index 4143327d64993700d82362c22eda148d744ac574..3b5757d8d7d1362d1fcef9b2a2c54dbf59610a5a 100644 --- a/src/agent/post.cpp +++ b/src/agent/post.cpp @@ -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; } @@ -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; }