[文章作者:张宴 本文版本: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) | 阅读(143319)
小灵子 Email
2011-11-21 08:43
请问下张老师,如果我只需要监控tcp协议的80端口,是不是只要运行第二条语句就可以了,其他2条不需要写入。
   /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
coach factory outlet Email Homepage
2012-5-17 11:41
Today, following half a century, mentor leather-bases coach factory outlet continues to be the delicate craft of leather-based master is accountable for,Would you like to meet more friends, or go with the times? If yes, coach factory online is opening welcome doors to you.in the market you definitely can find various colorways that are designed in as well as the high quality that applied in. For most of you would like to come. So just come to our coach factory outlet online store to choose one.
louis vuitton sale Email Homepage
2012-5-17 11:41
Louis Vuitton belt at louis vuitton sale is one kind of fashion accessory with high cost performance among the Louis Vuitton accessories.Offering quality LV products with favorable prices, louis vuitton outlet store is at your service. Hurry up, or you can not seize the chance.in fact, louis vuitton is one of the most famous fashion design master.he opened the fist suitcase shop called after his name.
coach outlet online Email Homepage
2012-5-17 11:41
I heard of coach outlet online through the advertisement when I was shopping. And now I often brow the webpage and buy Coach bags online.It is a symbol regarding position not to mention nature.Here I would like to launch a excellent bags pertaining to business men.Which may be coach outlet store.coach outlet has always been simple,durable style features to win consumers.The products are more flexible,with easy bleaching,wear characteristics,and simply use a damp cloth.
coach outlet, Email Homepage
2012-5-17 11:41
Remember the coach outlet provide coach bags which won't be deteriorated into its overall styles by any means. It will maintain its looks, colors, and uniqueness for long time.coach outlet store online has been voted by Hour Detroit magazine readers as the Best of Detroit in their 12th annual readers'poll.Lots of women like which usually amount normally include a coach outlet online ,it provides coziness to many girls that don't even think it is a great bushel of great interest directly to them.
xujie777 Email
2012-5-18 16:37
We aim to make all our customers satisfy with our products. You will find a variety of Men's fashion louis vuitton uk, fashion Women's cheap Louis Vuitton bags in our store at affordable price.Thinking of interesting ways to cost a milestone birthday? louis vuitton online shop had one of the most distinctive distinctive celebrations.bakery along with living room operated by means of about three moment louis vuitton online Most effective Pastry Chef’s of the year Rammy Nominee Chef’s.
xujie777 Email
2012-5-18 16:37
The choices are likely to be basically countless seeing that louis vuitton outlet occurs with the help of completely new and also incredible concepts once in a while.Louis vuitton Wholesale Monogram Canvas HandbagsLouis Vuitton Collection Beach Handbags louis vuitton bags outlet Damier Canvas HandbagsLouis vuitton Mahina HandbagsLouis Vuitton Monogram Mini Lin HandbagsLouis Vuitton Monogram Multicolore HandbagsLouis vuitton Monogram Vernis HandbagsLouis Vuitton Wholesale Epi Leather HandbagsLouis Vuitton For Men HandbagsLouis Vuitton Damier Canvas WalletsLouis Vuitton Epi Leather WalletsLouis Vuitton Monogram Canvas WalletsLouis Vuitton Monogram Vernis WalleLouis Vuitton ShoesLouis Vuitton Men wallets.As the Authentic Louis Vuitton are so high-priced, so came the louis vuitton handbags outlet.
xujie777 Email
2012-5-18 16:37
coach outlet has become a popular shopping experience for consumers around the world, and a desirable distribution channel for manufacturer's and retailers.Getting your hands on coach outlet store online can be a hefty investment of hundreds of dollars.But do not despair,the Coach Outlet store could be the answer to your prayers.coach outlet online is your smart choice when you want to get the discount Coach accessory. You can find the exact Coach Bags and other accessory you want at a low price that's right for you. My dear friends, let the coach outlet online pave your way into the world of high fashion with their ultimate fashion factory.
xujie123
2012-5-21 11:20
Our online store offers you discounted Designer louis vuitton replica wallet at present. You could find them in desirable quality and price. If you don't mind high class louis vuitton uk, have a good time here.louis vuitton Store Online Handbags can also bring great accuracy as well as practical applicability and fashionable.Have you ever dreamed of being as charming as Madonna? Have you ever thought of becoming an envy of all your friends? If so, come to louis vuitton outlet.<br/>
xujie555
2012-5-21 13:48
coach factory outlet uses graceful accessories to match the classical logo of coach, which is the best combination of coach. The handmade coach products?can make you more charming and graceful.coach factory online provides people many coach goods. If you wish to snatch the coach handbag, then this best method is made for that you like for coach discount.coach factory outlet online is a fashion brand to ensure its quality. With designer coach shoulder bags, you will always attract people's attention. The bags will emphasize your personal style and taste.<br/>
xujie333
2012-5-21 15:09
coach outlet online Store guarantee that all the coach handbags offered are own high quality. In addition , all of them are sold at an unexpected low price.If you want to purchase, just visit their website.coach factory outlet is really sizzling kinds of shopping way for you. With the usage of the replica designer coach bags, you can surely be able to change your individual looks in a stunning manner.Coach bags on sale from the coach outlet are cheap or discount prices that you certainly will stand out from the crowd on your next camping trip!<br/>
xiaohai123 Email Homepage
2012-6-8 10:36
Les produits de la marque Versace, y compris les verres,<a href="http://www.saclongchampss.com">Sac Longchamp Pliage,Boutique,Sacs Longchamp Pas Cher</a>  le parfum des sacs de maquillage décoration intérieure écharpe montres bijoux soins de la peau http://www.saclongchampss.com ceinture, Gianni Versace SpA Groupe est le leader mondial de salle internationale design de mode. Le sac Mme les Xiekua (6:27) sacs Mme d'acheter (6:27), le classement de Mme la marque de sac (6:27) liste le sac de Mme. Bo?te A propos de Global conquérir le monde! Mondial bagages agent de gros net des sacs en cuir de marque Marchands Chine,
<a href="http://www.saclongchampss.com">sac pas cher</a> du cuir, sacs en cuir de rejoindre, à la conception des sacs, sacs à main en cuir d'information
coach88888888 Email
2012-8-13 17:50
If you desire to go to Coach Factory Outlet, but have no idea in which to go, you can research online. It is no doubt that there is drastically information and details about it for the reference.Coach Factory Online are loved by many people, when you walk in the street, you could see many people take coach styles.hat experts claim Coach Outlet Online shopping is in the changes they are available in.Coach Outlet will send you a coupon in the post to use in their upon only after you type a achieve. The Coach bags are customarily somewhat many than the ones in the stores.
GHWERW
2013-1-29 17:02
买轴承就到:www.jkzhoucheng.cn
lamp Email Homepage
2014-4-15 01:04
这个功能不错,值得借鉴!
Cheap LED Lamps Email Homepage
2014-4-15 09:57
We sell Cheap LED Lamps at http://www.cheapledlamps.com
shz Email
2022-7-19 01:05
I'm happy to see the considerable subtle element here!.  Scam Risk
shz Email
2022-7-19 01:52
I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.  Scam Risk
shz Email
2022-7-19 01:57
This is truly an practical and pleasant information for all. Thanks for sharing this to us and more power  Scam Risk
shz Email
2022-7-19 02:01
Great post! I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have. Keep up the good work you are doing here.  Scam Risk
分页: 3/4 第一页 上页 1 2 3 4 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]