这几天要写一个监控之类东东,其中网卡一项要计算利用率,那就要取得网卡本身速度才能计算出来,本来想用perl实现,但发现网上没有现成的东东,后来几经辗转,最后想起ethtool能取到,就参考了此源码,贴出来供大家以后有个思路吧,
有时间再转成perl的:)
直接编译命令:gcc -p -g getNet.c && gcc -o getNet getNet.c && ./getNet eth0
源码如下:
// filename: getNet.c
// command sample: ./getNet eth0
// compile command: gcc -p -g getNet.c && gcc -o getNet getNet.c
#include <string.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#ifndef SIOCETHTOOL
#define SIOCETHTOOL 0x8946
#endif
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif
/* CMDs currently supported */
#define ETHTO

该博客介绍了如何在Linux环境中使用C语言通过ethtool工具来获取网卡的速度,以实现监控网卡利用率。作者分享了相关源码,并提供了编译和运行的命令,帮助读者理解并应用到自己的项目中。
最低0.47元/天 解锁文章
1087

被折叠的 条评论
为什么被折叠?



