In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to monitor network card traffic in shell programming practice. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Recently, there is a slow phenomenon in the customer's website. Through the check, the reason is that the bandwidth is tight, so the customer increases the server bandwidth from the original 2m to 4m, and the speed of the website is improved at once. So how to monitor the network card traffic, linux has many commands to view. Such as iftop,sar and so on.
Here, I want to write a script to monitor the traffic of the network card. Count the traffic of the network card every minute and input it into the specified file. You can use sar-n DEV 1 59 to view the average Nic traffic within a minute.
The core of this script is to get the average traffic of the network card within 1 minute. Let's see what information the sar command outputs.
# sar-n DEV 1 59Linux 4.18.0-80.11.2.el8_0.x86_64 (30gk.com) 09 take 20 CPU 11:25:25 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s% ifutil11:25:26 AM eth0 16.00 11.00 1.26 1.38 0.00 0.00 0.00 0.0011 ifutil11:25:27 AM eth0 25 AM lo 26 12.00 12.00 4.77 4.77 0.00 0.00 0.00 0.0011 VR 25 ifutil11:25:27 AM eth0 26 ifutil11:25:27 AM eth0 18.00 23.00 2.98 28.85 0.00 0.00 0.00 0.0011 Portuguese 25 AM lo 12.00 12.00 4.77 4.77 0.00 0.00 0.00. Average: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s% ifutilAverage: eth0 8.56 12.59 1.07 7.05 0.00 0.00 0.00Average: lo 12.20 12.20 4.79 4.79 0.00 0.00 0.00
The information we need is the AVERAGE: eth0 line. Rxkb/s represents the number of bytes per second accepted by the network card, and rxkb/s represents the number of bytes sent per second, all in kb units. We will convert it to kbit/s later, because it is customary to use kbit/s to represent network bandwidth usage.
Then, we need to filter other unwanted lines, which can be easily done with the grep command. First grep filters out all rows that do not contain Average and then filters rows that do not contain eth0.
# sar-n DEV 1 59 | grep-I average | grep eth0Average: eth0 7.44 7.00 0.83 6.72 0.00 0.00 0.00
Then, using the awk command to filter out unwanted columns, we only need column 5 and column 6 information
# sar-n DEV 1 4 | grep-I average | grep eth0 | awk'{print $5x 8 "\ t" $6x 8} '28.32 182.96
So far, we have obtained the average input and output traffic of the eth0 network card within 1 minute, and the core problem of the script has been solved. The complete code is posted below:
#! / bin/bash# monitors the network card traffic and outputs it to the export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/binexport LANG=en# log storage directory in the log file, PATH_DIR=/root/netdatas/ `date +% F` # log file name FILE_NAME= `date +% F`.log # monitored network card name NET_NAME=eth0 [- d PATH_DIR] | | mkdir-p $PATH_DIR while [1] Do date + "% haug% 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_NAMEdone
The writing of the script is very simple, figuring out how to get the network card traffic within 1 minute and 90% of it. However, it should be noted that the name of the network card on my server is eth0, and your possible name is not the same as mine, so if you want to use this script, you need to change all the eth0 to the network card name on your server. Of course, you can pass the name of the network card as a parameter to the script, which is not done here for convenience (the main reason is that you don't want to check the name of the network card entered).
To verify that the script is working properly. After executing the script, check the contents of the log as follows:
12:15eth0 input:16.64kbit/seth0 output:16.72kbit/s#12:16eth0 input:16.72kbit/seth0 output:17.12kbit/s#12:17eth0 input:17.92kbit/seth0 output:17.6kbit/s
As you can see, this script works properly.
About shell programming practice how to monitor network card traffic is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.