Exemplo n.º 1
0
 /**
  * 发送消息
  *
  * @param string $type
  * @param $to
  * @param $subject
  * @param $body
  */
 public static function send($type, $to, $subject, $body, $ip, $app = 'monitor')
 {
     switch ($type) {
         case 'mail':
             Ym_Logger::debug('------------------sendMail to:' . $to);
             $result = self::sendMail($to, $subject, $body);
             break;
         case 'sms':
             Ym_Logger::debug('------------------sendSms to:' . $to);
             $result = self::sendSms($to, $ip, $subject, $app);
             break;
     }
     return $result;
 }
 /**
  * 
  * 检测脚本主要程序(只需要编写该方法即可)
  */
 public function getCheckResult($monitorConfig, $lastTime, $currentTime)
 {
     if (empty($monitorConfig)) {
         Ym_CommonTool::myoutput(FALSE, 'get monitor data error or empty!');
     }
     $model = new Model_Alarm_Monitorscript();
     $serviceId = isset($monitorConfig['id']) ? $monitorConfig['id'] : '';
     $log_app_id = isset($monitorConfig['monitor_app']) ? $monitorConfig['monitor_app'] : '';
     $ip = isset($monitorConfig['monitor_ip']) ? $monitorConfig['monitor_ip'] : '';
     $app = isset($monitorConfig['app_name']) ? $monitorConfig['app_name'] : 'system';
     $service = isset($monitorConfig['monitor_service']) ? $monitorConfig['monitor_service'] : '';
     $param = isset($monitorConfig['monitor_param']) ? $monitorConfig['monitor_param'] : '';
     $checkTime = strtotime(date('Y-m-d H:i', $currentTime));
     $code = 0;
     $maxTime = 0;
     $msg = array();
     $logid = 0;
     $log = '';
     $string = '';
     //parse the params
     $alarm_arr = array();
     $param_arr = explode("\n", $param);
     foreach ($param_arr as $v) {
         $arr = explode('=', $v);
         $key = isset($arr[0]) ? $arr[0] : 'error';
         $alarm = isset($arr[1]) ? $arr[1] : 0;
         $alarm_arr[$key] = $alarm;
     }
     //cpu load
     $cpuLoad = `cat /proc/loadavg`;
     $mem = `free -m | grep Mem |awk -F' ' '{print \$4}'`;
     $disk = `df -h | awk -F' ' '{print\$5}' | grep -v Use |xargs`;
     $accessprocess = `ps -ef | grep 'accesslog' | grep -v grep | wc -l`;
     $weblogprocess = `ps -ef | grep 'weblog' | grep -v grep | wc -l`;
     $memalarm = isset($alarm_arr['mem']) ? $alarm_arr['mem'] : 100;
     if ($mem) {
         if ((int) $mem <= $memalarm) {
             $code = 1;
         }
     }
     $cpualarm = isset($alarm_arr['cpu']) ? $alarm_arr['cpu'] : 10;
     if ($cpuLoad) {
         $d = explode(' ', $cpuLoad);
         if ((int) $d[0] > $cpualarm) {
             $code = 2;
         }
     }
     $diskalarm = isset($alarm_arr['disk']) ? $alarm_arr['disk'] : 80;
     if ($disk) {
         foreach (explode(' ', $disk) as $v) {
             if ((int) trim($v, '%') > $diskalarm) {
                 $code = 2;
             }
         }
     }
     $processAlarm = isset($alarm_arr['accessnum']) ? $alarm_arr['accessnum'] : 20;
     if ($accessprocess) {
         if ((int) $accessprocess >= $processAlarm or (int) $accessprocess == 0) {
             $code = 2;
         }
     }
     $processAlarm1 = isset($alarm_arr['weblognum']) ? $alarm_arr['weblognum'] : 20;
     if ($weblogprocess) {
         if ((int) $weblogprocess >= $processAlarm1 or (int) $weblogprocess == 0) {
             $code = 2;
         }
     }
     $string .= 'cpuload:' . $cpuLoad . ' alarm:' . $cpualarm . '<br/>';
     $string .= 'mem:' . $mem . ' alarm:' . $memalarm . '<br/>';
     $string .= 'disk:' . $disk . ' alarm:' . $diskalarm . '<br/>';
     $string .= 'accessnum:' . $accessprocess . ' alarm:' . $processAlarm . '<br/>';
     $string .= 'weblognum:' . $weblogprocess . ' alarm:' . $processAlarm1 . '<br/>';
     $thealarm = ' Id:' . $serviceId;
     $thealarm .= ' Time:' . date('Y/m/d H:i:s', $checkTime);
     $thealarm .= ' Code:' . $code;
     $thealarm .= ' IP:' . long2ip($ip);
     $thealarm .= ' App:' . $app;
     $thealarm .= ' Msg:"' . $string;
     Ym_Logger::debug($thealarm);
     return array("time" => $currentTime, "code" => $code, "ip" => $ip, "app" => $app, "msg" => $string, "logid" => $logid, "log" => $log);
 }
Exemplo n.º 3
0
 /**
  * 
  * 检测脚本主要程序(只需要编写该方法即可)
  */
 public function getCheckResult($monitorConfig, $lastTime, $currentTime)
 {
     if (empty($monitorConfig)) {
         Ym_CommonTool::myoutput(FALSE, 'get monitor data error or empty!');
     }
     //$this->plugins($monitorConfig);
     $model = new Model_Alarm_Monitorscript();
     $serviceId = isset($monitorConfig['id']) ? $monitorConfig['id'] : '';
     $log_app_id = isset($monitorConfig['monitor_app']) ? $monitorConfig['monitor_app'] : '';
     $ip = isset($monitorConfig['monitor_ip']) ? $monitorConfig['monitor_ip'] : '';
     $app = isset($monitorConfig['app_name']) ? $monitorConfig['app_name'] : 'system';
     $param = isset($monitorConfig['monitor_param']) ? $monitorConfig['monitor_param'] : '';
     $checkTime = strtotime(date('Y-m-d H:i', $currentTime));
     $code = 0;
     $maxTime = 0;
     $msg = array();
     $logid = 0;
     $log = '';
     $string = '';
     //parse the params
     $alarm_arr = array();
     $param_arr = explode("\n", $param);
     foreach ($param_arr as $v) {
         $arr = explode('=', $v);
         $key = isset($arr[0]) ? $arr[0] : 'error';
         $alarm = isset($arr[1]) ? $arr[1] : 0;
         $alarm_arr[$key] = $alarm;
     }
     $cmd = '/reportapi/accesslog/NumberOfNode';
     $return = Ym_CommonTool::phpCli($cmd, FALSE, TRUE);
     $data = json_decode($return, TRUE);
     foreach ($data['data']['series'] as $k => $v) {
         if (isset($alarm_arr['n']) and $k == $alarm_arr['n']) {
             continue;
         }
         if ($v <= $alarm_arr['c']) {
             $code = 2;
             $string .= $k . ' : <font color="red">' . $v . '</font><br/>';
             continue;
         }
         if ($v <= $alarm_arr['w']) {
             $code = 1;
             $string .= $k . ' : <font color="#f89406">' . $v . '</font><br/>';
             continue;
         }
         $string .= $k . ' : <font color="green">' . $v . '</font><br/>';
     }
     $string .= 'warning:' . $alarm_arr['w'] . ' critical:' . $alarm_arr['c'];
     $thealarm = ' Id:' . $serviceId;
     $thealarm .= ' Time:' . date('Y/m/d H:i:s', $checkTime);
     $thealarm .= ' Code:' . $code;
     $thealarm .= ' IP:' . long2ip($ip);
     $thealarm .= ' App:' . $app;
     $thealarm .= ' Msg:"' . $string;
     Ym_Logger::debug(strip_tags($thealarm));
     return array("time" => $currentTime, "code" => $code, "ip" => $ip, "app" => $app, "msg" => $string, "logid" => $logid, "log" => $log);
 }
Exemplo n.º 4
0
 /**
 * 
 * 检测脚本主要程序(只需要编写该方法即可)
 * 
 * return array(
     		"time"	=>	$checkTime,
     		"code"	=>	$code,
     		"ip"	=>  $ip,
     		"app"	=>  $app,
     		"msg"	=>	$msg,
     		"logid" => 	$logid,
     		"log" 	=> 	$log
     	);
 */
 public function getCheckResult($monitorConfig, $lastTime, $currentTime)
 {
     if (empty($monitorConfig)) {
         Ym_CommonTool::myoutput(FALSE, 'get monitor data error or empty!');
     }
     $serviceId = isset($monitorConfig['id']) ? $monitorConfig['id'] : '';
     $log_app_id = isset($monitorConfig['monitor_app']) ? $monitorConfig['monitor_app'] : '';
     $ip = isset($monitorConfig['monitor_ip']) ? $monitorConfig['monitor_ip'] : '';
     $app = isset($monitorConfig['app_name']) ? $monitorConfig['app_name'] : 'system';
     $service = isset($monitorConfig['monitor_service']) ? $monitorConfig['monitor_service'] : '';
     $param = isset($monitorConfig['monitor_param']) ? explode("\n", $monitorConfig['monitor_param']) : '';
     $maxTime = $this->plugins(array('currentTime' => $currentTime, 'app' => $app));
     $checkTime = strtotime(date('Y-m-d H:i', $maxTime));
     if ($checkTime <= $lastTime) {
         Ym_Logger::debug('weblog.' . $service . ' checktime: ' . $checkTime . ' <= Last: ' . $lastTime . ', pass!');
         $checkTime = strtotime(date('Y-m-d H:i', $currentTime));
     }
     $model = new Model_Alarm_Monitorscript();
     $code = 0;
     $msg = array();
     if (count($param) > 0) {
         foreach ($param as $v) {
             $arr = explode('=', $v);
             $key = isset($arr[0]) ? $arr[0] : 'error';
             $alarm = isset($arr[1]) ? explode(',', $arr[1]) : array(5, 10);
             $alarm_w = isset($alarm[0]) ? $alarm[0] : 5;
             $alarm_c = isset($alarm[1]) ? $alarm[1] : 10;
             $return = $model->{__FUNCTION__}($key, $log_app_id, $lastTime, $checkTime);
             //if (!isset($return['num'])) {Ym_Logger::error('getCheckResult error!');continue;}
             if (isset($return['num'])) {
                 if ($return['num'] >= $alarm_c) {
                     $code = 2;
                 } elseif ($return['num'] < $alarm_c and $return['num'] >= $alarm_w) {
                     $code = 1;
                 }
             }
             $num = (isset($return['num']) and $return['num']) ? $return['num'] : 0;
             $msg[] = array("code" => $code, "key" => $key, "num" => $num, "alarm" => 'w_' . $alarm_w . ' c_' . $alarm_c, "logid" => isset($return['content_id']) ? $return['content_id'] : '', "log" => isset($return['sample']) ? $return['sample'] : '');
         }
     }
     $status_detail = '';
     $logid = '';
     $log = '';
     foreach ($msg as $msgd) {
         $logid .= (isset($msgd['logid']) and $msgd['logid']) ? $msgd['logid'] . '<br/>' : '';
         $log .= (isset($msgd['log']) and $msgd['log']) ? str_replace($msgd['key'], '<font color="red">' . $msgd['key'] . '</font>', $msgd['log']) . '<br/>' : '';
         $color = 'green';
         switch ($msgd['code']) {
             case 0:
                 $color = 'green';
                 break;
             case 1:
                 $color = '#f89406';
                 break;
             case 2:
                 $color = 'red';
                 break;
         }
         $status_detail .= 'Key:' . $msgd['key'];
         $status_detail .= ' Num:<font color="' . $color . '">' . $msgd['num'] . '</font>';
         $status_detail .= ' Alarm:' . $msgd['alarm'];
         $status_detail .= '<br/>';
     }
     $thealarm = ' Id:' . $serviceId;
     $thealarm .= ' Time:' . date('Y/m/d H:i:s', $checkTime);
     $thealarm .= ' Code:' . $code;
     $thealarm .= ' IP:' . long2ip($ip);
     $thealarm .= ' App:' . $app;
     $thealarm .= ' Msg:"' . rtrim($status_detail, '<br/>') . '"';
     Ym_Logger::debug(strip_tags($thealarm));
     return array("time" => $checkTime + 10, "code" => $code, "ip" => $ip, "app" => $app, "msg" => rtrim($status_detail, '<br/>'), "logid" => $logid, "log" => $log);
 }
Exemplo n.º 5
0
 /**
  * 
  * 检测脚本主要程序(只需要编写该方法即可)
  */
 public function getCheckResult($monitorConfig, $lastTime, $currentTime)
 {
     if (empty($monitorConfig)) {
         Ym_CommonTool::myoutput(FALSE, 'get monitor data error or empty!');
     }
     $serviceId = isset($monitorConfig['id']) ? $monitorConfig['id'] : '';
     $log_app_id = isset($monitorConfig['monitor_app']) ? $monitorConfig['monitor_app'] : '';
     $ip = isset($monitorConfig['monitor_ip']) ? $monitorConfig['monitor_ip'] : '';
     $app = isset($monitorConfig['app_name']) ? $monitorConfig['app_name'] : 'system';
     $service = isset($monitorConfig['monitor_service']) ? $monitorConfig['monitor_service'] : '';
     $param = isset($monitorConfig['monitor_param']) ? $monitorConfig['monitor_param'] : '';
     $maxTime = $this->plugins(array('currentTime' => $currentTime, 'app' => $app));
     $checkTime = strtotime(date('Y-m-d H:i', $maxTime));
     if ($checkTime <= $lastTime) {
         Ym_Logger::debug('weblog.' . $service . ' checktime: ' . $checkTime . ' <= Last: ' . $lastTime . ', pass!*');
         $checkTime = strtotime(date('Y-m-d H:i', $currentTime));
     }
     $model = new Model_Alarm_Monitorscript();
     $code = 0;
     $maxTime = 0;
     $msg = array();
     $logid = 0;
     $log = '';
     $string = '';
     $color = 'green';
     $num = 0;
     //parse the params
     $alarm_arr = array();
     $param_arr = explode("\n", $param);
     foreach ($param_arr as $v) {
         $arr = explode('=', $v);
         $key = isset($arr[0]) ? $arr[0] : 'error';
         $alarm = isset($arr[1]) ? $arr[1] : 0;
         $alarm_arr[$key] = $alarm;
     }
     $return = $model->checkLbsError($checkTime);
     if ($return['num'] and $return['num'] >= $alarm_arr['c']) {
         $code = 2;
         $color = 'red';
     } elseif ($return['num'] and $return['num'] >= $alarm_arr['w'] and $return['num'] <= $alarm_arr['c']) {
         $code = 1;
         $color = '#f89406';
     }
     $return['num'] == NULL ? $num = 0 : ($num = $return['num']);
     $string .= date('H:i', $checkTime) . ' = <font color="' . $color . '">' . $num . '</font> (status>=500)<br/>';
     if ($code > 0) {
         $detail = $model->checkLbsErrorDetail($checkTime);
         if ($detail) {
             foreach ($detail as $v) {
                 $string .= $v['host'] . ' ' . $v['path'] . ' ' . $v['status'] . ' ' . $v['server'] . ' = ' . $v['num'] . '<br/>';
             }
         }
     }
     $string .= 'warning:' . $alarm_arr['w'] . ' critical:' . $alarm_arr['c'];
     $thealarm = ' Id:' . $serviceId;
     $thealarm .= ' Time:' . date('Y/m/d H:i:s', $checkTime);
     $thealarm .= ' Code:' . $code;
     $thealarm .= ' IP:' . long2ip($ip);
     $thealarm .= ' App:' . $app;
     $thealarm .= ' Msg:"' . $string;
     Ym_Logger::debug(strip_tags($thealarm));
     return array("time" => $currentTime, "code" => $code, "ip" => $ip, "app" => $app, "msg" => $string, "logid" => $logid, "log" => $log);
 }
Exemplo n.º 6
0
 /**
  * 
  * 检测脚本主要程序(只需要编写该方法即可)
  */
 public function getCheckResult($monitorConfig, $lastTime, $currentTime)
 {
     if (empty($monitorConfig)) {
         Ym_CommonTool::myoutput(FALSE, 'get monitor data error or empty!');
     }
     $serviceId = isset($monitorConfig['id']) ? $monitorConfig['id'] : '';
     $log_app_id = isset($monitorConfig['monitor_app']) ? $monitorConfig['monitor_app'] : '';
     $ip = isset($monitorConfig['monitor_ip']) ? $monitorConfig['monitor_ip'] : '';
     $app = isset($monitorConfig['app_name']) ? $monitorConfig['app_name'] : 'system';
     $service = isset($monitorConfig['monitor_service']) ? $monitorConfig['monitor_service'] : '';
     $param = isset($monitorConfig['monitor_param']) ? $monitorConfig['monitor_param'] : '';
     $maxTime = $this->plugins(array('currentTime' => $currentTime, 'app' => $app));
     $checkTime = strtotime(date('Y-m-d H:i', $maxTime));
     if ($checkTime <= $lastTime) {
         Ym_Logger::debug('weblog.' . $service . ' checktime: ' . $checkTime . ' <= Last: ' . $lastTime . ', pass!*');
         $checkTime = strtotime(date('Y-m-d H:i', $currentTime));
     }
     $model = new Model_Alarm_Monitorscript();
     $code = 0;
     $maxTime = 0;
     $msg = array();
     $logid = 0;
     $log = '';
     $string = '';
     //parse the params
     $alarm_arr = array();
     $param_arr = explode("\n", $param);
     foreach ($param_arr as $v) {
         $arr = explode('=', $v);
         $key = isset($arr[0]) ? $arr[0] : 'error';
         $alarm = isset($arr[1]) ? $arr[1] : 0;
         $alarm_arr[$key] = $alarm;
     }
     $return1 = $model->checkLbsAccess($checkTime - 60);
     $return2 = $model->checkLbsAccess($checkTime);
     $num1 = $return1['num'] ? $return1['num'] : 0;
     $num2 = $return2['num'] ? $return2['num'] : 0;
     $currentdiff = 0;
     $go = 'fall';
     $flug = '↓';
     $color = 'green';
     if ($num1 <= $num2) {
         $go = 'rise';
         $flug = '↑';
     }
     //上涨了
     if ($num1 != 0) {
         $currentdiff = round(abs($num1 - $num2) / $num1 * 100, 2);
     }
     if (isset($alarm_arr['c']) and $currentdiff >= $alarm_arr['c']) {
         $code = 2;
         $color = 'red';
     } elseif (isset($alarm_arr['w']) and $currentdiff >= $alarm_arr['w'] and $currentdiff <= $alarm_arr['c']) {
         $code = 1;
         $color = '#f89406';
     }
     $string .= date('H:i', $checkTime - 60) . ' = ' . $num1 . '<br/>';
     $string .= date('H:i', $checkTime) . ' = ' . $num2 . '<br/>';
     $string .= $go . ' <font color="' . $color . '">' . $currentdiff . '% ' . $flug . '</font><br/>';
     $string .= 'warning:' . $alarm_arr['w'] . '% critical:' . $alarm_arr['c'] . '%';
     $thealarm = ' Id:' . $serviceId;
     $thealarm .= ' Time:' . date('Y/m/d H:i:s', $checkTime);
     $thealarm .= ' Code:' . $code;
     $thealarm .= ' IP:' . long2ip($ip);
     $thealarm .= ' App:' . $app;
     $thealarm .= ' Msg:"' . $string;
     Ym_Logger::debug(strip_tags($thealarm));
     return array("time" => $currentTime, "code" => $code, "ip" => $ip, "app" => $app, "msg" => $string, "logid" => $logid, "log" => $log);
 }