feat(posts): simplifie les ack et recheck : match sur le nom du service ou de l hote

This commit is contained in:
François Poulain 2020-08-23 18:01:53 +02:00
parent 86a89976cf
commit f99889622e

View File

@ -35,9 +35,9 @@ Requirements: python3-irc python3-requests
The known commands are:
"""
commands = {
"ack": {"help": "Acknowledge a given service."},
"ack": {"help": "Acknowledge all services matching the query."},
"recheck": {
"help": "Recheck a given service or all services.",
"help": "Recheck all services matching the query.",
"synonyms": [r"refresh"],
},
"list": {"help": "List all KO services.", "synonyms": [r"lsit", r"lits"]},
@ -135,84 +135,45 @@ class Icinga2ServiceManager:
]
self.ko_services = new_ko_services
return (lost, news)
except Exception:
self.send("Unable to fetch from Icinga2")
except Exception as e:
self.send("Unable to fetch from Icinga2: {}".format(e))
return (False, False)
def ack_service(self, srv, comment, nick):
# weird but needed:
if comment == "":
comment = " "
# /weird
def post_on_services(self, pattern, uri, data={}):
headers = {
"Accept": "application/json",
"X-HTTP-Method-Override": "POST",
}
data = {
"author": nick,
"comment": comment,
}
params = {
"type": "Service",
"filter": (
'service.__name=="{}"'.format(srv)
if srv is not None
else "service.state!=ServiceOK"
),
"filter": "service.state!=ServiceOK"
}
if pattern:
params['filter'] += '&& match("*{}*", service.__name)'.format(pattern)
try:
r = requests.post(
self.build_request_url(
"/v1/actions/acknowledge-problem", params=params
),
self.build_request_url(uri, params=params),
headers=headers,
auth=(settings["icinga2user"], settings["icinga2pass"]),
data=json.dumps(data),
verify=settings["icinga2ca"],
)
if r.status_code == 200:
for a in r.json()["results"]:
if a["code"] == 200.0:
self.send(a["status"])
if srv is not None and not r.json()["results"]:
self.send("No result for service name « {} »".format(srv))
else:
self.send("{} for service name « {} »".format(r.text, srv))
except Exception:
self.send("Unable to post to Icinga2")
for a in r.json()["results"]:
self.send(a["status"])
if pattern is not None and not r.json()["results"]:
self.send("No matching service for « {} »".format(pattern))
except Exception as e:
self.send("Unable to post to Icinga2: {}".format(e))
def recheck_service(self, srv):
headers = {
"Accept": "application/json",
"X-HTTP-Method-Override": "POST",
def ack_service(self, pattern, comment, nick):
data = {
"author": nick,
"comment": comment or " ", # never "" !
}
params = {
"type": "Service",
"filter": (
'service.__name=="{}"'.format(srv)
if srv is not None
else "service.state!=ServiceOK"
),
}
try:
r = requests.post(
self.build_request_url(
"/v1/actions/reschedule-check", params=params
),
headers=headers,
auth=(settings["icinga2user"], settings["icinga2pass"]),
verify=settings["icinga2ca"],
)
if r.status_code == 200:
for a in r.json()["results"]:
if a["code"] == 200.0:
self.send(a["status"])
if srv is not None and not r.json()["results"]:
self.send("No result for service name « {} »".format(srv))
else:
self.send("{} for service name « {} »".format(r.text, srv))
except Exception:
self.send("Unable to post to Icinga2")
self.post_on_services(pattern, '/v1/actions/acknowledge-problem', data)
def recheck_service(self, pattern):
self.post_on_services(pattern, '/v1/actions/reschedule-check')
class IcingaBot(Icinga2ServiceManager, irc.bot.SingleServerIRCBot):
@ -356,16 +317,16 @@ class IcingaBot(Icinga2ServiceManager, irc.bot.SingleServerIRCBot):
def do_ack(self, c, e):
if self.args is None:
self.send(
e.source.nick + ": usage: !ack <service|all> [: comment]"
e.source.nick + ": usage: !ack <pattern or all> [: comment]"
)
return
tokens = self.args.split(":", 1)
srv, comment = tokens[0].strip(), ""
pattern, comment = tokens[0].strip(), ""
if len(tokens) > 1:
comment = tokens[1].strip()
if srv == "all":
srv = None
self.ack_service(srv, comment, e.source.nick)
if pattern == "all":
pattern = None
self.ack_service(pattern, comment, e.source.nick)
def do_recheck(self, c, e):
if self.args == "all":