#!/usr/bin/python # # chack_qmail_processed : Nagios agent to check how many e-mail are processed by qmail. # # The MIT License (MIT) # # Copyright (c) 2016 Antoine Pernot # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import sys import time import getopt import os qmail_log_path = '/var/log/qmail/qmail-send/current' delai = 600 # In seconds def usage(): print "Usage : " print " check_qmail_proceed -w -c " print "Warning and critical level is a ratio in percent of failed delveried emails" print " and successful deliveried emails. " def tai64_unix(time): return int(str(time).replace('@','')[8:16], 16)-28 def unix_tai64(time): return '4000000' + str(hex(int(time)+28)).replace('x','') try: opts, args = getopt.getopt(sys.argv[1:], "w:c:h", ["warning=","critical=","help"]) except getopt.GetoptError: print "Invalid argument. " usage() sys.exit(3) warning_level = -1 critical_level = -1 for opt,arg in opts: if opt in ("-w", "--warning"): try: warning_level = int(arg) except ValueError: print "Warning level must be an integer (ex. 20 for 20%). " sys.exit(3) elif opt in ("-c", "--critical"): try: critical_level = int(arg) except ValueError: print "Critical level must be an integer (ex. 20 for 20%). " sys.exit(3) elif opt in ("-h", "--help"): usage() sys.exit(3) if warning_level == -1 or critical_level == -1: print "Warning level and critical level must be set. " usage() sys.exit(3) elif warning_level > critical_level: print "Warning level must be smaller than critical level. " sys.exit(3) if os.path.isfile(qmail_log_path) == False: print "qmail log path not found. " sys.exit(3) endtime = int(time.time()) starttime = endtime-delai empty_section = True success_msg_cnt = 0 fail_msg_cnt = 0 local_msg_cnt = 0 remote_msg_cnt = 0 filestream = open(qmail_log_path,'r') for line in filestream.readlines(): line_time = tai64_unix(line[:26]) if line_time < endtime and line_time>starttime: empty_section = False if "delivery" in line and "success:" in line: success_msg_cnt +=1 elif "delivery" in line and "failure:" in line: fail_msg_cnt +=1 elif 'to local' in line: local_msg_cnt +=1 elif 'to remote' in line: remote_msg_cnt +=1 if empty_section: print "CRITICAL - Empty log" sys.exit(2) else: try: ratio = round(fail_msg_cnt*100.0/(success_msg_cnt+fail_msg_cnt),2) except ZeroDivisionError: print "CRITICAL - No messages processsed. " sys.exit(2) appendix = "\n|'Failure ratio'=" + str(ratio) + "%;"+str(warning_level)+";"+str(critical_level)+";0;100" appendix += "\n|'Successful delivery'=" + str(success_msg_cnt) appendix += "\n|'Failed delivery'=" + str(fail_msg_cnt) appendix += "\n|'Local delivery'=" + str(local_msg_cnt) appendix += "\n|'Remote delivery'=" + str(remote_msg_cnt) if ratio > critical_level: print "CRITICAL - " + str(ratio) + "% delivery failure ratio. " + appendix sys.exit(2) elif ratio > warning_level: print "WARNING - " + str(ratio) + "% delivery failure ratio. " + appendix sys.exit(1) else: print "OK - " + str(ratio) + "% delivery failure ratio. " + appendix sys.exit(0)