File: //lib/python2.7/site-packages/lap/postfix-queue.py
from __future__ import generators
from glob import glob as ls
import yaml
import sys
import commands
import socket
import os
def dirwalk(dir):
"walk a directory tree, using a generator"
for f in os.listdir(dir):
fullpath = os.path.join(dir,f)
if os.path.isdir(fullpath) and not os.path.islink(fullpath):
for x in dirwalk(fullpath): # recurse into subdir
yield x
else:
yield fullpath
def __run__(params):
socket.setdefaulttimeout(5)
total = 0
queue = {}
aicm = ['active','incoming','maildrop','deferred']
thresholds = yaml.load(params.get('thresholds'))
for postfix in ls('/var/spool/postfix*'):
name = str(postfix.split('/')[-1:])
mails = 0
for directory in aicm:
mails = mails + len([ msgid for msgid in dirwalk("%s/%s" % (postfix,directory))])
queue[name] = mails
total = queue[name] + total
nmessages = queue[name]
if nmessages:
if int(nmessages) >= thresholds['critical']:
return [2, "CRITICAL - %s: %s in queue" % (name,nmessages)]
elif int(nmessages) >= thresholds['warning']:
return [1, "WARNING - %s: %s in queue" % (name,nmessages)]
return [0, "OK - less than %s messages in queue" % thresholds['warning']]