# -*- coding: utf-8 -*-
import json,re
import urllib.request, urllib.error, urllib.parse
class ZabbixAPI:
# 初始化
def __init__(self):
self.__url = 'http://192.168.74.133/api_jsonrpc.php'
self.__user = 'admin'
self.__password = 'zabbix'
self.__header = {"Content-Type": "application/json-rpc"}
self.__token_id = self.UserLogin()
# 登陆获取token
def UserLogin(self):
data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": self.__user,
"password": self.__password
},
"id": 0,
}
return self.PostRequest(data)
# 推送请求
def PostRequest(self, data):
request = urllib.request.Request(self.__url,json.dumps(data).encode('utf-8'),self.__header)
result = urllib.request.urlopen(request)
response = json.loads(result.read().decode('utf-8'))
try:
return response['result']
except KeyError:
pass
# 获取指定模板ID名称
def GetTemplatesID(self,Template_name):
data = {
"jsonrpc":"2.0",
"method":"template.get",
"params":{
"output":"templateid",
"filter": {
"host": Template_name
}
},
"auth": self.__token_id,
"id": 0
}
Template_ID_List = self.PostRequest(data)
return Template_ID_List[0]["templateid"]
# 从指定模板ID获取所有触发器ID并返回触发器名称description
def GetAllTriggersIDFromTemplatesID(self,Template_name):
TemplatesID = self.GetTemplatesID(Template_name)
data = {
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"templateids": TemplatesID,
"output": ["description"]
},
"auth": self.__token_id,
"id": 0
}
return self.PostRequest(data)
# 更新触发器名称
def updateTriggerName(self,TriggerID=None,description=None):
data = {
"jsonrpc": "2.0",
"method": "trigger.update",
"params": {
"triggerid": TriggerID,
"description": description
},
"auth": self.__token_id,
"id": 0
}
return self.PostRequest(data)
# 获取原触发名称,并且替换特定字符串进行批量更新触发器名称
def ReWriteTriggerName(self,Template_name):
StringJson = self.GetAllTriggersIDFromTemplatesID(Template_name)
# print(StringJson)
# print(type(StringJson))
for i in range(len(StringJson)):
ret = re.sub('已关闭', 'is down',StringJson[i]["description"])
self.updateTriggerName(StringJson[i]["triggerid"],ret)
def main():
# 实例化zabbix类
zapi=ZabbixAPI()
# 模板选择
hosts=zapi.ReWriteTriggerName("Template OS Linux")
if __name__ == '__main__':
main()
之前Templates-OS-linux模板中触发名称列表:
为阳城等地区用户提供了全套网页设计制作服务,及阳城网站建设行业解决方案。主营业务为成都网站建设、成都做网站、阳城网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
用完之后的列表: