最近项目需要获取linux主机的一些信息,如CPU使用率,内存使用情况等。由于我们本身就装了zabbix系统,所以我只用知道如何获取信息即可,总结有两种方法可以获取。
一、通过ZABBIX API获取主机信息
这种方式获取的主机信息相对是比较新的(每分钟更新一次)。但因为每次都需要请求接口,所以相对比较慢,如果并发查询的主机数量比较多,就会非常慢。
开源监控系统ZABBIX的官方文档提供了丰富的API。我这里http请求是用的Guzzle 6。当然你也可以用php内置的curl函数自己写一个http请求,非常简单。
1、用户认证以获取token。
$responst = $this->httpClient->request('POST', 'http://zabbix.xxxxx.com/zabbix/api_jsonrpc.php', [
'headers' => [
'Content-Type' => 'application/json-rpc',
],
'json' => [
'jsonrpc' => '2.0',
'method' => 'user.login',
'params' => [
"user"=> 'your username',
"password"=> 'your password'
],
'id' => 1,
'auth' => null
],
]);
由于这里是用户认证,所有 auth 可以直接写 null 。返回结果为:
{
"jsonrpc": "2.0",
"result": "0424bd59b807674191e7d77572075f33",
"id": 1
}
result 里就是 token ,在后面的请求中都是需要的。
2、根据主机的IP获取hostid。
$responst = $this->httpClient->request('POST', 'http://zabbix.xxxxx.com/zabbix/api_jsonrpc.php', [
'headers' => [
'Content-Type' => 'application/json-rpc',
],
'json' => [
'jsonrpc' => '2.0',
'method' => 'host.get',
'params' => [
"output" => ["hostid"],
"filter" => [
"host" => '192.168.1.1'
]
],
'id' => 1,
'auth' =>"0424bd59b807674191e7d77572075f33"
], ]);
上面的 output 是限制返回项,如果想要返回所有的主机信息,可以去掉 output 。上面请求的返回结果为:
{
"jsonrpc": "2.0",
"result": [
{
"hostid": "10160",
}
],
"id": 1
}
3、获取主机的监控项itemid。
zabbix提供了很多监控项,那么问题来了,哪些才是我们需要的呢?下面是博主给大家介绍几个常用的监控项:
$items = array(
'vm.memory.size[available]', // 内存可用值 (KB)
'vm.memory.size[total]', // 内存总数 (KB)
'system.cpu.util[,idle]', // 当前CPU IDLE值 (%)
'vfs.fs.size[/,used]', // 当前 / 盘使用值 (KB)
'vfs.fs.size[/,total]', // 当前 / 盘总数 (KB)
);
$item_ids = array();
foreach ($items as $item) {
$responst = $this->httpClient->request('POST', $this->url, [
'headers' => [
'Content-Type' => 'application/json-rpc',
],
'json' => [
'jsonrpc' => $this->jsonrpc,
'method' => $this->METHOD_ITEM_GET,
'params' => [
"output" => 'extend',
"hostids" => $this->hostid,
"search" => [
"key_" => $item
],
'sortfield' => 'name'
],
'id' => 1,
'auth' => $this->token
],
]);
$body = json_decode($responst->getBody()->getContents());
$item_ids[] = $body->result[0]->itemid;
}
返回的结果为:
{
"jsonrpc": "2.0",
"result": [
{
"itemid": "23298",
"type": "0",
"snmp_community": "",
"snmp_oid": "",
"hostid": "10084",
"name": "Context switches per second",
"key_": "vm.memory.size[available]",
"delay": "60",
"history": "7",
"trends": "365",
"lastvalue": "2552",
"lastclock": "1351090998",
"prevvalue": "2641",
"state": "0",
"status": "0",
"value_type": "3",
"trapper_hosts": "",
"units": "sps",
"multiplier": "0",
"delta": "1",
"snmpv3_securityname": "",
"snmpv3_securitylevel": "0",
"snmpv3_authpassphrase": "",
"snmpv3_privpassphrase": "",
"snmpv3_authprotocol": "0",
"snmpv3_privprotocol": "0",
"snmpv3_contextname": "",
"formula": "1",
"error": "",
"lastlogsize": "0",
"logtimefmt": "",
"templateid": "22680",
"valuemapid": "0",
"delay_flex": "",
"params": "",
"ipmi_sensor": "",
"data_type": "0",
"authtype": "0",
"username": "",
"password": "",
"publickey": "",
"privatekey": "",
"mtime": "0",
"lastns": "564054253",
"flags": "0",
"interfaceid": "1",
"port": "",
"description": "",
"inventory_link": "0",
"lifetime": "0",
"evaltype": "0"
}
],
"id": 1
}
4、获取对应监控项的历史信息
上一步中我们获取到了所有对应监控项的itemid。现在获取这些监控项的历史信息。这个接口中的信息是每分钟更新一次的,所以具体要去多久的信息看各自的需求。
$items_result = array();
foreach ($this->itemids as $k=>$itemid) {
if($this->items[$k] == 'system.cpu.util[,idle]') {
$history = 0;
}else {
$history = 3;
} $responst = $this->httpClient->request('POST', 'http://zabbix.xxxxx.com/zabbix/api_jsonrpc.php', [
'headers' => [
'Content-Type' => 'application/json-rpc',
],
'json' => [
'jsonrpc' => '2.0',
'method' => 'history.get',
'params' => [
"output" => 'extend',
"history" => $history,
"itemids" => $itemid,
"sortfield" => 'clock',
'sortorder' => 'DESC',
'limit' => '1',
],
'id' => 1,
'auth' => $this->token
],
]);
$body = json_decode($responst->getBody()->getContents()); if(property_exists($body, 'result')) {
$items_result[$this->items[$k]] = $body->result[0]->value;
}else {
Log::error(json_encode($body));
return false;
}
}
返回结果为:
{
"jsonrpc": "2.0",
"result": [
{
"itemid": "23296",
"clock": "1351090996",
"value": "0.0850",
"ns": "563157632"
},
{
],
"id": 1
}
最终的结果应该为:
array:5 [▼
"system.cpu.util[,idle]" => 98.9622
"vfs.fs.size[/,total]" => "42141548544"
"vfs.fs.size[/,used]" => "6917797137"
"vm.memory.size[available]" => "57394996906"
"vm.memory.size[total]" => "67439050752"
]
二、直接从数据库获取信息
这种方式获取的数据并不是最新的(每小时更新一次)。但查询速度大大的提升了。
因为我是用laravel框架写的代码,所有就偷懒一下,不写原生的sql语句了,大家凑合看。
1、通过ip从hosts表获取hostid
$host_id = Host::where('host', '10.50.150.80')->value('hostid');
返回结果为: 11101
2、通过hostid从items表获取items监控项的itemid
$items = array(
'vm.memory.size[available]', // 内存可用值 (KB)
'vm.memory.size[total]', // 内存总数 (KB)
'system.cpu.util[,idle]', // 当前CPU IDLE值 (%)
'vfs.fs.size[/,used]', // 当前 / 盘使用值 (KB)
'vfs.fs.size[/,total]', // 当前 / 盘总数 (KB)
); $item_ids = Item::where('hostid', 11106)->whereIn('key_', $items)->pluck('itemid', 'key_');
返回结果为:
Collection {#183 ▼
#items: array:5 [▼
"system.cpu.util[,idle]" => 152511
"vfs.fs.size[/,total]" => 155584
"vfs.fs.size[/,used]" => 155587
"vm.memory.size[available]" => 152533
"vm.memory.size[total]" => 152534
]
}
3、通过itemid从trends表或trends_uint表获取历史信息
$result = array();
foreach ($item_ids as $key=>$item_id) {
if($key == 'system.cpu.util[,idle]') {
$value = Trend::where('itemid', $item_id)->orderBy('clock', 'DESC')->value('value_avg');
}else {
$value = TrendsUint::where('itemid', $item_id)->orderBy('clock', 'DESC')->value('value_avg');
}
$result[$key] = $value;
}
返回结果为:
array:5 [▼
"system.cpu.util[,idle]" => 98.9622
"vfs.fs.size[/,total]" => "42141548544"
"vfs.fs.size[/,used]" => "6917797137"
"vm.memory.size[available]" => "57394996906"
"vm.memory.size[total]" => "67439050752"
]