User Tools

Site Tools


nagiosdeliverytest

Nagios Delivery Test

<toc></toc> I put together the following python script in order to verify that mail is being delivered through a server and can be retrieved by IMAP-SSL on the next Nagios test cycle.

#!/usr/bin/python
# $Id: check_delivery_imap.py,v 1.3 2015/05/16 19:48:11 root Exp root $

import sys
import re
import imaplib
import smtplib
from email.Utils import formatdate

date = formatdate(localtime=True)

host = sys.argv[1]
recipient = sys.argv[2]
password = sys.argv[3]

warning = 0

# IMAP existing messages

try:
        imapserver = imaplib.IMAP4_SSL(host)
        # imapserver.debug=4
        imapserver.login(recipient,password)
        resp, messagecount = imapserver.select()
        if int(messagecount[0]) &lt; 1:
          print &quot;WARNING - no message in INBOX&quot;
          warning = 1
        resp, ids = imapserver.search(None, 'ALL')
        for id in ids[0].split():
          imapserver.store(id, '+FLAGS', '\\Deleted')
        imapserver.expunge()
        imapserver.close()
        imapserver.logout()

except:
        print &quot;CRITIAL - IMAP server error&quot;
        sys.exit(2)

# Use SMTP to send new message

try:
        testmessage = '''Date: ''' + date + '''
Subject: Nagios Delivery test message
From: ''' + recipient + '''
To: ''' + recipient + '''

Nagios Delivery test message: ''' + date

        smtpserver = smtplib.SMTP(host,25)
        # smtpserver.set_debuglevel(1)
        smtpserver.starttls()
        smtpserver.sendmail(sys.argv[2], sys.argv[2], testmessage)
        smtpserver.quit()

except:
        print &quot;CRITIAL - SMTP server error&quot;
        sys.exit(2)

if warning == 1:
        sys.exit(warning)
else:
        print &quot;Mail Delivery OK&quot;
nagiosdeliverytest.txt · Last modified: 2020/02/13 22:55 (external edit)

free spam filter