您好,登錄后才能下訂單哦!
# -*- 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模板中觸發名稱列表:
用完之后的列表:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。