目的
利用 PHP 调用 zabbix api 接口, 查询主机, 并返回下面 JSON 数据
内存可用值 (KB)
内存总数 (KB)
当前 CPU IDLE 值 100%
当前 / 盘使用值 (KB)
当前 / 盘总数 (KB)
调用方法
http://zabbix.xxxx.com/ext/get_resource.php?host=hostname,hostname,hostname
说明
可以输入一台或以上主机进行查询, 以逗号进行主机名分隔
返回结果说明
可能会出现三种情况
1 主机已在 ZABBIX 注册, 并返回正常的数据
2 主机已经在 ZABBIX 注册, 但 ZABBIX AGENT 没有打开, 无法获得当前值
3. 主机可能是 WINDOWS 或者没有在 ZABBIX 中注册, 无法获得任何数据
测试返回说明
主机已在 ZABBIX 注册, 并返回正常的数据
http://zabbix.xxx.com/ext/get_resource.php?host=terry-w6im9.vclound.com,pjstaging-x225n.vclound.com,terry-elk-exquv.vclound.com
{
"hostlist": {
"terry-w6im9.vclound.com": {
"vm.memory.size[available]": "3874770944",
"vm.memory.size[total]": "4018950144",
"system.cpu.util[,idle]": "99.5082",
"vfs.fs.size[/,used]": "6038204416",
"vfs.fs.size[/,total]": "21002579968"
},
"pjstaging-x225n.vclound.com": {
"vm.memory.size[available]": "2140098560",
"vm.memory.size[total]": "4018950144",
"system.cpu.util[,idle]": "98.5289",
"vfs.fs.size[/,used]": "20229439488",
"vfs.fs.size[/,total]": "21002579968"
},
"terry-elk-exquv.vclound.com": {
"vm.memory.size[available]": "3793350656",
"vm.memory.size[total]": "4018728960",
"system.cpu.util[,idle]": "99.6417",
"vfs.fs.size[/,used]": "3584937984",
"vfs.fs.size[/,total]": "21002579968"
}
}
}
主机已经在 ZABBIX 注册, 但 ZABBIX AGENT 没有打开, 无法获得当前值
http://zabbix.vclound.com/ext/get_resource.php?host=terry-w6im9.vclound.com,terry-zskvt.vclound.com
{
"hostlist": {
"terry-w6im9.vclound.com": {
"vm.memory.size[available]": "3874770944",
"vm.memory.size[total]": "4018950144",
"system.cpu.util[,idle]": "99.5082",
"vfs.fs.size[/,used]": "6038208512",
"vfs.fs.size[/,total]": "21002579968"
},
"terry-zskvt.vclound.com": {
"vm.memory.size[available]": null,
"vm.memory.size[total]": null,
"system.cpu.util[,idle]": null,
"vfs.fs.size[/,used]": null,
"vfs.fs.size[/,total]": null
}
}
}
主机可能是 WINDOWS 或者没有在 ZABBIX 中注册, 无法获得任何数据,
http://zabbix.vclound.com/ext/get_resource.php?host=terry-w6im9.vclound.com,terry.gz.vclound.com
{
"hostlist": {
"terry-w6im9.vclound.com": {
"vm.memory.size[available]": "3875835904",
"vm.memory.size[total]": "4018950144",
"system.cpu.util[,idle]": "99.4999",
"vfs.fs.size[/,used]": "6039134208",
"vfs.fs.size[/,total]": "21002579968"
},
"terry.gz.vclound.com": {
"vm.memory.size[available]": null,
"vm.memory.size[total]": null,
"system.cpu.util[,idle]": null,
"vfs.fs.size[/,used]": null,
"vfs.fs.size[/,total]": null
}
}
}
api 缺点
数据返回时间值太慢
经过测试, 30 台主机并发查询,
返回时间结果约 60 秒
参考 api 接口 php
<?php
$hostname=$_GET['host'];
$historylimit=1;
$url = 'http://zabbix.xxxxx.com/zabbix/api_jsonrpc.php';
$header = array("Content-type: application/json-rpc");
// get token start
function Curl($url,$header, $info){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $info);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
}
function get_token($url, $header)
{
$user = 'admin';
$password = '*******';
$logininfo = array(
'jsonrpc' => '2.0',
'method' => 'user.login',
'params' => array(
'user' => $user,
'password' => $password,
),
'id' => 1,
);
$data = json_encode($logininfo);
$result = Curl($url, $header, $data);
$token = $result -> result;
return $token;
}
// get token end
// 用于吧 classobject 返回为数组
function object_array($array)
{
if(is_object($array))
{
$array = (array)$array;
}
if(is_array($array))
{
foreach($array as $key=>$value)
{
$array[$key] = object_array($value);
}
}
return $array;
}
// get hostid
function get_hostid($hostname, $token, $header, $url)
{
$gethostid = array(
'jsonrpc' => '2.0',
'method' => 'host.get',
"params" => array(
"output" => ["hostid"],
"filter" => array(
"host" => [$hostname]
)
),
"auth"=>$token,
"id"=>1
);
$data = json_encode($gethostid);
$result = Curl($url,$header,$data);
$hostinfo= $result -> result;
if ( empty($hostinfo) )
{
$hostid ='';
}else
{
$hostid = $hostinfo[0] -> hostid;
}
return $hostid;
}
// get itemid
function get_itemid($hostid, $key, $token, $header, $url)
{
$getitemid = array(
"jsonrpc" => "2.0",
"method" => "item.get",
"params" => array(
"output" => ["itemids"],
"hostids" => $hostid,
"search" => array(
"key_" => $key
),
"sortfield" => "name"
),
"auth" => $token,
"id" => 1
);
$data = json_encode($getitemid);
$result = Curl($url,$header,$data);
$iteminfo = $result -> result;
if ( empty($iteminfo ))
{
$itemid = 'null';
}else
{
$itemid = $iteminfo[0] -> itemid;
}
return $itemid;
}
// get zabbix value
function get_resource($itemid, $token, $header, $url, $history, $historylimit)
{
$gethistory = array(
"jsonrpc" => "2.0",
"method" => "history.get",
"params" => array(
"output" => "extend",
"history" => $history,
"itemids" => $itemid,
"sortfield" => "clock",
"sortorder" => "DESC",
"limit" => $historylimit
),
"auth" => $token,
"id" => 1
);
$data = json_encode($gethistory);
$result = Curl($url,$header,$data);
$historytotal = $result -> result;
$resource = object_array($historytotal);
return $resource;
}
// 逻辑开始
if ( empty($hostname) )
{
echo "hostname not exists";
exit;
}else
{
$hostlist = explode(",",$hostname);
$hostlistarr = array( 'hostlist' => array());
foreach( $hostlist as $hostname )
{
$source = array('vm.memory.size[available]', 'vm.memory.size[total]', 'system.cpu.util[,idle]', 'vfs.fs.size[/,used]', 'vfs.fs.size[/,total]' );
$hostarr = array( 'hostlist' => array($hostname => array() ));
foreach ($source as $key)
{
if ( $key == 'system.cpu.util[,idle]' )
{
$history = 0;
}else
{
$history = 3;
}
// $zabbix_info = get_info($url, $header, $token, $hostname, $key, $history, $historylimit);
$token = get_token($url, $header);
$hostid = get_hostid($hostname, $token, $header, $url);
if ( empty($hostid) )
{
$json_info = array( 'hostlist' => array( $hostname => array( $key => 'null')));
$hostarr = array_merge_recursive( $hostarr, $json_info );
continue;
}
$itemid = get_itemid($hostid, $key, $token,$header, $url);
if ( empty($itemid) )
{
$json_info = array('hostlist' => array( $hostname => array( $key => 'null' )));
$hostarr = array_merge_recursive( $hostarr, $json_info );
continue;
}
$resource = get_resource($itemid, $token, $header, $url, $history, $historylimit);
$zabbix_info = $resource[0]['value'];
$json_info = array( 'hostlist' => array($hostname => array( $key => $zabbix_info )) );
$hostarr = array_merge_recursive( $hostarr, $json_info );
}
$hostlistarr = array_merge_recursive( $hostlistarr, $hostarr);
}
echo json_encode($hostlistarr);
}
?>
重写
重新修改, 直接通过数据库查询方式获得对应数据
特点
取 7 天中的最大 (磁盘使用空间) 最小值(CPU 空闲, 可用内存)
经测试 365 台主机并发查询, 返回数据约 18 秒
参考 php
<?php
$hostname = $_POST['host'];
if ( empty ($hostname) )
{
$hostname = $_GET['host'];
if ( empty ($hostname) )
{
$err_info = array( 'hostlist' => array( 'error' ));
echo json_encode($err_info);
exit;
}
}
$hostlist = explode(",",$hostname);
$target = array('vm.memory.size[available]', 'vm.memory.size[total]', 'system.cpu.util[,idle]', 'vfs.fs.size[/,used]', 'vfs.fs.size[/,total]' );
$hostarr = array( 'hostlist' => array($hostname => array() ));
$hostlistarr = array( 'hostlist' => array());
/*
$gz_zabbix_db = 'XXXXX:3306';
$sh_zabbix_db = 'XXXX:3306';
*/
function get_value($hostname, $sql, $zabbix_db)
{
$zabbix_user = 'your db username';
$zabbix_password = 'user db password';
$link = mysql_connect($zabbix_db, $zabbix_user, $zabbix_password);
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
mysql_close($link);
return $row[0];
}
foreach( $hostlist as $hostname )
{
$hostarr = array( 'hostlist' => array($hostname => array() ));
if ( strstr( $hostname, 'sh.vclound.com', true) )
{
$zabbix_db = 'your db link1:3306';
}else
{
$zabbix_db = 'your db link2:3306';
}
foreach ( $target as $index )
{
if ( $index == 'vm.memory.size[available]' )
{
$db_column = 'min(value_min)';
}else
{
$db_column = 'max(value_max)';
}
if ( $index == 'system.cpu.util[,idle]' )
{
$db_name = 'zabbix.trends';
$db_column = 'min(value_min)';
}else
{
$db_name = 'zabbix.trends_uint';
}
$sql = "select " . $db_column . " value from " . $db_name . " a, zabbix.items b, zabbix.hosts c where a.itemid=b.itemid and b.hostid = c.hostid and b.key_='" . $index . "' and a.clock > ( unix_timestamp() - 7200*24*7) and c.name='" . $hostname . "';";
$db_value = get_value($hostname, $sql, $zabbix_db);
$json_info = array( 'hostlist' => array($hostname => array( $index => $db_value )) );
$hostarr = array_merge_recursive( $hostarr, $json_info );
}
$hostlistarr = array_merge_recursive( $hostlistarr, $hostarr);
}
echo json_encode($hostlistarr);
?>