shell 监控服务器联网状态

-rw-r--r-- 1 root root 1037 04-20 15:26 monitor_adsl_connect.log

-rw-r--r-- 1 root root 1876 04-20 15:05 monitor_adsl_connect.sh-rw-r--r-- 1 root root 1664 04-20 14:39 monitor_adsl_connect.sh.bak-rw-r--r-- 1 root root 122 04-20 15:26 monitor.pid-rw-r--r-- 1 root root 300 04-20 15:12 status.log

#!/bin/bash
#filename monitor_adsl_connect
#usg sh /bin/sh /root/monitor_adsl_connect/monitor_adsl_connect.sh
export LANG=zh_CN.UTF-8

eval `cat /root/monitor_adsl_connect/monitor.pid |grep pid | awk -F '=' '{print "pid="$2}'`  #得到进程文件中的进程pid值
if [ ! -z "$pid" ];                     #如果pid不为空,说明有进程正在执行,退出程序
then 
        eval `ps aux | grep $pid | grep -v grep | awk '{print "p_id="$2}'`
        if [ "$pid" = "$p_id" ];
        then 
                #echo "pid="$pid "正在运行,程序退出"
                exit
        fi
fi
echo pid=$$ > /root/monitor_adsl_connect/monitor.pid
file=/root/monitor_adsl_connect/monitor_adsl_connect.log        #输出文件
eval `/sbin/adsl-status /etc/sysconfig/network-scripts/ifcfg-ppp0 | grep adsl-status | awk -F ":" '{print $2}' | awk '{print "status="$5}'`     #得到adsl的状态值
flag="running"
if [ "$status" = "$flag" ];     #如果状态值是 running ,则说明正在运行,ok,设置sta 为 running
then 
        sta="running"
else
        sta="down and start"

        /sbin/adsl-status /etc/sysconfig/network-scripts/ifcfg-ppp0 >> /root/monitor_adsl_connect/status.log

        /sbin/adsl-start /etc/sysconfig/network-scripts/ifcfg-ppp0      #start adsl

        while(( 1 ))
        do
                eval `/sbin/adsl-status /etc/sysconfig/network-scripts/ifcfg-ppp0 | grep adsl-status | awk -F ":" '{print $2}' | awk '{print "status="$5}'`

                /sbin/adsl-status /etc/sysconfig/network-scripts/ifcfg-ppp0 >> /root/monitor_adsl_connect/status.log

                if [ "$status" = "$flag" ];
                then
                        break
                fi
        done
fi
eval `/sbin/adsl-status /etc/sysconfig/network-scripts/ifcfg-ppp0 | grep inet | awk '{print "ip="$2}'`  #get ip 
eval `date | awk '{print "year="$1,"month="$2,"day="$3,"time="$5}'`     #get time
echo -e "$year $month $day $time\t$sta\t$ip">>$file                     #output to file
echo "pid=" > /root/monitor_adsl_connect/monitor.pid  #清空进程pid
env >> /root/monitor_adsl_connect/monitor.pid

定时运行:每隔一分钟运行一次,

[root@centos200 monitor_adsl_connect]# crontab -l
0-59/1 * * * * /bin/sh /root/monitor_adsl_connect/monitor_adsl_connect.sh
 

相关推荐