public function actionShit() { //Init curl /** * cpu demo * curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"history.get","params":{"history":0,"itemids":["99612"],"time_from":"1410403076.3190279","time_till":"1410489466.6890171","output":"extend"},"auth": "4023ceeb084e87211f2373626a7b1ea5","id": 0}' http://192.168.49.90/zabbix/api_jsonrpc.php; */ $hashString = self::hashString(); if (!$hashString) { echo 11; } //配置请求参数 $endtime = time(); $starttime = strtotime("-10 min", $endtime); $postData = ['jsonrpc' => '2.0', 'method' => 'history.get', 'params' => ['history' => 0, 'itemids' => 23660, 'time_from' => $starttime, 'time_till' => $endtime, 'output' => 'extend'], 'id' => 0, 'auth' => $hashString]; $reposeData = ZabbixCurlService::curlPostResult($postData); if ($reposeData['status'] === false) { return FALSE; } print_r($reposeData['info']); }
/** * 通过curl POST数据后获得信息 * @return type */ public static function curlPostResult($postData, $auth = false) { if ($auth === FALSE) { if (ZabbixCurlService::hashString() === FALSE) { return ['status' => FALSE, 'error' => 'Auth incorrect']; } $postData['auth'] = ZabbixCurlService::$hashString; } else { $postData['auth'] = null; } //Init curl $curl = new Curl(); $options = [CURLOPT_HTTPHEADER => self::$header, CURLOPT_TIMEOUT => 60]; if (!empty($postData) && is_array($postData)) { $jsonString = json_encode($postData); $curl = $curl->setOption(CURLOPT_POSTFIELDS, $jsonString); } if (!empty($options) && is_array($options)) { foreach ($options as $key1 => $value1) { $curl = $curl->setOption($key1, $value1); } } $url = \Yii::$app->params['zabbixapiurl']; $response = $curl->post($url); switch ($curl->responseCode) { case 'timeout': return ['status' => FALSE, 'error' => 'timeout']; break; case 200: return ['status' => TRUE, 'info' => json_decode($response, FALSE)]; break; case 404: return ['status' => FALSE, 'error' => '404']; break; } }
/** * 处理昨天的数据 */ public static function fitZabbixYestoday() { //如果说程序突然OVER了,所以就找昨天没有数据的 //数据太多了,只能取一个钟头的数据,要不会中弹,安全保证标准是一分钟处理6个数据,一个钟头就处理360个作为上限 //配置昨天的参数信息 $date = date("Y-m-d H:00:00", strtotime("-1 hour")); //时间标记 $fitdate = date("Y-m-d H:30:00", strtotime("-1 hour")); $timemark = strtotime($fitdate); $starttime = strtotime($date); $endtime = strtotime(date("Y-m-d H:00:00")); $fitIdLists = MonitorLog::find()->asArray()->distinct("monitor_id")->where("log_date=:lastDate", [':lastDate' => $date])->all(); if (empty($fitIdLists)) { //为空时处理所有的ID $fitArrayLists = Monitor::find()->orderBy("id asc")->limit(360)->all(); } else { $donotfitstring = array(); foreach ($fitIdLists as $oneFit) { $donotfitstring[] = $oneFit['monitor_id']; } //只处理还没有处理的ID,当前语句未处理完整,后续测试再处理 $fitArrayLists = Monitor::find()->where(['not in', 'id', $donotfitstring])->orderBy("id asc")->limit(360)->all(); } //获得需要处理的数据,默认只处理(昨天还没有处理的)500个数据,超过500个的话,那么就需要做优化了 if (empty($fitArrayLists)) { echo '没有可以处理的数据'; \Yii::$app->end(); } foreach ($fitArrayLists as $oneDate) { //直接处理 //通过接口获得数据 $postData = ['jsonrpc' => '2.0', 'method' => 'history.get', 'params' => ['history' => 0, 'itemids' => $oneDate->monitor_item, 'time_from' => $starttime, 'time_till' => $endtime, 'output' => 'extend'], 'id' => 0, 'auth' => '']; $reposeData = ZabbixCurlService::curlPostResult($postData, FALSE); //没有找到对应数据时处理异常 if ($reposeData['status'] === false || empty($reposeData['info']->result)) { //没有信息就继续处理下一个 continue; } //对得到的数据进行平均值处理 $logtime = [0 => ['value' => 0, 'count' => 0], 1 => ['value' => 0, 'count' => 0]]; foreach ($reposeData['info']->result as $oneResDate) { if ($oneResDate->clock <= $timemark) { $logtime[0]['value'] += $oneResDate->value; $logtime[0]['count'] += 1; } else { $logtime[1]['value'] += $oneResDate->value; $logtime[1]['count'] += 1; } } $monitorlogs = [[$oneDate->id, round($logtime[0]['value'] / $logtime[0]['count'], 2), $date], [$oneDate->id, round($logtime[0]['value'] / $logtime[0]['count'], 2), $fitdate]]; //处理平均值并入库 try { $command = \Yii::$app->db->createCommand(); $command->batchInsert(MonitorLog::tableName(), ['monitor_id', 'serize_string', 'log_date'], $monitorlogs); $command->execute(); } catch (yii\db\Exception $e) { //记录读到的最后一行,没有处理的行会被后退 print_r($e); continue; } } }
/** * 取得一个钟头的数据,这个数据会跟频率有关,因为一个钟头的数据有600条 */ public static function fitDetailData() { //获得ID,获得时间,详细时间 $monitor_id = \Yii::$app->request->get('monitor_id'); $date = \Yii::$app->request->get('date'); $detialtime = \Yii::$app->request->get('detialtime'); //如果这两个都为空那么就直接出错吧 if (empty($monitor_id) || empty($date) || empty($detialtime)) { return []; } //获得指定数据的信息 $oneItem = Monitor::find()->where("id=:id", [':id' => $monitor_id])->one(); if (!$oneItem) { return []; } $fitendtime = $date . date(" H:i:s", strtotime($detialtime)); $fitendtimestr = strtotime($fitendtime); $fitstarttimestr = $fitendtimestr - 1800; //先得到当前一排的数据 //配置数据并向ZABBIX获得数据 //配置请求参数 $postData = ['jsonrpc' => '2.0', 'method' => 'history.get', 'params' => ['history' => 0, 'itemids' => $oneItem->monitor_item, 'time_from' => $fitstarttimestr, 'time_till' => $fitendtimestr, 'output' => 'extend'], 'id' => 0, 'auth' => '']; $reposeData1 = ZabbixCurlService::curlPostResult($postData, FALSE); //没有找到对应数据时处理异常 if ($reposeData1['status'] === false) { $reposeData1 = []; } if (empty($reposeData1['info']->result)) { $reposeData1 = []; } //得到第二排的数据 $postData = ['jsonrpc' => '2.0', 'method' => 'history.get', 'params' => ['history' => 0, 'itemids' => $oneItem->monitor_item, 'time_from' => $fitstarttimestr - 86400, 'time_till' => $fitendtimestr - 86400, 'output' => 'extend'], 'id' => 0, 'auth' => '']; $reposeData2 = ZabbixCurlService::curlPostResult($postData, FALSE); //没有找到对应数据时处理异常 if ($reposeData2['status'] === false) { $reposeData2 = []; } if (empty($reposeData2['info']->result)) { $reposeData2 = []; } //如果全为空就返回没数据 if (empty($reposeData1) && empty($reposeData2)) { return []; } //配置数据 //二级处理数据其实只需要处理series.data xAxis.data dataZoom这三组数据的设置 $seriesdata[1] = []; $seriesdata[2] = []; $xAxisdata = []; $nums = 1; if (!empty($reposeData1)) { $nums = count($reposeData1['info']->result); foreach ($reposeData1['info']->result as $oneDate) { $xAxisdata[] = date('H:i:s', $oneDate->clock); $ceilnum = $oneDate->value * 1000; if ($ceilnum > 0) { $ceilnum += 10; } $ceilnum = ceil($ceilnum); $ceilnum = $ceilnum / 1000; $seriesdata[1][] = floatval(round($ceilnum, 2)); if (empty($reposeData2)) { $seriesdata[2][] = 0; } } } if (!empty($reposeData2)) { $nums = count($reposeData2['info']->result); foreach ($reposeData2['info']->result as $oneDate) { $xAxisdata[] = date('H:i:s', $oneDate->clock); $ceilnum = $oneDate->value * 1000; if ($ceilnum > 0) { $ceilnum += 10; } $ceilnum = ceil($ceilnum); $ceilnum = $ceilnum / 1000; $seriesdata[2][] = floatval(round($ceilnum, 2)); if (empty($reposeData1)) { $seriesdata[1][] = 0; } } } //处理显示的比例,初始显示48条柱子,计算显示的比例 $showlimit = round(4800 / $nums, 2); if ($showlimit > 100) { $showlimit = 100; } $fitShowlimit = 100 - $showlimit; //返回处理后的数组 $fitReturnArray = ['seriesdata1' => $seriesdata[1], 'seriesdata2' => $seriesdata[2], 'xAxisdata' => $xAxisdata, 'dataZoom' => [['type' => 'slider', 'start' => $fitShowlimit, 'end' => 100], ['type' => 'inside', 'start' => $fitShowlimit, 'end' => 100]]]; return $fitReturnArray; }