Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tracking total network traffic #267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 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,38 @@ 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)
|> Enum.map(fn data ->
%{
"#{data.interface}_bytes_received_total" => data.bytes_received_total,
"#{data.interface}_bytes_sent_total" => data.bytes_sent_total
}
end)
|> Enum.reduce(%{}, &Map.merge/2)
rescue
_ ->
%{}
end

defp vintage_net() do
case Application.ensure_loaded(:vintage_net) do
:ok ->
Expand Down