#!/usr/bin/python # -*- coding: iso-8859-15 -*- # # Copyright (C) 2008 Guillaume Nadot # This file may be distributed and/or modified under the terms of # the GNU General Public License version 3 as published by # the Free Software Foundation. # This file is distributed without any warranty; without even the implied # warranty of merchantability or fitness for a particular purpose. # See "LICENSE.GPL" in the source distribution for more information. import poplib import smtplib import datetime import sys, getopt, os from os import chdir,remove, rename def usage(): print "--relay " print "--popserver " print "--popuser " print "--poppassword " print "--to " print "--from " print "--idfile " def checkmail(popserver,user,password): connexion = poplib.POP3(popserver) try: connexion.user(user) connexion.pass_(password) except poplib.error_proto,error_message : print "CRITICAL: " + user + " :" + str(error_message) sys.exit(2) ids = [] #get the id of each message numMessages = len(connexion.list()[1]) for i in range(numMessages): for msg in connexion.retr(i+1)[1]: try: id = msg.split("#",2)[1] ids.append(id) except: pass #clear the messages on the server for num in range(len(connexion.list()[1])): connexion.dele(num + 1 ) connexion.quit() return ids def main(): status = "Unknown" returncode = 3 #get the options from the command line try: opts, args = getopt.getopt(sys.argv[1:],"",[ 'relay=', 'popserver=', 'popuser=', 'poppassword=', 'to=','from=','idfile=']) except getopt.GetoptError: usage() return 3 relay = popserver = popuser = poppassword = destination = source = idfile = None for o, a in opts: if o == "--relay": relay = a elif o == "--popserver": popserver = a elif o == "--popuser": popuser = a elif o == "--poppassword": poppassword = a elif o == "--to": destination = a elif o == "--from": source = a elif o == "--idfile": idfile = a if relay == None or popserver == None or popuser == None or poppassword == None or destination == None or source == None or idfile == None: usage() return 1 #Get the id of the previously sent message ( if any ) #sentmsgids = open(str(idfile),'a') try: sentmsgids = open(str(idfile)) except: sentmsgids = open(str(idfile),'a') sentmsgids.close() sentmsgids = open(str(idfile)) tmpids = open(idfile + "tmp", 'a') #get the ids of all the messages on the pop server, and delete them receivedmsgids = checkmail(popserver,popuser, poppassword ) #print "ids of received messages : " + str(receivedmsgids) #compare the sent and received msg ids try: for sentmsgid in sentmsgids: #print "------ id of sent message : " + sentmsgid + "-------------" for receivedmsgid in receivedmsgids: #print "id of received message : " + receivedmsgid if sentmsgid.find(receivedmsgid) == 1 : #print "match : " + str(sentmsgid.find(receivedmsgid)) #the mail has arrived, delete this id from the file pass else: #print "no match" #the mail didn't arrive yet, keep id in the file. tmpids.write(sentmsgid) except IOError, error_message: print error_message pass tmpids.close() sentmsgids.close() rename(idfile + "tmp",idfile) #If the file is empty, everythin is OK sentmsgids = open(str(idfile)) ids = str(sentmsgids.read()) sentmsgcount = ids.count('#')/2 #print "sentmsgcount : " + str(sentmsgcount) if sentmsgcount == 0: status = "OK: all sent messages received" returncode = 0 elif sentmsgcount >= 1 and sentmsgcount < 5: status = "WARNING: " + str(sentmsgcount) +" have not been received (yet)" returncode = 1 elif sentmsgcount <= 5: status = "CRITICAL: " + str(sentmsgcount) +" have not been received (yet)" returncode = 2 #Send the mail sentmsgids = open(str(idfile),'a') try: #message creation msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (source,destination,"check mail system")) now = datetime.datetime.now() #print now msg = "#" + str(now) + "#" #write the message containing the time to a file sentmsgids.write(msg + '\n') sentmsgids.close() #envoi du message server = smtplib.SMTP(relay) server.sendmail(source, destination , msg) except IOError,error_message : print "CRITICAL :" + str(error_message) sys.exit(2) print status return returncode if __name__ == "__main__": sys.exit(main())