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

View File

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