Glen Pitt-Pladdy :: Blog

Postfix stats on Cacti (via SNMP)

Following on from the basics of SNMP I did previously, this article now adds the next set of SNMP extension scripts, config, and Cacti templates to monitor a Postfix mail server.

Update: Also see my recent article about basic Postfix configuration  which works with these templates.

Postfix Stats

Postfix produces a minimum of statistics it's self, but as is common in Unix, it does produce comprehensive logs of activity which provides plenty of material for generating statistics from.

There are already a load of logfile analysers around, but for the purposes of this monitoring, I decided the best thing was to write my own and then it would be easy to make it work exactly how I wanted for producing stats.

The script parses two log files given on the command line (this allows it to deal with log file rotations). The third argument is the store for the stats which includes the point in the file it has already processed to. It loads this, adds the additional stats and then replaces the file with the latest data.

Download:  Perl script to analyse Postfix log files for SNMP

I place this script (make it executable first: chmod +x postfix-loganalyser) in /etc/snmp

One feature I have built into this script is that it ouputs any lines that it does not completely understand. Cron will email this back to the administrator. This means that over time the script can be enhanced to extract more information from the logs. If you want to ignore this then just send all the output to /dev/null in the cron job.

Getting Postfix stats over SNMP

Like discussed previously, Postfix logfiles require root privilege to access, and snmpd runs as a low privilege user. What I do is have a CRON job that reads this data and stores it in files for snmpd to access via extension scripts.

If you are using the same config I described previously, then simply add the lines to your /etc/snmp/local-snmp-cronjob file to make it look something like this (may have other content for other tasks):

#!/bin/sh

# run postfix stats
/etc/snmp/postfix-loganalyser \
    /var/log/mail.log.0 \
    /var/log/mail.log \
    /var/local/snmp/postfix

From the there, I have a load of small scripts for each aspect of the stats I monitor. One thing to consider when writing these scripts is to ensure that if more parameters are added to them, they are all added to the ends of the scripts to ensure that the order of the data given to snmpd does not change.

Downloads:

I place these scripts (make them executable first: chmod +x postfix-stats-*) in /etc/snmp

In /etc/snmp/snmpd.conf add the following lines (or others if you want to monitor them):

extend postfixlocal /etc/snmp/postfix-stats-local
extend postfixpickup /etc/snmp/postfix-stats-pickup
extend postfixqueue /etc/snmp/postfix-stats-queue
extend postfixsmtp /etc/snmp/postfix-stats-smtp-connect
extend postfixsmtpstatus /etc/snmp/postfix-stats-smtp-status
extend postfixsmtptls /etc/snmp/postfix-stats-smtp-tls
extend postfixsmtpwarn /etc/snmp/postfix-stats-smtp-warning
extend postfixsmtpd /etc/snmp/postfix-stats-smtpd-connect
extend postfixsmtpdnq /etc/snmp/postfix-stats-smtpd-noqueue
extend postfixsmtpdnqrec /etc/snmp/postfix-stats-smtpd-noqueue-recipient
extend postfixsmtpdnqrel /etc/snmp/postfix-stats-smtpd-noqueue-relay
extend postfixsmtpdnqsnd /etc/snmp/postfix-stats-smtpd-noqueue-sender
extend postfixsmtpdqd /etc/snmp/postfix-stats-smtpd-queued
extend postfixsmtpdtls /etc/snmp/postfix-stats-smtpd-tls
extend postfixsmtpdwarn /etc/snmp/postfix-stats-smtpd-warning
extend postfixmilter /etc/snmp/postfix-stats-milter

Once you have added all this in you can test these scripts by running them from the command line, and via SNMP by appending the appropriate SNMP OID to the "snmpwalk" commands shown previously.

Cacti Templates

I have generated some basic Cacti Templates for these Postfix stats.

Download: Cacti Templates for Postfix statistics over SNMP

Simply import this template, and add the graphs you want to the appropriate device graphs in Cacti. It should just work if your SNMP is working correctly for that device (ensure other SNMP parameters are working for that device).

Graph Screen Shots

.... for a very quiet mail server

Postfix Stats over SNMP on Cacti : ClamAV Milter

Postfix Stats over SNMP on Cacti : Queues

Postfix Stats over SNMP on Cacti : Local

Postfix Stats over SNMP on Cacti : Pickup

Postfix Stats over SNMP on Cacti : smtp connections

Postfix Stats over SNMP on Cacti : smtp status

Postfix Stats over SNMP on Cacti : smtp TLS

Postfix Stats over SNMP on Cacti : smtp TLS Certificate Verification

Postfix Stats over SNMP on Cacti : smtp Warnings

Postfix Stats over SNMP on Cacti : smtpd accept / reject

Postfix Stats over SNMP on Cacti : smtpd connections

Postfix Stats over SNMP on Cacti : smtpd TLS

Postfix Stats over SNMP on Cacti : smtpd TLS Certificate Verification

Postfix Stats over SNMP on Cacti : smtpd Warnings

Update:

One person had pointed out that there has been some problems on some versions of Cacti with importing the templates. The template was generated with version 0.8.7b (from Debain Lenny).

The problem manifests it's self as "Cacti version does not exist" error, and appears to be cured by adding in this version, although in my version the file is actually global_arrays.php

The relevant array from my global_arrays.php / config_array.php:

$hash_version_codes = array(
    "0.8.4"  => "0000",
    "0.8.5"  => "0001",
    "0.8.5a" => "0002",
    "0.8.6"  => "0003",
    "0.8.6a" => "0004",
    "0.8.6b" => "0005",
    "0.8.6c" => "0006",
    "0.8.6d" => "0007",
    "0.8.6e" => "0008",
    "0.8.6f" => "0009",
    "0.8.6g" => "0010",
    "0.8.6h" => "0011",
    "0.8.6i" => "0012",
    "0.8.6j" => "0013",
    "0.8.7"  => "0014",
    "0.8.7a" => "0015",
    "0.8.7b" => "0016"
    );

Update 19/6/2010:

Another updated version - this again improves scripts and templates, adding monitoring for the ClamAV Milter and smtpd Certificate Verification Failures. Also you can now download all the neccesary scripts and templates in tar ball: 20100619-postfix-cacti-update.tar.bz2

Comments:

30/06/2010 20:42 :: Pedro Díaz

Good evening, in the office we test your script and we found that running that script in Fedora Core 12 with snmp v5.4.2.1 generates a error that cant find the stats file when generates the new one in '/var/local/snmp/postfix', we find the solution replacing the code in the local-snmp-cronjob file original contaning '/etc/snmp/postfix-loganalyser \

/var/log/mail.log.0 \

/var/log/mail.log \

/var/local/snmp/postfix'

for this one

'#!/bin/sh

LASTFILE=`ls -lat /var/log/maillog* | gawk 'NR==2 {print $9}'`

CURRENTFILE=`ls -lat /var/log/maillog* | gawk 'NR==1 {print $9}'`

STATSFILE=/var/local/snmp/postfix

# run postfix stats

/etc/snmp/postfix-loganalyser $LASTFILE $CURRENTFILE $STATSFILE'

We hope that this code can help the community thanks.

19/07/2010 13:01 :: Umberto Bernardi

Sorry,

but what is the "appropriate OID" to test via snmp?

If I query without OID I get a lot of output, but nothing relative to postfix.

thank you

24/07/2010 12:30 :: Glen Pitt-Pladdy

Hi Umberto - sorry for the delay getting back to you.... It's been one of those weeks.

By "appropriate OID", I mean adding the OID for the extensions added to the config file to the snmpwalk example from my previous article on SNMP basics.

For example, to walk down all the extensions you could do something like:

$ snmpwalk -v 3 -u monitor -l authPriv -a SHA -x AES -A AUTHPASS -X PRIVPASS localhost NET-SNMP-EXTEND-MIB::nsExtendOutLine

30/07/2010 10:43 :: Umberto Bernardi

Thank you so much,

I've solved.




Are you human? (reduces spam) Note: Identity details will be stored in a cookie. Posts may not appear immediately.