Commit f69e8726 authored by Diego Giovane Pasqualin's avatar Diego Giovane Pasqualin
Browse files

Merge branch 'issue/131' into 'development'

SCRUM#131 - Make client compatible with simmc web service

See merge request !96
parents 28fcf396 b6b28e1a
Pipeline #12084 passed with stages
in 22 minutes and 13 seconds
......@@ -4,4 +4,4 @@
"webService": "%s",
"version": "%s",
"project" : "%s"
}
}
\ No newline at end of file
#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
......@@ -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.
......
......@@ -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*);
......@@ -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
......@@ -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&);
......@@ -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>
......
......@@ -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>*);
#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>*);
......@@ -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);
......@@ -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)
......
......@@ -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>
*/
......@@ -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;
}
......
......@@ -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");
......
......@@ -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;
}