使用飞信监控服务器是否在线或宕机及相应服务端口状态监控的shell

5207阅读 0评论2012-08-09 fengzhanhai
分类:LINUX

本文主要描述并实现了如何在unix主机下使用shell实现主机存活监控及端口存活监控功能。

#!/bin/bash

#Author by fengzhanhai

#It used by mon web service and net and host living

#It was created on 2012-8-9

#webdate=`date -d "today" +"%Y-%m-%d %H:%M:%S"`

webdate=`date -d "today" +"%H:%M:%S"`

#记录系统故障发生时间点

#echo $webdate

webinfo="The service's 80 port error!"

Hostinfo="The mon has been checked  your PCserver power off or Disaster"

#记录检查到的故障现象 此处由于笔者改了好几版 暂时没有用到

myphone=youphone

mypwd=yourpassword

#你的飞信号及口令

sendsmsweb() {

           cd /root/mon

           for iphone in `cat smspphone`

           do

           #echo $iphone

           LD_LIBRARY_PATH=. ./fetion --mobile=$myphone --pwd=$mypwd --to=$iphone --msg-utf8="$webinfo $webdate" >>smsp.log

           done

                                          

          }

#发送web类故障告警短信模块 此处笔者在调用时直接套用没有引用之

sendsmshost(){

           cd /root/mon

           for iphone in `cat smspphone`

                       do               

                       LD_LIBRARY_PATH=. ./fetion --mobile=$myphone --pwd=$mypwd --to=$iphone --msg-utf8="$webinfo $webdate" >>smsp.log

                       done

                                  

          }

#发送主机类故障短信告警模块 此处笔者在调用时直接套用没有引用之

CheckHost() {

              cd /root/mon

              for webip in `cat smspip|awk -F : '{print $1}'`

                 #此处smspip是一个文本文件其格式如下192.168.163.110:80

                 #使用awk命令去其域的第一部份即ip部分

                 #当然你也可以直接在smspip文本文件内只存ip那么语句改成

                 # 这样for webip in `cat smspip`既可

                     do

                     ping -c 1 $webip > /dev/null 2>&1

                     if [ $? != 0 ] && ! [ -f smsp.lock ]

                     #告警完毕后上锁以防多次告警影响工程师的情绪 呵呵

                     then

                        # sendsmshost

                        cd /root/mon

                        for iphone in `cat smspphone`

                        do

                        #touch ./smsp.lock

                        #echo $iphone

                        LD_LIBRARY_PATH=. ./fetion --mobile=$myphone --pwd=$mypwd --to=$iphone --msg-utf8="The $webip can not connect at $webdate" >>smsp.log

                       done

                       touch ./smsp.lock

                       fi

                     done

            }

##以上为主机故障检测模块

CheckWeb1() {

              status=`telnet 192.168.202.45 80 < /dev/null 2>/dev/null | grep '\^' | wc -l`

              #此处由于只有两台web服务器需要检测所以没有模块化 需要的同学可以#自行编写一下下啦

              if [ $status != 1 ] && ! [ -f smsp1.lock ]

                  then

                  #sendsmsweb

                   cd /root/mon

                   for iphone in `cat smspphone`

                   do

                   #echo $iphone

                   LD_LIBRARY_PATH=. ./fetion --mobile=$myphone --pwd=$mypwd --to=$iphone --msg-utf8="The smsp.allook.cn Web Service has been down at $webdate" >>smsp.log

                    done

                   touch ./smsp1.lock

              fi

           }

CheckWeb2() {

              status=`telnet 192.168.202.46 9001 < /dev/null 2>/dev/null | grep '\^' | wc -l`

             #此处由于只有两台web服务器需要检测所以没有模块化 需要的同学可以自#行编写一下下啦

              if [ $status != 1 ] && ! [ -f smsp2.lock ]

                  then

                  #sendsmsweb

           cd /root/mon

           for iphone in `cat smspphone`

           do

           #echo $iphone

           LD_LIBRARY_PATH=. ./fetion --mobile=$myphone --pwd=$mypwd --to=$iphone --msg-utf8="The sms gw 192.168.202.46  has been down at $webdate" >>smsp.log

           done

           touch ./smsp2.lock

              fi

           }

##Running the monapp

CheckHost

CheckWeb1

CheckWeb2

最后保存退出后加入到你的crontab中格式如下*/5 * * * * /root/mon/monwh.sh 。

上一篇:Shell脚本中参数传递方法介绍
下一篇:Struts框架下Nginx与Tomcat容器的深度整合实践