Add uptime route
This commit is contained in:
parent
b8df32111d
commit
f49c557f82
36
src/components/state.cpp
Normal file
36
src/components/state.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Tyler Perkins
|
||||
// 29-4-22
|
||||
// System functions implementation
|
||||
//
|
||||
|
||||
#include "state.hpp"
|
||||
|
||||
bool state::getUptime(crow::json::wvalue& ret){
|
||||
std::ifstream f ("/proc/uptime");
|
||||
std::string line;
|
||||
if(f.is_open()){
|
||||
int space = -1;
|
||||
std::getline(f, line);
|
||||
|
||||
for(int i = 0; line[i] != '\0'; ++i){
|
||||
if(line[i] == ' '){
|
||||
space = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ret["uptime"] = std::stof(line.substr(0, space));
|
||||
ret["idle"] = std::stof(line.substr(space+1));
|
||||
|
||||
} else {
|
||||
ret["message"] = "Failed to open proc filesystem";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool state::getRawUptime(std::string& ret){
|
||||
return true;
|
||||
}
|
17
src/components/state.hpp
Normal file
17
src/components/state.hpp
Normal file
@ -0,0 +1,17 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Tyler Perkins
|
||||
// 29-4-22
|
||||
// System functions definitions
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <crow.h>
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
namespace state{
|
||||
bool getUptime(crow::json::wvalue&);
|
||||
bool getRawUptime(std::string&);
|
||||
}
|
@ -24,7 +24,17 @@ void setRoutes(crow::SimpleApp& app){
|
||||
}
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/proc/uptime")([](){
|
||||
crow::json::wvalue json;
|
||||
bool status;
|
||||
|
||||
status = state::getUptime(json);
|
||||
return crow::response(status ? 200: 503, json.dump());
|
||||
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/mem")([]{
|
||||
return "mem";
|
||||
});
|
||||
|
||||
}
|
||||
|
@ -11,5 +11,6 @@
|
||||
|
||||
#include <crow.h>
|
||||
#include "components/memory.hpp"
|
||||
#include "components/state.hpp"
|
||||
|
||||
void setRoutes(crow::SimpleApp&);
|
||||
|
Loading…
Reference in New Issue
Block a user