有時候我們需要監控伺服器的運作狀態,ZABBIX就是這樣一個線上監控系統。同時ZABBIX提供了API等方式供其他程式取得資料 ,本文就以PHP實例程式碼分享來讓大家了解如何透過ZABBIX取得伺服器資訊。
由於我們本身就安裝了zabbix系統,所以我只用知道如何取得資訊即可,總結有兩種方法可以取得。
安裝可以參考:centos7安裝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。
$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語句了,大家湊合看。
$host_id = Host::where('host', '10.50.150.80')->value('hostid');
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 ] }
$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; }