监控每秒流量的脚本

#!/bin/bash
pwd=$(pwd)
cata=$pwd/wangyu
wen=$cata/wen.log
eth=$1
if [ ! -d $cata ];then
mkdir $cata
fi
while ((1))
do
q_jdytes=$(ifconfig $eth|grep RX|grep bytes|awk -F " " '{print $5}')
q_cdytes=$(ifconfig $eth|grep TX|grep bytes|awk -F " " '{print $5}')
q_jpackets=$(ifconfig $eth|grep RX|grep bytes|awk -F " " '{print $3}')
q_cpackets=$(ifconfig $eth|grep TX|grep bytes|awk -F " " '{print $3}')
q_jerrors=$(ifconfig $eth|grep RX|grep errors|awk -F " " '{print $3}')
q_cerrors=$(ifconfig $eth|grep TX|grep errors|awk -F " " '{print $3}')
q_jdrop=$(ifconfig $eth|grep RX|grep errors|awk -F " " '{print $5}')
q_cdrop=$(ifconfig $eth|grep TX|grep errors|awk -F " " '{print $5}')
sleep 1
f_jdytes=$(ifconfig $eth|grep RX|grep bytes|awk -F " " '{print $5}')
f_cdytes=$(ifconfig $eth|grep TX|grep bytes|awk -F " " '{print $5}')
f_jpackets=$(ifconfig $eth|grep RX|grep bytes|awk -F " " '{print $3}')
f_cpackets=$(ifconfig $eth|grep TX|grep bytes|awk -F " " '{print $3}')
f_jerrors=$(ifconfig $eth|grep RX|grep errors|awk -F " " '{print $3}')
f_cerrors=$(ifconfig $eth|grep TX|grep errors|awk -F " " '{print $3}')
f_jdrop=$(ifconfig $eth|grep RX|grep errors|awk -F " " '{print $5}')
f_cdrop=$(ifconfig $eth|grep TX|grep errors|awk -F " " '{print $5}')
a=$(($f_jdytes-$q_jdytes))
w_jdytes=`echo "$a 1024"|awk '{printf ("%.2f\n",($1/$2))}'`
b=$(($f_cdytes - $q_cdytes))
w_cdytes=`echo "$b 1024"|awk '{printf ("%.2f\n",($1/$2))}'`
w_jpackets=$(($f_jpackets-$q_jpackets))
w_cpackets=$(($f_cpackets-$q_cpackets))
w_jerrors=$(($f_jerrors-$q_jerrors))
w_cerrors=$(($f_cerrors-$q_cerrors))
w_jdrop=$(($f_jdrop-$q_jdrop))
w_cdrop=$(($f_cdrop-$q_cdrop))
cpu=$(top -n 2 |grep Cpu|awk -F "," '{print $2}'|awk -F "." '{print $1}'|tail -n 1)
used=$(top -n 2|grep Mem|head -n 1|awk -F "," '{print $3}'|awk '{print $2}'|tail -n 1)
total=$(top -n 2|grep Mem|head -n 1|awk -F "," '{print $1}'|awk '{print $4}'|tail -n 1)
mem=$(($used * 100 / $total))
zcpuname=$(ps aux | sort -k3 -rn | head -n 1 | awk '{print $1}')
zcpuid=$(ps aux | sort -k3 -rn | head -n 1 | awk '{print $2}')
zcpu=$(ps aux|sort -k3 -rn|head -n 1|awk '{print $3}')
zmemname=$(ps aux|sort -k4 -rn|head -n 1|awk '{print $1}')
zmemid=$(ps aux|sort -k4 -rn|head -n 1|awk '{print $2}')
zmem=$(ps aux|sort -k4 -rn|head -n 1|awk '{print $4}')
date=$(date "+%Y-%m-%d-%H:%M:%S")
echo $date  R_dytes $w_jdytes R_packets $w_jpackets R_errors $w_jerrors R_drop $w_jdrop T_dytes $w_cdytes T_packets $w_cpackets T_errors $w_cerrors T_drop $w_cdrop CPU $cpu MEM $mem cpu_name $zcpuname id $zcpuid cpu $zcpu% mem_name $zmemname id $zmemid mem $zmem%  >>$wen
sleep 5
done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值