Zabbix提供了API接口和方法,可以利用方法实现创建主机、删除主机、创建宏、创建模板等目的。
下面列举一些实例,看看如何使用Zabbix-API
zabbix官方文档:https://www.zabbix.com/documentation/2.2/ru/start
要使用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
|
tar
zxf setuptools-19.6.2.
tar
.gz
cd
setuptools-19.6.2
python setup.py
install
|
安装pip
下载地址:https://pypi.python.org/pypi/pip/
安装:
1
2
3
|
tar
zxf pip-8.1.0.
tar
.gz
cd
pip-8.1.0
python setup.py
install
|
安装Zabbix-API模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
pip
install
zabbix-api
[root@dnsserver pip-8.1.0]
# pip list
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
for
Python 2.6
iniparse (0.3.1)
ordereddict (1.2)
pip (8.1.0)
pycurl (7.19.0)
pygpgme (0.1)
setuptools (19.6.2)
urlgrabber (3.9.1)
yum-metadata-parser (1.1.2)
zabbix-api (0.4)
You are using pip version 8.1.0, however version 8.1.2 is available.
You should consider upgrading via the
'pip install --upgrade pip'
command
.
|
Zabbix-API使用演示:
host.exists:
官网介绍:
Request:
Response:
如何利用zabbix-api查询某主机是否存在呢?
写一个python脚本,内容如下:
vim host_exists.py
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/python
#coding:utf-8
from zabbix_api
import
ZabbixAPI
server =
"http://172.16.206.131/zabbix"
username =
"Admin"
password =
"zabbix"
zapi = ZabbixAPI(server=server, path=
""
, log_level=0)
zapi.login(username, password)
result = zapi.host.exists({
"host"
:
"Zabbix server"
})
print result
|
运行脚本,得到结果为
1
2
|
[root@dnsserver scripts]
# ./host_exists.py
True
|
表示主机存在
获取hostid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/usr/bin/python
#coding:utf-8
from zabbix_api
import
ZabbixAPI
import
json
server=
"http://172.16.206.130/zabbix"
username=
"Admin"
password=
"zabbix"
zapi = ZabbixAPI(server=server,path=
""
,log_level=0)
zapi.login(username,password)
##通过计算机名查找hostid
hostinfo=zapi.host.get({
"output"
:
"extend"
,
"filter"
:{
"host"
:
"Zabbix server"
}})
hostid_01=hostinfo[0][
'hostid'
]
print hostid_01
##通过主机可见名查找hostid
hostinfo=zapi.host.get({
"output"
:
"extend"
,
"filter"
:{
"name"
:
"ECS0001"
}})
hostid_02=hostinfo[0][
'hostid'
]
print hostid_02
|
1
2
3
|
[root@ZabbixServer zabbix_api]
# python get.hostid.py
10084
10084
|
本文转自 曾哥最爱 51CTO博客,原文链接:http://blog.51cto.com/zengestudy/1787080,如需转载请自行联系原作者