From 5ffa1c10b3ec7068e38848dd73a6fde45b751b0d Mon Sep 17 00:00:00 2001 From: Clara Daia Hilgenberg Daru <cdhd12@inf.ufpr.br> Date: Tue, 30 Aug 2016 12:02:00 -0300 Subject: [PATCH] Issue #46: Create get_memory_size() --- include/agent/linux/get_memory_size.h | 7 +++ include/agent/linux/inventory.h | 1 + src/linux/get_memory_size.cpp | 62 +++++++++++++++++++++++++++ src/linux/inventory.cpp | 9 +++- 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 include/agent/linux/get_memory_size.h create mode 100644 src/linux/get_memory_size.cpp diff --git a/include/agent/linux/get_memory_size.h b/include/agent/linux/get_memory_size.h new file mode 100644 index 00000000..1dc19d01 --- /dev/null +++ b/include/agent/linux/get_memory_size.h @@ -0,0 +1,7 @@ +#pragma once +#include <fstream> +#include <string> +#include "agent/linux/open_file.h" +#include <algorithm> + +int get_memory_size(); diff --git a/include/agent/linux/inventory.h b/include/agent/linux/inventory.h index b57d6f45..7ed15a14 100644 --- a/include/agent/linux/inventory.h +++ b/include/agent/linux/inventory.h @@ -6,6 +6,7 @@ #include "agent/linux/get_distro.h" #include "agent/linux/get_macaddr.h" #include "agent/linux/get_machine_type.h" +#include "agent/linux/get_memory_size.h" #include "agent/linux/get_processor_model.h" #include "agent/linux/get_time.h" #include "agent/linux/get_user_count.h" diff --git a/src/linux/get_memory_size.cpp b/src/linux/get_memory_size.cpp new file mode 100644 index 00000000..df66e963 --- /dev/null +++ b/src/linux/get_memory_size.cpp @@ -0,0 +1,62 @@ +/* Copyright (C) 2004-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_memory_size.h" + +int get_memory_size() { + + std::ifstream meminfo_file; + std::string line; + std::string memsize; + + if (open_file("/proc/meminfo", meminfo_file)) { + + const std::string key = "MemTotal:"; + std::size_t found; + + while (getline(meminfo_file, line)) { + + found = line.find(key); + + if (found != std::string::npos) { + // remove key and 'kB' at the end + std::string value = line.substr(found + key.length(), + line.length() - + (found+key.length()+3)); + // remove whitespaces + remove_copy(value.begin(), value.end(), + std::back_inserter(memsize), ' '); + // convert to integer before returning + return std::stoi(memsize); + } + } + // If EOF and no info returned + std::string err = "MemTotal not found in /proc/meminfo."; + throw err; + + } else { + + std::string err = "Failed to open /proc/meminfo."; + throw err; + + } + +} diff --git a/src/linux/inventory.cpp b/src/linux/inventory.cpp index 1863056b..136de497 100644 --- a/src/linux/inventory.cpp +++ b/src/linux/inventory.cpp @@ -51,9 +51,16 @@ Json::Value get_inventory(){ inv["error"]["processor"] = err; } + try { + inv["data"]["memory"] = get_memory_size(); + } + catch ( std::string err ) { + inv["data"]["memory"] = Json::nullValue; + inv["error"]["memory"] = err; + } + // hw info /* - inv["memory"] = inv["disk1_model"] = inv["disk1_size"] = inv["disk1_used"] = -- GitLab