jiaxp 发表于 2015-11-23 13:14:41

zabbix api 使用

  之前一直纠结从mysql表里取数据,因为不清楚怎么取表之间的关系,故只能使用zabbix的api来取数据,使用api后,发现还是很方便的。废话不说,开始!
  


  1.首先php必须支持curl,我使用的是ubuntu,故需要在php.ini配置上curl.so文件。至于怎么找curl.so,大家可以网上下载curl的包来编译。
  修改php.ini,添加extension=cURL.so,so文件自己找准路径啊。
  


  2. 添加php api调用封装如下:
  zbx.inc.php


  


  

<?php
class jsonrpc{   
protected function connect($server, $query){
$http = curl_init($server);
echo $query;
echo &quot;<br/><br/>&quot;;
curl_setopt($http, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($http, CURLOPT_POSTFIELDS, $query);   
curl_setopt($http, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($http, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($http, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($http, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$response = curl_exec($http);
return json_decode($response, true);
curl_close($http);
}
}
class zbx extends jsonrpc{
public $method;
public $access_token;
public $url;
public $query;
function call(){
$data['jsonrpc'] = '2.0';
$data['method'] = $this->method;
$data['params'] = $this->query;
$this->query = '';
if(!empty($this->access_token)) $data['auth'] = $this->access_token;   
$data['id'] = rand(1,100);
$data = json_encode($data, JSON_PRETTY_PRINT);
return $this->connect($this->url, $data);
}
}


?>
  
  



测试的test.php  

<?php
require dirname(__FILE__).'/zbx.inc.php';
$zbx = new zbx;
$zbx->url = 'http://1.2.3.1/zabbix/api_jsonrpc.php';
$zbx->method = 'user.login';
$zbx->query['user'] = 'admin';
$zbx->query['password'] = 'zabbix';
$zbx->access_token = $zbx->call()['result'];
$zbx->method = 'history.get';
$zbx->query['output'] = 'extend';
$zbx->query['history'] = 3;
$zbx->query['itemids'] = '23985';
$zbx->query['sortfield'] = 'clock';
$zbx->query['sortorder'] = 'DESC';
$zbx->query['limit'] = 1;
$hostid = $zbx->call();
var_dump($hostid);

?>
  
  



这里有个地方需要注意的$zbx->query['history'] = 3;这个类型要根据select value_type from items where itemid='23985';   切记!!  


  输出结果如下:
  request:
  

</pre><pre name=&quot;code&quot; class=&quot;html&quot;>{ &quot;jsonrpc&quot;: &quot;2.0&quot;, &quot;method&quot;: &quot;history.get&quot;, &quot;params&quot;: { &quot;output&quot;: &quot;extend&quot;, &quot;history&quot;: 3, &quot;itemids&quot;: &quot;23985&quot;, &quot;sortfield&quot;: &quot;clock&quot;, &quot;sortorder&quot;: &quot;DESC&quot;, &quot;limit&quot;: 1 }, &quot;auth&quot;: &quot;f926d068fc5ba59e222b2c157ef993a1&quot;, &quot;id&quot;: 79 }

result:



array(3) { [&quot;jsonrpc&quot;]=> string(3) &quot;2.0&quot; [&quot;result&quot;]=> array(1) { => array(4) { [&quot;itemid&quot;]=> string(5) &quot;23985&quot; [&quot;clock&quot;]=> string(10) &quot;1416371085&quot; [&quot;value&quot;]=> string(5) &quot;42304&quot; [&quot;ns&quot;]=> string(9) &quot;828133527&quot; } } [&quot;id&quot;]=> int(79) }
  another way:
  


  $zbx->method = 'item.get';
$zbx->query['output'] = 'extend';
$zbx->query['itemids'] = '24038';

$result = $zbx->call()['result']['0'];

echo 'item.key_:' . $result['key_'] . 'lastvalue' . $result['lastvalue'];






  
  
页: [1]
查看完整版本: zabbix api 使用