Free Plugin for Monitoring Rackspace Postfix Email Queue

Karen McRae
Administrative Assistant

When Rackspace added “intelligence” dashboards to our hosting account, we were excited.  Finally, one place to keep an eye on metrics for our cloud servers!

Unfortunately, the new dashboard didn’t include a pre-built plugin to monitor the Postfix email queue.  To combat this omission, and to help others facing a similar Postfix monitoring issue, our CTO, Rob, created a script in Python.  After setting up the plugin and configuring it within Rackspace, we were able to get graphs of the email queues and set thresholds that will issue alerts.

To add it to Rackspace Monitoring, follow the instructions below (the Rackspace Postfix Queue Plugin is open source Apache 2.0 licensed):

  1. Depending on the distro, copy and paste the below code into the location /usr/lib/rackspace-monitoring-agent/plugins/postfix-queue.py
  2. Edit the queue dir variable if different than /var/spool/postfix
  3. chmod 755 postfix-queue.py
  4. In Rackspace Monitoring, create a new Monitor Check
  5. Choose “Custom Plugin” with the Command “postfix-queue.py”
  6. Success, profit, and much celebration!
#!/usr/bin/env python
"""
Copyright 2015 Ender Technology Corp. - Rob Olmos ([email protected])

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----

Rackspace Cloud Monitoring Plugin for Postfix Mail Queues

Retrieves Stats for Postfix mail queues: active, bounce, corrupt, deferred, hold, incoming

Usage:
Place plug-in in /usr/lib/rackspace-monitoring-agent/plugins

Setup a CLoud Monitoring Check of type agent.plugin to run
python ./postfix-queues.py

Update the queue_dir to your Postfix installation's queue directory.

The following is an example 'criteria' for a Rackspace Monitoring Alarm:
if (metric['queue.active'] >= 100) {
 return new AlarmStatus(CRITICAL, 'over 100 active emails'
if (metric['queue.deferred'] >= '50') {
 return new AlarmStatus(WARNING, 'more than 50 deferred emails');
}
return new AlarmStatus(OK, 'Less than 50 deferred emails');

Please note that you will need to adjust the thresholds based on what works for you.

Available metrics are
queue.active
queue.bounce
queue.corrupt
queue.deferred
queue.hold
queue.incoming
"""

import os

def walk_error(e):
    raise e

def main():
    try:
        queue_dir = '/var/spool/postfix'
        postfix_dirs = {'active': 0, 'bounce': 0, 'corrupt': 0, 'deferred': 0, 'hold': 0, 'incoming': 0}

        for postfix_dir in postfix_dirs.keys():
            file_count = 0
            dir_path = os.path.join(queue_dir, postfix_dir)
            for root, dirs, files in os.walk(dir_path, False, walk_error):
                file_count += len(files)
            postfix_dirs[postfix_dir] = file_count

        print "status ok succeeded in obtaining metrics"
        for dir_name, count in postfix_dirs.items():
            print "metric queue.%s gauge %d" % (dir_name, count)
    except Exception, e:
        print "status err %s" % e

if __name__ == '__main__':
    main()

________________________
Endertech is a Cloud Computing and Hosting Company in Los Angeles able to provide solutions for your website. Contact us for your free consultation.

- Karen McRaeAdministrative Assistant | 

Filed under: <Cloud>