Add more robust error handling to /proc/meminfo

This commit is contained in:
Tyler Perkins 2022-04-28 22:12:38 -04:00
parent 88f7aa6115
commit e8d3b5f2d8
3 changed files with 29 additions and 8 deletions

View File

@ -6,7 +6,7 @@
#include "memory.hpp"
void memory::getProcMem(crow::json::wvalue& ret){
bool memory::getProcMem(crow::json::wvalue& ret){
std::ifstream f ("/proc/meminfo");
std::string line;
if(f.is_open()){
@ -20,11 +20,27 @@ void memory::getProcMem(crow::json::wvalue& ret){
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";
return false;
}
return true;
}
bool memory::getRawProcMem(std::string& ret){
std::ifstream f ("/proc/meminfo");
std::string line;
if(f.is_open()){
while(std::getline(f, line)){
ret += line;
}
} else {
ret = "Failed to open proc filesystem";
return false;
}
return true;
}

View File

@ -12,6 +12,7 @@
#include <fstream>
namespace memory{
void getProcMem(crow::json::wvalue&);
void getEasyMem(crow::json::wvalue&);
bool getProcMem(crow::json::wvalue&);
bool getRawProcMem(std::string&);
bool getEasyMem(crow::json::wvalue&);
}

View File

@ -7,11 +7,15 @@
#include "routes.hpp"
void setRoutes(crow::SimpleApp& app){
CROW_ROUTE(app, "/proc/meminfo")([]{
CROW_ROUTE(app, "/proc/meminfo")([](const crow::request& req){
crow::json::wvalue json;
memory::getProcMem(json);
return json;
std::cerr << "Accept: " << req.get_header_value("Accept") << "\n";
if(memory::getProcMem(json))
return crow::response(200, json.dump());
else
return crow::response(503, json.dump());
});
CROW_ROUTE(app, "/mem")([]{