转自:http://blog.s135.com/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(核心部分,仅提供部分代码):
- <?php
-
- if (htmlspecialchars($_POST["menu"]) == "http")
- {
- $date = htmlspecialchars($_POST["date"]);
- $ip = htmlspecialchars($_POST["ip"]);
- $port = htmlspecialchars($_POST["port"]);
- $status = htmlspecialchars($_POST["status"]);
-
- }
-
-
- if (htmlspecialchars($_POST["menu"]) == "tcp")
- {
- $date = htmlspecialchars($_POST["date"]);
- $ip = htmlspecialchars($_POST["ip"]);
- $port = htmlspecialchars($_POST["port"]);
- $status = htmlspecialchars($_POST["status"]);
-
- }
-
-
- if (htmlspecialchars($_POST["menu"]) == "mysql")
- {
- $date = htmlspecialchars($_POST["date"]);
- $ip = htmlspecialchars($_POST["ip"]);
- $port = htmlspecialchars($_POST["port"]);
- $abstract = htmlspecialchars($_POST["abstract"]);
- $info = htmlspecialchars($_POST["info"]);
- $failback = htmlspecialchars($_POST["failback"]);
-
- }
- ?>
二、主动探测监控(“监控机”主动探测“被监控机”)
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
- <?php
-
- $server_list[]="192.168.1.11:3306:root:password";
- $server_list[]="192.168.1.12:3306:root:password";
- $server_list[]="192.168.1.13:3306:root:password";
-
- $database="mysql";
-
- $curl = new Curl_Class();
-
- foreach ($server_list as $server) {
- $status=1;
- unset($data);
- $data["menu"] = "mysql";
- $data["info"] = "";
- list($data["ip"], $data["port"], $username, $password) = explode(":", $server);
-
- $connect = @mysql_connect($data["ip"].":".$data["port"], $username, $password);
- if(! $connect)
- {
- $status=0;
- $data["info"] = $data["info"] . "无法连接MySQL服务器\r\n";
- }
-
- $select = @mysql_select_db($database, $connect);
- $result = @mysql_query("show slave status");
- $rs_slave = @mysql_fetch_array($result);
- $result = @mysql_query("show global status like 'Threads_running'");
- $rs_threads = @mysql_fetch_array($result);
- if($rs_slave["Slave_SQL_Running"] == "No")
- {
- $status=0;
- $data["abstract"] = "从库不同步";
- $data["info"] = $data["info"] . "Slave_SQL_Running = No\r\n";
- }
- if($rs_slave["Slave_IO_Running"] == "No")
- {
- $status=0;
- $data["abstract"] = "从库不同步";
- $data["info"] = $data["info"] . "Slave_IO_Running = No\r\n";
- }
- if($rs_slave["Last_Error"] != "")
- {
- $status=0;
- $data["abstract"] = "从库同步出错";
- $data["info"] = $data["info"] . "Last_Error = ".substr($rs_slave["Last_Error"], 0, 40)."\r\n";
- }
- if($rs_slave["Seconds_Behind_Master"] > 180)
- {
- $status=0;
- $data["abstract"] = "从库同步延迟时间高达".$rs_slave["Seconds_Behind_Master"]."秒";
- $data["info"] = $data["info"] . "Seconds_Behind_Master = ".$rs_slave["Seconds_Behind_Master"]."\r\n";
- }
- if($rs_threads["Value"] > 60)
- {
- $status=0;
- $data["abstract"] = "活动连接数多达".$rs_threads["Value"];
- $data["info"] = $data["info"] . "Threads_running = ".$rs_threads["Value"]."\r\n";
- }
-
- $data["date"] = date("Y-m-d_H:i:s");
- if($status == 0)
- {
- $post = @$curl->post("http://127.0.0.1:8888/interface.php", $data);
- echo "MySQL服务器“".$data["ip"].":".$data["port"]."”发生故障!\n";
- print_r($post);
- }
- else
- {
- $data["failback"] = "active";
- $post = @$curl->post("http://127.0.0.1:8888/interface.php", $data);
- echo "MySQL服务器“".$data["ip"].":".$data["port"]."”运行正常!\n";
- print_r($post);
- }
- }
-
-
-
-
-
- class Curl_Class
- {
- function Curl_Class()
- {
- return true;
- }
-
- function execute($method, $url, $fields = '', $userAgent = '', $httpHeaders = '',
- $username = '', $password = '')
- {
- $ch = Curl_Class::create();
- if (false === $ch)
- {
- return false;
- }
-
- if (is_string($url) && strlen($url))
- {
- $ret = curl_setopt($ch, CURLOPT_URL, $url);
- }
- else
- {
- return false;
- }
-
- curl_setopt($ch, CURLOPT_HEADER, false);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
- if ($username != '')
- {
- curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
- }
-
- $method = strtolower($method);
- if ('post' == $method)
- {
- curl_setopt($ch, CURLOPT_POST, true);
- if (is_array($fields))
- {
- $sets = array();
- foreach ($fields as $key => $val)
- {
- $sets[] = $key . '=' . urlencode($val);
- }
- $fields = implode('&', $sets);
- }
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
- }
- else
- if ('put' == $method)
- {
- curl_setopt($ch, CURLOPT_PUT, true);
- }
-
-
-
-
- curl_setopt($ch, CURLOPT_TIMEOUT, 600);
-
- if (strlen($userAgent))
- {
- curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
- }
-
- if (is_array($httpHeaders))
- {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
- }
-
- $ret = curl_exec($ch);
-
- if (curl_errno($ch))
- {
- curl_close($ch);
- return array(curl_error($ch), curl_errno($ch));
- }
- else
- {
- curl_close($ch);
- if (!is_string($ret) || !strlen($ret))
- {
- return false;
- }
- return $ret;
- }
- }
-
- function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '',
- $password = '')
- {
- $ret = Curl_Class::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username,
- $password);
- if (false === $ret)
- {
- return false;
- }
-
- if (is_array($ret))
- {
- return false;
- }
- return $ret;
- }
-
- function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password =
- '')
- {
- $ret = Curl_Class::execute('GET', $url, '', $userAgent, $httpHeaders, $username,
- $password);
- if (false === $ret)
- {
- return false;
- }
-
- if (is_array($ret))
- {
- return false;
- }
- return $ret;
- }
-
- function create()
- {
- $ch = null;
- if (!function_exists('curl_init'))
- {
- return false;
- }
- $ch = curl_init();
- if (!is_resource($ch))
- {
- return false;
- }
- return $ch;
- }
-
- }
- ?>
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” 》中已经实现,就不再详细写出。