Skip to content

Commit

Permalink
Overall statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Aug 13, 2021
1 parent 6af5911 commit 2d9a098
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
4 changes: 0 additions & 4 deletions lib/bc-stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,7 @@ bool bc_stats::update_net_info()
snprintf(iface_path, sizeof(iface_path), "%s%s/statistics/tx_packets", BC_DIR_NETWORK, iface.name.c_str());
if (load_file(iface_path, buffer, sizeof(buffer)) > 0) iface.pkts_sent = atol(buffer);

snprintf(iface_path, sizeof(iface_path), "%s%s/", BC_DIR_NETWORK, iface.name.c_str());
DIR *pIfaceDir = opendir(iface_path);

network_it it = _network.find(iface.name.c_str());

if (it != _network.end())
{
if (iface.bytes_recv > it->second.bytes_recv && it->second.bytes_recv > 0)
Expand Down
78 changes: 50 additions & 28 deletions server/bc-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,30 @@ bool api_session::handle_request(const std::string &request)

if (!url.compare(0, 10, "/stats/cpu"))
{
_api->get_cpu_stats(_tx_buffer);
std::string body;
_api->get_cpu_stats(body);
_api->create_http_response(_tx_buffer, body);
return true;
}
else if (!url.compare(0, 13, "/stats/memory"))
{
_api->get_memory_stats(_tx_buffer);
std::string body;
_api->get_memory_stats(body);
_api->create_http_response(_tx_buffer, body);
return true;
}
else if (!url.compare(0, 14, "/stats/network"))
{
_api->get_network_stats(_tx_buffer);
std::string body;
_api->get_network_stats(body);
_api->create_http_response(_tx_buffer, body);
return true;
}
else if (!url.compare(0, 14, "/stats/overall"))
{
std::string body;
_api->get_overall_stats(body);
_api->create_http_response(_tx_buffer, body);
return true;
}

Expand Down Expand Up @@ -481,6 +494,17 @@ int api_read_event(bc_events *events, bc_events::event_data *ev_data)
return -1;
}

void bc_api::create_http_response(std::string &outout, const std::string &body)
{
outout = std::string("HTTP/1.1 200 OK\r\n");
std_string_append(outout, "Access-Control-Allow-Origin: %s\r\n", "*");
std_string_append(outout, "User-Agent: bluechery/%s\r\n", __VERSION__);
std_string_append(outout, "Content-Type: %s\r\n", "application/json");
std_string_append(outout, "Cache-Control: %s\r\n", "no-cache");
std_string_append(outout, "Content-Length: %zu\r\n\r\n", body.length());
outout.append(body);
}

void bc_api::get_cpu_stats(std::string &outout)
{
bc_stats::cpu cpu;
Expand Down Expand Up @@ -519,12 +543,6 @@ void bc_api::get_cpu_stats(std::string &outout)
bc_stats::bc_u32_to_float(cpu.sum.hard_ints),
bc_stats::bc_u32_to_float(cpu.sum.io_wait));

outout = std::string("HTTP/1.1 200 OK\r\n");
std_string_append(outout, "Access-Control-Allow-Origin: %s\r\n", "*");
std_string_append(outout, "User-Agent: bluechery/%s\r\n", __VERSION__);
std_string_append(outout, "Content-Type: %s\r\n", "application/json");
std_string_append(outout, "Cache-Control: %s\r\n", "no-cache");
std_string_append(outout, "Content-Length: %zu\r\n\r\n", length);
outout.append(response, length);
}

Expand Down Expand Up @@ -557,22 +575,14 @@ void bc_api::get_memory_stats(std::string &outout)
meminfo.free
);

outout = std::string("HTTP/1.1 200 OK\r\n");
std_string_append(outout, "Access-Control-Allow-Origin: %s\r\n", "*");
std_string_append(outout, "User-Agent: bluechery/%s\r\n", __VERSION__);
std_string_append(outout, "Content-Type: %s\r\n", "application/json");
std_string_append(outout, "Cache-Control: %s\r\n", "no-cache");
std_string_append(outout, "Content-Length: %zu\r\n\r\n", length);
outout.append(response, length);
}

void bc_api::get_network_stats(std::string &outout)
{
bc_stats::network netstat;
_stats->get_net_info(&netstat);

std::string body;
body = std::string("[");
outout = std::string("[");

for (bc_stats::network_it it = netstat.begin(); it != netstat.end(); it++)
{
Expand Down Expand Up @@ -607,19 +617,31 @@ void bc_api::get_network_stats(std::string &outout)
iface.pkts_recv_per_sec,
iface.pkts_sent_per_sec);

body.append(buffer, length);
if (std::next(it) != netstat.end()) body.append(",");
outout.append(buffer, length);
if (std::next(it) != netstat.end()) outout.append(",");
}

body.append("]");
outout.append("]");
}

outout = std::string("HTTP/1.1 200 OK\r\n");
std_string_append(outout, "Access-Control-Allow-Origin: %s\r\n", "*");
std_string_append(outout, "User-Agent: bluechery/%s\r\n", __VERSION__);
std_string_append(outout, "Content-Type: %s\r\n", "application/json");
std_string_append(outout, "Cache-Control: %s\r\n", "no-cache");
std_string_append(outout, "Content-Length: %zu\r\n\r\n", body.length());
outout.append(body);
void bc_api::get_overall_stats(std::string &outout)
{
outout = std::string("{\"cpu\":");

std::string stats;
get_cpu_stats(stats);
outout.append(stats);
stats.clear();

outout.append(",\"memory\":");
get_memory_stats(stats);
outout.append(stats);
stats.clear();

outout.append(",\"network\":");
get_network_stats(stats);
outout.append(stats);
outout.append("}");
}

int api_write_event(bc_events *events, bc_events::event_data *ev_data)
Expand Down
3 changes: 3 additions & 0 deletions server/bc-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class bc_api {
void get_cpu_stats(std::string &outout);
void get_memory_stats(std::string &outout);
void get_network_stats(std::string &outout);
void get_overall_stats(std::string &outout);

void create_http_response(std::string &outout, const std::string &body);

private:
bc_stats* _stats;
Expand Down

0 comments on commit 2d9a098

Please sign in to comment.