1.1 zabbix認證和請求函數
1、zabbix配置一般流程
1、新建主機
2、新建模板
3、新建應用集
4、新建監控項
5、新建觸發器
6、鏈接模版到主機
2、zabbix認證和請求函數
#! -- coding:utf8 -- import urllib2 import json url = 'http://1.1.1.5/zabbix/api_jsonrpc.php</span><span style="color: #800000;">' username = 'Admin' password = '1' ################################ 一:登陸腳本 login.py ########################### #1、定義通過HTTP方式訪問API地址的函數,后面每次請求API的各個方法都會調用這個函數 def requestJson(url,values): data = json.dumps(values) req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'}) response = urllib2.urlopen(req, data) output = json.loads(response.read()) try: message = output['result'] except: message = output['error']['data'] print message quit() return output['result'] #2、API接口認證的函數,登錄成功會返回一個Token def authenticate(url, username, password): values = {'jsonrpc': '2.0', 'method': 'user.login', 'params': { 'user': username, 'password': password }, 'id': '0' } idvalue = requestJson(url,values) return idvalue # 結果是一個token值:cc75ed2a314906a835ac0786266468ac print authenticate(url,username,password) # 5aff9f42e4dcf551f08feb3b192be8e0
1.2 主機組操作
1、主機組常用操作
參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/hostgroup/create</a></p>
# 創建主機組: 運行此函數就會創建組 "New Create Group"
def creategroup(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.create",
"params": {
"name": "New Create Group"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
print create_group(auth) # {u'groupids': [u'17']}
#1、根據組名獲取組id: 獲取"New Create Group" 組的id
def get_group(auth,group_name):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": { # 如果沒有filter 默認會獲取所有組信息
"name": [
group_name,
]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['groupid']
auth = authenticate(url,username,password)
print get_group(auth,"New Create Group") # 17
#2、獲取這個server中所有組信息
def get_groups(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": ['name','groupid'],
"filter": { # 如果沒有filter 默認會獲取所有組信息
"name": [
# group_name,
]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
print get_groups(auth) # [{u'groupid': u'17', u'name': u'New Create Group'},{},{},{},{},....]
#1、先根據組名獲取組id
def get_group(auth,group_name):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": { # 如果沒有filter 默認會獲取所有組信息
"name": [
group_name,
]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['groupid']
#2、然后根據組id,刪除這個組
def del_group(auth,gid):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.delete",
"params": [gid], # 如果刪除多個組可以直接傳入一個列表
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
gid = get_group(auth,"New Create Group") # 17
del_group(auth,gid)
#1、先根據組名獲取組id
def get_group(auth,group_name):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": { # 如果沒有filter 默認會獲取所有組信息
"name": [
group_name,
]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['groupid']
#2、然后根據組id,修改組名
def del_group(auth,gid):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.update",
"params": {
"groupid": gid,
"name": "New Create Group Change"
},
"auth":auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
gid = get_group(auth,"New Create Group") # 17
del_group(auth,gid)
#1、將主機加入組
def massadd_hosts_groups(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.massadd",
"params": {
"groups": [
{
"groupid": "5"
},
{
"groupid": "6"
}
],
"hosts": [
{
"hostid": "30050"
},
{
"hostid": "30001"
}
]
},
"auth": "f223adf833b2bf2ff38574a67bba6372",
"id": 1
}
output = requestJson(url,values)
return output
#2、將模板加入組
def massadd_templates_groups(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.massadd",
"params": {
"groups": [
{
"groupid": "5"
},
{
"groupid": "6"
}
],
"templates": [
{
"templateid": "30050"
},
{
"templateid": "30001"
}
]
},
"auth": "f223adf833b2bf2ff38574a67bba6372",
"id": 1
}
output = requestJson(url,values)
return output
#1、替換主機組中的所有主機
def massupdate_hosts_groups(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.massupdate",
"params": {
"groups": [
{
"groupid": "6"
}
],
"hosts": [
{
"hostid": "30050"
}
]
},
"auth": "f223adf833b2bf2ff38574a67bba6372",
"id": 1
}
output = requestJson(url,values)
return output
#2、替換主機組中的所有模板
def massupdate_templates_groups(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.massupdate",
"params": {
"groups": [
{
"groupid": "6"
}
],
"templates": [
{
"templateid": "30050"
}
]
},
"auth": "f223adf833b2bf2ff38574a67bba6372",
"id": 1
}
output = requestJson(url,values)
return output
#1、從主機組中刪除主機
def massremove_hosts_groups(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.massremove",
"params": {
"groupids": [
"5",
"6"
],
"hostids": [
"30050",
"30001"
]
},
"auth": "038e1d7b1735c6a5436ee9eae095879e",
"id": 1
}
output = requestJson(url,values)
return output
#2、從主機組中刪除模板
def massremove_templates_groups(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.massremove",
"params": {
"groups": [
{
"groupid": "6"
}
],
"templates": [
{
"templateid": "30050"
}
]
},
"auth": "f223adf833b2bf2ff38574a67bba6372",
"id": 1
}
output = requestJson(url,values)
return output
2、主機組更多查詢操作
# 1、獲取這個zabbix server中所有主機組
def get_all_groups(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": ['name','groupid'],
"filter": { # filter為空時會返回所有組信息
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_all_groups(auth) )
ret = [{
"groupid": "5",
"name": "Discovered hosts"
}, {
"groupid": "7",
"name": "Hypervisors"
},]
# 2、根據組名稱獲取 組信息
def get_group_by_groupname(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": {
"name": [
"New Create Group",
"New Group 02"
]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(get_group_by_groupname(auth))
ret = [{
"internal": "0",
"flags": "0",
"groupid": "19",
"name": "New Create Group"
}, {
"internal": "0",
"flags": "0",
"groupid": "20",
"name": "New Group 02"
}]
# 3、根據組id獲取 組信息
def get_group_by_groupid(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
'groupids':['19','20']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(get_group_by_groupid(auth))
ret = [{
"internal": "0",
"flags": "0",
"groupid": "19",
"name": "New Create Group"
}, {
"internal": "0",
"flags": "0",
"groupid": "20",
"name": "New Group 02"
}]
# 4、根據主機id獲取 組信息
def get_group_by_hostid(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
'hostids':['10084','10264']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(get_group_by_hostid(auth))
ret = [{
"internal": "0",
"flags": "0",
"groupid": "19",
"name": "New Create Group"
}, {
"internal": "0",
"flags": "0",
"groupid": "20",
"name": "New Group 02"
}]
# 5、只返回包含給定模板的主機組。
def get_group_by_templateids(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
'templateids':['10266']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(get_group_by_templateids(auth))
ret = [{
"internal": "0",
"flags": "0",
"groupid": "19",
"name": "New Create Group"
}, {
"internal": "0",
"flags": "0",
"groupid": "20",
"name": "New Group 02"
}]
# 6、返回屬於主機組的所有主機
def get_hosts_by_group(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": 'extend',
"filter": {
"groupid": '20'
},
"selectHosts": [ # 添加這個參數為了獲取interfaceid的值
"name",
"hostid"
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(get_hosts_by_group(auth))
ret = [{
"hosts": [{
"hostid": "10084",
"name": "Zabbix server"
}, {
"hostid": "10264",
"name": "zabbix_agent_1.1.1.3"
}],
"internal": "0",
"flags": "0",
"groupid": "20",
"name": "New Group 02"
}]
# 7、返回屬於主機組的所有模板
def get_templates_by_group(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": 'extend',
"filter": {
"groupid": '19'
},
"selectTemplates": [ # 添加這個參數為了獲取interfaceid的值
"host",
"templateid"
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(get_templates_by_group(auth))
ret = [{
"templates": [{
"host": "New Create Template",
"templateid": "10266"
}],
"internal": "0",
"flags": "0",
"groupid": "19",
"name": "New Create Group"
}]
3、查詢"主機組"時返回更多關聯信息:主機、模板
# 查詢時返回更多關聯信息:主機、模板
def get_more_info_by_group(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": 'extend',
"filter": {
"groupid": '20'
},
"selectHosts": [ # 1、屬性中返回屬於主機組的主機
"name",
"hostid"
],
"selectTemplates": [ # 2、在“”模板“”屬性中返回屬於主機組的模板
"host",
"host"
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_more_info_by_group(auth) )
'''
[{
"templates": [{
"host": "New Create Template",
"templateid": "10266"
}],
"name": "New Group 02",
"internal": "0",
"hosts": [{
"hostid": "10084",
"name": "Zabbix server"
}, {
"hostid": "10264",
"name": "zabbix_agent_1.1.1.3"
}],
"flags": "0",
"groupid": "20"
}]
'''
1.3 主機操作
1、主機常用操作 (創建的主機至少包含一個主機組)
參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/host/create</a></p>
#1、創建主機,並添加到 "New Create Group" 主機組中
def create_host(auth,hostname,ip,gid,templateid=None):
values = {
"jsonrpc": "2.0",
"method": "host.create",
"params": {
#1、主機名稱
"host": hostname,
#2、為主機創建的接口
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": ip,
"dns": "",
"port": "10050"
}
],
#3、將主機添加到主機組中
"groups": [
{
"groupid": gid,
}
],
#4、鏈接一個模板
# "templates": [
# {
# "templateid": "20045"
# }
# ],
# 主機資產清單屬性:把MAC地址設置到主機資產清單里
# "inventory_mode": 0,
# "inventory": {
# "macaddress_a": "01234",
# "macaddress_b": "56768"
# }
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、獲取主機組 id
def get_group(auth,group_name):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": { # 如果沒有filter 默認會獲取所有組信息
"name": [
group_name,
]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['groupid']
auth = authenticate(url,username,password)
gid = get_group(auth,"New Create Group") # 19
# 創建新主機 ip=1.1.1.3 添加到“New Create Group”組中
print create_host(auth,'zabbix_agent_1.1.1.3','1.1.1.3',gid) # {u'hostids': [u'10258']}
#1、獲取主機id:根據主機名獲取主機id
def get_host(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid'],
"filter": {
"host": [hostname,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['hostid']
auth = authenticate(url,username,password)
print get_host(auth,'zabbix_agent_1.1.1.3') # 10258
#1、根據主機id刪除主機
def del_host(auth,hostid):
values = {
"jsonrpc": "2.0",
"method": "host.delete",
"params": [hostid,],
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['hostid']
#2、獲取主機id:根據主機名獲取主機id
def get_host(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid'],
"filter": {
"host": [hostname,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['hostid']
auth = authenticate(url,username,password)
hostid = get_host(auth,'zabbix_agent_1.1.1.3') # 10258
del_host(auth,hostid)
#1、禁用/啟用 主機
def update_host_status(auth):
# status=0 啟用agent,status=1 禁用agent
values = {
"jsonrpc": "2.0",
"method": "host.update",
"params": {
"hostid": '10264',
"status": 0
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、替換當前主機所屬主機組
def update_host_group(auth):
# status=0 啟用agent,status=1 禁用agent
values = {
"jsonrpc": "2.0",
"method": "host.update",
"params": {
"hostid": '10264',
"groups": ['19']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#3、替在指定主機中替換當前鏈接的模板: 以前模板的 監控項等不會刪除
def update_host_templates(auth):
# status=0 啟用agent,status=1 禁用agent
values = {
"jsonrpc": "2.0",
"method": "host.update",
"params": {
"hostid": '10264',
"templates": ['10266']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#4、在指定主機中刪除指定模板鏈接並清除: 會清除這個模板的監控項等
def update_host_templates_clear(auth):
# status=0 啟用agent,status=1 禁用agent
values = {
"jsonrpc": "2.0",
"method": "host.update",
"params": {
"hostid": '10264',
"templates_clear": ['10266']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#1、主機組添加到指定的主機中(原有的組機組不變)
def massadd_groups_hosts(auth):
values = {
"jsonrpc": "2.0",
"method": "host.massadd",
"params": {
"hosts": [
{
"hostid": "10084"
},
{
"hostid": "10264"
}
],
"groups": [
{
"groupid": '20',
},
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、模板添加到指定的主機中
def massadd_template_hosts(auth, templateid, hostid):
values = {
"jsonrpc": "2.0",
"method": "template.massadd",
"params": {
"templates": [
{
"templateid": templateid
},
],
"hosts": [
{
"hostid": hostid
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url, values)
return output
#3、為指定主機創建的主機接口: 如果創建主機時已經創建了默認接口,就不能再創建默認接口了
def massadd_groups_hosts(auth,ip):
values = {
"jsonrpc": "2.0",
"method": "host.massadd",
"params": {
"hosts": [
{
"hostid": "10264"
},
],
"interfaces": [
{
"type": 1, # 1 - agent; 2 - SNMP; 3 - IPMI; 4 - JMX. 接口類型
"main": 1, # 在主機上是否使用該接口作為默認值,一個主機僅能設置一個默認值(0不默認,1默認是)
"useip": 1, # 是否應該通過IP進行連接,0:使用dns連接,1:使用此主機接口的主機IP地址進行連接
"ip": ip, # 接口使用的IP地址,如果通過DNS進行連接,則可以為空。
"dns": "", # 接口使用的DNS名稱,如果通過IP建立連接,可以為空。
"port": "10050" # 接口使用的端口號
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
# massadd_groups_hosts(auth,ip='1.1.1.3')
#1、從指定的主機中移除主機組
def massremove_groups_hosts(auth):
values = {
"jsonrpc": "2.0",
"method": "host.massremove",
"params": {
"hostids": ["69665", "69666"],
"groupids": "325"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、模板添加到指定的主機中
def massremove_template_hosts(auth, templateid, hostid):
values = {
"jsonrpc": "2.0",
"method": "host.massremove",
"params": {
"hostids": ["69665", "69666"],
"templateids": "325"
},
"auth": auth,
"id": 1
}
output = requestJson(url, values)
return output
#3、從指定的主機中移除主機接口
def massremove_interfaces_hosts(auth,ip):
values = {
"jsonrpc": "2.0",
"method": "host.massremove",
"params": {
"hosts":["10264", ],
"interfaces": [
{
"type": 1, # 1 - agent; 2 - SNMP; 3 - IPMI; 4 - JMX. 接口類型
"main": 1, # 在主機上是否使用該接口作為默認值,一個主機僅能設置一個默認值(0不默認,1默認是)
"useip": 1, # 是否應該通過IP進行連接,0:使用dns連接,1:使用此主機接口的主機IP地址進行連接
"ip": ip, # 接口使用的IP地址,如果通過DNS進行連接,則可以為空。
"dns": "", # 接口使用的DNS名稱,如果通過IP建立連接,可以為空。
"port": "10050" # 接口使用的端口號
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
print massremove_interfaces_hosts(auth,ip='1.1.1.3')
# Interface is linked to item "login_user" on "zabbix_agent_1.1.1.3". 若果關聯的有item會報錯
#1、替換當前主機所屬主機組
def massupdate_groups_hosts(auth):
values = {
"jsonrpc": "2.0",
"method": "host.massupdate",
"params": {
"hosts": [
{
"hostid": "10084"
},
{
"hostid": "10264"
}
],
"groups": [
{
"groupid": '20',
},
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、僅僅是刪除模板關聯,模板中的應用集、監控項、觸發器等還在主機中
def massupdate_template_hosts(auth):
values = {
"jsonrpc": "2.0",
"method": "host.massupdate",
"params": {
"hosts": [
{
"hostid": "10084"
},
{
"hostid": "10264"
}
],
"templates": [
{
"templateid": '10050',
},
]
},
"auth": auth,
"id": 1
}
output = requestJson(url, values)
return output
#3、刪除模板關聯關系,在指定主機中刪除模板鏈接並清除(如:應用集、監控項)
def massupdate_templates_clear_hosts(auth):
values = {
"jsonrpc": "2.0",
"method": "host.massupdate",
"params": {
"hosts": [
{
"hostid": "10084"
},
{
"hostid": "10264"
}
],
"templates_clear": [
{
"templateid": '10050',
},
]
},
"auth": auth,
"id": 1
}
output = requestJson(url, values)
return output
2、主機更多查詢操作
# 1、獲取這個zabbix server監控的所有主機信息
def get_all_hosts(auth):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 2,
"auth": auth
}
output = requestJson(url,values)
return output
print json.dumps( get_all_hosts(auth) )
'''
[{
"host": "Zabbix server",
"hostid": "10084",
"interfaces": [{
"interfaceid": "1",
"ip": "1.1.1.5"
}]
}, {
"host": "zabbix_agent_1.1.1.3",
"hostid": "10264",
"interfaces": [{
"interfaceid": "4",
"ip": "1.1.1.3"
}]
}]
'''
# 2、僅返回指定主機組所屬的主機
def get_hosts_by_group(auth):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
'groupids':['19']
},
"id": 2,
"auth": auth
}
output = requestJson(url,values)
return output
print json.dumps( get_hosts_by_group(auth) )
'''
[{
"host": "Zabbix server",
"hostid": "10084"
}, {
"host": "zabbix_agent_1.1.1.3",
"hostid": "10264"
}]
'''
# 3、僅返回含有指定應用集的主機
def get_hosts_by_app(auth):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
'applicationid':['1069']
},
"id": 2,
"auth": auth
}
output = requestJson(url,values)
return output
print json.dumps( get_hosts_by_app(auth) )
'''
[{
"host": "Zabbix server",
"hostid": "10084"
}, {
"host": "zabbix_agent_1.1.1.3",
"hostid": "10264"
}]
'''
# 4、僅返回指定主機ID的主機。
def get_hosts_by_hostname(auth):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
'hostids':['10264']
},
"id": 2,
"auth": auth
}
output = requestJson(url,values)
return output
print json.dumps( get_hosts_by_hostname(auth) )
'''
[{
"host": "zabbix_agent_1.1.1.3",
"hostid": "10264"
}]
'''
# 5、根據主機名/主機id 獲取interfaceid
def get_interfaceid_by_host(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid',],
"filter": {
# "host": [hostname,], # 根據主機名獲取
'hostids':['10264'] #根據主機id獲取
},
"selectInterfaces": [ # 添加這個參數為了獲取interfaceid的值
"interfaceid",
"ip"
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
interfaceid = output[0]['interfaces'][0]['interfaceid']
hostid = output[0]['hostid']
return {'interfaceid':interfaceid, 'hostid':hostid}
host_dic = get_interfaceid_by_host(auth,'zabbix_agent_1.1.1.3') # 10258
print json.dumps(host_dic)
'''
{
"interfaceid": "4",
"hostid": "10264"
}
'''
# 6、僅返回含有指定監控項的主機
def get_hosts_by_item(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid',],
"filter": {
'itemids':['10264'] #根據主機id獲取
},
"selectInterfaces": [ # 添加這個參數為了獲取interfaceid的值
"interfaceid",
"ip"
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
interfaceid = output[0]['interfaces'][0]['interfaceid']
hostid = output[0]['hostid']
return {'interfaceid':interfaceid, 'hostid':hostid}
host_dic = get_hosts_by_item(auth,'zabbix_agent_1.1.1.3') # 10258
print json.dumps(host_dic)
'''
{
"interfaceid": "4",
"hostid": "10264"
}
'''
# 7、僅返回與指定模板鏈接的主機。
def get_hosts_by_template(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid',],
"filter": {
'templateids':['10266'] #根據主機id獲取
},
"selectInterfaces": [ # 添加這個參數為了獲取interfaceid的值
"interfaceid",
"ip"
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
interfaceid = output[0]['interfaces'][0]['interfaceid']
hostid = output[0]['hostid']
return {'interfaceid':interfaceid, 'hostid':hostid}
host_dic = get_hosts_by_template(auth,'zabbix_agent_1.1.1.3') # 10258
print json.dumps(host_dic)
'''
{
"interfaceid": "4",
"hostid": "10264"
}
'''
3、查詢"主機"時返回更多關聯信息:主機組、模板、應用集、監控項、觸發器、interfaceid
# 查詢"主機"時返回更多關聯信息:主機組、模板、應用集、監控項、觸發器、interfaceid
def get_more_info_by_host(auth):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
'hostids':['10264'],
"selectGroups": [ # 1.返回在groups屬性中主機所屬的主機組。
"name",
"groupid"
],
"selectParentTemplates": [ # 2.返回在parentTemplates屬性中與主機關聯的模板(主機關聯的模板)
"host",
"templateid"
],
"selectApplications": [ # 3.返回在applications屬性中來自主機的應用集。
"applicationid",
"name"
],
"selectItems": [ # 4.返回在items屬性中主機里的監控項
"key",
"itemid",
"interfaceid",
],
"selectTriggers": [ # 5.返回在triggers屬性中主機里的觸發器
"description",
"triggerid",
],
"selectInterfaces": [ # 6.添加這個參數為了獲取interfaceid的值
"interfaceid",
"ip"
],
},
"id": 2,
"auth": auth
}
output = requestJson(url,values)
return output
print json.dumps( getmore_info_by_host(auth) )
'''
[{
"hostid": "10264",
"parentTemplates": [{
"host": "New Create Template",
"templateid": "10266"
}],
"triggers": [{
"triggerid": "15601",
"description": "User_Login"
}],
"items": [{
"itemid": "28439",
"interfaceid": "4",
"key": "loguser"
}],
"interfaces": [{
"interfaceid": "4",
"ip": "1.1.1.3"
}],
"applications": [{
"applicationid": "1101",
"name": "App01"
}],
"host": "zabbix_agent_1.1.1.3",
"groups": [{
"groupid": "19",
"name": "New Create Group"
}, {
"groupid": "20",
"name": "New Group 02"
}]
}]
'''
1.4 模板操作
1、模板常用操作(創建的模板至少屬於一個主機組)
參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/template/create</a></p>
#1、新建模板:模板必須至少關聯一個主機組
def create_template(auth,template_name,gid):
values = {
"jsonrpc": "2.0",
"method": "template.create",
"params": {
"host": template_name,
"groups": { # 模板關聯的主機組(必須要有)
"groupid": gid,
},
# "hosts": [{"hostid": "10084"},{"hostid": "10090"}] # 模板關聯的主機
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、獲取組id
def get_group(auth,group_name):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": { # 如果沒有filter 默認會獲取所有組信息
"name": [
group_name,
]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['groupid']
auth = authenticate(url,username,password)
gid = get_group(auth,"New Create Group")
print create_template(auth,"New Create Template",gid)
# 獲取模板id:根據模板名稱獲取模板id
def get_template(auth,template_name):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ["host","templateid"],
"filter": {
"host": [template_name,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['templateid']
# return output # [{u'host': u'New Create Template', u'templateid': u'10260'}]
auth = authenticate(url,username,password)
print get_template(auth,"New Create Template") # 10260
#1、刪除模板:根據模板id
def del_template(auth,templateid):
values = {
"jsonrpc": "2.0",
"method": "template.delete",
"params": [templateid],
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['templateid']
# return output # [{u'host': u'New Create Template', u'templateid': u'10260'}]
#2、獲取模板id:根據模板名稱獲取模板id
def get_template(auth,template_name):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ["host","templateid"],
"filter": {
"host": [template_name,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['templateid']
auth = authenticate(url,username,password)
templateid = get_template(auth,"New Create Template") # 10260
print del_template(auth,templateid)
#1、修改模板名稱
def update_template(auth,templateid,new_template_name):
values = {
"jsonrpc": "2.0",
"method": "template.update",
"params": {
"templateid": templateid,
"name": new_template_name
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、獲取模板id:根據模板名稱獲取模板id
def get_template(auth,template_name):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ["host","templateid"],
"filter": {
"host": [template_name,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['templateid']
# return output # [{u'host': u'New Create Template', u'templateid': u'10260'}]
auth = authenticate(url,username,password)
templateid = get_template(auth,"New Create Template") # 10260
print update_template(auth,templateid,"New Create Template Change")
#1、給模板添加主機組
def massadd_groups_templates(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massadd",
"params": {
"templates": [
{
"templateid": "10266"
},
# {
# "templateid": "10086"
# }
],
"groups": [
{
"groupid": "20"
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、給模板添加主機組
def massadd_hosts_templates(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massadd",
"params": {
"templates": [
{
"templateid": "10266"
},
# {
# "templateid": "10086"
# }
],
"hosts": [
{
"hostid": "10264"
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#3、鏈接到給定模板的模板.
def massadd_templates_templates(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massadd",
"params": {
"templates": [
{
"templateid": "10266"
},
# {
# "templateid": "10086"
# }
],
"templates_link": [
{
"templateid": "10093"
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#1、從模板中刪除主機組
def massremove_groups_templates(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massremove",
"params": {
"templateids": [
"10266",
# "10086"
],
"groupids": "20"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、將模板從指定主機中刪除:但是模板中的 應用集、監控項等還在
def massremove_hosts_templates(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massremove",
"params": {
"templateids": [
"10266",
# "10086"
],
"hostids": ["10264"]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
# 3、從給定的模板中取消鏈接和清除的模板: 刪除了對應的應用集和監控項
def massremove_template_templateids_clear(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massremove",
"params": {
"templateids": ['10266'],
"templateids_clear": ['10093'],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
# 4、取消與給定模板鏈接的模板:不會刪除已關聯的應用集、監控項等
def massremove_template_templateids_link(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massremove",
"params": {
"templateids": ['10266'],
"templateids_link": ['10093'],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#1、從給定的模板中取消鏈接並清除模板“10093”(刪除已鏈接的 應用集、監控項等)
def massupdate_templates_templates_clear(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massupdate",
"params": {
"templates": [
{
"templateid": "10266"
},
],
"templates_clear": [
{
"templateid": "10093"
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、主機組替換模板所屬的當前主機組: 其他所有組機組都會取消關聯
def massupdate_group_templates(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massupdate",
"params": {
"templates": [
{
"templateid": "10266"
},
],
"groups": [
{
"groupid": "20"
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#3、去除這個模板鏈接的其他所有主機,只鏈接這一個主機
def massupdate_host_templates(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massupdate",
"params": {
"templates": [
{
"templateid": "10266"
},
],
"hosts": [
{
"hostid": "10264"
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#4、用於替換當前鏈接的模板的模板:鏈接的其他模板都會取消,但他們的應用集和監控項不會刪除
def massupdate_templates_templates_link(auth):
values = {
"jsonrpc": "2.0",
"method": "template.massupdate",
"params": {
"templates": [
{
"templateid": "10266"
},
],
"templates_link": [
{
"templateid": "10094"
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
2、模板更多查詢操作
#1、根據模板名稱檢索
def get_templates_by_templatename(auth):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"filter": {
"host": [
"New Create Template", # 要檢索的模板名稱
]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_templates_by_templatename(auth) )
'''
[{
"host": "New Create Template",
"templateid": "10266"
}]
'''
#2、根據模板id檢索
def get_templates_by_templateids(auth):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"templateids": ['10266']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_templates_by_templateids(auth) )
'''
[{
"host": "New Create Template",
"templateid": "10266"
}]
'''
#3、根據主機組檢索 關聯模板
def get_templates_by_hostgroup(auth):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"groupids": ['20']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_templates_by_hostgroup(auth) )
'''
[{
"host": "New Create Template",
"templateid": "10266"
}]
'''
#4、根據主機檢索關聯的模板
def get_templates_by_host(auth):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"hostids": ['10264']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_templates_by_host(auth) )
'''
[{
"host": "New Create Template",
"templateid": "10266"
}]
'''
#5、只返回包含指定監控項的模板.
def get_templates_by_item(auth):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"itemids": ['28284']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_templates_by_item(auth) )
'''
[{
"host": "New Create Template",
"templateid": "10266"
}]
'''
#6、只只返回包含指定觸發器的模板
def get_templates_by_trigger(auth):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"triggerids": ['15567']
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_templates_by_trigger(auth) )
'''
[{
"host": "New Create Template",
"templateid": "10266"
}]
'''
#7、返回templates屬性中更多屬性:主機組、主機、子模板
def get_templates_by_templateids(auth):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"templateids": ['10266'],
"selectGroups": [ # 返回模板所屬的主機組
"name",
"groupid"
],
"selectHosts": [ # 返回鏈接到模板的主機
"name",
"hostid"
],
"selectTemplates": [ # 返回templates屬性中的子模板(那個模板鏈接了自己)
"host",
"templateid"
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_templates_by_templateids(auth) )
'''
[{
"templates": [{
"host": "Template App Apache Tomcat JMX",
"templateid": "10168"
}],
"host": "New Create Template",
"hosts": [{
"hostid": "10264",
"name": "zabbix_agent_1.1.1.3"
}],
"groups": [{
"groupid": "20",
"name": "New Group 02"
}],
"templateid": "10266"
}]
'''
3、查詢"模板"時返回更多關聯信息:主機組、主機、監控項、觸發器、子模板、父模板
# 查詢時返回更多關聯信息:主機組、主機、監控項、觸發器、子模板、父模板
def get_more_info_by_template(auth):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"templateids": ['10266'],
"selectGroups": [ #1.返回模板所屬的主機組
"name",
"groupid"
],
"selectHosts": [ #2.返回鏈接到模板的主機
"name",
"hostid"
],
"selectItems": [ #3.返回模板中的監控項.
"name",
"key",
"itemid",
"interfaceid",
],
"selectTriggers": [ #4.返回模板中的觸發器
"description",
"triggerid",
],
"selectTemplates": [ #5.返回templates屬性中的子模板(那個模板鏈接了自己)
"host",
"templateid"
],
"selectParentTemplates": [ # 6.返回templates屬性中的父模板(自己鏈接了哪些模板)
"host",
"templateid"
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( getmore_info_by_template(auth) )
'''
[{
"templates": [],
"parentTemplates": [],
"triggers": [{
"triggerid": "15567",
"description": "User_Login"
}],
"items": [{
"itemid": "28284",
"interfaceid": "0",
"key": "loguser",
"name": "login_user"
}],
"host": "New Create Template",
"hosts": [{
"hostid": "10264",
"name": "zabbix_agent_1.1.1.3"
}],
"groups": [{
"groupid": "20",
"name": "New Group 02"
}],
"templateid": "10266"
}]
'''
1.5 應用集操作
1、應用常用集操作 (應用集 必須歸屬 主機/模板)
參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/application/create</a></p>
#1、創建應用集
def create_application(auth, app_name, hostid):
values = {
"jsonrpc": "2.0",
"method": "application.create",
"params": {
"name": app_name,
"hostid": hostid
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output # 運行后就可以在對應主機的監控項頁面看到剛創建的監控項了
#2、獲取主機id
def get_host(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid'],
"filter": {
"host": [hostname,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['hostid']
auth = authenticate(url,username,password)
hostid = get_host(auth,'zabbix_agent_1.1.1.3')
print create_application(auth, "New Create App", hostid)
#1、獲取指定主機所有應用集
def get_application(auth, hostid):
values = {
"jsonrpc": "2.0",
"method": "application.get",
"params": {
"output": "extend",
"hostids": hostid,
"sortfield": "name"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output # 運行后就可以在對應主機的監控項頁面看到剛創建的監控項了
#2、獲取主機id
def get_host(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid'],
"filter": {
"host": [hostname,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['hostid']
auth = authenticate(url,username,password)
hostid = get_host(auth,'zabbix_agent_1.1.1.3')
print get_application(auth, hostid)
# [{u'flags': u'0', u'hostid': u'10259', u'applicationid': u'1084', u'name': u'New Create App', u'templateids': []}]
# 根據應用集id進行刪除
def get_application(auth, applicationid):
values = {
"jsonrpc": "2.0",
"method": "application.delete",
"params": [applicationid],
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output # 運行后就可以在對應主機的監控項頁面看到剛創建的監控項了
#1、修改應集名稱
def update_application(auth, applicationid, new_app_name):
values = {
"jsonrpc": "2.0",
"method": "application.update",
"params": {
"applicationid": applicationid,
"name": new_app_name
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output # 運行后就可以在對應主機的監控項頁面看到剛創建的監控項了
auth = authenticate(url,username,password)
update_application(auth, '1084', 'New Create App Change')
# 添加多個監控項到指定的應用集
def massadd_application(auth):
values = {
"jsonrpc": "2.0",
"method": "application.massadd",
"params": {
"applications": [
{
"applicationid": "1118"
},
],
"items": [
{
"itemid": "28439"
},
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
massadd_application(auth)
2、應用集更多查詢操作
#1、只返回指定 ID 的應用集
def get_application(auth,):
values = {
"jsonrpc": "2.0",
"method": "application.get",
"params": {
"output": "extend",
"applicationids": ['1118'],
"sortfield": "name"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_application(auth) )
'''
[{
"flags": "0",
"hostid": "10264",
"applicationid": "1118",
"name": "app02",
"templateids": []
}]
'''
#2、只返回指定主機組所屬主機的應用集
def get_application_by_group(auth,):
values = {
"jsonrpc": "2.0",
"method": "application.get",
"params": {
"output": "extend",
"groupids": ['19',],
"sortfield": "name"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_application_by_group(auth) )
'''
[{
"flags": "0",
"hostid": "10264",
"applicationid": "1101",
"name": "App01",
"templateids": ["1111"]
}, {
"flags": "0",
"hostid": "10264",
"applicationid": "1118",
"name": "app02",
"templateids": []
}]
'''
#3、只返回指定主機所屬的應用集
def get_application_by_host(auth,):
values = {
"jsonrpc": "2.0",
"method": "application.get",
"params": {
"output": "extend",
"hostids": ['10264',],
"sortfield": "name"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_application_by_host(auth) )
'''
[{
"flags": "0",
"hostid": "10264",
"applicationid": "1101",
"name": "App01",
"templateids": ["1111"]
}, {
"flags": "0",
"hostid": "10264",
"applicationid": "1118",
"name": "app02",
"templateids": []
}]
'''
#4、只返回指定模板的應用集
def get_application_by_template(auth,):
values = {
"jsonrpc": "2.0",
"method": "application.get",
"params": {
"output": "extend",
"templateids": ['10266',],
"sortfield": "name"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_application_by_template(auth) )
'''
[{
"flags": "0",
"hostid": "10266",
"applicationid": "1111",
"name": "App01",
"templateids": []
}]
'''
3、查詢"應用集"時返回更多關聯信息:主機、監控項
# 查詢"應用集"時返回更多關聯信息:主機、監控項
def get_application(auth,):
values = {
"jsonrpc": "2.0",
"method": "application.get",
"params": {
"output": "extend",
"applicationids": ['1118'],
"sortfield": "name",
"selectHost": [ #1.返回鏈接到模板的主機
"name",
"hostid"
],
"selectItems": [ #2.返回模板中的監控項.
"name",
"key",
"itemid",
"interfaceid",
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( getapplication(auth) )
'''
[{
"hostid": "10264",
"name": "app02",
"items": [{
"itemid": "28439",
"interfaceid": "4",
"key": "loguser",
"name": "login_user"
}],
"templateids": [],
"host": {
"hostid": "10264",
"name": "zabbix_agent_1.1.1.3"
},
"flags": "0",
"applicationid": "1118"
}]
'''
1.6 監控項操作
1、監控項常用操作 (監控項 必須歸屬 主機/模板 可以同時 關聯到 應用集 且監控項的鍵值必須和agent的key值相同)
參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/item/create</a></p>
#1、創建監控項
def create_item(auth, item_name, item_key, hostid, interfaceid):
values = {
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"name": item_name,
"key": itemkey, # 鍵值必須和agent的key值相同
"hostid": hostid,
"type": 0,
"value_type": 3,
"interfaceid": interfaceid,
# "applications": ["609","610"], # 監控項可以歸屬默寫 "應用集" 這里就不關聯了
"delay": "30s"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、獲取主機id 和 interfaceid
def get_host(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid',],
"filter": {
"host": [hostname,]
},
"selectInterfaces": [ # 添加這個參數為了獲取interfaceid的值
"interfaceid",
"ip"
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
interfaceid = output[0]['interfaces'][0]['interfaceid']
hostid = output[0]['hostid']
return {'interfaceid':interfaceid, 'hostid':hostid}
'''
[{
u 'interfaces': [{
u 'interfaceid': u '6',
u 'ip': u '1.1.1.3'
}],
u 'hostid': u '10259',
u 'name': u 'zabbix_agent_1.1.1.3'
}]
'''
auth = authenticate(url,username,password)
host_dic = get_host(auth,'zabbix_agent_1.1.1.3') # 10258
print create_item(auth, 'login_user', 'log_user', host_dic['hostid'], host_dic['interfaceid'])
#1、根據主機 和 鍵值key 匹配 監控項id
def get_item(auth, hostid, item_key):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"hostids": hostid,
"search": {
"key": "log_user" # 鍵值必須和agent的key值相同
},
"sortfield": "name"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['itemid'] # 28344
# return output # [{u'itemid': u'28344', u'key': u'loguser'}]
#2、獲取主機id:根據主機名獲取主機id
def get_host(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid'],
"filter": {
"host": [hostname,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['hostid']
auth = authenticate(url,username,password)
hostid = get_host(auth,'zabbix_agent_1.1.1.3') # 10258
print get_item(auth, hostid, 'log_user') # 28344
#1、刪除監控項:更具監控項id
def del_item(auth, itemid):
values = {
"jsonrpc": "2.0",
"method": "item.delete",
"params": [itemid],
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、根據主機 和 鍵值key 匹配 監控項id
def get_item(auth, hostid, item_key):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"hostids": hostid,
"search": {
"key": "log_user" # 鍵值必須和agent的key值相同
},
"sortfield": "name"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['itemid'] # 28344
# return output # [{u'itemid': u'28344', u'key': u'loguser'}]
#3、獲取主機id:根據主機名獲取主機id
def get_host(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid'],
"filter": {
"host": [hostname,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['hostid']
auth = authenticate(url,username,password)
hostid = get_host(auth,'zabbix_agent_1.1.1.3') # 10258
itemid = get_item(auth, hostid, 'log_user') # 28344
del_item(auth, itemid)
#1、禁用/啟用 指定監控項
def update_item(auth, itemid):
values = {
"jsonrpc": "2.0",
"method": "item.update",
"params": {
"itemid": itemid,
"status": 0 # status = 0 啟用 status = 1 禁用
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
#2、根據主機 和 鍵值key 匹配 監控項id
def get_item(auth, hostid, item_key):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"hostids": hostid,
"search": {
"key": "log_user" # 鍵值必須和agent的key值相同
},
"sortfield": "name"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['itemid'] # 28344
# return output # [{u'itemid': u'28344', u'key': u'loguser'}]
#3、獲取主機id:根據主機名獲取主機id
def get_host(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid'],
"filter": {
"host": [hostname,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['hostid']
auth = authenticate(url,username,password)
hostid = get_host(auth,'zabbix_agent_1.1.1.3') # 10258
itemid = get_item(auth, hostid, 'log_user') # 28344
print update_item(auth, itemid)
2、監控項更多查詢操作
#1、只返回具有給定 ID 的監控項
def get_item_by_itemid(auth,):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"itemids": ['28284'],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(getitem_by_itemid(auth,))
'''
[{
"itemid": "28284",
"key": "loguser"
}]
#2、只返回屬於給定組的監控項
def get_item_by_group(auth,):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"groupids": ['19'],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(getitem_by_group(auth,))
'''
[{
"itemid": "28284",
"key": "loguser"
}]
'''
#3、僅返回屬於給定模板的監控項
def get_item_by_template(auth,):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"templateids": ['10266'],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(getitem_by_template(auth,))
'''
[{
"itemid": "28284",
"key": "loguser"
}]
'''
#4、僅返回屬於給定主機的監控項
def get_item_by_host(auth,):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"hostids": ['10264'],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(getitem_by_host(auth,))
'''
[{
"itemid": "28284",
"key": "loguser"
}]
'''
#5、僅返回屬於給定應用程序的監控項
def get_item_by_applicationid(auth,):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"applicationids": ['1118'],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(getitem_by_applicationid(auth,))
'''
[{
"itemid": "28284",
"key": "loguser"
}]
'''
#6、僅返回在給定觸發器中使用的監控項
def get_item_by_triggerids(auth,):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"triggerids": ['15601'],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(getitem_by_triggerids(auth,))
'''
[{
"itemid": "28284",
"key": "loguser"
}]
'''
#7、僅返回屬於具有 主機組名稱 的監控項
def get_item_by_groupname(auth,):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"group": 'New Create Group',
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(getitem_by_groupname(auth,))
'''
[{
"itemid": "28284",
"key": "loguser"
}]
'''
#8、僅返回屬於具有 主機名稱 的監控項
def get_item_by_groupname(auth,):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"host": 'zabbixagent_1.1.1.3',
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(get_item_by_groupname(auth,))
'''
[{
"itemid": "28284",
"key": "loguser"
}]
'''
#9、僅返回屬於具有 應用集名稱 的監控項
def get_item_by_applicationname(auth,):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"application": 'app02',
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(getitem_by_applicationname(auth,))
'''
[{
"itemid": "28284",
"key": "loguser"
}]
3、查詢"監控項"時返回更多關聯信息:主機、應用程序、觸發器
# 查詢"監控項"時返回更多關聯信息:主機、應用程序、觸發器
def get_more_info_item(auth,):
values = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": ['key','itemid'],
"itemids": ['28284'],
"selectHosts": [ # 1.返回應用這個監控項的所有主機
"host",
"hostid",
],
"selectApplications": [ # 2.返回該項所屬的應用程序
"name",
"applicationid",
],
"selectTriggers": [ # 3.返回這個監控項包含的觸發器
"description",
"triggerid",
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps(getmore_info_item(auth,))
'''
[{
"itemid": "28284",
"hosts": [{
"host": "New Create Template",
"hostid": "10266"
}],
"triggers": [{
"triggerid": "15567",
"description": "User_Login"
}],
"key": "loguser",
"applications": [{
"applicationid": "1111",
"name": "App01"
}]
}]
'''
1.7 觸發器操作
1、觸發器常用操作 (監控項 必須歸屬 主機/模板 並且必須同時歸屬 監控項)
參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/trigger/create</a></p>
# 1、創建 一個/多個 觸發器
def create_trigger(auth,description,expression):
values = {
"jsonrpc": "2.0",
"method": "trigger.create",
"params": [
{
"description": description, # 名稱:告警描述信息
"expression": expression, # 表達式
"priority": "2", # 設置告警級別(0:未分類; 1:信息; 2:警告; 3:一般嚴重 ...)
},
# { # 創建多個只需加一個字典即可
# "description": "Too many processes on {HOST.NAME}",
# "expression": "{Linux server:proc.num[].avg(5m)}>300",
# }
],
"auth": auth,
"id": 4
}
output = requestJson(url,values)
return output # 運行后就可以在對應主機的監控項頁面看到剛創建的監控項了
auth = authenticate(url,username,password)
description = "The login user is greater than 2 on {HOST.NAME}"
expression = "{zabbix_agent_1.1.1.3:log_user.last()}>2"
create_trigger(auth,description,expression)
#1、檢索觸發器:這里只返回指定主機所屬的觸發器信息多個以列表形式返回
def get_trigger(auth,hostid):
values = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"hostids": hostid,
"output": "extend",
"selectFunctions": "extend"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output # 運行后就可以在對應主機的監控項頁面看到剛創建的監控項了
#2、獲取主機id
def get_host(auth,hostname):
values = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ['name','hostid'],
"filter": {
"host": [hostname,]
}
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output[0]['hostid']
auth = authenticate(url,username,password)
hostid = get_host(auth,'zabbix_agent_1.1.1.3') # 10258
print json.dumps(get_trigger(auth, hostid))
################ 這里是上面的運行結果:本主機關聯觸發器的信息 ###############
ret = [{
"status": "0",
"functions": [{
"itemid": "28344",
"triggerid": "15586",
"functionid": "17453"
}],
"description": "The login user is greater than 2 on {HOST.NAME}", # 報警描述
"state": "0",
"templateid": "0",
"triggerid": "15586", # 觸發器id
"expression": "{17453}>2", # 表達式
}]
def del_trigger(auth,triggerid):
values = {
"jsonrpc": "2.0",
"method": "trigger.delete",
"params": [triggerid], # 要刪除那個trigger需要我們自己先獲取到它的triggerid
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output # 運行后就可以在對應主機的監控項頁面看到剛創建的監控項了
def update_trigger(auth,triggerid):
values ={
"jsonrpc": "2.0",
"method": "trigger.update",
"params": {
"triggerid": triggerid,
"status": 0 # status=0 啟用 status=1 禁用
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output # 運行后就可以在對應主機的監控項頁面看到剛創建的監控項了
auth = authenticate(url,username,password)
update_trigger(auth,15586)
2、監控項更多查詢操作
#1、只返回指定 ID 的觸發器
def get_trigger_by_triggerids(auth):
values = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"triggerids": ['15567'],
"output": ['triggerid','expression','description',],
"selectFunctions": "extend", # 顯示functions這個字典中的內容
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_trigger_by_triggerids(auth))
'''
[{
"triggerid": "15567",
"expression": "{17438}>3",
"description": "User_Login",
"functions": [{
"itemid": "28284",
"function": "last",
"triggerid": "15567",
"parameter": "",
"functionid": "17438"
}]
}]
'''
#2、只返回指定 主機組 的觸發器
def get_trigger_by_groupids(auth):
values = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"groupids": ['19'],
"output": ['triggerid','expression','description',],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_trigger_by_groupids(auth))
'''
[{
"triggerid": "15601",
"expression": "{17484}>3",
"description": "User_Login"
}]
'''
#3、只返回指定 模板 的觸發器
def get_trigger_by_templateids(auth):
values = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"templateids": ['10266'],
"output": ['triggerid','expression','description',],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_trigger_by_templateids(auth))
'''
[{
"triggerid": "15601",
"expression": "{17484}>3",
"description": "User_Login"
}]
'''
#4、只返回指定 主機 的觸發器
def get_trigger_by_hostids(auth):
values = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"hostids": ['10264'],
"output": ['triggerid','expression','description',],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_trigger_by_hostids(auth))
'''
[{
"triggerid": "15601",
"expression": "{17484}>3",
"description": "User_Login"
}]
'''
#5、只返回指定 監控項 的觸發器
def get_trigger_by_itemids(auth):
values = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"itemids": ['28439'],
"output": ['triggerid','expression','description',],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_trigger_by_itemids(auth))
'''
[{
"triggerid": "15601",
"expression": "{17484}>3",
"description": "User_Login"
}]
'''
#6、只返回指定 應用集 的觸發器
def get_trigger_by_applicationids(auth):
values = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"applicationids": ['1111'],
"output": ['triggerid','expression','description',],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_trigger_by_applicationids(auth))
'''
[{
"triggerid": "15601",
"expression": "{17484}>3",
"description": "User_Login"
}]
'''
#7、只返回指定 主機組名稱 的觸發器
def get_trigger_by_groupname(auth):
values = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"group":'New Create Group',
"output": ['triggerid','expression','description',],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_trigger_by_groupname(auth))
'''
[{
"triggerid": "15601",
"expression": "{17484}>3",
"description": "User_Login"
}]
'''
#8、只返回指定 主機名稱 的觸發器
def get_trigger_by_hostname(auth):
values = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"host":'zabbix_agent_1.1.1.3',
"output": ['triggerid','expression','description',],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( get_trigger_by_hostname(auth))
'''
[{
"triggerid": "15601",
"expression": "{17484}>3",
"description": "User_Login"
}]
'''
3、查詢時返回更多關聯信息:主機組、主機、監控項
# 查詢時返回更多關聯信息:主機組、主機、監控項
def get_more_info_trigger(auth):
values = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"triggerids": ['15567'],
"output": ['triggerid','expression','description',],
"selectGroups": [ # 1.返回模板所屬的主機組
"name",
"groupid"
],
"selectHosts": [ # 2.返回鏈接到模板的主機
"name",
"hostid"
],
"selectItems": [ # 3.返回模板中的監控項.
"name",
"key",
"itemid",
"interfaceid",
],
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
print json.dumps( getmore_info_trigger(auth))
'''
[{
"description": "User_Login",
"items": [{
"itemid": "28284",
"interfaceid": "0",
"key": "log_user",
"name": "login_user"
}],
"triggerid": "15567",
"hosts": [{
"hostid": "10266",
"name": "New Create Template"
}],
"groups": [{
"groupid": "20",
"name": "New Group 02"
}],
"expression": "{17438}>3"
}]
'''
2.1 通過api 接口從 創建主機 到創建並關聯模板全過程
1、主機組操作
# 創建主機組: 運行此函數就會創建組 "New Create Group"
def create_group(auth):
values = {
"jsonrpc": "2.0",
"method": "hostgroup.create",
"params": {
"name": "New Create Group"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
print create_group(auth) # {u'groupids': [u'17']}
2、主機操作
#1、創建主機,並添加到 "New Create Group" 主機組中
def create_host(auth,hostname,ip,gid,templateid=None):
values = {
"jsonrpc": "2.0",
"method": "host.create",
"params": {
#1、主機名稱
"host": hostname,
#2、為主機創建的接口
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": ip,
"dns": "",
"port": "10050"
}
],
#3、將主機添加到主機組中
"groups": [
{
"groupid": gid,
}
],
#4、鏈接一個模板
# "templates": [
# {
# "templateid": "20045"
# }
# ],
# 主機資產清單屬性:把MAC地址設置到主機資產清單里
# "inventory_mode": 0,
# "inventory": {
# "macaddress_a": "01234",
# "macaddress_b": "56768"
# }
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
# 注:這里要傳入主機組id,需要自己獲取
print create_host(auth,'zabbix_agent_1.1.1.3','1.1.1.3',gid='17') # {u'hostids': [u'10254']}
3、創建模板
參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/template/create</a></span></p>
#1、新建模板:模板必須至少關聯一個主機組
def create_template(auth,template_name,gid):
values = {
"jsonrpc": "2.0",
"method": "template.create",
"params": {
"host": template_name,
"groups": { # 模板關聯的主機組(必須要有)
"groupid": gid,
},
# "hosts": [{"hostid": "10084"},{"hostid": "10090"}] # 模板關聯的主機
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
print create_template(auth,"New Create Template",gid='17') # {u'templateids': [u'10255']}
4、導出模板
參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/configuration/export</a></p>
說明:需要到zabbix web界面中自己創建 "應用集","監控項","觸發器",然后再導出模板 zbx_export_templates.xml
# 導出模板
def export_template(auth, templates):
values = {
"jsonrpc": "2.0",
"method": "configuration.export",
"params": {
"options": {
"templates": [
templates
]
},
"format": "xml"
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
print export_template(auth, templates='10255') # {u'templateids': [u'10255']}
export_template = export_template(auth, templates='10255') # {u'templateids': [u'10255']}
template_filename = 'zbx_export_templates.xml'
with open(template_filename, 'w') as f: # 將導出的模板 寫入文件中
f.write(export_template)
5、將模板導入其他zabbix-server中
參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/configuration/import</a></p>
# 導入模板
def import_template(auth, source):
values ={
"jsonrpc": "2.0",
"method": "configuration.import",
"params": {
"format": "xml",
"rules": {
"templates": {
"createMissing": True,
"updateExisting": True
},
"applications": {
"createMissing": True,
"deleteMissing": True
},
"items": {
"createMissing": True,
"updateExisting": True,
"deleteMissing": True
},
"triggers": {
"createMissing": True,
"updateExisting": True,
"deleteMissing": True
},
"groups": {
"createMissing": True,
},
"hosts": {
"createMissing": True,
"updateExisting": True
},
},
"source": source
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
template_filename = 'zbx_export_templates.xml'
with open(template_filename, 'r') as f: # 從文件中讀取剛剛導出的模板
source = f.read()
print import_template(auth, source=source)
# 注:參數說明
# 1、createMissing:如果設置為true,沒有就會創建新的
# 2、deleteMissing:如果設置為true,不在導入數據中的將會從數據庫中被刪除;
# 3、updateExisting:如何設置為true,已有的將會被更新;
6、將指定主機應用模板
參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/template/massadd</a></p>
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>3.4</version>
<date>2018-06-04T10:01:14Z</date>
<groups>
<group>
<name>New Create Group</name>
</group>
</groups>
<templates>
<template>
<template>New Create Template</template>
<name>New Create Template</name>
<description/>
<groups>
<group>
<name>New Create Group</name>
</group>
</groups>
<applications>
<application>
<name>App01</name>
</application>
</applications>
<items>
<item>
<name>login_user</name>
<type>0</type>
<snmp_community/>
<snmp_oid/>
<key>log_user</key>
<delay>30s</delay>
<history>90d</history>
<trends>365d</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<params/>
<ipmi_sensor/>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>App01</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<preprocessing/>
<jmx_endpoint/>
<master_item/>
</item>
</items>
<discovery_rules/>
<httptests/>
<macros/>
<templates/>
<screens/>
</template>
</templates>
<triggers>
<trigger>
<expression>{New Create Template:log_user.last()}>3</expression>
<recovery_mode>0</recovery_mode>
<recovery_expression/>
<name>User_Login</name>
<correlation_mode>0</correlation_mode>
<correlation_tag/>
<url/>
<status>0</status>
<priority>2</priority>
<description/>
<type>0</type>
<manual_close>0</manual_close>
<dependencies/>
<tags/>
</trigger>
</triggers>
</zabbix_export>
# 將指定主機加入模板
def massadd_template(auth,templateid,hostid):
values = {
"jsonrpc": "2.0",
"method": "template.massadd",
"params": {
"templates": [
{
"templateid": templateid
},
],
"hosts": [
{
"hostid": hostid
}
]
},
"auth": auth,
"id": 1
}
output = requestJson(url,values)
return output
auth = authenticate(url,username,password)
print massadd_template(auth, templateid='10266', hostid='10264')
