From 15c9b61f433f0fd24fc70ec99d0ebeb9a8d12e99 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Fri, 24 Jan 2025 16:39:01 +1300 Subject: [PATCH] Add support for tracking total network traffic --- .../extensions/health/default_report.ex | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/nerves_hub_link/extensions/health/default_report.ex b/lib/nerves_hub_link/extensions/health/default_report.ex index 83a26d5..5eb562e 100644 --- a/lib/nerves_hub_link/extensions/health/default_report.ex +++ b/lib/nerves_hub_link/extensions/health/default_report.ex @@ -40,7 +40,8 @@ defmodule NervesHubLink.Extensions.Health.DefaultReport do cpu_utilization(), load_averages(), memory(), - disk() + disk(), + network_traffic() ] |> Enum.reduce(%{}, &Map.merge/2) end @@ -213,6 +214,31 @@ defmodule NervesHubLink.Extensions.Health.DefaultReport do end end + defp network_traffic() do + {output, _} = System.cmd("cat", ["/proc/net/dev"]) + + [_top, _header | data] = String.split(output, "\n") + + {_, data} = List.pop_at(data, -1) + + data + |> Enum.map(fn line -> + [interface, bytes_received, _, _, _, _, _, _, _, bytes_sent | _] = String.split(line) + + %{ + interface: String.replace(interface, ":", ""), + bytes_received_total: bytes_received, + bytes_sent_total: bytes_sent + } + end) + |> Enum.reject(fn %{interface: interface} -> + interface == "lo" + end) + rescue + _ -> + %{} + end + defp vintage_net() do case Application.ensure_loaded(:vintage_net) do :ok ->