mirror of
https://framagit.org/framasoft/framaspace/argos.git
synced 2025-04-28 18:02:41 +02:00
🎨 — Make each handler build its own message
This commit is contained in:
parent
2ab9afabc9
commit
9ed58220f4
3 changed files with 31 additions and 19 deletions
|
@ -22,11 +22,19 @@ def handle_alert(config: Config, result, task, severity, request):
|
||||||
result.status,
|
result.status,
|
||||||
severity)
|
severity)
|
||||||
|
|
||||||
if config.general.mail is not None or \
|
if config.general.mail is not None and \
|
||||||
config.general.gotify is not None:
|
'mail' in getattr(config.general.alerts, severity):
|
||||||
|
notify_by_mail(result, task, severity, config.general.mail, request)
|
||||||
|
|
||||||
subject = f"{urlparse(task.url).netloc}: status {severity}"
|
if config.general.gotify is not None and \
|
||||||
msg = f"""\
|
'gotify' in getattr(config.general.alerts, severity):
|
||||||
|
notify_with_gotify(result, task, severity, config.general.gotify, request)
|
||||||
|
|
||||||
|
|
||||||
|
def notify_by_mail(result, task, severity, config: Mail, request) -> None:
|
||||||
|
logger.debug('Will send mail notification')
|
||||||
|
|
||||||
|
msg = f"""\
|
||||||
URL: {task.url}
|
URL: {task.url}
|
||||||
Check: {task.check}
|
Check: {task.check}
|
||||||
Status: {severity}
|
Status: {severity}
|
||||||
|
@ -35,18 +43,8 @@ Time: {result.submitted_at}
|
||||||
See results of task on {request.url_for('get_task_results', task_id=task.id)}
|
See results of task on {request.url_for('get_task_results', task_id=task.id)}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if config.general.mail is not None and \
|
|
||||||
'mail' in getattr(config.general.alerts, severity):
|
|
||||||
notify_by_mail(subject, msg, config.general.mail)
|
|
||||||
if config.general.gotify is not None and \
|
|
||||||
'gotify' in getattr(config.general.alerts, severity):
|
|
||||||
notify_with_gotify(subject, msg, severity, config.general.gotify)
|
|
||||||
|
|
||||||
|
|
||||||
def notify_by_mail(subject: str, msg: str, config: Mail) -> None:
|
|
||||||
logger.debug('Will send mail notification')
|
|
||||||
mail = f"""\
|
mail = f"""\
|
||||||
Subject: [Argos] {subject}
|
Subject: [Argos] {urlparse(task.url).netloc}: status {severity}
|
||||||
|
|
||||||
{msg}"""
|
{msg}"""
|
||||||
|
|
||||||
|
@ -75,16 +73,29 @@ Subject: [Argos] {subject}
|
||||||
smtp.sendmail(config.mailfrom, address, mail)
|
smtp.sendmail(config.mailfrom, address, mail)
|
||||||
|
|
||||||
|
|
||||||
def notify_with_gotify(subject: str, msg: str, severity: str, config: List[GotifyUrl]) -> None:
|
def notify_with_gotify(result, task, severity: str, config: List[GotifyUrl], request) -> None:
|
||||||
logger.debug('Will send gotify notification')
|
logger.debug('Will send gotify notification')
|
||||||
headers = {'accept': 'application/json',
|
headers = {'accept': 'application/json',
|
||||||
'content-type': 'application/json'}
|
'content-type': 'application/json'}
|
||||||
|
|
||||||
priority = 9
|
priority = 9
|
||||||
|
icon = '❌'
|
||||||
if severity == Severity.OK:
|
if severity == Severity.OK:
|
||||||
priority = 1
|
priority = 1
|
||||||
|
icon = '✅'
|
||||||
elif severity == Severity.WARNING:
|
elif severity == Severity.WARNING:
|
||||||
priority = 5
|
priority = 5
|
||||||
|
icon = '⚠️'
|
||||||
|
|
||||||
|
subject = f"{icon} {urlparse(task.url).netloc}: status {severity}"
|
||||||
|
msg = f"""\
|
||||||
|
URL: {task.url}
|
||||||
|
Check: {task.check}
|
||||||
|
Status: {severity}
|
||||||
|
Time: {result.submitted_at}
|
||||||
|
|
||||||
|
See results of task on {request.url_for('get_task_results', task_id=task.id)}
|
||||||
|
"""
|
||||||
|
|
||||||
payload = {'title': subject,
|
payload = {'title': subject,
|
||||||
'message': msg,
|
'message': msg,
|
||||||
|
@ -99,7 +110,7 @@ def notify_with_gotify(subject: str, msg: str, severity: str, config: List[Gotif
|
||||||
headers=headers,
|
headers=headers,
|
||||||
json=payload)
|
json=payload)
|
||||||
res.raise_for_status()
|
res.raise_for_status()
|
||||||
except httpx.RequestError as exc:
|
except httpx.RequestError as err:
|
||||||
logger.error('An error occurred while sending a message to %s with token %s',
|
logger.error('An error occurred while sending a message to %s with token %s',
|
||||||
exc.request.url,
|
err.request.url,
|
||||||
token)
|
token)
|
||||||
|
|
|
@ -59,6 +59,7 @@ async def create_results(
|
||||||
|
|
||||||
# Don’t create an alert if the severity has not changed
|
# Don’t create an alert if the severity has not changed
|
||||||
if last_severity != severity:
|
if last_severity != severity:
|
||||||
|
# XXX Use a job queue or make it async
|
||||||
handle_alert(config, result, task, severity, request)
|
handle_alert(config, result, task, severity, request)
|
||||||
|
|
||||||
db_results.append(result)
|
db_results.append(result)
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
{{ counts_dict['ok'] }}
|
{{ counts_dict['ok'] }}
|
||||||
</article>
|
</article>
|
||||||
<article>
|
<article>
|
||||||
<header> ⚠️ Warning</header>
|
<header>⚠️ Warning</header>
|
||||||
{{ counts_dict['warning'] }}
|
{{ counts_dict['warning'] }}
|
||||||
</article>
|
</article>
|
||||||
<article>
|
<article>
|
||||||
|
|
Loading…
Reference in a new issue