以下shell 脚本,可以监测内存使用量、内存的剩余量,以及网卡的流量。
代码:
#!/bin/bash
#program:
#监测网卡流量与内存使用情况
#filename:memory_inet.sh
if [ "$1" == "" ];then
echo "格式为‘脚本名 网卡’,请重新执行!" && exit 0
fi
while true
do
eth=$1
RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | linuxjishu/13830.html target=_blank class=infotextkey>awk '{print $2}')
TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
sleep 1
RXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
TXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
clear
echo -e "t `date +%k:%M:%S` "
RX=$((${RXnext}-${RXpre}))
TX=$((${TXnext}-${TXpre}))
if [[ $RX -lt 1024 ]];then
RX="${RX}B/s"
elif [[ $RX -gt 1048576 ]];then
RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
else
RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
fi
if [[ $TX -lt 1024 ]];then
TX="${TX}B/s"
elif [[ $TX -gt 1048576 ]];then
TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
else
TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
fi
echo -e "$eth t 下载速率:$RX 上传速率:$TX "
usemem=$(free -h | grep "cache:" | awk '{print $3}')
idlemem=$(free -h | grep "cache:" | awk '{print $4}')
echo " 已使用内存:$usemem 剩余内存:$idlemem"
done