zabbix的API |
api體系: cobbler的api,zabbix的api,saltstack的api
不僅有api還有吩咐的例子,只有會簡單的python就可以了
在cmdb中錄入ip,角色,dns自動解析,cobbler自動裝機。zabbix自動添加這個角色的模板。
複雜的cmdb至少有五六十列,根據不同的維度進行不同的劃分。
api的共性就是先是獲取tocken然後帶着tocken工作。
API最大的好處有兩點:一是連接世界,二是避免重複。
我們身處的時代到處是分享、轉載、評論,這裏面的核心都是API的使用,可以說API無處不在。同時對於產品設計而言,API既可以豐富產品功能,又能使開發更加方便,避免重複造輪子
更多api調用例子參考zabbix手冊:https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/host/get 1、 獲取主機的信息 curl -s -X POST -H 'Content-Type: application/json' -d ' { "jsonrpc": "2.0", "method": "host.get", "params": { "filter": { "host": [ "888Y-6F-bangong-1" ] } }, "auth": "2bed9416d8773ed714a300101d7ccd78", "id": 1 }' http://10.108.15.231/zabbix/api_jsonrpc.php | python -m json.tool curl -s -X POST -H 'Content-Type: application/json' -d ' { "jsonrpc": "2.0", "method": "host.get", "params": { "filter": { "host": [ "888Y-6F-bangong-1" ] } }, "auth": "2bed9416d8773ed714a300101d7ccd78", "id": 1 }' http://10.108.15.231/zabbix/api_jsonrpc.php | python -m json.tool 2、獲取主機組的信息; curl -s -X POST -H 'Content-Type: application/json' -d ' { "jsonrpc": "2.0", "method": "host.get", "params": { "output": ["hostid"], "selectGroups": "extend", "filter": { "host": [ "Zabbix server" ] } }, "auth": "2bed9416d8773ed714a300101d7ccd78", "id": 1 }' http://10.108.15.231/zabbix/api_jsonrpc.php | python -m json.tool
加主機也是一個事件。
自動註冊可以開啓或者關閉。
自動發現有個坑比如vip怎麼算,而且link的模板很少,所以真正做自動化我們都是採用zabbix的api(服務化),可以管理或者查詢,
id就是這次請求的標識,不是主機的標識
zabbix中的filter是過濾的
通過zabbix的api判斷是否添加監控是很容易的
Pyzabbix:
pyzabbix的定製化API:https://gitee.com/kbsonlong/pyzabbix
https://www.cnblogs.com/richardzgt/articles/7889404.html#_label1
pyzabbix是zabbixAPI的第三方python包裝
from pyzabbix import ZabbixAPI
zapi = ZabbixAPI("http://10.108.15.231/zabbix/api_jsonrpc.php")
zapi.login("Admin","zabbix")
print("Connected to Zabbix API Version %s" % zapi.api_version())
一個API的庫:
#!/usr/bin/env python2.7 #coding=utf-8 import json import urllib2 # based url and required header url = "http://monitor.example.com/api_jsonrpc.php" header = {"Content-Type": "application/json"} # auth user and password data = json.dumps( { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 0 }) # create request object request = urllib2.Request(url,data) for key in header: request.add_header(key,header[key]) # auth and get authid try: result = urllib2.urlopen(request) except URLError as e: print "Auth Failed, Please Check Your Name And Password:",e.code else: response = json.loads(result.read()) result.close() print "Auth Successful. The Auth ID Is:",response['result']
from pyzabbix.api import ZabbixAPI # Create ZabbixAPI class instance zapi = ZabbixAPI(url='http://10.108.15.231/zabbix/api_jsonrpc.php', user='Admin', password='zabbix') # Get all monitored hosts result1 = zapi.host.get(monitored_hosts=1, output='extend') # Get all disabled hosts result2 = zapi.do_request('host.get', { 'filter': {'status': 1}, 'output': 'extend' }) # Filter results hostnames1 = [host['host'] for host in result1] hostnames2 = [host['host'] for host in result2['result']] # Logout from Zabbix zapi.user.logout()