Add more robust error handling to /proc/meminfo
This commit is contained in:
parent
88f7aa6115
commit
e8d3b5f2d8
@ -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;
|
||||
}
|
||||
|
@ -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&);
|
||||
}
|
||||
|
@ -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")([]{
|
||||
|
Loading…
Reference in New Issue
Block a user