From 88f7aa611560ba938e7f10e451295f30058cdce6 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Thu, 28 Apr 2022 21:32:07 -0400 Subject: [PATCH] Add /proc/meminfo json functionality --- src/components/memory.cpp | 30 ++++++++++++++++++++++++++++++ src/components/memory.hpp | 17 +++++++++++++++++ src/routes.cpp | 5 ++++- src/routes.hpp | 2 ++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/components/memory.cpp create mode 100644 src/components/memory.hpp diff --git a/src/components/memory.cpp b/src/components/memory.cpp new file mode 100644 index 0000000..f8eb5b6 --- /dev/null +++ b/src/components/memory.cpp @@ -0,0 +1,30 @@ +/////////////////////////////////////////////////////////////////////////////// +// Tyler Perkins +// 28-4-22 +// memory functions implementation +// + +#include "memory.hpp" + +void memory::getProcMem(crow::json::wvalue& ret){ + std::ifstream f ("/proc/meminfo"); + std::string line; + if(f.is_open()){ + while(std::getline(f, line)){ + size_t colon; + int last_space = -1; + for(int i = 0; line[i] != '\0' ; ++i){ + if(line[i] == ':') + colon = i; + else if(line[i-1] == ' ' && line[i] != ' ' && last_space == -1) + 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"; + } +} diff --git a/src/components/memory.hpp b/src/components/memory.hpp new file mode 100644 index 0000000..a452830 --- /dev/null +++ b/src/components/memory.hpp @@ -0,0 +1,17 @@ +/////////////////////////////////////////////////////////////////////////////// +// Tyler Perkins +// 28-4-22 +// memory functions defintion +// + +#pragma once + +#include + +#include +#include + +namespace memory{ + void getProcMem(crow::json::wvalue&); + void getEasyMem(crow::json::wvalue&); +} diff --git a/src/routes.cpp b/src/routes.cpp index 74dbbc0..b6758d1 100644 --- a/src/routes.cpp +++ b/src/routes.cpp @@ -8,7 +8,10 @@ void setRoutes(crow::SimpleApp& app){ CROW_ROUTE(app, "/proc/meminfo")([]{ - return "Meminfo"; + crow::json::wvalue json; + memory::getProcMem(json); + + return json; }); CROW_ROUTE(app, "/mem")([]{ diff --git a/src/routes.hpp b/src/routes.hpp index 453f796..b6d1003 100644 --- a/src/routes.hpp +++ b/src/routes.hpp @@ -6,6 +6,8 @@ #pragma once + #include +#include "components/memory.hpp" void setRoutes(crow::SimpleApp&);