diff --git a/include/agent/linux/get_memory_size.h b/include/agent/linux/get_memory_size.h new file mode 100644 index 0000000000000000000000000000000000000000..1dc19d0110fd6ed7e6bfe541a1fc59ef5cc3c6a8 --- /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 b57d6f45d5969a1b8ec5dc406485329ca8d005c1..7ed15a14c1060705a6134e402f3bdbdae5c4cdb2 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 0000000000000000000000000000000000000000..df66e963cf8494f87d5c9259e7073c9e58f3d0a1 --- /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 1863056bb7825d78bb3d32411bbc6c0920243f51..136de49733455db3d2bfadcc07faebfca5667ab6 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"] =