public function doMonitorAsJobs() { $canCheckTime = time() - 60; $ids = $this->getCheckServiceIds($canCheckTime); $cmd = ''; if ($ids) { Ym_Logger::info('Start monitor check:-----------------------------------------------------'); foreach ($ids as $id) { $status = isset($id['status']) ? $id['status'] : 3; //3默认为unknown $theTime = $id['next_check_time'] ? $id['next_check_time'] : time(); $lastTime = $id['last_check_time'] ? $id['last_check_time'] : $theTime - 60; $lastNotifyTime = $id['last_notify_time'] ? $id['last_notify_time'] : 0; $monitorApi = $id['monitor_api'] ? $id['monitor_api'] : ''; if (!$monitorApi) { Ym_Logger::error('Unknow monitor api on service_id : ' . $id["service_id"] . '!'); continue; } $cmd = '/alarm/monitorscript/' . $monitorApi . '/id/'; $cmd .= $id['service_id'] . '/lastStatus/' . $status; $cmd .= '/lastTime/' . $lastTime . '/currentTime/' . $theTime . '/lastNotifyTime/' . $lastNotifyTime; Ym_CommonTool::phpCli($cmd, TRUE, FALSE); } Ym_Logger::info("End monitor check:-----------------------------------------------------"); } return FALSE; }
/** * * 检测脚本主要程序(只需要编写该方法即可) */ 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); }
/** * * 检测脚本主要程序(只需要编写该方法即可) */ public function getCheckResult($monitorConfig, $lastTime, $currentTime) { if (empty($monitorConfig)) { Ym_CommonTool::myoutput(FALSE, 'get monitor data error or empty!'); } $model = new Model_Alarm_Monitorscript(); $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 = ''; $arr = explode('=', $param); $key = isset($arr[0]) ? $arr[0] : 'error'; $alarm = isset($arr[1]) ? $arr[1] : 0; $cmd = '/reportapi/accesslog/NumberOfNode'; $return = Ym_CommonTool::phpCli($cmd, FALSE, TRUE); $data = json_decode($return, TRUE); //$nodestr = Ym_Config::getAppItem("monitor:mongo.accesslog.tables"); //$nodes = explode(',', $nodestr); $i = 0; foreach ($data['data']['series'] as $k => $v) { if ($i < 3 and $v <= $alarm) { $code = 2; } $string .= $k . ' : ' . $v . '<br/>'; $i++; } $string .= 'alarm_num:' . $alarm; $thealarm = 'Time:' . date('Y/m/d H:i:s', $currentTime); $thealarm .= ' Code:' . $code; $thealarm .= ' IP:' . long2ip($ip); $thealarm .= ' App:' . $app; $thealarm .= ' Msg:"' . $string; Ym_Logger::info(strip_tags($thealarm)); return array("time" => $currentTime, "code" => $code, "ip" => $ip, "app" => $app, "msg" => $string, "logid" => $logid, "log" => $log); }