Skip to content
Snippets Groups Projects
Commit 5ffa1c10 authored by Clara Daia Hilgenberg Daru's avatar Clara Daia Hilgenberg Daru Committed by Clara Daia Hilgenberg Daru
Browse files

Issue #46: Create get_memory_size()

parent 3a694760
No related branches found
No related tags found
2 merge requests!51Merge development to master,!24Issue/46
#pragma once
#include <fstream>
#include <string>
#include "agent/linux/open_file.h"
#include <algorithm>
int get_memory_size();
......@@ -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"
......
/* 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;
}
}
......@@ -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"] =
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment