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

  我管理服务器太多了,不可能每时每刻都登录到上面查看服务器状况,于是写了一个“服务器监控系统 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技术 | 评论(48) | 引用(0) | 阅读(51133)
shz Email
2022-7-3 20:32
Please give some advice on how to achieve this kind of posts.  Digital real estate business
shz Email
2022-7-3 20:35
Much thanks for composing such an intriguing article on this point. This has truly made me think and I plan to peruse more  Digital real estate business
shz Email
2022-7-3 20:38
This website is remarkable information and facts it's really excellent  Digital real estate business
shz Email
2022-7-3 20:42
Thanks a lot for one’s intriguing write-up. It’s actually exceptional. Searching ahead for this sort of revisions.  Digital real estate business
shz Email
2022-7-3 20:45
I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own Blog Engine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.  Digital real estate business
shz Email
2022-7-3 20:48
Welcome to the party of my life here you will learn everything about me.  Digital real estate business
shz Email
2022-7-3 20:55
I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!  Digital real estate business
shz Email
2022-7-3 20:58
They're produced by the very best degree developers who will be distinguished for your polo dress creating. You'll find polo Ron Lauren inside exclusive array which include particular classes for men, women.  Digital real estate business
分页: 3/3 第一页 上页 1 2 3 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]