diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index e01bc2d..ff6e46b 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -82,7 +82,5 @@ Sample response "memtotal" : 16255116, "memfree" : 2127320, "memavailable" : 12324924, - "swaptotal" : 0, - "swapfree" : 0, } ``` diff --git a/src/components/memory.cpp b/src/components/memory.cpp index ae69f4d..cb0f1cf 100644 --- a/src/components/memory.cpp +++ b/src/components/memory.cpp @@ -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); + +} diff --git a/src/components/memory.hpp b/src/components/memory.hpp index 3d55e97..32b0985 100644 --- a/src/components/memory.hpp +++ b/src/components/memory.hpp @@ -11,8 +11,11 @@ #include #include +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&); } diff --git a/src/routes.cpp b/src/routes.cpp index d1756a8..daadbc9 100644 --- a/src/routes.cpp +++ b/src/routes.cpp @@ -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, "/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){ - crow::json::wvalue json; 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"; - }); - }