mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:27:45 +00:00
Kernel: Record network statistics and expose as JSON
This is comprised of five small changes: * Keep a counter for tx/rx packets/bytes per TCP socket * Keep a counter for tx/rx packets/bytes per network adapter * Expose that data in /proc/net_tcp and /proc/netadapters * Convert /proc/netadapters to JSON * Fix up ifconfig to read the JSON from netadapters
This commit is contained in:
parent
061c092fae
commit
7ed54d86d5
7 changed files with 88 additions and 20 deletions
|
@ -1,6 +1,21 @@
|
|||
#include <AK/AKString.h>
|
||||
#include <AK/JsonArray.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibCore/CFile.h>
|
||||
#include <stdio.h>
|
||||
|
||||
String si_bytes(unsigned bytes)
|
||||
{
|
||||
if (bytes >= GB)
|
||||
return String::format("%fGiB", (double)bytes / (double)GB);
|
||||
if (bytes >= MB)
|
||||
return String::format("%fMiB", (double)bytes / (double)MB);
|
||||
if (bytes >= KB)
|
||||
return String::format("%fKiB", (double)bytes / (double)KB);
|
||||
return String::format("%dB", bytes);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
UNUSED_PARAM(argc);
|
||||
|
@ -12,19 +27,28 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
auto line = file.read_line(1024);
|
||||
if (line.is_null())
|
||||
break;
|
||||
auto parts = String::copy(line, Chomp).split(',');
|
||||
if (parts.size() < 4)
|
||||
continue;
|
||||
printf("%s:\n", parts[0].characters());
|
||||
printf(" mac: %s\n", parts[2].characters());
|
||||
printf(" ipv4: %s\n", parts[3].characters());
|
||||
printf(" class: %s\n", parts[1].characters());
|
||||
auto file_contents = file.read_all();
|
||||
auto json = JsonValue::from_string(file_contents).as_array();
|
||||
json.for_each([](auto& value) {
|
||||
auto if_object = value.as_object();
|
||||
|
||||
auto name = if_object.get("name").to_string();
|
||||
auto class_name = if_object.get("class_name").to_string();
|
||||
auto mac_address = if_object.get("mac_address").to_string();
|
||||
auto ipv4_address = if_object.get("ipv4_address").to_string();
|
||||
auto packets_in = if_object.get("packets_in").to_u32();
|
||||
auto bytes_in = if_object.get("bytes_in").to_u32();
|
||||
auto packets_out = if_object.get("packets_out").to_u32();
|
||||
auto bytes_out = if_object.get("bytes_out").to_u32();
|
||||
|
||||
printf("%s:\n", name.characters());
|
||||
printf(" mac: %s\n", mac_address.characters());
|
||||
printf(" ipv4: %s\n", ipv4_address.characters());
|
||||
printf(" class: %s\n", class_name.characters());
|
||||
printf(" RX: %u packets %u bytes (%s)\n", packets_in, bytes_in, si_bytes(bytes_in).characters());
|
||||
printf(" TX: %u packets %u bytes (%s)\n", packets_out, bytes_out, si_bytes(bytes_out).characters());
|
||||
printf("\n");
|
||||
}
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue