Skip to content

Commit

Permalink
Add support for tracking total network traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
joshk committed Jan 24, 2025
1 parent 1076415 commit 15c9b61
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/nerves_hub_link/extensions/health/default_report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down

0 comments on commit 15c9b61

Please sign in to comment.