请输入内容
 
管理服务器太多了,不可能每时每刻都登录到上面查看服务器状况,从http://blog.s135.com上看到,电子邮件监控linux服务器,和msn消息监控(没有开源核心技术),结合其文章和提示,自己写了一个msn消息监控Linux服务器。监控程序以shell和PHP程序编写:
系统监控接口程序(boobooke.php)具有的报警方式
     1、MSN实时报警
    Shell脚本在计划任务设定时间内执行,结合PHP程序判断是否发送短消息到管理员的MSN。
         发送MSN短消息用了一个PHP类:sendMsg(网上可以下载到),使用该PHP类发消息,必须将发送、接收双方的MSN加为联系  人,发送中文时,先用iconv将字符集转为UTF-8: $sendMsg->sendMessage(iconv("GBK", "UTF-8", $message), 'Times New Roman', '008000');
     2、被监控服务器Shell脚本
      ①磁盘分区使用率监控 

/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' ';')    
date=$(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=$date -d ip=$ip -d data=$disk $1

      ②交换分区使用率监控 
/usr/local/servmon/bin/swapstat_send    
#!/bin/sh    
LANG=zh_cn    
data=$(/usr/bin/free -m | grep Swap | awk '{print $2":"$3":"$4}')    
date=$(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=$date -d ip=$ip -d data=$data $1


      ③负载情况使用率监控 
/usr/local/servmon/bin/loadstat_send    
#!/bin/sh    
LANG=zh_cn    
load=$(/usr/bin/uptime | awk -F'load average: ' '{print $2}' | awk '{print $1$2$3}')    
date=$(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=loadstat -d password=$2 -d date=$date -d ip=$ip -d data=$load $1

 

      ④Linux服务器计划任务
 
/etc/cron.d/boobooke    
*/30 * * * * root /bin/sh /usr/local/servmon/bin/diskstat_send http://127.0.0.1/boobooke.php password    
*/10 * * * * root /bin/sh /usr/local/servmon/bin/swapstat_send http://127.0.0.1/boobooke.php password    
1,16,31,46 * * * * root /bin/sh /usr/local/servmon/bin/loadstat_send http://127.0.0.1/boobooke.php password    

      ⑤service crond start

     3、PHP接口程序
http://192.168.1.22/boobooke.php
<?php    
/**    
*********************************************************************    
* Copyright (c) 2008, [赶星]    
* All rights reserved.    
*    
* 文件名称: boobooke.php    
* 摘        要: 服务器监控系统※赶星修改※接口程序    
* 描        述:    
* 作        者: 张宴    新浪UC:103500    博客:blog.s135.com    
* 作        者: 赶星    QQ:10908105        博客:51CTO提醒您,请勿滥发广告!    
* 版        本: 1.1    
* 时        间: 2008-10-07    
*********************************************************************    
*/    
//------------------配置信息------------------    
$password = "123123";//设置数据传输验证密码,※赶星修改※客户端安装时需要指定此密码    
$disk_max = "70";//磁盘分区使用百分比超过此值,则报警。默认为90%    
$swap_max = "1";//Swap交换空间使用百分比超过此值,则报警。默认为50%    
//--------------------------------------------    
//-----------------定义发msn函数---------------    
function sendmsn()    
{    
$send_msn = "10908105@qq.com";//机器人MSN帐号    
$send_msn_password = "boobooke";//机器人MSN密码    
$re_msn = "onlyloveyou2005@163.com";//接收管理员MSN帐号    
global $message;    
error_reporting(E_ALL);    
include('sendMsg.php');    
$sendMsg = new sendMsg();    
$sendMsg->login($send_msn,$send_msn_password);    
$sendMsg->createSession($re_msn);    
$sendMsg->sendMessage(iconv("GBK", "UTF-8", $message), 'Times New Roman', '008000');    
}    
//--------------函数定义结束-------------------    
//密码验证    
if (htmlspecialchars($_POST["password"]) != $password)    
{    
        echo "数据传输验证密码错误!\n";    
        exit();    
}    
//磁盘分区报警(分区使用率超过90%后会发MSN消息报警)    
if (htmlspecialchars($_POST["menu"]) == "diskstat")    
{    
        $is_send_msn = "0";//是否发MSN消息,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 = "服务器监控系统 ※赶星修改※ 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";    
                        $is_send_msn = "1";//是否发MSN消息,1为发送MSN消息    
                }    
        }    
        if ($is_send_msn == "1")    
                        {    
                     sendmsn();    
                        }    
                     else    
            {    
                 echo "磁盘分区空间足够,无须发送报警MSN消息!\n";    
            }    
        exit();    
}    
//Swap交换空间报警(Swap交换空间使用率超过60%后会发MSN消息报警)    
if (htmlspecialchars($_POST["menu"]) == "swapstat")    
{    
        $is_send_msn = "0";//是否发MSN消息,0为不发MSN消息    
        $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 = "服务器监控系统 ※赶星修改※ 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";    
                $is_send_msn = "1";//是否发MSN消息,1为发送MSN消息    
        }    
        if ($is_send_msn == "1")    
        {    
                sendmsn();    
        }    
        else    
        {    
                echo "Swap交换空间使用率不高,无须发送报警MSN!\n";    
        }    
        exit();    
}    
//系统负载过高报警    
if (htmlspecialchars($_POST["menu"]) == "loadstat")    
{    
        $is_send_msn = "0";//是否发MSN消息,0为不发MSN消息    
        $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 = "服务器监控系统 ※赶星修改※ 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";    
                $is_send_msn = "1";//是否发MSN消息,1为发送MSN消息    
        }    
        if ($is_send_msn == "1")    
        {    
                sendmsn();    
        }    
        else    
        {    
                echo "系统负载不高,无须发送报警MSN消息!\n";    
        }    
        exit();    
}    

===========================================
在线视频讲解地址: http://51CTO提醒您,请勿滥发广告!/v/bbk1679
===========================================