百度首页 | 百度空间
 
查看文章
 
Linux服务器系统监控框架与MSN、E-mail、手机短信报警的实现[3]
2008-07-01 12:10
  1. /**
  2. *********************************************************************
  3. * Curl_Class :curl 类
  4. *********************************************************************/  
  5. class Curl_Class  
  6. {  
  7.     function Curl_Class()  
  8.      {  
  9.         return true;  
  10.      }  
  11.   
  12.     function execute($method, $url, $fields = '', $userAgent = '', $httpHeaders = '',  
  13.         $username = '', $password = '')  
  14.      {  
  15.         $ch = Curl_Class::create();  
  16.         if (false === $ch)  
  17.          {  
  18.             return false;  
  19.          }  
  20.   
  21.         if (is_string($url) && strlen($url))  
  22.          {  
  23.             $ret = curl_setopt($ch, CURLOPT_URL, $url);  
  24.          }  
  25.         else  
  26.          {  
  27.             return false;  
  28.          }  
  29.         //是否显示头部信息  
  30.          curl_setopt($ch, CURLOPT_HEADER, false);  
  31.         //  
  32.          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  33.   
  34.         if ($username != '')  
  35.          {  
  36.              curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);  
  37.          }  
  38.   
  39.         $method = strtolower($method);  
  40.         if ('post' == $method)  
  41.          {  
  42.              curl_setopt($ch, CURLOPT_POST, true);  
  43.             if (is_array($fields))  
  44.              {  
  45.                 $sets = array();  
  46.                 foreach ($fields as $key => $val)  
  47.                  {  
  48.                     $sets[] = $key . '=' . urlencode($val);  
  49.                  }  
  50.                 $fields = implode('&', $sets);  
  51.              }  
  52.              curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);  
  53.          }  
  54.         else  
  55.             if ('put' == $method)  
  56.              {  
  57.                  curl_setopt($ch, CURLOPT_PUT, true);  
  58.              }  
  59.   
  60.         //curl_setopt($ch, CURLOPT_PROGRESS, true);  
  61.         //curl_setopt($ch, CURLOPT_VERBOSE, true);  
  62.         //curl_setopt($ch, CURLOPT_MUTE, false);  
  63.          curl_setopt($ch, CURLOPT_TIMEOUT, 600);  
  64.   
  65.         if (strlen($userAgent))  
  66.          {  
  67.              curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);  
  68.          }  
  69.   
  70.         if (is_array($httpHeaders))  
  71.          {  
  72.              curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);  
  73.          }  
  74.   
  75.         $ret = curl_exec($ch);  
  76.   
  77.         if (curl_errno($ch))  
  78.          {  
  79.              curl_close($ch);  
  80.             return array(curl_error($ch), curl_errno($ch));  
  81.          }  
  82.         else  
  83.          {  
  84.              curl_close($ch);  
  85.             if (!is_string($ret) || !strlen($ret))  
  86.              {  
  87.                 return false;  
  88.              }  
  89.             return $ret;  
  90.          }  
  91.      }  
  92.   
  93.     function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '',  
  94.         $password = '')  
  95.      {  
  96.         $ret = Curl_Class::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username,  
  97.             $password);  
  98.         if (false === $ret)  
  99.          {  
  100.             return false;  
  101.          }  
  102.   
  103.         if (is_array($ret))  
  104.          {  
  105.             return false;  
  106.          }  
  107.         return $ret;  
  108.      }  
  109.   
  110.     function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password =  
  111.         '')  
  112.      {  
  113.         $ret = Curl_Class::execute('GET', $url, '', $userAgent, $httpHeaders, $username,  
  114.             $password);  
  115.         if (false === $ret)  
  116.          {  
  117.             return false;  
  118.          }  
  119.   
  120.         if (is_array($ret))  
  121.          {  
  122.             return false;  
  123.          }  
  124.         return $ret;  
  125.      }  
  126.   
  127.     function create()  
  128.      {  
  129.         $ch = null;  
  130.         if (!function_exists('curl_init'))  
  131.          {  
  132.             return false;  
  133.          }  
  134.         $ch = curl_init();  
  135.         if (!is_resource($ch))  
  136.          {  
  137.             return false;  
  138.          }  
  139.         return $ch;  
  140.      }  
  141.   
  142. }  
  143. ?>  
<?php //$server_list[]="服务器地址:端口:帐号:密码"; $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); } } /** ********************************************************************* * Curl_Class :curl 类 *********************************************************************/ 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_PROGRESS, true); //curl_setopt($ch, CURLOPT_VERBOSE, true); //curl_setopt($ch, CURLOPT_MUTE, false); 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” 》中已经实现,就不再详细写出。

类别:freebsd&linux | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu