• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

利用ZABBIX API获取服务器信息

php 搞代码 4年前 (2022-01-22) 17次浏览 已收录 0个评论

有时候我们需要监控服务器的运行状态,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。

zabbix提供了很多监控项,那么问题来了,哪些才是我们需要的呢?下面是博主给大家介绍几个常用的监控项:

$items = array(    'vm.memory.size[available]',        // 内存可用值  (KB)    'vm.memory.size[total]',            // 内存总数  (KB)    'system.cpu.util[,idle]',           // 当前CPU IDLE值 (%)    'vfs.fs.size[/,used]',              // 当前 / 盘使用值 <span>!本文来源gaodai#ma#com搞*!代#%^码网5</span><pre>搞gaodaima代码

(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"]

相关推荐:

centos7安装zabbix的详细介绍

zabbix实现邮件报警实例教程

详解关于zabbix监控服务器时间问题的实例

以上就是利用ZABBIX API获取服务器信息的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:利用ZABBIX API获取服务器信息

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址