Add Easy memtotal json
This commit is contained in:
parent
531c5a7a68
commit
cb944e5623
@ -82,7 +82,5 @@ Sample response
|
||||
"memtotal" : 16255116,
|
||||
"memfree" : 2127320,
|
||||
"memavailable" : 12324924,
|
||||
"swaptotal" : 0,
|
||||
"swapfree" : 0,
|
||||
}
|
||||
```
|
||||
|
@ -7,11 +7,11 @@
|
||||
#include "memory.hpp"
|
||||
|
||||
bool memory::getProcMem(crow::json::wvalue& ret){
|
||||
std::ifstream f ("/proc/meminfo");
|
||||
std::ifstream f (procmempath);
|
||||
std::string line;
|
||||
if(f.is_open()){
|
||||
while(std::getline(f, line)){
|
||||
size_t colon;
|
||||
size_t colon = -1;
|
||||
int last_space = -1;
|
||||
int final_space = -1;
|
||||
for(int i = 0; line[i] != '\0' ; ++i){
|
||||
@ -36,7 +36,7 @@ bool memory::getProcMem(crow::json::wvalue& ret){
|
||||
}
|
||||
|
||||
bool memory::getRawProcMem(std::string& ret){
|
||||
std::ifstream f ("/proc/meminfo");
|
||||
std::ifstream f (procmempath);
|
||||
std::string line;
|
||||
if(f.is_open()){
|
||||
while(std::getline(f, line)){
|
||||
@ -51,3 +51,37 @@ bool memory::getRawProcMem(std::string& ret){
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool memory::getEasyMem(crow::json::wvalue& ret){
|
||||
std::ifstream f (procmempath);
|
||||
std::string line;
|
||||
if(f.is_open()){
|
||||
for(int j = 0; j < 3; ++j){
|
||||
size_t colon = -1;
|
||||
int last_space = -1;
|
||||
int final_space = -1;
|
||||
std::getline(f, line);
|
||||
for(int i = 0; line[i] != '\0'; ++i){
|
||||
if(line[i] == ':')
|
||||
colon = i;
|
||||
else if(last_space == -1 && line[i-1] == ' ' && line[i] != ' ')
|
||||
last_space = i;
|
||||
else if(last_space != -1 && line[i] == ' ')
|
||||
final_space = i;
|
||||
}
|
||||
|
||||
ret[line.substr(0,colon)] = std::stof(line.substr(last_space, final_space - last_space));
|
||||
}
|
||||
f.close();
|
||||
} else {
|
||||
ret["message"] = "Failed to open proc filesystem";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool memory::getRawEasyMem(std::string& ret){
|
||||
std::ifstream f (procmempath);
|
||||
|
||||
}
|
||||
|
@ -11,8 +11,11 @@
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
constexpr char procmempath[] = "/proc/meminfo";
|
||||
|
||||
namespace memory{
|
||||
bool getProcMem(crow::json::wvalue&);
|
||||
bool getRawProcMem(std::string&);
|
||||
bool getEasyMem(crow::json::wvalue&);
|
||||
bool getRawEasyMem(std::string&);
|
||||
}
|
||||
|
@ -8,7 +8,6 @@
|
||||
|
||||
void setRoutes(crow::SimpleApp& app){
|
||||
CROW_ROUTE(app, "/proc/meminfo")([](const crow::request& req){
|
||||
crow::json::wvalue json;
|
||||
bool status;
|
||||
std::string accept = req.get_header_value("Accept");
|
||||
|
||||
@ -19,13 +18,29 @@ void setRoutes(crow::SimpleApp& app){
|
||||
status = memory::getRawProcMem(accept);
|
||||
return crow::response(status ? 200 : 503, accept);
|
||||
} else {
|
||||
crow::json::wvalue json;
|
||||
status = memory::getProcMem(json);
|
||||
return crow::response(status ? 200 : 503, json.dump());
|
||||
}
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/proc/uptime")([](const crow::request& req){
|
||||
CROW_ROUTE(app, "/mem")([](const crow::request& req){
|
||||
bool status;
|
||||
std::string accept = req.get_header_value("Accept");
|
||||
|
||||
std::transform(accept.begin(), accept.end(), accept.begin(), ::tolower);
|
||||
|
||||
if(accept == "text/plain"){
|
||||
accept.clear();
|
||||
} else {
|
||||
crow::json::wvalue json;
|
||||
status = memory::getEasyMem(json);
|
||||
return crow::response(status ? 200 : 503, json.dump());
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/proc/uptime")([](const crow::request& req){
|
||||
bool status;
|
||||
std::string accept = req.get_header_value("Accept");
|
||||
|
||||
@ -36,6 +51,7 @@ void setRoutes(crow::SimpleApp& app){
|
||||
status = state::getRawUptime(accept);
|
||||
return crow::response(status ? 200 : 503, accept);
|
||||
} else {
|
||||
crow::json::wvalue json;
|
||||
status = state::getUptime(json);
|
||||
return crow::response(status ? 200: 503, json.dump());
|
||||
}
|
||||
@ -47,8 +63,4 @@ void setRoutes(crow::SimpleApp& app){
|
||||
return ret;
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/mem")([]{
|
||||
return "mem";
|
||||
});
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user