Commit 84e22771c6cb097714fca8e8621e6c950c47f991

Authored by Eric Menezes Noronha
2 parents ba316028 c87d0f3e
Exists in master

Merge branch 'master' of https://github.com/lightbase/cacic-agente

Conflicts:
	cacic-teste/testcacic.cpp
	cacic-teste/testcacic.h
	src/cacic_hardware.cpp
	src/cacic_hardware.h
	src/cacic_software.h
cacic-teste/cacic-teste.pro
... ... @@ -24,6 +24,7 @@ SOURCES += \
24 24 ../src/cacic_hardware.cpp \
25 25 ../src/operatingsystem.cpp \
26 26 ../src/cacic_software.cpp \
  27 + ../src/cacic_hardware.cpp \
27 28 # ../src/qtservice/src/qtservice.cpp \
28 29 # ../src/qtservice/src/qtservice_unix.cpp \
29 30 # ../src/qtservice/src/qtservice_win.cpp \
... ... @@ -40,10 +41,12 @@ HEADERS += \
40 41 ../src/console.h \
41 42 ../src/operatingsystem.h \
42 43 ../src/cacic_software.h \
  44 + ../src/cacic_hardware.h \
43 45 # ../src/qtservice/src/qtservice.h \
44 46 # ../src/qtservice/src/qtservice_p.h \
45 47 # ../src/qtservice/src/qtunixserversocket.h \
46 48 # ../src/qtservice/src/qtunixsocket.h
  49 + ../src/cacic_hardware.h
47 50  
48 51 INCLUDEPATH += ../src \
49 52 ../src/crypto++/include/
... ...
cacic-teste/testcacic.cpp
... ... @@ -248,6 +248,14 @@ void CTestCacic::testColetaHardware()
248 248 OCacicHardware.iniciaColeta();
249 249 QVERIFY(false);
250 250 }
  251 +void CTestCacic::testConvertDouble()
  252 +{
  253 + double number = 4.0905;
  254 +
  255 + QString converted = OCacic.convertDouble(number);
  256 +
  257 + QVERIFY(converted.toDouble() == number);
  258 +}
251 259  
252 260 void CTestCacic::cleanupTestCase()
253 261 {
... ...
cacic-teste/testcacic.h
... ... @@ -73,6 +73,7 @@ private slots:
73 73 void testIniciarDaemon();
74 74 void testColetaSoftware();
75 75 void testColetaHardware();
  76 + void testConvertDouble();
76 77 void cleanupTestCase();
77 78 };
78 79  
... ...
cacicD/Readme.txt 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +Para instalar um serviço no Windows:
  2 + ~$ sc create "CacicD" binPath= "C:\endereco\do\cacicd\CacicD.exe" start= auto
  3 +
  4 +Para instalar um serviço no Linux:
  5 + Usar o Upstart
  6 +
... ...
cacicD/cacicd.cpp
1 1 #include "cacicd.h"
2 2  
3   -cacicD::cacicD()
  3 +cacicD::cacicD(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, "CacicD")
4 4 {
  5 + try{
  6 + setServiceDescription("Cacic Daemon");
  7 + setServiceFlags(QtServiceBase::CannotBeStopped);
  8 + } catch (...){
  9 + qCritical() << "Error desconhecido no construtor.";
  10 + }
  11 +}
  12 +
  13 +cacicD::~cacicD()
  14 +{
  15 + try{
  16 +
  17 + } catch (...){
  18 + qCritical() << "Error desconhecido no desconstrutor.";
  19 + }
  20 +}
  21 +
  22 +void cacicD::start()
  23 +{
  24 + try{
  25 + QCoreApplication *app = application();
  26 + qDebug() << "Serviço iniciado.";
  27 + qDebug() << app->applicationDirPath();
  28 +
  29 + } catch (...){
  30 + qCritical() << "Error desconhecido no desconstrutor.";
  31 + }
  32 +}
  33 +
  34 +void cacicD::pause()
  35 +{
  36 + try{
  37 + qDebug() << "Serviço pausado.";
  38 + } catch (...){
  39 + qCritical() << "Error desconhecido ao pausar o serviço.";
  40 + }
  41 +}
  42 +
  43 +void cacicD::resume()
  44 +{
  45 + try{
  46 + qDebug() << "Serviço resumido.";
  47 + } catch (...){
  48 + qCritical() << "Error desconhecido ao resumir o serviço.";
  49 + }
  50 +}
  51 +
  52 +void cacicD::stop()
  53 +{
  54 + try{
  55 + qDebug() << "Serviço parado.";
  56 + } catch (...){
  57 + qCritical() << "Error desconhecido ao parar o serviço.";
  58 + }
5 59 }
... ...
cacicD/cacicd.h
1 1 #ifndef CACICD_H
2 2 #define CACICD_H
3 3  
4   -class cacicD
  4 +#include <QCoreApplication>
  5 +#include <QObject>
  6 +#include <QDebug>
  7 +#include "qtservice.h"
  8 +
  9 +
  10 +class cacicD : public QtService<QCoreApplication>
5 11 {
6 12 public:
7   - cacicD();
  13 + /**
  14 + * @brief cacicD Construtor
  15 + * @param argc
  16 + * @param argv
  17 + */
  18 + cacicD(int argc, char **argv);
  19 + /**
  20 + * @brief cacicD Desconstrutor
  21 + */
  22 + ~cacicD();
  23 + /**
  24 + * @brief inicia o serviço
  25 + */
  26 + void start();
  27 + /**
  28 + * @brief pausa o serviço
  29 + */
  30 + void pause();
  31 + /**
  32 + * @brief resume a execução do serviço
  33 + */
  34 + void resume();
  35 + /**
  36 + * @brief para a execução do serviço
  37 + */
  38 + void stop();
  39 +
  40 +
8 41 };
9 42  
10 43 #endif // CACICD_H
... ...
cacicD/main.cpp
1 1 #include <QCoreApplication>
2   -#include <QStringList>
3   -#include <QDir>
4   -#include <QSettings>
5   -#include <qtservice/src/qtservice.h>
  2 +#include "cacicd.h"
6 3  
7 4 int main(int argc, char **argv)
8 5 {
9   -#if !defined(Q_OS_WIN)
10   - QSettings::setPath(QSettings::NativeFormat, QSettings::SystemScope, QDir::tempPath());
11   - qWarning("(Example uses dummy settings file: %s/QtSoftware.conf)", QDir::tempPath().toLatin1().constData());
12   -#endif
13   - //int result = processArgs(argc, argv);
14   - if (QString::fromLocal8Bit(argv[argc-1]) == QLatin1String("-w") ||
15   - QString::fromLocal8Bit(argv[argc-1]) == QLatin1String("-wait")) {
16   - printf("\nPress Enter to continue...");
17   - QFile input;
18   - input.open(stdin, QIODevice::ReadOnly);
19   - input.readLine();
20   - printf("\n");
21   - }
22   - //return result;
  6 + cacicD sevice(argc, argv);
  7 + return sevice.exec();
23 8 }
24   -
25   -
... ...
gercols/gercols.cpp
... ... @@ -32,7 +32,7 @@ qDebug() &lt;&lt; &quot;TESTE&quot;;
32 32  
33 33 void Gercols::run()
34 34 {
35   - QJsonObject coleta;
  35 +
36 36 /* Pega configurações do Json de configurações localizado
37 37 * na pasta principal do cacic (deverá ser pega do registro,
38 38 * estou tentando implementar isso no installcacic).
... ...
gercols/gercols.pro
... ... @@ -40,7 +40,8 @@ HEADERS += \
40 40 ../src/operatingsystem.h \
41 41 ../src/console.h \
42 42 ../src/ccoleta.h \
43   - ./gercols.h
  43 + ./gercols.h \
  44 + ../src/cacic_hardware.h
44 45  
45 46 INCLUDEPATH += ../src \
46 47 ../src/crypto++/include/
... ...
src/cacic_hardware.cpp
... ... @@ -8,16 +8,17 @@ void cacic_hardware::iniciaColeta()
8 8 {
9 9 #ifdef Q_OS_WIN
10 10 this->coletaHardware = coletaWin();
11   -#else
  11 +#elif defined(Q_OS_LINUX)
12 12 this->coletaHardware = coletaLinux();
13 13 #endif
14 14 }
15 15  
16 16 QJsonObject cacic_hardware::coletaWin()
17 17 {
18   - QJsonObject coleta;
19   - wmi();
20   - return coleta;
  18 +#if defined(Q_OS_WIN)
  19 + // Implementação seguindo as definições na documentação WMI.
  20 +#endif
  21 + return QJsonObject();
21 22 }
22 23  
23 24 QJsonObject cacic_hardware::coletaLinux()
... ... @@ -187,4 +188,89 @@ int cacic_hardware::wmi()
187 188 // CoUninitialize();
188 189  
189 190 return 0; // Program successfully completed.
  191 +=======
  192 +
  193 + OperatingSystem operatingSystem;
  194 + ConsoleObject console;
  195 +
  196 + QJsonObject hardware;
  197 +
  198 + QFile lshwFile("lshwJson.json");
  199 + if( lshwFile.exists() )
  200 + lshwFile.remove();
  201 +
  202 + console("lshw -json >> lshwJson.json");
  203 +
  204 +
  205 + QJsonObject lshwJson = oCacic.getJsonFromFile("lshwJson.json")["children"].toArray().first().toObject();
  206 +
  207 + if( lshwJson.contains("id") && lshwJson["id"] == "core") {
  208 + if ( lshwJson["children"].isArray() ){
  209 + qDebug() << "IS ARRAY!!";
  210 + QJsonArray componentsArray = lshwJson["children"].toArray();
  211 +
  212 + foreach(QJsonValue componentValue, componentsArray ) {
  213 + QJsonObject component = componentValue.toObject();
  214 +
  215 + /* TODO:
  216 + * - Formatar direito as quantidades (memória,clock do cpu)
  217 + * com unidades mais amigáveis para humanos em todos métodos.
  218 + *
  219 + * coletaLinuxMem
  220 + * coletaLinuxCpu
  221 + * coletaLinuxPci - a fazer
  222 + */
  223 +
  224 + if( component["id"] == "memory" ) {
  225 + coletaLinuxMem(hardware,component);
  226 + } else if ( component["id"] == "cpu" ) {
  227 + coletaLinuxCpu(hardware,component);
  228 + } else if ( component["id"] == "pci" ) {
  229 + QJsonArray pciArray = component["children"].toArray();
  230 +
  231 + foreach(QJsonValue pciValue, pciArray){
  232 + QJsonObject pciObject = pciValue.toObject();
  233 +
  234 + coletaLinuxPci(hardware, pciObject);
  235 + }
  236 +
  237 + }
  238 +
  239 + }
  240 + }
  241 +
  242 + }
  243 +
  244 +
  245 + return hardware;
  246 +}
  247 +
  248 +void cacic_hardware::coletaLinuxMem(QJsonObject &hardware, const QJsonObject &component)
  249 +{
  250 + QJsonObject memory;
  251 +
  252 + memory["size"] = QJsonValue::fromVariant(oCacic.convertDouble(component["size"].toDouble(),0) + " bytes");
  253 +
  254 + hardware["memory"] = memory;
  255 +}
  256 +
  257 +void cacic_hardware::coletaLinuxCpu(QJsonObject &hardware, const QJsonObject &component)
  258 +{
  259 + QJsonObject cpu;
  260 +
  261 + cpu["name"] = component["product"];
  262 + cpu["vendor"] = component["vendor"];
  263 + cpu["clock"] = QJsonValue::fromVariant(oCacic.convertDouble(component["capacity"].toDouble(),0) + " Hz");
  264 +
  265 + hardware["cpu"] = cpu;
  266 +}
  267 +
  268 +void cacic_hardware::coletaLinuxPci(QJsonObject &hardware, const QJsonObject &pciJson)
  269 +{
  270 +
  271 +}
  272 +
  273 +QJsonObject cacic_hardware::toJsonObject() {
  274 + return coletaHardware;
  275 +>>>>>>> c87d0f3e5ae47bd0d7f7f8c5f5583bb46dd7235b
190 276 }
... ...
src/cacic_hardware.h
1 1 #ifndef CACIC_HARDWARE_H
2 2 #define CACIC_HARDWARE_H
3 3 #include <ccacic.h>
4   -#include <wbemidl.h>
5 4 #include <querysink.h>
6   -#include <iostream>
7   -using namespace std;
8   -#include <comutil.h>
9   -# pragma comment(lib, "wbemuuid.lib")
  5 +#include <QtCore>
  6 +#include <QJsonArray>
  7 +#include <QJsonArray>
  8 +#include <cmath>
  9 +#include <ccacic.h>
  10 +#include <console.h>
  11 +#include <operatingsystem.h>
  12 +
  13 +#if defined(Q_OS_WIN)
  14 + #define _WIN32_DCOM
  15 + #include <iostream>
  16 + //using namespace std;
  17 + #include <wbemidl.h>
  18 + #include <windows.h>
  19 + # pragma comment(lib, "wbemuuid.lib")
  20 +
  21 +#endif
10 22  
11 23 class cacic_hardware
12 24 {
... ... @@ -14,13 +26,19 @@ public:
14 26 cacic_hardware();
15 27 void iniciaColeta();
16 28 QJsonObject toJsonObject();
  29 + QJsonObject coletaWin();
  30 + QJsonObject coletaLinux();
17 31  
18 32 private:
19   - QJsonObject coletaHardware;
20 33 int wmi();
21   -
22 34 QJsonObject coletaWin();
23 35 QJsonObject coletaLinux();
  36 + void coletaLinuxMem(QJsonObject &hardware, const QJsonObject &component);
  37 + void coletaLinuxCpu(QJsonObject &hardware, const QJsonObject &component);
  38 + void coletaLinuxPci(QJsonObject &hardware, const QJsonObject &pciJson);
  39 +
  40 + CCacic oCacic;
  41 + QJsonObject coletaHardware;
24 42 };
25 43  
26 44 #endif // CACIC_HARDWARE_H
... ...
src/cacic_software.cpp
... ... @@ -8,8 +8,9 @@ void cacic_software::iniciaColeta()
8 8 {
9 9 #ifdef Q_OS_WIN
10 10 this->coletaSoftware = coletaWin();
11   -#else
  11 +#elif defined(Q_OS_LINUX)
12 12 this->coletaSoftware = coletaLinux();
  13 +
13 14 #endif
14 15 }
15 16  
... ... @@ -48,10 +49,102 @@ QJsonObject cacic_software::coletaWin()
48 49 return softwaresJson;
49 50 }
50 51  
51   -void cacic_software::coletaLinux()
  52 +QJsonObject cacic_software::coletaLinux()
  53 +{
  54 +
  55 + OperatingSystem operatingSystem;
  56 +
  57 + if( operatingSystem.getIdOs() == OperatingSystem::LINUX_ARCH ) {
  58 + return coletaArch();
  59 + } else if ( operatingSystem.getIdOs() == OperatingSystem::LINUX_DEBIAN ||
  60 + operatingSystem.getIdOs() == OperatingSystem::LINUX_UBUNTU ) {
  61 + return coletaDebian();
  62 + }
  63 +
  64 + return QJsonObject();
  65 +}
  66 +
  67 +QJsonObject cacic_software::coletaArch()
  68 +{
  69 + ConsoleObject console;
  70 + QJsonObject softwaresJson;
  71 +
  72 + QStringList packages = console("pacman -Qe").split("\n");
  73 +
  74 + foreach(QString package, packages) {
  75 + QString packageName = package.split(" ")[0];
  76 + QJsonObject packageJson;
  77 +
  78 + QStringList packageInfo = console(QString("pacman -Qi ").append(packageName)).split("\n");
  79 +// qDebug() << packageInfo;
  80 +
  81 + packageJson["name"] = QJsonValue::fromVariant(QString(packageName));
  82 + foreach(QString line, packageInfo) {
  83 + if(line.contains("Version"))
  84 + packageJson["version"] = line.split(":")[1].mid(1);
  85 + if(line.contains("Description"))
  86 + packageJson["description"] = line.split(":")[1].mid(1);
  87 + if(line.contains("URL")) {
  88 + QStringList url = line.split(":");
  89 + QString urlString;
  90 +
  91 + for(int i = 1 ; i < url.size() ; ++i){
  92 + urlString.append(url[i]);
  93 + if(i == 1 ) urlString.append(":");
  94 + }
  95 +
  96 + packageJson["url"] = urlString.mid(1);
  97 + }
  98 + if(line.contains("Installed size"))
  99 + packageJson["installed_size"] = line.split(":")[1].mid(1);
  100 + if(line.contains("Install Date"))
  101 + packageJson["install_date"] = line.split(":")[1].mid(1);
  102 + }
  103 + softwaresJson[packageName] = packageJson;
  104 + }
  105 + return softwaresJson;
  106 +}
  107 +
  108 +QJsonObject cacic_software::coletaDebian()
52 109 {
  110 + ConsoleObject console;
  111 + QJsonObject softwaresJson;
  112 +
  113 + QStringList packages = console("dpkg --get-selections").split("\n");
  114 +
  115 + foreach(QString package, packages) {
  116 + QString packageName = package.split(" ")[0];
  117 + QJsonObject packageJson;
53 118  
  119 + QStringList packageInfo = console(QString("apt-cache show ").append(packageName)).split("\n");
  120 +// qDebug() << packageInfo;
  121 +
  122 + packageJson["name"] = QJsonValue::fromVariant(QString(packageName));
  123 + foreach(QString line, packageInfo) {
  124 + if(line.contains("Version"))
  125 + packageJson["version"] = line.split(":")[1].mid(1);
  126 + if(line.contains("Description"))
  127 + packageJson["description"] = line.split(":")[1].mid(1);
  128 + if(line.contains("Homepage")) {
  129 + QStringList url = line.split(":");
  130 + QString urlString;
  131 +
  132 + for(int i = 1 ; i < url.size() ; ++i){
  133 + urlString.append(url[i]);
  134 + if(i == 1 ) urlString.append(":");
  135 + }
  136 +
  137 + packageJson["url"] = urlString.mid(1);
  138 + }
  139 + if(line.contains("Installed-Size"))
  140 + packageJson["installed_size"] = line.split(":")[1].mid(1);
  141 + }
  142 + softwaresJson[packageName] = packageJson;
  143 + }
  144 +
  145 + return softwaresJson;
54 146 }
  147 +
55 148 QJsonObject cacic_software::toJsonObject()
56 149 {
57 150 return coletaSoftware;
... ...
src/cacic_software.h
... ... @@ -2,7 +2,8 @@
2 2 #define CACIC_SOFTWARE_H
3 3 #include <QtCore>
4 4 #include <ccacic.h>
5   -
  5 +#include <console.h>
  6 +#include <operatingsystem.h>
6 7 class cacic_software
7 8 {
8 9 public:
... ... @@ -12,7 +13,9 @@ public:
12 13  
13 14 private:
14 15 QJsonObject coletaWin();
15   - void coletaLinux();
  16 + QJsonObject coletaLinux();
  17 + QJsonObject coletaArch();
  18 + QJsonObject coletaDebian();
16 19  
17 20 CCacic oCacic;
18 21 QJsonObject coletaSoftware;
... ...
src/ccacic.cpp
... ... @@ -286,6 +286,14 @@ void CCacic::setChaveCrypt(const QString &amp;value)
286 286 chaveCrypt = value;
287 287 }
288 288  
  289 +QString CCacic::convertDouble(const double &number, const int &precision)
  290 +{
  291 + std::ostringstream ss;
  292 + ss.precision(precision);
  293 + ss << std::fixed << number;
  294 +
  295 + return QString::fromStdString(ss.str());
  296 +}
289 297  
290 298 /*Getters/Setters
291 299 * End.
... ...
src/ccacic.h
... ... @@ -11,6 +11,8 @@
11 11 #include <QJsonDocument>
12 12 #include <QJsonObject>
13 13 #include <QJsonValue>
  14 +#include <sstream>
  15 +#include <iostream>
14 16 #include "../src/crypto++/include/aes.h"
15 17 #include "../src/crypto++/include/base64.h"
16 18 #include "../src/crypto++/include/modes.h"
... ... @@ -38,7 +40,7 @@ public:
38 40 void setValueToRegistry(QString organization, QString application, QVariantMap values);
39 41 QVariant getValueFromRegistry(QString organization, QString application, QString key);
40 42 void removeRegistry(QString organization, QString application);
41   -
  43 + QString convertDouble(const double &number, const int &precision = 10);
42 44  
43 45 //Geters/seters:
44 46  
... ...
src/ccoleta.cpp
... ... @@ -8,7 +8,7 @@ CColeta::CColeta(QObject *parent)
8 8 void CColeta::coletaHardware()
9 9 {
10 10 qDebug() << "coletaHardware() executado";
11   -// oHardware.iniciaColeta();
  11 + oHardware.iniciaColeta();
12 12 }
13 13  
14 14  
... ... @@ -16,6 +16,7 @@ void CColeta::coletaSoftware()
16 16 {
17 17 qDebug() << "coletaSoftware() executado";
18 18 oSoftware.iniciaColeta();
  19 +
19 20 }
20 21  
21 22 void CColeta::configuraColetas(){
... ... @@ -33,6 +34,11 @@ cacic_software CColeta::getOSoftware() const
33 34 return oSoftware;
34 35 }
35 36  
  37 +cacic_hardware CColeta::getOHardware() const
  38 +{
  39 + return oHardware;
  40 +}
  41 +
36 42 void CColeta::run()
37 43 {
38 44 QJsonObject coleta = oCacic.getJsonFromFile("configReq.json");
... ... @@ -49,5 +55,6 @@ QJsonObject CColeta::toJsonObject()
49 55 QJsonObject coletaJson;
50 56 coletaJson["computer"] = oComputer.toJsonObject();
51 57 coletaJson["software"] = oSoftware.toJsonObject();
  58 + coletaJson["hardware"] = oHardware.toJsonObject();
52 59 return coletaJson;
53 60 }
... ...
src/ccoleta.h
... ... @@ -7,6 +7,7 @@
7 7 #include <ccacic.h>
8 8 #include <cacic_computer.h>
9 9 #include <cacic_software.h>
  10 +#include <cacic_hardware.h>
10 11  
11 12  
12 13 class CColeta : public QObject
... ... @@ -17,12 +18,14 @@ private:
17 18 CCacic oCacic;
18 19 CACIC_Computer oComputer;
19 20 cacic_software oSoftware;
  21 + cacic_hardware oHardware;
20 22  
21 23 public:
22 24 explicit CColeta(QObject *parent = 0);
23 25  
24 26 CACIC_Computer getOComputer() const;
25 27 cacic_software getOSoftware() const;
  28 + cacic_hardware getOHardware() const;
26 29  
27 30 QJsonObject toJsonObject();
28 31  
... ... @@ -35,10 +38,7 @@ public slots:
35 38 signals:
36 39 void beginComputer();
37 40 void beginHardware();
38   - void beginNetworkInterfaces();
39   - void beginOperatingSystem();
40 41 void beginSoftware();
41   - void beginUser();
42 42 void finished();
43 43 };
44 44  
... ...