bundles/check-mail-received: move check source to icinga2 itself

This commit is contained in:
Franzi 2022-01-02 14:50:32 +01:00
parent 8be40c4adc
commit dc2b2ae86b
Signed by: kunsi
GPG key ID: 12E3D2136B818350
5 changed files with 16 additions and 12 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

@ -86,6 +86,13 @@ object CheckCommand "check_imap" {
}
}
object CheckCommand "check_imap_for_mail_from" {
import "plugin-check-command"
import "ipv4-or-ipv6"
command = [ "/usr/local/share/icinga/plugins/check_imap_for_mail_from", "$imap_host$", "$imap_user$", "$imap_pass$", "$imap_from$" ]
}
object CheckCommand "check_sipgate_account_balance" {
import "plugin-check-command"
import "ipv4-or-ipv6"