PHP通過Zabbix API擷取伺服器監控資訊

來源:互聯網
上載者:User

標籤:logs   param   ip地址   put   取資料   type   word   str   inf   

開源監控系統Zabbix提供了豐富的API,供第三方系統調用。

基本步驟如下:

1、擷取合法認證;串連對應Zabbix URL,並提供使用者名稱和密碼,HTTP方法為“POST”,HTTP頭部類型為“application/json”

 1 public function zabbixJsonRequest($uri, $data) { 2         try{$json_data = json_encode($data); 3             $c = curl_init(); 4             curl_setopt($c, CURLOPT_URL, $uri); 5             curl_setopt($c, CURLOPT_CUSTOMREQUEST, "POST"); 6             curl_setopt($c, CURLOPT_RETURNTRANSFER, true); 7             curl_setopt($c, CURLOPT_POST, $json_data); 8             curl_setopt($c, CURLOPT_POSTFIELDS, $json_data); 9             curl_setopt($c, CURLOPT_HTTPHEADER, array(10                     ‘Content-Type: application/json‘,11                     ‘Content-Length: ‘ . strlen($json_data))12             );13             curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);14             $result = curl_exec($c);15 16             return json_decode($result, true);17         }catch(Exception $e) {18             CCLog::LogErr(‘ZabbixInfoLogic: zabbixJsonRequest Err ->‘ . $e->getMessage());19             return array();20         }21     }
 1     /** 2      * @Zabbix鑒權 3      * @param  $uri Zabbix地址 4      * @param  $username Zabbix使用者名稱 5      * @param  $password Zabbix密碼 6      * @return 許可權 7      */ 8     public function zabbixAuth($uri, $username, $password) { 9         try{$data = array(10             ‘jsonrpc‘ => "2.0",11             ‘method‘ => "user.login",12             ‘params‘ => array(13                 ‘user‘ => $username,14                 ‘password‘ => $password15             ),16             ‘id‘ => "1"17         );18             $response = $this->zabbixJsonRequest($uri, $data);19             return $response[‘result‘];20         }catch(Exception $e) {21             CCLog::LogErr(‘ZabbixInfoLogic: zabbixAuth Err ->‘ . $e->getMessage());22             return array();23         }24     }

 

2、調用API擷取資料;取得認證後,根據需要POST封裝好的data,格式為json,配置不同的方法擷取需要的資料。方法列表可在官方網站(https://www.zabbix.com/documentation/3.0/manual/api/reference)查閱。以下執行個體根據主機IP地址擷取主機ID

 1     /** 2      * @根據IP擷取hostid 3      * @param  $uri Zabbix地址 4      * @param  $authtoken 認證資訊 可通過上述zabbixAuth方法擷取 5      * @param  $ip 主機IP地址 6      * @return hostid 擷取主機ID 7      */ 8     public function zabbixGetHostIdByIp($uri, $authtoken,$ip) { 9         try{$data = array(10             ‘jsonrpc‘ => "2.0",11             ‘method‘ => "host.get",12             ‘params‘ => array(13                 "output"=>[ "host" ],14                 "filter" => array(15                     "ip" => $ip,16 17                 )18             ),19             ‘id‘ => "1",20             ‘auth‘ => $authtoken21         );22             $response = $this->zabbixJsonRequest($uri, $data);23             return $response[‘result‘][0][‘hostid‘];24         }catch(Exception $e) {25             CCLog::LogErr(‘ZabbixInfoLogic: zabbixGetHostIdByIp Err ->‘ . $e->getMessage());26             return array();27         }28     }

 

PHP通過Zabbix API擷取伺服器監控資訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.