pyzabbix是zabbixAPI的第三方python包装
2019-05-22Zabbix API是基于JSON-RPC 2.0规格,具体实现可以选择任何自己爱好的编程语言,可以采用Perl、Ruby、PHP之类的。
python zabbix api模块较多,使用较为方便。
1、yum -y install python-pyzabbix.noarch
[root@zabbix02 ]# yum search zabbix|grep 36
python36-zabbix-api-erigones.noarch : Zabbix API Python Library
yum -y install python36-zabbix-api-erigones.noarch
from zabbix_api import ZabbixAPI
dir(zabbix_api.ZabbixAPI)
['LOGIN_ERRORS', 'QUERY_COUNT', 'QUERY_EXTEND', 'SORT_ASC', 'SORT_DESC', '_ZabbixAPI__auth', '_ZabbixAPI__password', '_ZabbixAPI__username', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_api_url', '_http_handler', '_http_headers', 'api_version', 'call', 'check_auth', 'convert_datetime', 'debug', 'do_request', 'get_age', 'get_datetime', 'get_severity', 'id', 'init', 'json_obj', 'last_login', 'log', 'logged_in', 'login', 'recent_query', 'relogin', 'set_log_level', 'timestamp_to_datetime']
2、pip install pyzabbix
3、wget https://files.pythonhosted.org/packages/11/ad/24e19d0cf16d05b7ee19f337f02058ee9b760649171865469ccceef83027/pyzabbix-0.7.5.tar.gz
python setup.py build
python setup.py install
4、实例1:
from pyzabbix import ZabbixAPI
zapi.hostgroup.get(filter={'groupid':'xxx'},output=['name','groupid'],selectHosts=['name','hostid'])
zapi = ZabbixAPI("server") #server是指zabbixweb界面的url,比如http://192.168.1.101/zabbix
zapi.login("username","password") #指的是zabbix系统里的用户名和密码,不是服务器本身的用户名和密码
5、实例2:
from pyzabbix import ZabbixAPI
zapi = ZabbixAPI("http://zabbixserver.example.com")
zapi.login("zabbix user", "zabbix pass")
print("Connected to Zabbix API Version %s" % zapi.api_version())
for h in zapi.host.get(output="extend"):
print(h['hostid'])
6、实例3
from zabbix_api import ZabbixAPI
zx = ZabbixAPI(server='http://127.0.0.1')
zx.login('username', 'password')
# Example: list zabbix users
zx.call('user.get', {'output': zx.QUERY_EXTEND})
# Or use the old dot notation method
zx.user.get({'output': zx.QUERY_EXTEND})