#!/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
监控每秒流量的脚本
最新推荐文章于 2023-01-14 15:54:19 发布