hades是根據(jù)python2.7下的django下開發(fā)的運(yùn)維管理系統(tǒng)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 17/8/15 下午1:16
# @Author : lee
# @File : test.py
# @Software: PyCharm
# 說明: 輸入 IP或者是Cxx 得到詳細(xì)信息 例如:C0751
import requests
import json
import re
import sys
regex_XXX = re.compile(r"[CLSND]\d\d\d\d") # 匹配規(guī)則
regex_IP = re.compile(r"\d+\.\d+\.\d+\.\d+") # 匹配ip
class Cmdb_hades(object):
def __init__(self,key):
self.key = key
self.flag2 = 0
# hades_ip_cid 該方法可以獲取IP或者CXX對(duì)應(yīng)的數(shù)據(jù)
def hades_ip_cid(self, name):
try:
# regex_XXX.match(self.key):
if regex_IP.match(name):
url = 'http://hades.xx.com.cn/api/query_cmdb/?machine_ip=%s' %name
elif regex_XXX.match(name):
url = 'http://hades.xx.com.cn/api/query_cmdb/?cid=%s' %name
response = requests.get(url)
content = response.text
json_dict = json.loads(content)
j = 1
if len(json_dict['result'])==0:
print("**沒有相關(guān)%s的數(shù)據(jù)**"%name)
else:
for i in json_dict['result']:
if self.flag2 == 1:
print("++++++++++第%s項(xiàng)++++++++++"%j)
print("環(huán)境:",i['env'])
print("SID:",i["sid"])
print("應(yīng)用:",i["app"])
if i["domain"] != "":
print("域名:",i["domain"])
print("工程:",i["project"])
if len(i["department"]) !=0:
print("部門:",i["department"])
if len(i["operator"]) !=0:
print("運(yùn)維:",str(i["operator"]).replace('@xx.com',''))
if len(i["developer"]) !=0:
print("研發(fā):",str(i["developer"]).replace('@xx.com',''))
if len(i["qa"]) !=0:
print("測(cè)試:",str(i["qa"]).replace('@xx.com',''))
if len(i["producter"]) !=0:
print("產(chǎn)品:",str(i["producter"]).replace('@xx.com',''))
if len(i["redis"]) !=0:
print("緩存:",i["redis"])
if len(i["db"]) !=0:
print("數(shù)據(jù)庫(kù):",(i["db"]))
j += 1
list_machine = []
for i in i['machine']:
list_machine.append(i['ip'][0])
print("主機(jī):",list_machine)
except:
print("**程序錯(cuò)誤踏施,請(qǐng)反饋noc組**")
# 該方法的兩個(gè)作用 找出數(shù)據(jù)中一二級(jí)目錄存放在 dict_roject_directory 中石蔗;列出一二級(jí)目錄的樹形圖
def get_roject_directory(self):
list_departmentName1 = [] # 為了保證順序,采取這個(gè)方法 放有重復(fù)數(shù)據(jù)的原始數(shù)據(jù)
list_departmentName2 = [] # 去重后的和源順序一致
dict_roject_directory = {}
remarks_url = "xxxx" # cmdb 接口
token= "xxxxx"
response_remarks = requests.get(remarks_url,token)
content_remarks = response_remarks.text
json_dict_remarks = json.loads(content_remarks)
for i in json_dict_remarks['resultinfo']:
list_departmentName1.append(i['departmentName'])
for id in list_departmentName1:
if id not in list_departmentName2:
list_departmentName2.append(id)
for i in list_departmentName2:
list = []
for j in json_dict_remarks['resultinfo']:
if j['departmentName'] == i:
list.append([j['productName'], j['cid'], j['cidName']])
dict_roject_directory[i] = list
self.dict_roject_directory = dict_roject_directory
self.flag = 0
if len(sys.argv) == 1:
print('```')
# 樹形圖
print('|--','一二級(jí)目錄樹形圖(new)')
for i in dict_roject_directory:
print(' |--',i)
list1 = []
for j in dict_roject_directory[i]:
list1.append(j[0])
list1 = (set(list1))
for i in list1:
print(' | |--',i)
print('```')
# 這個(gè)方法來調(diào)用之前的方法
def last(self):
if regex_IP.match(self.key):
self.flag2 = 1
name = self.key
self.hades_ip_cid(name)
elif regex_XXX.match(self.key):
self.flag2 = 1
name = self.key
self.hades_ip_cid(name)
else:
flag1 = 0
for i in self.dict_roject_directory:
for j in self.dict_roject_directory[i]:
if j[0] == self.key:
flag1 = j[0]
self.hades_ip_cid(j[1])
if flag1 != self.key:
print("**參數(shù)輸入有誤畅形,請(qǐng)檢查您的參數(shù)--“%s” **" % self.key)
if __name__ == "__main__":
if len(sys.argv) == 1:
item = Cmdb_hades('1') # 這個(gè) '1' 沒有意義养距,完全為了格式
item.get_roject_directory()
else:
item = Cmdb_hades(sys.argv[1])
item.get_roject_directory()
item.last()
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者