The open source Monitoring system Zabbix provides a rich API for third-party systems to invoke.
The basic steps are as follows:
1, obtain legal authentication; connect the corresponding Zabbix URL and provide the user name and password, the HTTP method is "POST", the HTTP header type is "Application/json"
1 Public functionZabbixjsonrequest ($uri,$data) {2 Try{$json _data= Json_encode ($data);3 $c=curl_init ();4curl_setopt ($c, Curlopt_url,$uri);5curl_setopt ($c, Curlopt_customrequest, "POST");6curl_setopt ($c, Curlopt_returntransfer,true);7curl_setopt ($c, Curlopt_post,$json _data);8curl_setopt ($c, Curlopt_postfields,$json _data);9curl_setopt ($c, Curlopt_httpheader,Array(Ten' Content-type:application/json ', One' Content-length: '.strlen($json _data)) A ); -curl_setopt ($c, Curlopt_ssl_verifypeer,false); - $result= Curl_exec ($c); the - returnJson_decode ($result,true); -}Catch(Exception $e) { -Cclog::logerr (' Zabbixinfologic:zabbixjsonrequest Err, '.$e-getMessage ()); + return Array(); - } +}
1 /**2 * @Zabbix Authentication3 * @param $uri Zabbix address4 * @param $username Zabbix user name5 * @param $password Zabbix password6 * @return Permissions7 */8 Public functionZabbixauth ($uri,$username,$password) {9 Try{$data=Array(Ten' Jsonrpc ' = ' 2.0 ', One' Method ' = ' user.login ', A' Params ' =Array( -' User ' =$username, -' Password ' =$password the), -' id ' = ' 1 ' - ); - $response=$this->zabbixjsonrequest ($uri,$data); + return $response[' Result ']; -}Catch(Exception $e) { +Cclog::logerr (' Zabbixinfologic:zabbixauth Err, '.$e-getMessage ()); A return Array(); at } -}
2, call the API to obtain data, after obtaining authentication, according to the need to post packaged data, in the format of JSON, configure different methods to obtain the required data. The method list is available on the official website (https://www.zabbix.com/documentation/3.0/manual/api/reference). The following instance obtains the host ID based on the host IP address
1 /**2 * @ Get hostid based on IP3 * @param $uri Zabbix address4 * @param $authtoken certification information can be obtained through the above Zabbixauth method5 * @param $ip host IP address6 * @return HostID get host ID7 */8 Public functionZabbixgethostidbyip ($uri,$authtoken,$ip) {9 Try{$data=Array(Ten' Jsonrpc ' = ' 2.0 ', One' Method ' = ' host.get ', A' Params ' =Array( -"Output" =>["host"], -"Filter" =Array( the"IP" =$ip, - - ) -), +' id ' = ' 1 ', -' Auth ' =$authtoken + ); A $response=$this->zabbixjsonrequest ($uri,$data); at return $response[' Result '] [0] [' HostID ']; -}Catch(Exception $e) { -Cclog::logerr (' Zabbixinfologic:zabbixgethostidbyip Err, '.$e-getMessage ()); - return Array(); - } -}
PHP gets server monitoring information via Zabbix API