对zabbix模板的增删改查
1.新建模板:模板必须至少关联一个主机组
def create_temp():
values = {
"jsonrpc": "2.0",
"method": "template.create",
"params": {
"host": "test1_temp", #模板名
"groups": {
"groupid": gid, #模板关联的主机组(必须要有)
},
#"hosts": [{"hostid": "10084"},{"hostid": "10090"}] #模板关联的主机(可选)
},
"auth": auth,
"id": 1
}
2.获取模板id:根据模板名称获取模板id
def get_tempid():
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ["host","templateid"],
"filter": {
"host": ["test1_temp",] #传入模板名
}
},
"auth": auth,
"id": 1
}
3.根据模板id删除模板
def del_temp():
values = {
"jsonrpc": "2.0",
"method": "template.delete",
"params": ["23"],
"auth": auth,
"id": 1
}
4.修改模板名称
def update_tempname():
values = {
"jsonrpc": "2.0",
"method": "template.update",
"params": {
"templateid": "23", #模板id
"name": "temp2_name" #新模板名
},
"auth": auth,
"id": 1
}
5.给模板添加主机组
def add_temp_group():
values = {
"jsonrpc": "2.0",
"method": "template.massadd",
"params": {
"templates": [
{
"templateid": "10266" #模板id
}
],
"groups": [
{
"groupid": "20" #主机组id
}
]
},
"auth": auth,
"id": 1
}
6.链接到给定模板的模板
def add_temp_temp():
values = {
"jsonrpc": "2.0",
"method": "template.massadd",
"params": {
"templates": [
{
"templateid": "10266" #模板id
}
],
"templates_link": [
{
"templateid": "10093" #要链接的模板id
}
]
},
"auth": auth,
"id": 1
}
7.从模板中删除主机组
def rm_group_temp():
values = {
"jsonrpc": "2.0",
"method": "template.massremove",
"params": {
"templateids": ["10266"], #模板id
"groupids": "20" #主机组id
},
"auth": auth,
"id": 1
}
8.将模板从指定主机中删除:但是模板中的 应用集、监控项等还在
def rm_host_temp():
values = {
"jsonrpc": "2.0",
"method": "template.massremove",
"params": {
"templateids": ["10266"], #模板id
"hostids": ["10264"] #主机id
},
"auth": auth,
"id": 1
}
9.从给定的模板中取消链接和清除的模板: 删除了对应的应用集和监控项
def rm_temp_temp():
values = {
"jsonrpc": "2.0",
"method": "template.massremove",
"params": {
"templateids": ['10266'], #模板id
"templateids_clear": ['10093'], #要清理的连接模板id
},
"auth": auth,
"id": 1
}
10.取消与给定模板链接的模板:不会删除已关联的应用集、监控项等
def rm_templ_temp():
values = {
"jsonrpc": "2.0",
"method": "template.massremove",
"params": {
"templateids": ['10266'], #模板id
"templateids_link": ['10093'], #要清理的连接模板id
},
"auth": auth,
"id": 1
}
11.从给定的模板中取消链接并清除模板“10093”(删除已链接的 应用集、监控项等)
def update_templ_templates():
values = {
"jsonrpc": "2.0",
"method": "template.massupdate",
"params": {
"templates": [
{
"templateid": "10266" #模板id
},
],
"templates_clear": [
{
"templateid": "10093" #要清理的连接模板id
}
]
},
"auth": auth,
"id": 1
}
12.主机组替换模板所属的当前主机组: 其他所有组机组都会取消关联
def update_group_temp():
values = {
"jsonrpc": "2.0",
"method": "template.massupdate",
"params": {
"templates": [
{
"templateid": "10266" #模板id
},
],
"groups": [
{
"groupid": "20" #主机组id
}
]
},
"auth": auth,
"id": 1
}
13.去除这个模板链接的其他所有主机,只链接这一个主机
def update_host_temp():
values = {
"jsonrpc": "2.0",
"method": "template.massupdate",
"params": {
"templates": [
{
"templateid": "10266" #模板id
},
],
"hosts": [
{
"hostid": "10264" #主机id
}
]
},
"auth": auth,
"id": 1
}
14.用于替换当前链接的模板的模板:链接的其他模板都会取消,但他们的应用集和监控项不会删除
def update_temp_temp():
values = {
"jsonrpc": "2.0",
"method": "template.massupdate",
"params": {
"templates": [
{
"templateid": "10266" #模板id
},
],
"templates_link": [
{
"templateid": "10094" #要链接的模板id
}
]
},
"auth": auth,
"id": 1
}
15.根据模板名称检索获取信息
def get_temp():
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"filter": {
"host": ["New Create Template"] #模板名
}
},
"auth": auth,
"id": 1
}
16.根据模板id检索
def get_tempid_tempinfo():
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"templateids": ['10266'] #模板id
},
"auth": auth,
"id": 1
}
17.查看主机组所关联的模板
def get_group_temp(auth):
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"groupids": ['20'] #主机组名
},
"auth": auth,
"id": 1
}
18.查看主机关联的模板
def get_host_temp():
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"hostids": ['10264'] #主机id
},
"auth": auth,
"id": 1
}
19.只返回包含指定监控项的模板
def get_item_temp():
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"itemids": ['28284'] #监控项id
},
"auth": auth,
"id": 1
}
20.只返回包含指定触发器的模板
def get_trigger_temp():
values = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": ['host', 'templateid'],
"triggerids": ['15567'] #触发器id
},
"auth": auth,
"id": 1
}
21.返回templates属性中更多属性:主机组、主机、子模板
def get_temp():
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
}
22.查询时返回更多关联信息:主机组、主机、监控项、触发器、子模板、父模板
def get_more_tempinfo():
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
}
23.导出模板,需要将输出写到文件里
def export_temp():
values = {
"jsonrpc": "2.0",
"method": "configuration.export",
"params": {
"options": {
"templates": ["23"] #模板id
},
"format": "xml"
},
"auth": auth,
"id": 1
}
24.导入模板
def import_temp():
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": "模板文件xml读取后的内容"
},
"auth": auth,
"id": 1
}
# 注:参数说明
# 1、createMissing:如果设置为true,没有就会创建新的
# 2、deleteMissing:如果设置为true,不在导入数据中的将会从数据库中被删除;
# 3、updateExisting:如何设置为true,已有的将会被更新;