[文章作者:张宴 本文版本:v1.0 最后修改:2007.08.03 转载请注明出处:http://blog.s135.com]

  我管理服务器太多了,不可能每时每刻都登录到上面查看服务器状况,于是写了一个“服务器监控系统 ServMon V1.0”。其原理是用shell脚本定时抓取被监控服务器上信息,再用curl POST到监控机的PHP接口程序上,由PHP接口程序对信息进行分析,如果存在异常,则发送报警邮件给管理员。

  下午,ServMon V1.0 已经在sina播客服务器上部署,以下为ServMon的删节版内容,只监控磁盘分区使用率和Swap交换空间两项:

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

  1、被监控端服务器的shell脚本(Linux)
  (1)、发送磁盘分区使用率给监控端接口http://192.168.0.28/servmon/interface.php的脚本
引用
/usr/local/servmon/bin/diskstat_send
#!/bin/sh
LANG=zh_cn
disk=$(/bin/df -kP | grep / | awk -F ' ' ' {print $6":"$5}' | awk -F '%' ' {print $1}' | tr -s '\n' ';')
today=$(date -d "today" +"%Y-%m-%d_%H:%M:%S")
ip=$(/sbin/ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{print $2;}' | awk -F':' '{print $2;}' | tr -s '\n' ';')
/usr/bin/curl -d menu=diskstat -d password=$2 -d date=$today -d ip=$ip -d data=$disk $1

  磁盘分区使用率监控报警邮件示例图:
  点击在新窗口中浏览此图片


  (2)、发送Swap交换空间使用率给监控端接口http://192.168.0.28/servmon/interface.php的脚本
  /usr/local/servmon/bin/swapstat_send
引用
#!/bin/sh
LANG=zh_cn
data=$(/usr/bin/free -m | grep Swap | awk '{print $2":"$3":"$4}')
today=$(date -d "today" +"%Y-%m-%d_%H:%M:%S")
ip=$(/sbin/ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{print $2;}' | awk -F':' '{print $2;}' | tr -s '\n' ';')
/usr/bin/curl -d menu=swapstat -d password=$2 -d date=$today -d ip=$ip -d data=$data $1

  Swap交换空间使用率监控报警邮件示例图:
  点击在新窗口中浏览此图片

  (3)、计划任务:每30分钟将磁盘分区使用率发送给监控端接口,每10分钟将Swap交换空间使用率发送给监控端接口。
  /etc/cron.d/servmon
引用
0-30/30 * * * * root /bin/sh /usr/local/servmon/bin/diskstat_send http://192.168.0.28/servmon/interface.php yourpassword
0-50/10 * * * * root /bin/sh /usr/local/servmon/bin/swapstat_send http://192.168.0.28/servmon/interface.php yourpassword





  2、监控端服务器配置(Linux)
  (1)、启动sendmail
  /usr/sbin/sendmail -bd -q30m
  -b:指定Sendmail在后台运行,并且监听端口25的请求。
  -d:指定Sendmail以Daemon方式运行(守护进程)。
  -q:当Sendmail无法将邮件成功地发送到目的地时,它会将邮件保存在队列里。该参数指定邮件在队列里保存的时间。其中的30m表示保留30分钟。

  (2)、修改php.ini的[mail function]部分为以下内容,然后重启Apache,使PHP能够使用mail()函数发送电子邮件
引用
[mail function]
; For Win32 only.
;SMTP = localhost
;smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = /usr/sbin/sendmail -t -i


  (4)、在MySQL上创建一个数据库servmon,然后在该数据库中执行以下SQL语句创建表:
引用
CREATE TABLE `tb_diskstat` (
 `ip` varchar(128) NOT NULL default '',
 `date` int(10) NOT NULL default '0',
 UNIQUE KEY `ip` (`ip`)
) TYPE=MyISAM;

  备注:这张表是PHP接口程序用来记录发送磁盘分区报警邮件的时间,避免在一段时间内重复发送大量报警邮件的,其实完全可以用Session、文本文件来代替它。这里之所以用MySQL数据库,是因为PHP接口中还将增加一些需要记录信息到数据库的监控模块,所以就统一用数据库了。)

  (3)、PHP接口程序
  http://192.168.0.28/servmon/interface.php


Tags: ,
技术大类 » 其他Unix技术 | 评论(24) | 引用(0) | 阅读(18348)
tankinc
2009-12-5 17:28
通过snmp可以监控到进程吗
宝鸡seo Homepage
2010-1-25 10:47
看不太懂 。。。就来感受一下这里的气氛
dresses Email Homepage
2011-7-26 10:20
Here are a variety of the empire <a href="http://www.romancearound.com/">dresses</a>, I want to introduce yourself. The first shoulder chiffon <a href="http://www.romancearound.com/bridesmaid-dresses-c-197.html">bridesmaid dresses</a> empire style wedding will <a href="http://www.romancearound.com/wedding-dresses-trumpet-wedding-dresses-c-180_253.html">trumpet wedding dresses</a>.This Tia cowries, dressed in her wedding day.Chiffon of the empire the best clothes, because they are the size of the drum as complete motion from the wind the site makes it easy to create a plan impression.One romantic flowers floating above shoulder function and help catch the eyes of the cut of the chest and neck problems, so that more comprehensive coverage of physical illusion bust.Clothing do not like big long torso, short legs, or pear-shaped<a href="http://www.romancearound.com/bridesmaid-dresses-ankle-length-bridesmaid-dresses-c-197_198.html">ankle length bridesmaid dresses</a> figure.special is also suitable for pregnant brides since the mobility as a dress can accommodate more Many of the abdomen.
louis vuitton uk Email Homepage
2011-11-23 08:48
This louis vuitton uk for sale belongs to the sounding just what are termed as Louis Vuitton vintage best sellers, many other products and services for the reason that range appearing companies.You will easily notice the unfold zippers of this coach outlet store online. That is the decoration. There are some inside pockets for you as well. They are easy to match your clothes and to carry.Let us inspire your inner beauty with fine christian louboutin sale. Purse the elegance in bridal wedding. Enjoy the fashion.
分页: 2/2 第一页 上页 1 2 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]