public function search($params) { $this->load($params); $query = Monitor::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]); //print_r($rownums['nums']);exit; //$values = $params['TraceLogSearch']; return $dataProvider; }
/** * 获得监控项目的列表 */ public static function findItems() { $stt = Monitor::find()->select('id,monitor_name')->indexBy("id")->asArray(TRUE)->all(); $returnArr = []; foreach ($stt as $key => $value) { $returnArr[$key] = $value['monitor_name']; } return $returnArr; }
/** * 获得首页推荐的五个服务器信息 */ public static function find5Column() { //获得推荐的五个数据的信息 //获得五个监控项的信息 $monitorItems = Monitor::find()->where('is_index=1')->limit(5)->orderBy("id desc")->all(); if (empty($monitorItems)) { return []; } return $monitorItems; }
/** * 处理为首页显示 */ public function actionSetindex() { $get = \Yii::$app->request->get(); $result = FALSE; if (isset($get['isindex']) && $get['isindex']) { $is_indx = 0; } else { $is_indx = 1; } if (isset($get['id'])) { $result = Monitor::updateAll(['is_index' => $is_indx], 'id=:id', [':id' => $get['id']]); } if ($result) { $upstatus = TRUE; } else { $upstatus = FALSE; } return $this->renderAjax('setindex', ['upstatus' => $upstatus]); }
/** * 处理昨天的数据 */ 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; }