[文章作者:张宴 本文版本:v1.0 最后修改:2008.06.25 转载请注明原文链接:http://blog.zyan.cc/read.php/354.htm]

  最近,在我原有的“Linux服务器系统监控程序”基础上,完善了HTTP、TCP、MySQL主动监控与MSN、E-mail、手机短信报警。监控程序以shell和PHP程序编写,以下为主要框架与部分代码:

  一、系统监控接口程序(interface.php)具有的报警方式
  1、MSN实时报警
  ①、监控程序每次检测到故障存在、或者故障恢复,都会发送短消息到管理员的MSN。
  点击在新窗口中浏览此图片

  点击在新窗口中浏览此图片

  发送MSN短消息用了一个PHP类:sendMsg,使用该PHP类发消息,必须将发送、接收双方的MSN加为联系人,发送中文时,先用iconv将字符集转为UTF-8:
引用
$sendMsg->sendMessage(iconv("GBK", "UTF-8", $message), 'Times New Roman', '008000');




  2、手机短信报警
  ①、工作日早上10点之前,晚上6点之后,以及周六、周日,监控程序检测到故障,会调用手机短信接口,发送短信给管理员的手机。
  ②、如果监控程序多次检测到同一台服务器的同一类故障,只会在第一次检测到故障时发送一条“故障报警”短信。服务器故障恢复后,监控程序会再发送一条“故障恢复”短信。

  如果没有手机短信网关接口,可以试试中国移动通信的www.139.com邮箱,具有免费的邮件到达手机短信通知功能,可以将收到的邮件标题以短信的形式发送到手机上。



  3、电子邮件报警
  ①、如果监控程序多次检测到同一台服务器的同一类故障,只会在第一次检测到故障时发送一封“故障报警”邮件。服务器故障恢复后,监控程序会再发送一封“故障恢复”邮件。

  系统监控接口程序interface.php(核心部分,仅提供部分代码):


  二、主动探测监控(“监控机”主动探测“被监控机”)
  1、HTTP服务器监控
  脚本:/data0/monitor/http.sh
引用
#!/bin/sh
LANG=C

#被监控服务器、端口列表
server_all_list=(\
192.168.1.1:80 \
192.168.1.2:80 \
192.168.1.3:80 \
)

date=$(date -d "today" +"%Y-%m-%d_%H:%M:%S")

#采用HTTP POST方式发送检测信息给接口程序interface.php,接口程序负责分析信息,决定是否发送报警MSN消息、手机短信、电子邮件。
send_msg_to_interface()
{
   /usr/bin/curl -m 600 -d menu=http -d date=$date -d ip=$server_ip -d port=$server_port -d status=$status http://127.0.0.1:8888/interface.php
}

server_all_len=${#server_all_list[*]}
i=0
while  [ $i -lt $server_all_len ]
do
   server_ip=$(echo ${server_all_list[$i]} | awk -F ':' '{print $1}')
   server_port=$(echo ${server_all_list[$i]} | awk -F ':' '{print $2}')
   if curl -m 10 -G http://${server_all_list[$i]}/ > /dev/null 2>&1
   then
     #status:    0,http down    1,http ok    2,http down but ping ok
     status=1
           echo "服务器${server_ip},端口${server_port}能够正常访问!"
   else
       if curl -m 30 -G http://${server_all_list[$i]}/ > /dev/null 2>&1
       then
           status=1
           echo "服务器${server_ip},端口${server_port}能够正常访问!"
       else
           if ping -c 1 $server_ip > /dev/null 2>&1
           then
               status=2
               echo "服务器${server_ip},端口${server_port}无法访问,但是能够Ping通!"
           else
               status=0
               echo "服务器${server_ip},端口${server_port}无法访问,并且无法Ping通!"
           fi
       fi
   fi
 send_msg_to_interface
   let i++
done



  2、TCP服务器监控
  脚本:/data0/monitor/tcp.sh
引用
#!/bin/sh
LANG=C

#被监控服务器、端口列表
server_all_list=(\
192.168.1.4:11211 \
192.168.1.5:11211 \
192.168.1.6:25 \
192.168.1.7:25 \
)

date=$(date -d "today" +"%Y-%m-%d_%H:%M:%S")

#采用HTTP POST方式发送检测信息给接口程序interface.php,接口程序负责分析信息,决定是否发送报警MSN消息、手机短信、电子邮件。
send_msg_to_interface()
{
   /usr/bin/curl -m 600 -d menu=tcp -d date=$date -d ip=$server_ip -d port=$server_port -d status=$status http://127.0.0.1:8888/interface.php
}

server_all_len=${#server_all_list[*]}
i=0
while  [ $i -lt $server_all_len ]
do
   server_ip=$(echo ${server_all_list[$i]} | awk -F ':' '{print $1}')
   server_port=$(echo ${server_all_list[$i]} | awk -F ':' '{print $2}')
   if nc -vv -z -w 3 $server_ip $server_port > /dev/null 2>&1
   then
       #status:    0,http down    1,http ok    2,http down but ping ok
       status=1
       echo "服务器${server_ip},端口${server_port}能够正常访问!"
   else
       if nc -vv -z -w 10 $server_ip $server_port > /dev/null 2>&1
       then
           status=1
           echo "服务器${server_ip},端口${server_port}能够正常访问!"
       else
           if ping -c 1 $server_ip > /dev/null 2>&1
           then
               status=2
               echo "服务器${server_ip},端口${server_port}无法访问,但是能够Ping通!"
           else
               status=0
               echo "服务器${server_ip},端口${server_port}无法访问,并且无法Ping通!"
           fi
       fi
   fi
   send_msg_to_interface
   let i++
done



  3、MySQL服务器监控
  ①、MySQL是否能够连接
  ②、MySQL是否发生表损坏等错误
  ③、MySQL活动连接数是否过多
  ④、MySQL从库是否同步正常
  ⑤、MySQL从库同步延迟时间是否过大
  脚本:/data0/monitor/mysql.php


  4、主动监控守护进程
  脚本:/data0/monitor/monitor.sh
引用
#!/bin/sh
while true
do
   /bin/sh /data0/monitor/http.sh > /dev/null 2>&1
   /bin/sh /data0/monitor/tcp.sh > /dev/null 2>&1
   /usr/local/php/bin/php /data0/monitor/mysql.php > /dev/null 2>&1
   sleep 10
done


  启动主动监控守护进程:
/usr/bin/nohup /bin/sh /data0/monitor/monitor.sh 2>&1 > /dev/null &



  三、被动报告监控(“被监控机”采集数据发送给“监控机”)
  1、磁盘空间使用量监控
  2、磁盘Inode使用量监控
  3、Swap交换空间使用量监控
  4、系统负载监控
  5、Apache进程数监控

  被动监控这部分,在我的文章《写完“Linux服务器监控系统 ServMon V1.1” 》中已经实现,就不再详细写出。



Tags: , , , ,



技术大类 » 其他Unix技术 | 评论(66) | 引用(1) | 阅读(140909)
shz Email
2022-7-19 02:04
I must admit that your post is really interesting. I have spent a lot of my spare time reading your content. Thank you a lot!  Scam Risk
shz Email
2022-7-19 02:08
Wonderful illustrated information. I thank you about that. No doubt it will be very useful for my future projects. Would like to see some other posts on the same subject!  Scam Risk
shz Email
2022-7-19 02:12
We are tied directly into the sate’s renewal database which allows us to process your request almost instantly.   Scam Risk
shz Email
2022-7-19 02:15
I read that Post and got it fine and informative. Please share more like that...  Scam Risk
shzz Email
2022-7-19 02:16
Going to graduate school was a positive decision for me. I enjoyed the coursework, the presentations, the fellow students, and the professors. And since my company reimbursed 100% of the tuition, the only cost that I had to pay on my own was for books and supplies. Otherwise, I received a free master’s degree. All that I had to invest was my time.  Scam Risk
shz Email
2022-7-19 02:19
It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it.  Scam Risk
分页: 4/4 第一页 上页 1 2 3 4 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]