bundles/icinga2: implement SMS notifications

This commit is contained in:
Franzi 2020-11-22 10:34:49 +01:00
parent 22d5ba12ee
commit 8c6c691e5e
Signed by: kunsi
GPG key ID: 12E3D2136B818350
3 changed files with 39 additions and 10 deletions

View file

@ -3,10 +3,14 @@
import email.mime.text import email.mime.text
import smtplib import smtplib
from argparse import ArgumentParser from argparse import ArgumentParser
from requests import get from json import dumps
from requests import post
from subprocess import run from subprocess import run
from sys import argv from sys import argv
SIPGATE_USER='${node.metadata['icinga2']['sipgate_user']}'
SIPGATE_PASS='${node.metadata['icinga2']['sipgate_pass']}'
parser = ArgumentParser( parser = ArgumentParser(
prog='icinga_notification_wrapper', prog='icinga_notification_wrapper',
description='Icinga2 Notification Wrapper', description='Icinga2 Notification Wrapper',
@ -55,14 +59,34 @@ def log_to_syslog(message):
def notify_per_sms(): def notify_per_sms():
log_to_syslog('SMS requested, but not implemented yet!') # FIXME TODO message = {
return 'message': 'ICINGA: {host}/{service} is {state}: {output}'.format(
msg = 'ICINGA: {host}/{service} is {state}: {output}'.format( host=args.host_name,
host=args.host_name, service=args.service_name,
service=args.service_name, state=args.state.upper(),
state=args.state, output=args.output
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(): def notify_per_mail():
@ -88,7 +112,7 @@ State: {state}
mail = email.mime.text.MIMEText(text.format( mail = email.mime.text.MIMEText(text.format(
host=args.host_name, host=args.host_name,
service=args.service_name, service=args.service_name,
state=args.state, state=args.state.upper(),
output=args.output output=args.output
), ),
'plain', 'plain',

View file

@ -93,6 +93,7 @@ files = {
}, },
'/etc/icinga2/scripts/icinga_notification_wrapper': { '/etc/icinga2/scripts/icinga_notification_wrapper': {
'source': 'scripts/icinga_notification_wrapper', 'source': 'scripts/icinga_notification_wrapper',
'content_type': 'mako',
'mode': '0755', 'mode': '0755',
}, },
'/etc/icinga2/features-available/ido-pgsql.conf': { '/etc/icinga2/features-available/ido-pgsql.conf': {

View file

@ -26,6 +26,10 @@ nodes['ovh.icinga2'] = {
'php-imagick': {}, 'php-imagick': {},
}, },
}, },
'icinga2': {
'sipgate_user': vault.decrypt('encrypt$gAAAAABfujAmCUnicSAllq8MskXnPodKp3cGcfA6Abvef-rAYwB2CtCwt9oBRVKFskJPVArDaF1wfjNTfLwgX3gTP7xFutJ1HA=='),
'sipgate_pass': vault.decrypt('encrypt$gAAAAABfui_4B7UmOosI_gsQ-xvmd3X_BUDSl-G2KF_Tg8O6RpUvk0gHexOKsrTb6se1ipXsh7RC9pbZCKMtesW0C6j24LHXDKCOjkqI77oO0ZjnG6SUwfcJqg61biNiRlXy8z-9LCGA'),
},
'nginx': { 'nginx': {
'vhosts': { 'vhosts': {
'icingaweb': { 'icingaweb': {