目录
一、需求
在调试AS-V1000视频监控平台的时候,发现web客户端出现黑屏现象,看不到实时视频。如下图示:
怀疑是设备没有发过来码流,最简单的办法是查看一下平台的网络端口,每秒中能够接收到多少字节的数据,但是,使用ifconfig 来查看接口接收到的数据包,字节数2263311585148,过几秒变成2263312246204,用眼睛根本没法看。
有没有更好的办法?
二、代码实现
有不少命令可以查看到接口接收的数据包的情况,若想要知道一定时间内收到的数据包的数量,需要编写代码来实现。最快捷的方法应该就是shell脚本。
(一)查看接口接收的字节数的方法
在Linux中,可以使用多种方法来查看网络接口(如ens2f0)的接收字节数。以下是一些常用的方法:
1、使用ifconfig或ip命令
(1)使用ifconfig
命令如下:
ifconfig ens2f0 | grep RX
(2)使用ip
ip -s link show ens2f0 | grep RX
这将显示与ens2f0接口相关的接收(RX)统计信息。
2. 使用ss命令:
虽然ss主要用于查看套接字的统计信息,但它也可以用来查看网络接口的统计信息。
ss -s -i ens2f0
请注意,不是所有的ss版本都支持-i选项来指定接口。
3. 查看/sys/class/net/目录
Linux内核将网络接口的统计信息暴露在/sys/class/net/目录下。可以直接读取这些文件来获取统计信息。命令如下:
cat /sys/class/net/ens2f0/statistics/rx_bytes
这将显示ens2f0接口的接收字节数。
(二)代码
1、代码内容
下面直接列出Bash脚本的代码,如下:
#!/bin/bash
# 初始化变量来存储上一次读取的字节数
# 该变量用于在后续的计算中,作为上一次循环时的网络接收字节数基准
last_rx_bytes=0
# 使用无限循环来持续监测网络接收字节数的变化
while true; do
# 读取当前的网络接收字节数
# 从Linux系统的sysfs文件系统中获取eth0网卡的接收字节数
current_rx_bytes=$(cat /sys/class/net/eth0/statistics/rx_bytes)
# 如果这是第一次循环,即last_rx_bytes为0时
# 则不计算差值,仅进行初始化提示并设置last_rx_bytes的值为current_rx_bytes
if [ $last_rx_bytes -eq 0 ]; then
echo "Initializing, waiting for first 5-second interval..."
last_rx_bytes=$current_rx_bytes
else
# 计算两次读取之间的接收字节数差值
rx_bytes_diff=$((current_rx_bytes - last_rx_bytes))
# 将字节数差值转换为比特数(因为1字节 = 8比特)
rx_bits_diff=$((rx_bytes_diff * 8))
# 假设每次循环间隔为5秒(由后面的sleep 5决定)
# 计算每秒接收的比特数
rx_bits_1s=$((rx_bits_diff / 5))
# 将每秒接收的比特数转换为每秒接收的千比特数(因为1千比特 = 1024比特)
rx_kb_1s=$((rx_bits_1s / 1024))
# 输出在过去5秒内接收字节数的差值,以及对应的比特数和千比特数的速率
echo "Received bytes difference in the last 5 seconds: $rx_bytes_diff bytes, $rx_bits_1s bits/s ,$rx_kb_1s kbits/s"
# 更新上一次读取的字节数,以便在下一次循环中进行差值计算
last_rx_bytes=$current_rx_bytes
fi
# 等待5秒,然后再次执行循环,以监测新的网络接收字节数
sleep 5
done
2、功能概述
这个脚本主要用于持续监测Linux系统中eth0网卡在5秒时间间隔内的网络接收速率,并输出这个间隔中的接收字节数、比特数,以及平均到每秒的千比特数的速率变化。方便查看接口的接收数据的总的速率。
二、代码执行演示和效果
(一)代码变为可执行程序
1、编辑代码
在命令行模式,输入如下命令:
vi rx_packets.sh
把上一章的代码输入进去,然后保存后退出vi编辑器。
2、赋予代码可执行能力
在命令行模式,输入如下命令:
chmod +x rx_packets.sh
然后,检查文件rx_packets.sh是否变为可执行文件,如下:
[121yunwei@ecs-52a1 home]$ chmod +x rx_packets.sh
[121yunwei@ecs-52a1 home]$
[121yunwei@ecs-52a1 home]$
[121yunwei@ecs-52a1 home]$ ll rx_packets.sh
-rwxrwxr-x 1 121yunwei 121yunwei 996 May 13 21:57 rx_packets.sh
[121yunwei@ecs-52a1 home]$
可以看出文件rx_packets.sh具有x的属性,即可以执行。
(二)执行
1、正常运行的执行效果
执行情况如下所示:
[121yunwei@ecs-52a1 home]$ sh rx_packets.sh
Initializing, waiting for first 5-second interval...
Received bytes difference in the last 5 seconds: 631950 bytes, 1011120 bits/s ,987 kbits/s
Received bytes difference in the last 5 seconds: 694529 bytes, 1111246 bits/s ,1085 kbits/s
Received bytes difference in the last 5 seconds: 696553 bytes, 1114484 bits/s ,1088 kbits/s
Received bytes difference in the last 5 seconds: 612895 bytes, 980632 bits/s ,957 kbits/s
Received bytes difference in the last 5 seconds: 692274 bytes, 1107638 bits/s ,1081 kbits/s
Received bytes difference in the last 5 seconds: 699132 bytes, 1118611 bits/s ,1092 kbits/s
Received bytes difference in the last 5 seconds: 644509 bytes, 1031214 bits/s ,1007 kbits/s
Received bytes difference in the last 5 seconds: 681062 bytes, 1089699 bits/s ,1064 kbits/s
Received bytes difference in the last 5 seconds: 689201 bytes, 1102721 bits/s ,1076 kbits/s
Received bytes difference in the last 5 seconds: 644389 bytes, 1031022 bits/s ,1006 kbits/s
Received bytes difference in the last 5 seconds: 696258 bytes, 1114012 bits/s ,1087 kbits/s
Received bytes difference in the last 5 seconds: 698437 bytes, 1117499 bits/s ,1091 kbits/s
Received bytes difference in the last 5 seconds: 640932 bytes, 1025491 bits/s ,1001 kbits/s
Received bytes difference in the last 5 seconds: 695784 bytes, 1113254 bits/s ,1087 kbits/s
Received bytes difference in the last 5 seconds: 693660 bytes, 1109856 bits/s ,1083 kbits/s
Received bytes difference in the last 5 seconds: 637153 bytes, 1019444 bits/s ,995 kbits/s
Received bytes difference in the last 5 seconds: 684393 bytes, 1095028 bits/s ,1069 kbits/s
Received bytes difference in the last 5 seconds: 696590 bytes, 1114544 bits/s ,1088 kbits/s
Received bytes difference in the last 5 seconds: 635913 bytes, 1017460 bits/s ,993 kbits/s
Received bytes difference in the last 5 seconds: 687950 bytes, 1100720 bits/s ,1074 kbits/s
Received bytes difference in the last 5 seconds: 703094 bytes, 1124950 bits/s ,1098 kbits/s
Received bytes difference in the last 5 seconds: 639665 bytes, 1023464 bits/s ,999 kbits/s
可以看出,每秒接收基本上都在1000kbit左右,大概1Mbit/s的速率。
2、看实时视频的执行效果
(1)在监控平台的web客户端浏览实时视频的情况
(2)然后,继续观察,出现如下显示:
Received bytes difference in the last 5 seconds: 686822 bytes, 1098915 bits/s ,1073 kbits/s
Received bytes difference in the last 5 seconds: 1257522 bytes, 2012035 bits/s ,1964 kbits/s
Received bytes difference in the last 5 seconds: 2427538 bytes, 3884060 bits/s ,3793 kbits/s
Received bytes difference in the last 5 seconds: 2490649 bytes, 3985038 bits/s ,3891 kbits/s
Received bytes difference in the last 5 seconds: 2529845 bytes, 4047752 bits/s ,3952 kbits/s
Received bytes difference in the last 5 seconds: 2290396 bytes, 3664633 bits/s ,3578 kbits/s
Received bytes difference in the last 5 seconds: 2444005 bytes, 3910408 bits/s ,3818 kbits/s
Received bytes difference in the last 5 seconds: 2447885 bytes, 3916616 bits/s ,3824 kbits/s
(3)由此可以看出,每秒的速率变为3850Kbps左右,增加了2850 Kbps,也就是这路实时监控视频的码率在2850 Kbps左右。
然后在web客户端停止浏览这路监控视频,继续观察,结果如下:
Received bytes difference in the last 5 seconds: 2444005 bytes, 3910408 bits/s ,3818 kbits/s
Received bytes difference in the last 5 seconds: 2447885 bytes, 3916616 bits/s ,3824 kbits/s
Received bytes difference in the last 5 seconds: 648234 bytes, 1037174 bits/s ,1012 kbits/s
Received bytes difference in the last 5 seconds: 687798 bytes, 1100476 bits/s ,1074 kbits/s
Received bytes difference in the last 5 seconds: 691119 bytes, 1105790 bits/s ,1079 kbits/s
Received bytes difference in the last 5 seconds: 658126 bytes, 1053001 bits/s ,1028 kbits/s
Received bytes difference in the last 5 seconds: 677605 bytes, 1084168 bits/s ,1058 kbits/s
Received bytes difference in the last 5 seconds: 695507 bytes, 1112811 bits/s ,1086 kbits/s
Received bytes difference in the last 5 seconds: 654685 bytes, 1047496 bits/s ,1022 kbits/s
^C
[121yunwei@ecs-52a1 home]$
[121yunwei@ecs-52a1 home]$
显然,接口接收数据的速率有降低到1Mbit/s左右。
(三)结论
由上述执行的实际情况看,这个脚本基本反应了网络接口接收数据的实际情况,代码执行达到了预期的要求,满足最初的需求。
文章正下方可以看到我的联系方式:鼠标“点击” 下面的 “威迪斯特-就是video system 微信名片”字样,就会出现我的二维码,欢迎沟通探讨。