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')
