create service list based on env

This commit is contained in:
saravanan30erd 2020-02-25 18:05:31 +04:00
parent b136bbeb5a
commit 0bdf6d0f58

View file

@ -26,21 +26,40 @@ web.config.debug = False
output = [] output = []
# Functions # Functions
def calculate_count(data): def calculate_count(data):
count = {} count = {}
ls = data.values()
z, nz = 0, 0 z, nz = 0, 0
for v in ls: for _,v in data.items():
if v == 0: ls = v.values()
z += 1 for v in ls:
else: if v == 0:
nz += 1 z += 1
else:
nz += 1
count['green'] = z count['green'] = z
count['red'] = nz count['red'] = nz
return count return count
def arrange_services(services):
checks = {}
for service in services:
name = service['name']
name1, _, name2 = name.partition('-')
if name2 == '':
name2 = name1
name1 = 'All'
if name1 not in list(checks.keys()):
checks[name1] = OrderedDict()
checks[name1][name2] = int(service['status'])
return checks
def sorted_checks(checks):
s_checks = {}
for k,v in checks.iteritems():
s_checks[k] = OrderedDict(
sorted(v.iteritems(),key=itemgetter(1), reverse=True)
)
return s_checks
def getMonit(): def getMonit():
output = [] output = []
@ -57,22 +76,14 @@ def getMonit():
allstat = json.loads(json.dumps(xmltodict.parse(r.text)['monit'])) allstat = json.loads(json.dumps(xmltodict.parse(r.text)['monit']))
services = allstat['service'] services = allstat['service']
status = {}
server = {} server = {}
checks = OrderedDict()
for service in services: checks = arrange_services(services)
name = service['name'] s_checks = sorted_checks(checks)
status[name] = int(service['status']) count = calculate_count(s_checks)
checks[name] = status[name]
sorted_checks = OrderedDict()
sorted_checks = OrderedDict(sorted(checks.iteritems(),
key=itemgetter(1), reverse=True))
count = calculate_count(sorted_checks)
server = dict(name=site, url=s['url'], server = dict(name=site, url=s['url'],
result=sorted_checks, s_rate=count) result=s_checks, s_rate=count)
print(server)
output.append(server) output.append(server)
print(datetime.datetime.now()) print(datetime.datetime.now())
return(output) return(output)