diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 27dcbce327b368ef26313f86e4aeac44e350bcc7..6f7353056b22302052683d137fb548fe3ed0cdc7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -130,7 +130,7 @@ testOpensuse: - opensuse script: - zypper -n update - - zypper -n install bats jq libfaketime expect tar cron + - zypper -n install bats jq libfaketime expect tar cron gzip - touch /var/log/wtmp - chown root.tty /var/log/wtmp - chmod 664 /var/log/wtmp diff --git a/include/agent/get_bios_date.h b/include/agent/get_bios_date.h index dce7feb56f2097ee4d9e0e9fb9b0a1338f3d4c59..4fd8ddf38fd838f1e1832c4053f6c2ff69970ffe 100644 --- a/include/agent/get_bios_date.h +++ b/include/agent/get_bios_date.h @@ -1,10 +1,12 @@ #pragma once #include <fstream> #include <string> +#define BIOS_DATE_PATH "/sys/devices/virtual/dmi/id" /** * @file get_bios_date.h * @brief Get data from bios format (YYYY-MM-DD) */ + std::string get_bios_date(); \ No newline at end of file diff --git a/include/agent/get_machine.h b/include/agent/get_machine.h new file mode 100644 index 0000000000000000000000000000000000000000..3f46cd16fe227ef3e14341fc3cc172a91e52d4ae --- /dev/null +++ b/include/agent/get_machine.h @@ -0,0 +1,14 @@ +#pragma once +#include <fstream> +#include <string> +#define MACHINE_PATH "/sys/devices/virtual/dmi/id" + +/** + * @file get_bios_date.h + * @brief Get data from the machine + */ + +std::string get_product_name(); +std::string get_product_version(); +std::string get_product_serial(); +std::string get_sys_vendor(); \ No newline at end of file diff --git a/include/agent/get_motherboard.h b/include/agent/get_motherboard.h new file mode 100644 index 0000000000000000000000000000000000000000..3d5dc52e84528afa0c882686469b6c1b3592ae16 --- /dev/null +++ b/include/agent/get_motherboard.h @@ -0,0 +1,14 @@ +#pragma once +#include <fstream> +#include <string> +#define MOTHERBOARD_PATH "/sys/devices/virtual/dmi/id" + +/** + * @file get_motherboard.h + * @brief Get data from the motherboard + */ + +std::string get_board_vendor(); +std::string get_board_serial(); +std::string get_board_version(); +std::string get_board_name(); \ No newline at end of file diff --git a/include/agent/inventory.h b/include/agent/inventory.h index 6efee4e0e1aac3b67e4a356e8388cc359e47efa6..809a5607b27aa5437044d1ceb0c5bfb36fee3e95 100644 --- a/include/agent/inventory.h +++ b/include/agent/inventory.h @@ -10,8 +10,10 @@ #include <agent/get_disks_info.h> #include <agent/get_distro.h> #include <agent/get_macaddr.h> +#include <agent/get_machine.h> #include <agent/get_machine_type.h> #include <agent/get_memory_size.h> +#include <agent/get_motherboard.h> #include <agent/get_processor_model.h> #include <agent/get_time.h> #include <agent/get_user_count.h> diff --git a/src/agent/get_bios_date.cpp b/src/agent/get_bios_date.cpp index 846c3f70d818aff60a6c0adc882f1487584eb3bc..2174ccb7b7d7bc61e4c12a3a2085c9f6bdaa750d 100644 --- a/src/agent/get_bios_date.cpp +++ b/src/agent/get_bios_date.cpp @@ -25,18 +25,23 @@ std::string get_bios_date() { #ifdef __unix__ std::ifstream file; - std::string line, d, m, y, format = ""; - file.open("/sys/devices/virtual/dmi/id/bios_date"); + std::string line, d, m, y, format = "", err; + file.open((std::string)BIOS_DATE_PATH + "/bios_date"); if (file.is_open()) { getline(file, line); + if (line.empty()) { + err = "The file " + (std::string)BIOS_DATE_PATH \ + + "/bios_date is empty"; + throw err; + } d = line.substr(0, 2); m = line.substr(3, 2); y = line.substr(6, 4); format = y + "-" + m + "-" + d; } else { - std::string err; - err = "Could not open /sys/devices/virtual/dmi/id/bios_date"; + err = "Could not open" + (std::string)BIOS_DATE_PATH \ + + "/bios_date"; throw err; } diff --git a/src/agent/get_machine.cpp b/src/agent/get_machine.cpp new file mode 100644 index 0000000000000000000000000000000000000000..764ba9def3666ce1e4948c2cebbc8da3f55b7f42 --- /dev/null +++ b/src/agent/get_machine.cpp @@ -0,0 +1,150 @@ +/* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre + * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + * + * This file is part of simmc-agent + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ + +#include <agent/get_machine.h> + +std::string get_product_name() { + #ifdef __unix__ + + std::ifstream file; + std::string data = "", err; + file.open((std::string)MACHINE_PATH + "/product_name"); + + if (file.is_open()) { + getline(file, data); + if (data.empty()) { + err = "The file " + (std::string)MACHINE_PATH \ + + "/product_name is empty"; + throw err; + } + } else { + err = "Could not open" + (std::string)MACHINE_PATH + "/product_name"; + throw err; + } + + file.close(); + + return data; + + #elif WIN32 + + std::string err = "Feature not supported on Windows"; + throw err; + + #endif +} + +std::string get_product_version() { + #ifdef __unix__ + + std::ifstream file; + std::string data = "", err; + file.open((std::string)MACHINE_PATH + "/product_version"); + + if (file.is_open()) { + getline(file, data); + if (data.empty()) { + err = "The file " + (std::string)MACHINE_PATH \ + + "/product_version is empty"; + throw err; + } + } else { + err = "Could not open" + (std::string)MACHINE_PATH \ + + "/product_version"; + throw err; + } + + file.close(); + + return data; + + #elif WIN32 + + std::string err = "Feature not supported on Windows"; + throw err; + + #endif +} + +std::string get_product_serial() { + #ifdef __unix__ + + std::ifstream file; + std::string data = "", err; + file.open((std::string)MACHINE_PATH + "/product_serial"); + + if (file.is_open()) { + getline(file, data); + if (data.empty()) { + err = "The file " + (std::string)MACHINE_PATH \ + + "/product_serial is empty"; + throw err; + } + } else { + err = "Could not open" + (std::string)MACHINE_PATH \ + + "/product_serial"; + throw err; + } + + file.close(); + + return data; + + #elif WIN32 + + std::string err = "Feature not supported on Windows"; + throw err; + + #endif +} + +std::string get_sys_vendor() { + #ifdef __unix__ + + std::ifstream file; + std::string data = "", err; + file.open((std::string)MACHINE_PATH + "/sys_vendor"); + + if (file.is_open()) { + getline(file, data); + if (data.empty()) { + err = "The file " + (std::string)MACHINE_PATH \ + + "/sys_vendor is empty"; + throw err; + } + } else { + err = "Could not open" + (std::string)MACHINE_PATH \ + + "/sys_vendor"; + throw err; + } + + file.close(); + + return data; + + #elif WIN32 + + std::string err = "Feature not supported on Windows"; + throw err; + + #endif +} + diff --git a/src/agent/get_motherboard.cpp b/src/agent/get_motherboard.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91bb35fd3c15266cff766e6236de0a24a58b1b8b --- /dev/null +++ b/src/agent/get_motherboard.cpp @@ -0,0 +1,153 @@ +/* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre + * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + * + * This file is part of simmc-agent + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ + + +#include <agent/get_motherboard.h> + + +std::string get_board_vendor() { + #ifdef __unix__ + + std::ifstream file; + std::string data = "", err; + file.open((std::string)MOTHERBOARD_PATH + "/board_vendor"); + + if (file.is_open()) { + getline(file, data); + if (data.empty()) { + err = "The file " + (std::string)MOTHERBOARD_PATH \ + + "/board_vendor is empty"; + throw err; + } + } else { + err = "Could not open" + (std::string)MOTHERBOARD_PATH \ + + "/board_vendor"; + throw err; + } + + file.close(); + + return data; + + #elif WIN32 + + std::string err = "Feature not supported on Windows"; + throw err; + + #endif +} + +std::string get_board_serial() { + #ifdef __unix__ + + std::ifstream file; + std::string data = "", err; + file.open((std::string)MOTHERBOARD_PATH + "/board_serial"); + + if (file.is_open()) { + getline(file, data); + if (data.empty()) { + err = "The file " + (std::string)MOTHERBOARD_PATH \ + + "/board_serial is empty"; + throw err; + } + } else { + err = "Could not open" + (std::string)MOTHERBOARD_PATH \ + + "/board_serial"; + throw err; + } + + file.close(); + + return data; + + #elif WIN32 + + std::string err = "Feature not supported on Windows"; + throw err; + + #endif +} + +std::string get_board_version() { + #ifdef __unix__ + + std::ifstream file; + std::string data = "", err; + file.open((std::string)MOTHERBOARD_PATH + "/board_version"); + + if (file.is_open()) { + getline(file, data); + if (data.empty()) { + err = "The file " + (std::string)MOTHERBOARD_PATH \ + + "/board_version is empty"; + throw err; + } + } else { + err = "Could not open" + (std::string)MOTHERBOARD_PATH \ + + "/board_version"; + throw err; + } + + file.close(); + + return data; + + #elif WIN32 + + std::string err = "Feature not supported on Windows"; + throw err; + + #endif +} + +std::string get_board_name() { + #ifdef __unix__ + + std::ifstream file; + std::string data = "", err; + file.open((std::string)MOTHERBOARD_PATH + "/board_name"); + + if (file.is_open()) { + getline(file, data); + if (data.empty()) { + err = "The file " + (std::string)MOTHERBOARD_PATH \ + + "/board_name is empty"; + throw err; + } + } else { + err = "Could not open" + (std::string)MOTHERBOARD_PATH \ + + "/board_name"; + throw err; + } + + file.close(); + + return data; + + #elif WIN32 + + std::string err = "Feature not supported on Windows"; + throw err; + + #endif +} + diff --git a/src/agent/inventory.cpp b/src/agent/inventory.cpp index ed8955766e3ce54775d65486c880ed73abafb961..252935a2281648b4d1c85a3fbe2dff177e40819f 100644 --- a/src/agent/inventory.cpp +++ b/src/agent/inventory.cpp @@ -152,6 +152,7 @@ Json::Value get_inventory(Agent* agent) { inv["error_inventory"]["disk1_used"] = err; } + // date bios try { inv["data_inventory"]["date_bios"] = get_bios_date(); } catch (std::string err) { @@ -159,18 +160,71 @@ Json::Value get_inventory(Agent* agent) { inv["error_inventory"]["date_bios"] = err; } - // hw info - /* - inv["disk1_used"] = - inv["disk2_used"] = - */ - // other - /* - inv["agent_version"] = - inv["contact_date"] = - */ + // board name + try { + inv["data_inventory"]["board_name"] = get_board_name(); + } catch (std::string err) { + inv["data_inventory"]["board_name"] = Json::nullValue; + + inv["error_inventory"]["board_name"] = err; + } + // board serial + try { + inv["data_inventory"]["board_serial"] = get_board_serial(); + } catch (std::string err) { + inv["data_inventory"]["board_serial"] = Json::nullValue; + + inv["error_inventory"]["board_serial"] = err; + } + // board vendor + try { + inv["data_inventory"]["board_vendor"] = get_board_vendor(); + } catch (std::string err) { + inv["data_inventory"]["board_vendor"] = Json::nullValue; + + inv["error_inventory"]["board_vendor"] = err; + } + // board version + try { + inv["data_inventory"]["board_version"] = get_board_version(); + } catch (std::string err) { + inv["data_inventory"]["board_version"] = Json::nullValue; + inv["error_inventory"]["board_version"] = err; + } + // product name + try { + inv["data_inventory"]["product_name"] = get_product_name(); + } catch (std::string err) { + inv["data_inventory"]["product_name"] = Json::nullValue; + + inv["error_inventory"]["product_name"] = err; + } + // product serial + try { + inv["data_inventory"]["product_serial"] = get_product_serial(); + } catch (std::string err) { + inv["data_inventory"]["product_serial"] = Json::nullValue; + + inv["error_inventory"]["product_serial"] = err; + } + // product version + try { + inv["data_inventory"]["product_version"] = get_product_version(); + } catch (std::string err) { + inv["data_inventory"]["product_version"] = Json::nullValue; + + inv["error_inventory"]["product_version"] = err; + } + // sys vendor + try { + inv["data_inventory"]["sys_vendor"] = get_sys_vendor(); + } catch (std::string err) { + inv["data_inventory"]["sys_vendor"] = Json::nullValue; + + inv["error_inventory"]["sys_vendor"] = err; + } // machine type #ifdef WIN32 diff --git a/test/returnTest.bats b/test/returnTest.bats index 873e82a64f01bdd30d64f2f833b01cbb46b01165..3c12602d019c8facdb9f15efb2a790147ef19630 100755 --- a/test/returnTest.bats +++ b/test/returnTest.bats @@ -2,69 +2,69 @@ function amount_users() { expected_number_of_users=3 - obj=$(cat inventory.json | jq -r '.data_inventory.amount_users') + obj=$(cat inventory.json | jq -r '.amount_users') [[ "$obj" -eq "$expected_number_of_users" ]] && [[ "$obj" != "null" ]] } function mac_address() { - obj=$(cat inventory.json | jq -r '.data_inventory.mac_address') + obj=$(cat inventory.json | jq -r '.mac_address') [[ "$obj" =~ ^(..:){5}..$ ]] && [[ "$obj" != "null" ]] } function machine_type() { - os=$(cat inventory.json | jq -r '.data_inventory.os_distro') - obj=$(cat inventory.json | jq -r '.data_inventory.machine_type') + os=$(cat inventory.json | jq -r '.os_distro') + obj=$(cat inventory.json | jq -r '.machine_type') ! [[ "$os" =~ .*Linux\ Comunicações.* ]] || ( [[ "$os" =~ .*Linux\ Comunicações.* ]] && [[ "$obj" != "null" ]] ) } function memory() { - obj=$(cat inventory.json | jq -r '.data_inventory.memory') + obj=$(cat inventory.json | jq -r '.memory') [[ "$obj" -eq "$obj" ]] && [[ "$obj" != "null" ]] } function os_distro() { - obj=$(cat inventory.json | jq -r '.data_inventory.os_distro') + obj=$(cat inventory.json | jq -r '.os_distro') [[ "$obj" != "null" ]] } function os_kernel() { - obj=$(cat inventory.json | jq -r '.data_inventory.os_kernel') + obj=$(cat inventory.json | jq -r '.os_kernel') [[ "$obj" != "null" ]] } function os_type() { - obj=$(cat inventory.json | jq -r '.data_inventory.os_type') + obj=$(cat inventory.json | jq -r '.os_type') [[ "$obj" != "null" ]] } function processor() { - obj=$(cat inventory.json | jq -r '.data_inventory.processor') + obj=$(cat inventory.json | jq -r '.processor') [[ "$obj" != "null" ]] } function date_bios() { - obj=$(cat inventory.json | jq -r '.data_inventory.date_bios') - [[ "$obj" != "null" ]] + obj=$(cat inventory.json | jq -r '.date_bios') + [[ "$obj" != "04/01/2014" ]] } function bytes_received() { - obj=$(cat net.json | jq -r '.data_net.bytes_received') + obj=$(cat net.json | jq -r '.bytes_received') [[ "$obj" -ge 0 ]] } function bytes_transmitted() { - obj=$(cat net.json | jq -r '.data_net.bytes_transmitted') + obj=$(cat net.json | jq -r '.bytes_transmitted') [[ "$obj" -ge 0 ]] } function packets_received() { - obj=$(cat net.json | jq -r '.data_net.packets_received') + obj=$(cat net.json | jq -r '.packets_received') [[ "$obj" -ge 0 ]] } function packets_transmitted() { - obj=$(cat net.json | jq -r '.data_net.packets_transmitted') + obj=$(cat net.json | jq -r '.packets_transmitted') [[ "$obj" -ge 0 ]] } @@ -98,11 +98,51 @@ function id_point() { [[ "$obj" -eq 1234 ]] } +function board_name() { + obj=$(cat inventory.json | jq -r '.board_name') + [[ "$obj" -eq "null" ]] +} + +function board_serial() { + obj=$(cat inventory.json | jq -r '.board_serial') + [[ "$obj" -eq "null" ]] +} + +function board_vendor() { + obj=$(cat inventory.json | jq -r '.board_vendor') + [[ "$obj" -eq "null" ]] +} + +function board_version() { + obj=$(cat inventory.json | jq -r '.board_version') + [[ "$obj" -eq "null" ]] +} + +function product_name() { + obj=$(cat inventory.json | jq -r '.product_name') + [[ "$obj" == "Standard PC (i440FX + PIIX, 1996)" ]] +} + +function product_serial() { + obj=$(cat inventory.json | jq -r '.product_serial') + [[ "$obj" -eq "null" ]] +} + +function product_version() { + obj=$(cat inventory.json | jq -r '.product_version') + [[ "$obj" == "pc-i440fx-3.0" ]] +} + +function sys_vendor() { + obj=$(cat inventory.json | jq -r '.sys_vendor') + [[ "$obj" == "QEMU" ]] +} + setup() { /opt/agentC3SL/agent-v* --print --once | head -n -1 > export.json - pt1=$(jq "del(."data_net")" export.json | head -n -1) + pt1=$(jq ."data_inventory" export.json | head -n -1) echo $pt1 > inventory.json - pt2=$(jq "del(."data_inventory")" export.json | tail -n +2) + pt2=$(jq ."data_net" export.json | tail -n +2) echo $pt2 > net.json [ -s export.json ] [ -s inventory.json ] @@ -210,3 +250,43 @@ teardown() { run web_service [ "$status" -eq 0 ] } + +@test "board_name" { + run board_name + [ "$status" -eq 0 ] +} + +@test "board_serial" { + run board_serial + [ "$status" -eq 0 ] +} + +@test "board_vendor" { + run board_vendor + [ "$status" -eq 0 ] +} + +@test "board_version" { + run board_version + [ "$status" -eq 0 ] +} + +@test "product_name" { + run product_name + [ "$status" -eq 0 ] +} + +@test "product_serial" { + run product_serial + [ "$status" -eq 0 ] +} + +@test "product_version" { + run product_version + [ "$status" -eq 0 ] +} + +@test "sys_vendor" { + run sys_vendor + [ "$status" -eq 0 ] +}