hades是根据python2.7下的django下开发的运维管理系统
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 17/8/15 下午1:16
# @Author : lee
# @File : test.py
# @Software: PyCharm
# 说明: 输入 IP或者是Cxx 得到详细信息 例如:C0751
import requests
import json
import re
import sys
regex_XXX = re.compile(r"[CLSND]\d\d\d\d") # 匹配规则
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对应的数据
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("**没有相关%s的数据**"%name)
else:
for i in json_dict['result']:
if self.flag2 == 1:
print("++++++++++第%s项++++++++++"%j)
print("环境:",i['env'])
print("SID:",i["sid"])
print("应用:",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("运维:",str(i["operator"]).replace('@xx.com',''))
if len(i["developer"]) !=0:
print("研发:",str(i["developer"]).replace('@xx.com',''))
if len(i["qa"]) !=0:
print("测试:",str(i["qa"]).replace('@xx.com',''))
if len(i["producter"]) !=0:
print("产品:",str(i["producter"]).replace('@xx.com',''))
if len(i["redis"]) !=0:
print("缓存:",i["redis"])
if len(i["db"]) !=0:
print("数据库:",(i["db"]))
j += 1
list_machine = []
for i in i['machine']:
list_machine.append(i['ip'][0])
print("主机:",list_machine)
except:
print("**程序错误,请反馈noc组**")
# 该方法的两个作用 找出数据中一二级目录存放在 dict_roject_directory 中;列出一二级目录的树形图
def get_roject_directory(self):
list_departmentName1 = [] # 为了保证顺序,采取这个方法 放有重复数据的原始数据
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('|--','一二级目录树形图(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('```')
# 这个方法来调用之前的方法
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("**参数输入有误,请检查您的参数--“%s” **" % self.key)
if __name__ == "__main__":
if len(sys.argv) == 1:
item = Cmdb_hades('1') # 这个 '1' 没有意义,完全为了格式
item.get_roject_directory()
else:
item = Cmdb_hades(sys.argv[1])
item.get_roject_directory()
item.last()