NIC Monitor Script

-------------------------------Main Program-------------------------------


Monitoring Script:

[root@config ~]# cd asptools/
[root@config asptools]# cat NIC-Monitor.sh

#!/bin/sh
#Author:zhai_kang,20131010
export PATH=$PATH:/sbin
Dest_Path=/var/log/Monitor
############################################NIC traffic monitoring##################################################
#Function-----Rate Units:k/s
NIC_Monitor()
{
        rx1=$(ifconfig $1|grep "RX bytes"|awk '{print $2}'|awk -F: '{print $2}')
        sleep 1
        rxa=$(($rx1 / 1024))
        rx2=$(ifconfig eth0|grep "RX bytes"|awk '{print $2}'|awk -F: '{print $2}')
        rxb=$(($rx2 / 1024))
        rx=$(($rxb-$rxa))
        if [[ $rx -lt 0 ]]
        then
                rx=`expr 0 - $rx`
        fi
        echo -e "$(date +%Y%m%d-%H:%M:%S)\t\t$i\t\t\t$rx"
}
##main program##
#Check the log directory exists and create
if [[ ! -d $Dest_Path ]]
then
        mkdir -p $Dest_Path
fi
#Log title
if [[ $(date +%H%M) == "0000" ]]
then
        echo -e "yyyymmdd-H:M:S\t\t\tInterface\t\tNIC-Flow(Units:k/s)" > $Dest_Path/NIC-Monitor-$(date +%Y%m%d).log
fi
#Monitoring data
sign=`ifconfig -s | grep -v lo | grep -v Iface | awk '{print $1}'`
for i in $sign
do
        NIC_Monitor $i  >> $Dest_Path/NIC-Monitor-$(date +%Y%m%d).log
done
exit


Cron job:
[root@config asptools]# crontab -l

*/1 00,8-16 * * * /root/asptools/NIC-Monitor.sh >> ~/asptools/log/nic.log 2>&1


See the log:
[root@config asptools]# ll log/
total 0
-rw-r--r-- 1 root root 0 Oct 10 17:59 nic.log


[root@config asptools]# cat /var/log/Monitor/NIC-Monitor-20131011.log
100918719.jpg




-------------------------------Syslog Server-------------------------------


Find the directory where the configuration file:
[root@config asptools]# cat /etc/logrotate.conf | grep -B 1 include

# RPM packages drop log rotation information into this directory
include     /etc/logrotate.d



Custom configuration file:
[root@config asptools]# cat /etc/logrotate.d/Monitor

/var/log/Monitor/*.log {
        compress
        notifempty
        rotate         6
        create         0400 root root
        olddir         /var/log/Monitor/old/
        sharedscripts
        weekly
}



Create a log backup directory:
[root@config asptools]# mkdir /var/log/Monitor/old


Manual testing:
[root@config asptools]# logrotate -f /etc/logrotate.d/Monitor





---------------------------------------------------------------------


For more information refer to:man logrotate



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章