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