From 3351767d565e1bfa2a5e81b6f92a9a79279c9fd2 Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Sat, 10 Jul 2021 14:22:19 +0200 Subject: [PATCH] add bundle:check-mail-received --- .../files/check_imap_for_mail_from | 70 +++++++++++++++++++ bundles/check-mail-received/items.py | 5 ++ bundles/check-mail-received/metadata.py | 41 +++++++++++ nodes/aurto.py | 9 ++- nodes/htz/ex42-1048908.py | 9 ++- nodes/rx300.py | 9 ++- 6 files changed, 137 insertions(+), 6 deletions(-) create mode 100644 bundles/check-mail-received/files/check_imap_for_mail_from create mode 100644 bundles/check-mail-received/items.py create mode 100644 bundles/check-mail-received/metadata.py diff --git a/bundles/check-mail-received/files/check_imap_for_mail_from b/bundles/check-mail-received/files/check_imap_for_mail_from new file mode 100644 index 0000000..f8db136 --- /dev/null +++ b/bundles/check-mail-received/files/check_imap_for_mail_from @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +from imaplib import IMAP4_SSL +from subprocess import check_output +from sys import argv, exit +from time import time + +if len(argv) < 5: + print('Usage: {} '.format(argv[0])) + exit(3) + +NOW = time() + +try: + imap = IMAP4_SSL(argv[1]) + imap.login(argv[2], argv[3]) + + imap.select('Inbox') + + _, data = imap.search(None, 'ALL') + + something_found = False + + for item in data: + for index in item.split(): + received_in_this_mail = None + from_in_this_mail = False + + try: + message = imap.fetch(index, '(RFC822)') + + message_text = bytearray() + for part in message[1][0]: + message_text.extend(part) + message_text = message_text.decode().splitlines() + + for line in message_text: + lline = line.strip().lower() + + if lline.startswith('from:') and argv[4].lower() in line: + from_in_this_mail = True + + if lline.startswith('date:'): + date = line.strip()[5:].strip() + unixtime = int(check_output([ + 'date', + '--date={}'.format(date), + '+%s', + ]).decode().strip()) + + if unixtime > (NOW-(60*60*25)): + received_in_this_mail = date + + if received_in_this_mail and from_in_this_mail: + print('Found message from "{}" sent at "{}"'.format(argv[4], received_in_this_mail)) + received_in_this_mail = None + from_in_this_mail = False + something_found = True + except: + pass + + if something_found: + # there should be output above + exit(0) + + print('No Mails found') + exit(2) +except Exception as e: + print(repr(e)) + exit(3) diff --git a/bundles/check-mail-received/items.py b/bundles/check-mail-received/items.py new file mode 100644 index 0000000..ed76f80 --- /dev/null +++ b/bundles/check-mail-received/items.py @@ -0,0 +1,5 @@ +files = { + '/usr/local/share/icinga/plugins/check_imap_for_mail_from': { + 'mode': '0755', + }, +} diff --git a/bundles/check-mail-received/metadata.py b/bundles/check-mail-received/metadata.py new file mode 100644 index 0000000..0eb666d --- /dev/null +++ b/bundles/check-mail-received/metadata.py @@ -0,0 +1,41 @@ +@metadata_reactor.provides( + 'cron/check-mail-received', + 'icinga2_api/check-mail-received/services', +) +def process_metadata(metadata): + cron = set() + services = {} + + my_mail_address = 'root@{}'.format(metadata.get('hostname')) + + for name, config in metadata.get('check-mail-received', {}).items(): + cron.add('{minute} {hour} * * * root date | mail -s "daily test mail from {node}" -r {source} {target}'.format( + minute=node.magic_number%60, + hour=node.magic_number%24, + node=node.name, + source=my_mail_address, + target=config['email'], + )) + + services[f'MAIL RECEIVED ON {name}'] = { + 'command_on_monitored_host': repo.libs.faults.join_faults([ + '/usr/local/share/icinga/plugins/check_imap_for_mail_from', + config['imap_host'], + config.get('imap_user', config['email']), + config['imap_pass'], + my_mail_address, + ]), + 'check_interval': '15m', + 'retry_interval': '5m', + } + + return { + 'cron': { + 'check-mail-received': '\n'.join(sorted(cron)), + }, + 'icinga2_api': { + 'check-mail-received': { + 'services': services, + }, + }, + } diff --git a/nodes/aurto.py b/nodes/aurto.py index 21899f2..449aa61 100644 --- a/nodes/aurto.py +++ b/nodes/aurto.py @@ -2,6 +2,7 @@ nodes['aurto'] = { 'hostname': '31.47.232.107', 'bundles': { 'backup-client', + 'check-mail-received', }, 'groups': { 'arch', @@ -18,8 +19,12 @@ nodes['aurto'] = { '/var/cache/pacman/aurto', }, }, - 'cron': { - 'telekom_nervkram': vault.decrypt('encrypt$gAAAAABg6X1pOUs_jVkqyHYChM4P6lpdpAUmLXkDPy2grxcL-R8Ab10Isxj52dvUkLFET-LhNgxgnGbdYtMhv1_awgS9klHW1A==').format_into('0 0 * * * root date | mail -s \'daily test mail\' -r root@aurto.kunbox.net {}@t-online.de'), + 'check-mail-received': { + 't-online': { + 'email': 'franzi.kunsmann@t-online.de', + 'imap_host': 'secureimap.t-online.de', + 'imap_pass': bwpass.attr('t-online.de/franzi.kunsmann@t-online.de', 'imap'), + }, }, 'interfaces': { 'enp1s0': { diff --git a/nodes/htz/ex42-1048908.py b/nodes/htz/ex42-1048908.py index 772226f..925e9ec 100644 --- a/nodes/htz/ex42-1048908.py +++ b/nodes/htz/ex42-1048908.py @@ -1,5 +1,6 @@ nodes['htz.ex42-1048908'] = { 'bundles': { + 'check-mail-received', 'dovecot', 'element-web', # 'gitea', @@ -86,8 +87,12 @@ nodes['htz.ex42-1048908'] = { '/opt/matrix/matrix-dimension', }, }, - 'cron': { - 'telekom_nervkram': vault.decrypt('encrypt$gAAAAABfqXi23M96wrSLhqlbhqgePYX06LjPXfyQU2y_07kqYYLztj_PhS1-dk4r5FiiL2Ofmx5iCKW1sZNqiQSuHj2uKaitH0GnwHqj5CI2JwkAS9HrFxw=').format_into('0 0 * * * root date | mail -s \'daily test mail \' -r postmaster@mx0.kunbox.net {}'), + 'check-mail-received': { + 't-online': { + 'email': 'franzi.kunsmann@t-online.de', + 'imap_host': 'secureimap.t-online.de', + 'imap_pass': bwpass.attr('t-online.de/franzi.kunsmann@t-online.de', 'imap'), + }, }, 'element-web': { 'url': 'chat.franzi.business', diff --git a/nodes/rx300.py b/nodes/rx300.py index 7ec072a..b3b34ef 100644 --- a/nodes/rx300.py +++ b/nodes/rx300.py @@ -7,6 +7,7 @@ nodes['rx300'] = { 'hostname': '31.47.232.106', 'bundles': { + 'check-mail-received', 'gitea', 'lm-sensors', 'miniflux', @@ -49,8 +50,12 @@ nodes['rx300'] = { }, }, }, - 'cron': { - 'telekom_nervkram': vault.decrypt('encrypt$gAAAAABg6X1pOUs_jVkqyHYChM4P6lpdpAUmLXkDPy2grxcL-R8Ab10Isxj52dvUkLFET-LhNgxgnGbdYtMhv1_awgS9klHW1A==').format_into('0 0 * * * root date | mail -s \'daily test mail\' -r root@rx300.kunbox.net {}@t-online.de'), + 'check-mail-received': { + 't-online': { + 'email': 'franzi.kunsmann@t-online.de', + 'imap_host': 'secureimap.t-online.de', + 'imap_pass': bwpass.attr('t-online.de/franzi.kunsmann@t-online.de', 'imap'), + }, }, 'gitea': { 'version': '1.14.4',