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