Add /proc/meminfo json functionality

This commit is contained in:
Tyler Perkins 2022-04-28 21:32:07 -04:00
parent 96d9c1b759
commit 88f7aa6115
4 changed files with 53 additions and 1 deletions

30
src/components/memory.cpp Normal file
View File

@ -0,0 +1,30 @@
///////////////////////////////////////////////////////////////////////////////
// Tyler Perkins
// 28-4-22
// memory functions implementation
//
#include "memory.hpp"
void memory::getProcMem(crow::json::wvalue& ret){
std::ifstream f ("/proc/meminfo");
std::string line;
if(f.is_open()){
while(std::getline(f, line)){
size_t colon;
int last_space = -1;
for(int i = 0; line[i] != '\0' ; ++i){
if(line[i] == ':')
colon = i;
else if(line[i-1] == ' ' && line[i] != ' ' && last_space == -1)
last_space = i;
}
std::cerr << colon << " " << last_space << std::endl;
ret[line.substr(0,colon)] = line.substr(last_space);
}
} else {
ret["message"] = "Failed to open proc filesystem";
}
}

17
src/components/memory.hpp Normal file
View File

@ -0,0 +1,17 @@
///////////////////////////////////////////////////////////////////////////////
// Tyler Perkins
// 28-4-22
// memory functions defintion
//
#pragma once
#include <crow.h>
#include <string>
#include <fstream>
namespace memory{
void getProcMem(crow::json::wvalue&);
void getEasyMem(crow::json::wvalue&);
}

View File

@ -8,7 +8,10 @@
void setRoutes(crow::SimpleApp& app){
CROW_ROUTE(app, "/proc/meminfo")([]{
return "Meminfo";
crow::json::wvalue json;
memory::getProcMem(json);
return json;
});
CROW_ROUTE(app, "/mem")([]{

View File

@ -6,6 +6,8 @@
#pragma once
#include <crow.h>
#include "components/memory.hpp"
void setRoutes(crow::SimpleApp&);