根據(jù) hades接口信息,做成樹形表;再將相關(guān)數(shù)據(jù)顯示出來

hades是根據(jù)python2.7下的django下開發(fā)的運維管理系統(tǒng)

相關(guān)數(shù)據(jù)的顯示
樹形目錄
#!/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") # 匹配規(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對應(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項++++++++++"%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("運維:",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("測試:",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ù)庫:",(i["db"]))
                    j += 1
                    list_machine = []

                    for i in i['machine']:
                        list_machine.append(i['ip'][0])

                    print("主機:",list_machine)
        except:
            print("**程序錯誤,請反饋noc組**")

    # 該方法的兩個作用 找出數(shù)據(jù)中一二級目錄存放在 dict_roject_directory 中;列出一二級目錄的樹形圖
    def get_roject_directory(self):
        list_departmentName1 = []   # 為了保證順序,采取這個方法 放有重復(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('|--','一二級目錄樹形圖(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('```')
    # 這個方法來調(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ù)輸入有誤,請檢查您的參數(shù)--“%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()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容