Zabbix提供了API接口和方法,可以利用方法實作建立主機、删除主機、建立宏、建立模闆等目的。
下面列舉一些執行個體,看看如何使用Zabbix-API
要使用zabbix的API接口,需要用pip安裝zabbix-api子產品。是以我們需要先安裝pip,再安裝zabbix-api子產品,而安裝pip前需要安裝setuptools子產品
安裝setuptools
下載下傳位址:https://pypi.python.org/pypi?%3Aaction=search&term=setuptools&submit=search
安裝:
1
2
3
<code>tar</code> <code>zxf setuptools-19.6.2.</code><code>tar</code><code>.gz</code>
<code>cd</code> <code>setuptools-19.6.2</code>
<code>python setup.py </code><code>install</code>
安裝pip
<code>tar</code> <code>zxf pip-8.1.0.</code><code>tar</code><code>.gz </code>
<code>cd</code> <code>pip-8.1.0</code>
安裝Zabbix-API子產品
4
5
6
7
8
9
10
11
12
13
14
<code>pip </code><code>install</code> <code>zabbix-api</code>
<code>[root@dnsserver pip-8.1.0]</code><code># pip list</code>
<code>DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support </code><code>for</code> <code>Python 2.6</code>
<code>iniparse (0.3.1)</code>
<code>ordereddict (1.2)</code>
<code>pip (8.1.0)</code>
<code>pycurl (7.19.0)</code>
<code>pygpgme (0.1)</code>
<code>setuptools (19.6.2)</code>
<code>urlgrabber (3.9.1)</code>
<code>yum-metadata-parser (1.1.2)</code>
<code>zabbix-api (0.4)</code>
<code>You are using pip version 8.1.0, however version 8.1.2 is available.</code>
<code>You should consider upgrading via the </code><code>'pip install --upgrade pip'</code> <code>command</code><code>.</code>
Zabbix-API使用示範:
官網介紹:
Request:
Response:
<a href="http://s2.51cto.com/wyfs02/M00/85/AB/wKioL1er-oeDh05eAABIQG30D5g247.png" target="_blank"></a>
如何利用zabbix-api查詢某主機是否存在呢?
寫一個python腳本,内容如下:
vim host_exists.py
<code>#!/usr/bin/python</code>
<code>#coding:utf-8</code>
<code>from zabbix_api </code><code>import</code> <code>ZabbixAPI</code>
<code>server = </code><code>"http://172.16.206.131/zabbix"</code>
<code>username = </code><code>"Admin"</code>
<code>password = </code><code>"zabbix"</code>
<code>zapi = ZabbixAPI(server=server, path=</code><code>""</code><code>, log_level=0)</code>
<code>zapi.login(username, password)</code>
<code>result = zapi.host.exists({</code><code>"host"</code><code>:</code><code>"Zabbix server"</code><code>})</code>
<code>print result</code>
運作腳本,得到結果為
<code>[root@dnsserver scripts]</code><code># ./host_exists.py </code>
<code>True</code>
表示主機存在
擷取hostid
15
16
17
18
19
<code>import</code> <code>json</code>
<code>server=</code><code>"http://172.16.206.130/zabbix"</code>
<code>username=</code><code>"Admin"</code>
<code>password=</code><code>"zabbix"</code>
<code>zapi = ZabbixAPI(server=server,path=</code><code>""</code><code>,log_level=0)</code>
<code>zapi.login(username,password)</code>
<code>##通過計算機名查找hostid</code>
<code>hostinfo=zapi.host.get({</code><code>"output"</code><code>:</code><code>"extend"</code><code>,</code><code>"filter"</code><code>:{</code><code>"host"</code><code>:</code><code>"Zabbix server"</code><code>}})</code>
<code>hostid_01=hostinfo[0][</code><code>'hostid'</code><code>]</code>
<code>print hostid_01</code>
<code>##通過主機可見名查找hostid</code>
<code>hostinfo=zapi.host.get({</code><code>"output"</code><code>:</code><code>"extend"</code><code>,</code><code>"filter"</code><code>:{</code><code>"name"</code><code>:</code><code>"ECS0001"</code><code>}})</code>
<code>hostid_02=hostinfo[0][</code><code>'hostid'</code><code>]</code>
<code>print hostid_02</code>
<code>[root@ZabbixServer zabbix_api]</code><code># python get.hostid.py </code>
<code>10084</code>
<code></code>
本文轉自 曾哥最愛 51CTO部落格,原文連結:http://blog.51cto.com/zengestudy/1787080,如需轉載請自行聯系原作者