feat(bot): regroupe les services pour un affichage plus efficace

This commit is contained in:
François Poulain 2020-05-01 12:07:40 +02:00
parent 2b6385b0b4
commit 226fc5bdbf
1 changed files with 29 additions and 11 deletions

View File

@ -15,6 +15,7 @@ import json
import os.path import os.path
import re import re
import sys import sys
from itertools import groupby
import requests import requests
@ -107,7 +108,7 @@ class Icinga2ServiceManager:
"X-HTTP-Method-Override": "GET", "X-HTTP-Method-Override": "GET",
} }
data = { data = {
"attrs": ["last_check_result"], "attrs": ["last_check_result", "display_name", "host_name"],
"filter": "service.state!=ServiceOK", "filter": "service.state!=ServiceOK",
} }
try: try:
@ -309,18 +310,35 @@ class IcingaBot(Icinga2ServiceManager, irc.bot.SingleServerIRCBot):
def do_leave(self, c, e): def do_leave(self, c, e):
self.disconnect() self.disconnect()
def regrouped_ko_services(self):
def regroup_key(elem):
return elem["attrs"]["display_name"]
return [
(group, [service["attrs"]["host_name"] for service in services])
for group, services in groupby(
sorted(self.ko_services, key=regroup_key), regroup_key
)
]
def do_list(self, c, e): def do_list(self, c, e):
if self.notifications: if self.ko_services:
for srv in self.notifications: host_by_service = [
try: (service, [hostname.split(".")[0] for hostname in hosts])
self.send( for service, hosts in sorted(
"{}: => {}".format( self.regrouped_ko_services(), key=lambda x: -len(x[1])
srv["name"], )
srv["attrs"]["last_check_result"]["output"], ]
self.send(
"\n".join(
[
"{} ({}): {}".format(
service, len(hostnames), ", ".join(hostnames)
) )
) for service, hostnames in host_by_service
except Exception: ]
self.send("{}: => No check result.".format(srv["name"])) )
)
else: else:
self.send("Nothing particularly exciting.") self.send("Nothing particularly exciting.")