diff --git a/include/agent/linux/get_distro.h b/include/agent/linux/get_distro.h index ae5556a308e2ecf37fe6e97dc026228da6bdf3a5..e9730beb1d5f67c40d2c26f7adb83dcbf6d2d19b 100644 --- a/include/agent/linux/get_distro.h +++ b/include/agent/linux/get_distro.h @@ -1,7 +1,5 @@ #pragma once #include <sys/utsname.h> -#include <fstream> -#include <cstring> +#include "agent/linux/open_file.h" -int open_file(std::string name, std::ifstream& fstream); std::string get_distro(); diff --git a/include/agent/linux/get_processor_model.h b/include/agent/linux/get_processor_model.h new file mode 100644 index 0000000000000000000000000000000000000000..e3008313d95a938b53a86c0b37711efacd8a2adf --- /dev/null +++ b/include/agent/linux/get_processor_model.h @@ -0,0 +1,6 @@ +#pragma once +#include <fstream> +#include <string> +#include "agent/linux/open_file.h" + +std::string get_processor_model(); diff --git a/include/agent/linux/inventory.h b/include/agent/linux/inventory.h index 3d96c0cfd1db85ccc955d72430d8fbba73648015..b57d6f45d5969a1b8ec5dc406485329ca8d005c1 100644 --- a/include/agent/linux/inventory.h +++ b/include/agent/linux/inventory.h @@ -3,15 +3,12 @@ #include <time.h> #include <iostream> #include "agent/linux/get_date.h" +#include "agent/linux/get_distro.h" #include "agent/linux/get_macaddr.h" #include "agent/linux/get_machine_type.h" +#include "agent/linux/get_processor_model.h" #include "agent/linux/get_time.h" #include "agent/linux/get_user_count.h" #include "json/json.h" -int open_file(std::string name, std::ifstream& fstream); - -std::string get_distro(); -std::string get_processor_model(); - Json::Value get_inventory(); diff --git a/include/agent/linux/open_file.h b/include/agent/linux/open_file.h new file mode 100644 index 0000000000000000000000000000000000000000..486377505034a8b1fe6bfc0120007314fa7731f8 --- /dev/null +++ b/include/agent/linux/open_file.h @@ -0,0 +1,5 @@ +#pragma once +#include <fstream> +#include <string> + +int open_file(std::string name, std::ifstream& fstream); diff --git a/src/linux/get_distro.cpp b/src/linux/get_distro.cpp index 4f8ecd3c50358e899edc9fa6acd55b3bcde2a000..a7c47703b567843b25d4fc332e667f5b3e2696cf 100644 --- a/src/linux/get_distro.cpp +++ b/src/linux/get_distro.cpp @@ -20,14 +20,7 @@ */ #include "agent/linux/get_distro.h" - -int open_file(std::string name, std::ifstream& fstream) { - fstream.open(name); - if (fstream.is_open()) - return 1; - else - return 0; -} +#include "agent/linux/open_file.h" std::string get_distro() { /* currently recorded distros are diff --git a/src/linux/get_processor_model.cpp b/src/linux/get_processor_model.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65a1ce8758d5b2a03735e013968c090fd3446ce3 --- /dev/null +++ b/src/linux/get_processor_model.cpp @@ -0,0 +1,44 @@ +/* Copyright (C) 2016 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/linux/get_processor_model.h" + +std::string get_processor_model() { + + std::ifstream cpuinfo_file; + std::string line; + if (open_file("/proc/cpuinfo", cpuinfo_file)) { + const std::string key = "model name"; + std::size_t found; + + while (getline(cpuinfo_file, line)) { + found = line.find(key); + if (found != std::string::npos) { + // remove ':' and space at the beginning + found = line.find(":"); + return (line.substr(found+2)); + } + } + throw std::string("Coudn't find model name in /proc/cpuinfo"); + } + + throw std::string("Coudn't open /proc/cpuinfo"); +} diff --git a/src/linux/inventory.cpp b/src/linux/inventory.cpp index ff41037b0034f5b82d2c1ab089d90d15548d8a7e..1863056bb7825d78bb3d32411bbc6c0920243f51 100644 --- a/src/linux/inventory.cpp +++ b/src/linux/inventory.cpp @@ -21,12 +21,9 @@ #include "agent/linux/inventory.h" -std::string get_processor_model() { - return "unknown"; -} +Json::Value get_inventory(){ -Json::Value get_inventory() { - Json::Value inv; + Json::Value inv; // os info @@ -46,9 +43,16 @@ Json::Value get_inventory() { inv["data"]["os_kernel"] = sysinfo.release; inv["data"]["mirror_timestamp"] = sysinfo.version; + try { + inv["data"]["processor"] = get_processor_model(); + } + catch ( std::string err ) { + inv["data"]["processor"] = Json::nullValue; + inv["error"]["processor"] = err; + } + // hw info /* - inv["processor"] = inv["memory"] = inv["disk1_model"] = inv["disk1_size"] = diff --git a/src/linux/open_file.cpp b/src/linux/open_file.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8585b2fc1b06f57b3ca5873e6c13bb121a9ae7bf --- /dev/null +++ b/src/linux/open_file.cpp @@ -0,0 +1,10 @@ +#include "agent/linux/open_file.h" + +int open_file(std::string name, std::ifstream& fstream){ + fstream.open(name); + if (fstream.is_open()) + return 1; + else + return 0; +} +