ServMon的原理是用shell脚本定时抓取被监控Linux服务器上的信息,再用curl POST到监控机的PHP接口程序上,由PHP接口程序对信息进行分析,如果存在异常,则发送报警邮件给管理员。报警邮件演示页面请见:http://blog.s135.com/read.php/276.htm
ServMon V1.1 在 1.0 的基础上,增加了对Linux系统负载的监控,并编写了客户端自动安装包:
1、被监控端服务器(Linux)
(1)、下载并安装ServMon Client
注:此处的password为interface.php程序中设置的数据传输验证密码。
客户端安装包源码:
│
├─install
├─[src]
│ ├─[bin]
│ │ ├─diskstat_send
│ │ ├─loadstat_send
│ │ └─swapstat_send
│ ├─[cron]
│ │ └─servmon
│ └─[data]
屏幕演示:
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接口程序用来记录发送磁盘分区报警邮件的时间,避免在一段时间内重复发送大量报警邮件的。
(3)、PHP接口程序
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $password = "yourpassword";
- $mysql_server = "127.0.0.1:3306";
- $mysql_db = "servmon";
- $mysql_username = "root";
- $mysql_password = "";
- $disk_max = "90";
- $disk_sendmail_interval = "43200";
- $swap_max = "50";
- $email = "webmaster@s135.com,info@s135.com";
-
-
-
- if (htmlspecialchars($_POST["password"]) != $password)
- {
- echo "数据传输验证密码错误!\n";
- exit();
- }
-
-
- if (htmlspecialchars($_POST["menu"]) == "diskstat")
- {
- $is_send_mail = "0";
- $date = htmlspecialchars($_POST["date"]);
- $ip = htmlspecialchars($_POST["ip"]);
- $data = htmlspecialchars($_POST["data"]);
- $arr1 = explode(";", $data);
- foreach ($arr1 as $key1 => $value1)
- {
- $arr2 = explode(":", $value1);
- if ($arr2[0] != "" && $arr2[1] != "" && $arr2[1] >= $disk_max)
- {
- $subject = "[系统监控]_磁盘分区空间不足_" . $ip;
- $message = "服务器监控系统 ServMon V1.1\r\n";
- $message = $message . "-------------------------------------------------------\r\n";
- $message = $message . "报警服务器:" . $ip . "\r\n";
- $message = $message . "-------------------------------------------------------\r\n";
- $message = $message . "报警时间:" . $date . "\r\n";
- $message = $message . "-------------------------------------------------------\r\n";
- $message = $message . "报警内容:\r\n";
- $message = $message . "●磁盘分区 " . $arr2[0] . " 空间不足(空间使用率达到" . $arr2[1] . "%)\r\n";
- $headers = 'From: ServMon ' . "\r\n" .
- 'Reply-To: ServMon ' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
- $is_send_mail = "1";
- }
- }
- if ($is_send_mail == "1")
- {
- $connect = mysql_connect($mysql_server, $mysql_username, $mysql_password);
- $select = mysql_select_db($mysql_db, $connect);
- $select_result = mysql_query("SELECT date FROM tb_diskstat WHERE ip='" . $ip .
- "' LIMIT 1");
- $rs = mysql_fetch_array($select_result);
- $interval = date("U") - $rs["date"];
- if ($interval >= $disk_sendmail_interval)
- {
- if (mail($email, $subject, $message, $headers))
- {
- echo "已成功发送报警邮件!\n";
- }
- else
- {
- echo "发送报警邮件失败!\n";
- }
- $insert_result = mysql_query("REPLACE INTO tb_diskstat (ip, date) VALUES ('" . $ip .
- "', '" . date("U") . "');");
- }
- else
- {
- if ($disk_sendmail_interval >= 3600)
- {
- $disk_sendmail_interval_text = $disk_sendmail_interval / 3600 . "小时";
- }
- else
- {
- $disk_sendmail_interval_text = $disk_sendmail_interval . "秒";
- }
- echo "已发送过报警邮件," . $disk_sendmail_interval_text . "内不再重复发送!\n";
- }
-
- }
- else
- {
- echo "磁盘分区空间足够,无须发送报警邮件!\n";
- }
- exit();
- }
-
-
- if (htmlspecialchars($_POST["menu"]) == "swapstat")
- {
- $is_send_mail = "0";
- $date = htmlspecialchars($_POST["date"]);
- $ip = htmlspecialchars($_POST["ip"]);
- list($total, $used, $free) = split(":", htmlspecialchars($_POST["data"]));
- $percentage = round($used / $total * 100, 0);
- if ($percentage >= $swap_max)
- {
- $subject = "[系统监控]_Swap交换空间使用率高达" . $percentage . "%_" . $ip;
- $message = "服务器监控系统 ServMon V1.1\r\n";
- $message = $message . "-------------------------------------------------------\r\n";
- $message = $message . "报警服务器:" . $ip . "\r\n";
- $message = $message . "-------------------------------------------------------\r\n";
- $message = $message . "报警时间:" . $date . "\r\n";
- $message = $message . "-------------------------------------------------------\r\n";
- $message = $message . "报警内容:\r\n";
- $message = $message . "●Swap交换空间使用率高达" . $percentage . "%,若继续增加到100%可能会导致服务器死机。\r\n";
- $message = $message . "●Swap交换空间总量:" . $total . " MB\r\n";
- $message = $message . "●Swap交换空间使用:" . $used . " MB\r\n";
- $message = $message . "●Swap交换空间剩余:" . $free . " MB\r\n";
- $headers = 'From: ServMon ' . "\r\n" .
- 'Reply-To: ServMon ' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
- $is_send_mail = "1";
- }
- if ($is_send_mail == "1")
- {
- if (mail($email, $subject, $message, $headers))
- {
- echo "已成功发送报警邮件!\n";
- }
- else
- {
- echo "发送报警邮件失败!\n";
- }
- }
- else
- {
- echo "Swap交换空间使用率不高,无须发送报警邮件!\n";
- }
- exit();
- }
-
-
- if (htmlspecialchars($_POST["menu"]) == "loadstat")
- {
- $is_send_mail = "0";
- $date = htmlspecialchars($_POST["date"]);
- $ip = htmlspecialchars($_POST["ip"]);
- list($load_last_1min, $load_last_5min, $load_last_15min) = split(",",
- htmlspecialchars($_POST["data"]));
- if ($load_last_15min >= 25)
- {
- if ($load_last_15min < 50)
- {
- $level = "轻微";
- }
- elseif ($load_last_15min < 100)
- {
- $level = "严重";
- }
- elseif ($load_last_15min < 500)
- {
- $level = "非常严重";
- }
- else
- {
- $level = "接近崩溃";
- }
- $subject = "[系统监控]_系统负载过高(" . $level . ")_" . $ip;
- $message = "服务器监控系统 ServMon V1.1\r\n";
- $message = $message . "-------------------------------------------------------\r\n";
- $message = $message . "报警服务器:" . $ip . "\r\n";
- $message = $message . "-------------------------------------------------------\r\n";
- $message = $message . "报警时间:" . $date . "\r\n";
- $message = $message . "-------------------------------------------------------\r\n";
- $message = $message . "报警内容:\r\n";
- $message = $message . "●系统负载过高,级别:" . $level . " (25~50轻微、50~100严重、100~500非常严重、>500接近崩溃)\r\n";
- $message = $message . "●最近 1分钟系统负载:" . $load_last_1min . "\r\n";
- $message = $message . "●最近 5分钟系统负载:" . $load_last_5min . "\r\n";
- $message = $message . "●最近15分钟系统负载:" . $load_last_15min . "\r\n";
- $headers = 'From: ServMon ' . "\r\n" .
- 'Reply-To: ServMon ' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
- $is_send_mail = "1";
- }
- if ($is_send_mail == "1")
- {
- if (mail($email, $subject, $message, $headers))
- {
- echo "已成功发送报警邮件!\n";
- }
- else
- {
- echo "发送报警邮件失败!\n";
- }
- }
- else
- {
- echo "系统负载不高,无须发送报警邮件!\n";
- }
- exit();
- }
- ?>
阅读(1662) | 评论(0) | 转发(0) |