公司伺服器太多,如果要一個一個的手動新增Screen的話,那估計我早累死了,所以呢,懶人必備神器,使用Python寫了個批量Screen新增和更新的指令碼。
說下此指令碼的功能和用處吧
1、如果Screen不存在,可以使用此指令碼批量新增Screen
2、如果Screen存在,也可以使用此指令碼批量更新Screen
3、此指令碼會判斷主機是否存在,不存在,會直接退出,然後列出可以使用的主機列表
好了,就這麼多了,下面貼程式碼。
#!/usr/bin/python # coding:utf-8 # 使用說明 # 指令碼可以不接受引數,不接受引數則是全部主機更新 # 指令碼接受一個或兩個引數 # 第一個引數是主機,第二引數是Screen名稱 # 省略第二個引數,Screen則與主機名稱一樣 # ./CUScreen.py # ./CUScreen.py 主機名稱 Screen名稱 # ./CUScreen.py Host_Name Screen_Name # ./CUScreen.py web01 # ./CUScreen.py web01 web01_screen import urllib2,json,sys def res(url="", values=""): data = json.dumps(values) # 請求API res = urllib2.Request(url, data, {"Content-Type": "application/json"}) resp = urllib2.urlopen(res, data) out = json.loads(resp.read()) try: error = out["result"] except: error = out["error"]["data"] return error return out["result"] def auth(url="", username="", password=""): values = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": username, "password": password, }, "id": 0, } id = res(url, values) return id def gethosts(auth=""): values = { "jsonrpc":"2.0", "method":"host.get", "params":{ "output":{"hostid":"name"}, }, "auth":auth, "id": 1 } out = res(url, values) print "Host Count :",len(out) for host in out: print "Host ID : %s Host Name : %s" % (host[`hostid`],host[`name`]) return out def getgraphs(auth="", hostname=""): values = { "jsonrpc":"2.0", "method":"graph.get", "params":{ # 過濾某個主機的Graph "filter":{"host":hostname} }, "auth":auth, "id": 1 } out = res(url, values) # 判斷主機是否存在 if out == []: print hostname,"not is exists" print "以下是存在的主機:" gethosts(auth) sys.exit(1) # 定義Screenitem的引數,為空列表 graphs_list = [] y = 0 for i in out: graphs_list.append( { "resourcetype": 0, # 新增graphid "resourceid": i["graphid"], # 定義寬度 "width":1000, # 定義高度 "height":100, # 是否為動態圖形 "dynamic":1, # 座標x軸,向右 "x": 0, # 座標y軸,向下 "y": y, } ) y += 1 return graphs_list # C代表建立 U代表更新 def CUScreen(auth="", screen_name="", graphs_list=""): # 判斷Screen是否存在 values1 = { "jsonrpc": "2.0", "method": "screen.get", "params": { "output": "extend", # 過濾Screen "filter":{"name":screen_name}, }, "auth": auth, "id": 1 } # 定義建立Screen的資料體 values2 = { "jsonrpc": "2.0", "method": "screen.create", "params": { "name":screen_name, # Screen的寬度 "hsize":1, # Screen的長度 "vsize":len(graphs_list), # Screen的item資訊 "screenitems": graphs_list, }, "auth": auth, "id": 1 } # 使用values1進行判斷 out = res(url, values1) #進行ScreenID判斷 if out == []: # ScreenID不存在則建立ScreenID並新增item print screen_name,"Screen Name Not is exists, Create Screen Name :",screen_name # 使用values2 out = res(url, values2) print screen_name,"Screen Name is Create OK, Screen Item Create OK" return out else: screen_id = out[0][`screenid`] # 定義更新Screen的資料體 values3 = { "jsonrpc": "2.0", "method": "screen.update", "params": { # ScreenID "screenid":screen_id, # 計算Screen的行數 "vsize":len(graphs_list), # Screen的item "screenitems":graphs_list, }, "auth": auth, "id": 1 } # ScreenID存在則更新所有的item print screen_name,"Screen Name is exists, Screen ID :",screen_id # 使用values3 out = res(url, values3) print screen_name,"Screen Item Update OK" return out if __name__ == "__main__": # zabbix的API地址,不同環境的不一樣 url = "http://zabbix.xxx.com/api_jsonrpc.php" # zabbix的登陸賬號 username = "xxx" # zabbix的登陸密碼 password = "xxx" # 預設不輸入引數則全部主機都進行更新 if len(sys.argv) == 1: # 獲取認證ID auth = auth(url,username,password) for i in gethosts(auth): # 獲取GraphID graphs_list = getgraphs(auth,i[`name`]) # 使用主機名稱(hostname變數)當Screen名稱 CUScreen(auth,i[`name`],graphs_list) # 如果你輸入一個引數,Screen名稱與主機名稱一樣 if len(sys.argv) == 2: # 主機名稱,賦值為hostname變數 hostname = sys.argv[1] # 獲取認證ID auth = auth(url,username,password) # 獲取GraphID graphs_list = getgraphs(auth,hostname) # 使用主機名稱(hostname變數)當Screen名稱 CUScreen(auth,hostname,graphs_list) # 如果你輸入二個引數,第一個為主機名稱,第二個為Screen名稱 if len(sys.argv) == 3: # 主機名稱,賦值為hostname變數 hostname = sys.argv[1] # Screen名稱,賦值為screen_name變數 screen_name = sys.argv[2] # 獲取認證ID auth = auth(url,username,password) # 獲取GraphID graphs_list = getgraphs(auth,hostname) # 使用screen_name(screen_name變數)來當Screen名稱 CUScreen(auth,screen_name,graphs_list)