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
安装:
tar zxf setuptools-19.6.2.tar.gz cd setuptools-19.6.2 python setup.py install
安装pip
下载地址:https://pypi.python.org/pypi/pip/
安装:
tar zxf pip-8.1.0.tar.gz cd pip-8.1.0 python setup.py install
安装Zabbix-API模块
pip install zabbix-api [[email protected] 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:
{ "jsonrpc": "2.0", "method": "host.exists", "params": { "host": "Zabbix Server", "nodeids": [ "1" ] }, "auth": "038e1d7b1735c6a5436ee9eae095879e", "id": 1}
Response:
{ "jsonrpc": "2.0", "result": true, "id": 1}
如何利用zabbix-api查询某主机是否存在呢?
写一个python脚本,内容如下:
vim host_exists.py
#!/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
运行脚本,得到结果为
[[email protected] scripts]# ./host_exists.py True
表示主机存在
Zabbix还提供了很多方法去获取信息,看官网吧。
时间: 2024-10-21 18:12:36