Home  >  Article  >  Operation and Maintenance  >  Shell programming practice monitoring network card traffic

Shell programming practice monitoring network card traffic

齐天大圣
齐天大圣Original
2020-09-20 12:22:091538browse

最近客户的网站出现卡慢现象,通过检查,出问题是原因是因为带宽吃紧,所以让客户将服务器带宽从原本的2M增加到4M的带宽,网站速度一下就提升了。那么如何监控网卡流量情况,linux有许多命令可以查看。比如iftop,sar等等。

这里,我想写一个监控网卡流量情况的脚本。统计每一分钟的网卡流量情况,输入到指定文件中。可以使用sar -n DEV 1 59查看一分钟内网卡平均流量。

此脚本的核心是获取1分钟内网卡的平均流量。我们来看看sar这个命令会输出哪些信息吧。

# sar -n DEV 1 59
Linux 4.18.0-80.11.2.el8_0.x86_64 (30gk.com)  09/20/2020  _x86_64_ (4 CPU)
11:25:25 AM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
11:25:26 AM      eth0     16.00     11.00      1.26      1.38      0.00      0.00      0.00      0.00
11:25:26 AM        lo     12.00     12.00      4.77      4.77      0.00      0.00      0.00      0.00
11:25:26 AM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
11:25:27 AM      eth0     18.00     23.00      2.98     28.85      0.00      0.00      0.00      0.00
11:25:27 AM        lo     12.00     12.00      4.77      4.77      0.00      0.00      0.00      0.00
……
Average:        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
Average:         eth0      8.56     12.59      1.07      7.05      0.00      0.00      0.00      0.00
Average:           lo     12.20     12.20      4.79      4.79      0.00      0.00      0.00      0.00

我们需要的信息是AVERAGE: eth0这行。rxkb/s表示网卡每秒接受的字节数,rxkb/s表示每秒发送的字节数,他们的单位都是kb。我们待会要将它转换为kbit/s,因为日常习惯用kbit/s表示网络带宽使用情况。

然后,我们需要过滤其他不需要的行,使用grep命令可以轻松办到。首先grep过滤掉所有不含Average的行,然后过滤不含eth0的行。

# sar -n DEV 1 59 | grep -i average | grep eth0
Average:         eth0      7.44      7.00      0.83      6.72      0.00      0.00      0.00      0.00

然后,使用awk命令过滤掉不需要的列,我们只需要第5列以及第6列信息;

# sar -n DEV 1 4 | grep -i average | grep eth0 | awk '{print $5*8"\t"$6*8}'
28.32 182.96

至此,我们已经获取到了1分钟内eth0网卡的平均输入输出流量了,该脚本的核心问题解决了。下面贴出完整的代码:

#!/bin/bash
# 监控网卡流量,并输出到日志文件中

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/bin
export LANG=en

# 日志存放目录
PATH_DIR=/root/netdatas/`date +%F`
# 日志文件名称
FILE_NAME=`date +%F`.log
# 监控的网卡名称
NET_NAME=eth0
 
[ -d PATH_DIR ] || mkdir -p $PATH_DIR
 
while [ 1 ];do
    date +"%H:%M" >> $PATH_DIR/$FILE_NAME
 
    netinfo=$(sar -n DEV 1 59 | grep -i average | grep $NET_NAME | \
        awk '{print $5*8":"$6*8}' )
    echo  "$NET_NAME input:$(echo $netinfo | cut -d: -f1)kbit/s" >> $PATH_DIR/$FILE_NAME
    echo "$NET_NAME output:$(echo $netinfo | cut -d: -f2)kbit/s" >> $PATH_DIR/$FILE_NAME
    echo '#########################' >> $PATH_DIR/$FILE_NAME
done

该脚本的编写非常的简单,搞清楚了如何获取1分钟内网卡流量就搞定了90%了。不过有点要注意,我服务器上的网卡名称为eth0,你的可能名称和我不一样,所以如果你要使用该脚本,需要将所有的eth0改为你服务器上的网卡名。当然,可以将网卡名称当做参数来传递给脚本,这里为了图方便就没有这么做了(主要原因是不想对输入的网卡名做校验)。

来验证下此脚本是否能正常运行。执行该脚本后,查看下日志里的内容如下:

12:15
eth0 input:16.64kbit/s
eth0 output:16.72kbit/s
#########################
12:16
eth0 input:16.72kbit/s
eth0 output:17.12kbit/s
#########################
12:17
eth0 input:17.92kbit/s
eth0 output:17.6kbit/s

可以看到,此脚本能正常运行。

The above is the detailed content of Shell programming practice monitoring network card traffic. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn