macos 如何在 linux/Mac OSX 中获取网络适配器统计信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1126790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:16:31  来源:igfitidea点击:

How to get network adapter stats in linux/Mac OSX?

clinuxmacosapinetworking

提问by Stefan

I'm looking for a way to get hold of network stats in C on Linux and MacOSX. Specifically, I need to monitor the number of bytes uploaded and downloaded from each network adapter on the system - I don't need to do packet inspection, or differentiate between protocols, just a 'total bytes' counter which I can poll at intervals would be fine. In Windows I can do this using the iphlpapi.dll library via GetIfTable (to list the network adapters) and GetIfEntry (to read the stats), but I can't find the Linux/OSX equivalents. My knowledge of C is fairly basic so I would appreciate a solution that isn't too involved. Any help would be much appreciated!

我正在寻找一种在 Linux 和 MacOSX 上使用 C 语言获取网络统计信息的方法。具体来说,我需要监控从系统上的每个网络适配器上传和下载的字节数 - 我不需要进行数据包检查或区分协议,只需一个“总字节数”计数器,我可以每隔一段时间轮询一次没事的。在 Windows 中,我可以通过 GetIfTable(列出网络适配器)和 GetIfEntry(读取统计信息)使用 iphlpapi.dll 库来执行此操作,但我找不到 Linux/OSX 等效项。我对 C 的了解是相当基础的,所以我希望有一个不太复杂的解决方案。任何帮助将非常感激!

回答by Stefan

The Darwin netstat source code uses sysctl. Here's some code that prints the number of bytes in and out on OSX:

Darwin netstat 源代码使用 sysctl。下面是一些在 OSX 上打印进出字节数的代码:

#import <Foundation/Foundation.h>
#include <sys/sysctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/route.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int mib[] = {
        CTL_NET,
        PF_ROUTE,
        0,
        0,
        NET_RT_IFLIST2,
        0
    };
    size_t len;
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        fprintf(stderr, "sysctl: %s\n", strerror(errno));
        exit(1);
    }
    char *buf = (char *)malloc(len);
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        fprintf(stderr, "sysctl: %s\n", strerror(errno));
        exit(1);
    }
    char *lim = buf + len;
    char *next = NULL;
    u_int64_t totalibytes = 0;
    u_int64_t totalobytes = 0;
    for (next = buf; next < lim; ) {
        struct if_msghdr *ifm = (struct if_msghdr *)next;
        next += ifm->ifm_msglen;
        if (ifm->ifm_type == RTM_IFINFO2) {
            struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm;
            totalibytes += if2m->ifm_data.ifi_ibytes;
            totalobytes += if2m->ifm_data.ifi_obytes;
        }
    }
    printf("total ibytes %qu\tobytes %qu\n", totalibytes, totalobytes);
    [pool drain];
    return 0;
}

回答by Javier

on Linux:

在 Linux 上:

  • low level: check /sys/class/net/eth0/statistics/
  • slightly higher level: ip -s link show eth0
  • graphical: iftop
  • interactive: iptraf
  • 低水平:检查 /sys/class/net/eth0/statistics/
  • 稍微高一点: ip -s link show eth0
  • 图形:iftop
  • 互动:iptraf

回答by Duck

I can't speak to OSX but on linux take a look at /proc/net/dev.

我无法与 OSX 交谈,但在 linux 上查看 /proc/net/dev。

If you do 'cat /proc/net/dev' you should see statistics including 'bytes' - the total number of bytes of data transmitted or received by the interface. You can read the file within your own program.

如果您执行“cat /proc/net/dev”,您应该会看到包括“字节”在内的统计信息——接口传输或接收的数据总字节数。您可以在自己的程序中读取该文件。

EDIT:

编辑:

I didn't read your whole question. This articleshould help you get started with /proc and has a section on /proc/net/dev.

我没有阅读你的整个问题。 这篇文章应该可以帮助您开始使用 /proc 并且有一个关于 /proc/net/dev 的部分。

Also, to list the interfaces you can call ioctlwith the SIOCGIFCONFoption. You can Google for a decent code example on how to loop through the returned data. Or you can simply pull it out of the /proc.net/dev data mentioned above, which should be easier.

此外,要列出接口,您可以使用SIOCGIFCONF选项调用ioctl。您可以在 Google 上搜索有关如何遍历返回数据的体面代码示例。或者你可以简单地从上面提到的/proc.net/dev 数据中提取它,这样应该更容易。