V
V
Vitaly2019-02-17 17:17:41
linux
Vitaly, 2019-02-17 17:17:41

How to get statistics on the network interface (/proc/net/dev) using sysctl?

Using the sysctl function from libc, you can get information from the kernel. What hierarchy of names should be in order to get statistical data on a network interface? Interested in the number of packets and bytes received by the interface.
This information is available in the "proc" - /proc/net/dev, but I'm trying to get the same through a sysctl() call.
I found information only on FreeBSD:

static int name[] = { CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT };

But that doesn't work for Linux.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2019-02-17
@iamFake

In general, I did not find a way to implement this through sysctl (or did not find the appropriate documentation), but with a hint, I was still able to implement the required one. Everything is solved through a function getifaddrsthat returns interfaces in the system by issuing separately AF_INET and AF_PACKET families for each real interface. A record with type AF_PACKET just contains the bytes and packets sent and received.
man7.org/linux/man-pages/man3/getifaddrs.3.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question