add bundle:check-mail-received
All checks were successful
bundlewrap/pipeline/head This commit looks good

This commit is contained in:
Franzi 2021-07-10 14:22:19 +02:00
parent 76b859c629
commit 3351767d56
Signed by: kunsi
GPG key ID: 12E3D2136B818350
6 changed files with 137 additions and 6 deletions

View file

@ -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: {} <imap host> <username> <password> <message sender>'.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)

View file

@ -0,0 +1,5 @@
files = {
'/usr/local/share/icinga/plugins/check_imap_for_mail_from': {
'mode': '0755',
},
}

View file

@ -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,
},
},
}

View file

@ -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': {

View file

@ -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',

View file

@ -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',