From 8c6c691e5eb6c86c99334b10ec3d5ee1f1b1eeba Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Sun, 22 Nov 2020 10:34:49 +0100 Subject: [PATCH] bundles/icinga2: implement SMS notifications --- .../files/scripts/icinga_notification_wrapper | 44 ++++++++++++++----- bundles/icinga2/items.py | 1 + nodes/ovh/icinga2.py | 4 ++ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/bundles/icinga2/files/scripts/icinga_notification_wrapper b/bundles/icinga2/files/scripts/icinga_notification_wrapper index 2caf9db..e3e6b5e 100644 --- a/bundles/icinga2/files/scripts/icinga_notification_wrapper +++ b/bundles/icinga2/files/scripts/icinga_notification_wrapper @@ -3,10 +3,14 @@ import email.mime.text import smtplib from argparse import ArgumentParser -from requests import get +from json import dumps +from requests import post from subprocess import run from sys import argv +SIPGATE_USER='${node.metadata['icinga2']['sipgate_user']}' +SIPGATE_PASS='${node.metadata['icinga2']['sipgate_pass']}' + parser = ArgumentParser( prog='icinga_notification_wrapper', description='Icinga2 Notification Wrapper', @@ -55,14 +59,34 @@ def log_to_syslog(message): def notify_per_sms(): - log_to_syslog('SMS requested, but not implemented yet!') # FIXME TODO - return - msg = 'ICINGA: {host}/{service} is {state}: {output}'.format( - host=args.host_name, - service=args.service_name, - state=args.state, - output=args.output - ) + message = { + 'message': 'ICINGA: {host}/{service} is {state}: {output}'.format( + host=args.host_name, + service=args.service_name, + state=args.state.upper(), + output=args.output + ), + 'smsId': 's0', # XXX what does this mean? Documentation is unclear + 'recipient': args.sms + } + headers = { + 'Content-type': 'application/json', + 'Accept': 'application/json' + } + try: + r = post( + 'https://api.sipgate.com/v2/sessions/sms', + json=message, + headers=headers, + auth=(SIPGATE_USER, SIPGATE_PASS), + ) + + if r.status_code == 204: + log_to_syslog('Sent a SMS to "{}"'.format(args.sms)) + else: + r.raise_for_status() + except Exception as e: + log_to_syslog('Sending a SMS to "{}" failed: {}'.format(args.sms, repr(e))) def notify_per_mail(): @@ -88,7 +112,7 @@ State: {state} mail = email.mime.text.MIMEText(text.format( host=args.host_name, service=args.service_name, - state=args.state, + state=args.state.upper(), output=args.output ), 'plain', diff --git a/bundles/icinga2/items.py b/bundles/icinga2/items.py index 453dda6..b0e5874 100644 --- a/bundles/icinga2/items.py +++ b/bundles/icinga2/items.py @@ -93,6 +93,7 @@ files = { }, '/etc/icinga2/scripts/icinga_notification_wrapper': { 'source': 'scripts/icinga_notification_wrapper', + 'content_type': 'mako', 'mode': '0755', }, '/etc/icinga2/features-available/ido-pgsql.conf': { diff --git a/nodes/ovh/icinga2.py b/nodes/ovh/icinga2.py index 86de8ae..5f95573 100644 --- a/nodes/ovh/icinga2.py +++ b/nodes/ovh/icinga2.py @@ -26,6 +26,10 @@ nodes['ovh.icinga2'] = { 'php-imagick': {}, }, }, + 'icinga2': { + 'sipgate_user': vault.decrypt('encrypt$gAAAAABfujAmCUnicSAllq8MskXnPodKp3cGcfA6Abvef-rAYwB2CtCwt9oBRVKFskJPVArDaF1wfjNTfLwgX3gTP7xFutJ1HA=='), + 'sipgate_pass': vault.decrypt('encrypt$gAAAAABfui_4B7UmOosI_gsQ-xvmd3X_BUDSl-G2KF_Tg8O6RpUvk0gHexOKsrTb6se1ipXsh7RC9pbZCKMtesW0C6j24LHXDKCOjkqI77oO0ZjnG6SUwfcJqg61biNiRlXy8z-9LCGA'), + }, 'nginx': { 'vhosts': { 'icingaweb': {