PHP gets server monitoring information via Zabbix API

Source: Internet
Author: User
Tags getmessage

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.