Graphing Postfix with Cacti

[Updated 2015-03-09]

There are a bunch of articles and information on this topic out there, but most of them either don’t work, are outdated, or are scattered across multiple pages on the Cacti forums.

I’ve worked out the kinks for you, so here’s what you need to do to graph Postfix with Cacti.
This was performed on Debian 6 with Cacti 0.8.7g from the apt repository but should work on other distros too.
Note that this article tackles graphing the sent, received, rejected, bounced, spam, and virus statistics. It does not graph mail queues.

You need two files, the first is mailgraph.pl which you’ll need to put on your mail server. It’s a modified version of David Schweikert’s mailgraph which writes mail statistics into /var/tmp instead of generating rrds. The second is the xml template file which you’ll import into Cacti. You’ll find both files in the link below.

[Download required files]

  1. Copy mailgraph.pl to your mail server and place in a suitable location, eg /usr/local/bin
  2. Run mailgraph in daemon mode with “mailgraph.pl -d”. Mailgraph will write its statistic files to /var/tmp.
  3. Configure your snmpd.conf file on your mail server by adding the following:
    extend .1.3.6.1.4.1.2021.50 mailcount_sent /bin/cat /var/tmp/mailsent
    extend .1.3.6.1.4.1.2021.51 mailcount_recv /bin/cat /var/tmp/mailrecv
    extend .1.3.6.1.4.1.2021.52 mailcount_reject /bin/cat /var/tmp/mailrejected
    extend .1.3.6.1.4.1.2021.53 mailcount_bounced /bin/cat /var/tmp/mailbounced
    extend .1.3.6.1.4.1.2021.54 mailcount_spam /bin/cat /var/tmp/mailspam
    extend .1.3.6.1.4.1.2021.55 mailcount_virus /bin/cat /var/tmp/mailvirus
  4. Restart snmpd
  5. Import the xml template into Cacti
  6. Create graphs for the mail server

Notes:
RBL spam rejections will show up under the rejected mailcount.
If you’re still having problems getting Cacti to graph, try adding this to snmpd.conf “view systemonly included .1.3.6.1.4.1”.

If you can’t get this to work or have any questions, i’d be happy to help.

49 thoughts on “Graphing Postfix with Cacti”

  1. Hello, first of all I want to thank you for the script and the post, I try test it, but by putting the file as daemon, but gives an error at line 212, file Tail, which was not found, I think it could be an permissions error. Can you tell me what’s the name of the file, in which folder should be and what permits the daemon to run smoothly. thank you very much

    p/d: I’m sorry for my bad english

    1. If I can understand you, your english isn’t bad 🙂
      File::Tail is a perl module. Depending on which Linux distribution you run, it may or may not be difficult to install.
      For example on Debian, just run “aptitude install libfile-tail-perl”.
      And on CentOS you can find and download the packages here: http://pkgs.repoforge.org/perl-File-Tail/

  2. Hey…I get some errors while running this script in my server. could you let me know if this is still active so I can post the question? …thanks.

  3. When you can’t get data from postfix server, try check permission of snmpd.conf
    You must allow transfer from OID. From my snmpd.conf :

    view systemonly included .1.3.6.1.4.1

  4. Guariento Frédéric

    Hi,

    Thanks for your work.
    I have done all the steps.
    I can see files are created in /var/tmp/
    I have the script active: /usr/bin/perl -w /usr/local/bin/mailgraph.pl -d
    I have a graph in cacti…

    But the graph is still empty…

    What should I look for?
    Thanks.

    1. Cacti might not be able talk to the snmp agent running on the server. If they are two separate servers, check that snmp isn’t being firewalled off and that your community string is correct.
      Also try some snmpgets against the oids mentioned in the article to see if you get a response from the agent.

  5. Hello,
    script is active: /usr/bin/perl -w /usr/local/bin/mailgraph.pl -d, but there is nothing in my /var/tmp.
    Could you please help me?
    Thanks.

      1. Yes I have done everything exactly by your directions. Script is running, started with root permissions, but it did not generated any files in /var/tmp, so there is nothing to be read by snmp. I am running Postfix 2.9.6 on my raspberrypi 3.6.11+ debian.

        1. Mailgraph will read /var/log/syslog by default. Perhaps Postfix on that particular distro doesn’t place mail logs in syslog.
          Try making Mailgraph read a different mail log, eg “mailgraph.pl -d -l /var/log/mail.info”.

          1. Sorry for such a late answer. I have chosen different log and everything works great! Thank you.

  6. Hello, mailgraph.pl is outputting the data but Cacti will not graph it… here is the output from the Debug log.
    the last line indicates the error.. missing files, in fact each file needed to build graph is missing i.e. *22.rrd – *27.rrd are missing from /var/lib/cacti/rra/

    Any help would be awesome!

    Thanks Jeff

    RRDTool Command:
    /usr/bin/rrdtool graph – \
    –imgformat=PNG \
    –start=-86400 \
    –end=-300 \
    –title=’Localhost – Postfix’ \
    –rigid \
    –base=1000 \
    –height=120 \
    –width=500 \
    –alt-autoscale-max \
    –lower-limit=0 \
    –vertical-label=’messages’ \
    –slope-mode \
    –font TITLE:10: \
    –font AXIS:7: \
    –font LEGEND:8: \
    –font UNIT:7: \
    DEF:a=”/var/lib/cacti/rra/localhost_send_25.rrd”:send:AVERAGE \
    DEF:b=”/var/lib/cacti/rra/localhost_recv_23.rrd”:recv:AVERAGE \
    DEF:c=”/var/lib/cacti/rra/localhost_reject_24.rrd”:reject:AVERAGE \
    DEF:d=”/var/lib/cacti/rra/localhost_bounced_22.rrd”:bounced:AVERAGE \
    DEF:e=”/var/lib/cacti/rra/localhost_spam_26.rrd”:spam:AVERAGE \
    DEF:f=”/var/lib/cacti/rra/localhost_virus_27.rrd”:virus:AVERAGE \
    AREA:a#00CF00FF:”Send” \
    GPRINT:a:AVERAGE:” Average\:%8.0lf msgs/5min” \
    GPRINT:a:MAX:”Maximum\:%8.0lf msgs/5min\n” \
    AREA:b#4668E4FF:”Recieved”:STACK \
    GPRINT:b:AVERAGE:”Average\:%8.0lf msgs/5min” \
    GPRINT:b:MAX:”Maximum\:%8.0lf msgs/5min\n” \
    AREA:c#AAABA1FF:”Reject”:STACK \
    GPRINT:c:AVERAGE:” Average\:%8.0lf msgs/5min” \
    GPRINT:c:MAX:”Maximum\:%8.0lf msgs/5min\n” \
    LINE2:d#FF0000FF:”Bounce” \
    GPRINT:d:AVERAGE:” Average\:%8.0lf msgs/5min” \
    GPRINT:d:MAX:”Maximum\:%8.0lf msgs/5min\n” \
    LINE2:e#F5F800FF:”Spam” \
    GPRINT:e:AVERAGE:” Average\:%8.0lf msgs/5min” \
    GPRINT:e:MAX:”Maximum\:%8.0lf msgs/5min\n” \
    LINE2:f#F24AC8FF:”Virus” \
    GPRINT:f:AVERAGE:” Average\:%8.0lf msgs/5min” \
    GPRINT:f:MAX:”Maximum\:%8.0lf msgs/5min”
    RRDTool Says:
    ERROR: opening ‘/var/lib/cacti/rra/localhost_send_25.rrd’: No such file or directory

  7. Pingback: Graphing Postfix via Cacti « SJA.ID.AU

  8. Hi,

    Thanks for putting this simplified guide up. Helped me immensely (I also wrote an article on my site based on this one, hope you don’t mind).

    Unfortunately it doesn’t seem to map sent mail. I sent a test email to ensure it counted it properly, but alas, no green line on the graph. Here is an example of my mail.log sent entry;

    Mar 5 15:22:09 www postfix/smtp[939]: 1B665A5A04A: to=, relay=as-av.iinet.net.au[203.0.178.180]:25, delay=1.6, delays=0.16/0.06/0.95/0.43, dsn=2.0.0, status=sent (250 ok: Message 200318133 accepted)

    I’m not the best reader of perl, but it seems that it *should* put this up as it contains the string sent and does not include the string 127.0.0.1. Any ideas?

    1. You’re most welcome Shane, and I don’t mind at all.

      Graphing sent mail works for me, and it’s strange that it doesn’t for you too given that your postfix entries look just about the same as mine. I’m thinking the problem could perhaps not lie with the graphing components but rather with the inability of Cacti to read the counters in /var/tmp. Could you please check the files in there. Perms 644 should do for all the counter files, and you should also see the “mailsent” file in there too.

  9. Martin Chamambo

    I have managed to set up as shown and /var/tmp is being populated which is ok. but cacti is not able to graph anything .
    cacti server is on another server and im able to do an SNMP walk from the cacti server to the postfix server .can you help ????

    1. I’d check the permissions on the files in /var/tmp. Perhaps snmpd on your postfix server is unable to read them.

  10. Hallo, can you help me. I did everything exactly by your directions. On debian wheezy. Postfix and mailgraph.pl and cacti is on the same machine. Everything seems to work, but in files mailsent, mailrecv, there is always only 2 messages and in mailrejected, mailbounced,mailspam,mailvirus 0.
    But in syslog file there is much more messages. In the mailgraph.log I can see these lines.
    Use of uninitialized value $this_minute in numeric eq (==) at ./mailgraph.pl line 528
    Use of uninitialized value $this_minute in numeric lt (<) at ./mailgraph.pl line 529
    When I tried to debug lines 528 and 529 I got.

    Use of uninitialized value $this_minute in numeric eq (==) at ./mailgraph.pl line 528.
    at ./mailgraph.pl line 528
    Parse::Syslog::update(1401786571) called at ./mailgraph.pl line 519
    Parse::Syslog::event(1401786571, 'received') called at ./mailgraph.pl line 420
    Parse::Syslog::process_line('ARRAY(0x2451650)') called at ./mailgraph.pl line 305
    Parse::Syslog::main() called at ./mailgraph.pl line 559
    Parse::Syslog::update(./mailgraph.pl:529):
    529: return 0 if $m < $this_minute;

    Use of uninitialized value $this_minute in numeric lt ( /var/tmp/mailsent`;

    Do you have any suggestion how to fix it ?
    Sorry about my english.

    1. First of all i’d check if there are any permissions problems preventing mailgraph.pl from accessing the mail log.
      And then next make sure that mailgraph.pl can write to the files in /var/tmp/.
      Give that a try.

      1. Thanks for your quick response.
        I thing, that it is not permission problem. Because files in var/tmp was successfully created, and in them appears numbers like 0 or 2 as I have writen above.
        What about errors I mentioned ?

        1. Lets not worry about the errors at this point and make sure that the basics are in place first.
          Can you check that mailgraph.pl can access your mail log in /var/log/. My concern is that you may have given it access at some point, then the log rotates thereby resetting the permissions and preventing mailgraph.pl from reading it anymore.

          1. I gave 644 to all files in /var/log , but nothing has changed. Still the same content of files.
            mailsent 2, mailrecv 2, mailrejected 0, mailbounced 0 mailspam 0,mailvirus 0

          2. Try making it read a different mail log file. Such as mail.info.
            For example: “mailgraph.pl -d -l /var/log/mail.info”

  11. Hi,
    i get the same errors,

    [root@centos bin]# perl mailgraph.pl -l /var/log/maillog
    Use of uninitialized value $this_minute in numeric eq (==) at mailgraph.pl line 528.
    Use of uninitialized value $this_minute in numeric lt (<) at mailgraph.pl line 529.

    and in /tmp i have the same results
    mailsent 2, mailrecv 2, mailrejected 0, mailbounced 0 mailspam 0,mailvirus 0

    i hope you have any idee or i give up 🙂 i use centos
    Thanks for help.
    Klaus

  12. I am getting the below error when walking, i tried changing permission of mailsent, btw I am using centos.

    UCD-SNMP-MIB::ucdavis.50.4.1.2.14.109.97.105.108.99.111.117.110.116.95.115.101.110.116.1 = STRING: “/bin/cat: /var/tmp/mailsent: Permission denied”

    1. It could be the SNMP daemon not being able to read mailsent. I remember having a similar problem. I had to run snmpd as a different user and that solved the problem.
      As a test, try running snmpd as root and see if the problem goes away. Then if it does, find a more suitable restricted user to run it under.

  13. hello,

    i installed successfully your script to more than 20 hosts..
    but, in one of them, your script does not create the files in /var/tmp

    so, the answer of snmpwalk is iso.3.6.1.4.1.2021.50.3.1.1.14.109.97.105.108.99.111.117.110.116.95.115.101.110.116 = STRING: “/bin/cat: /var/tmp/mailsent: No such file or directory”

    permissions are:
    root@host:/var/tmp# ls -al /var/tmp/
    total 8
    drwxrwxrwx 2 root root 4096 Aug 4 18:04 .
    drwxr-xr-x 14 root root 4096 Aug 4 17:48 ..

    please advice..

      1. yes, of course…
        is there any way to debug the script ?
        i have more than 25 servers running your script flawlessly..

        1. We need to figure out what’s different on that one server than from the 25 other servers. Troubleshooting something to that degree with the zero visibility I have to your system is like poking a stick in the dark.
          A few things that come to mind though:
          The script needs to be run as a user that is able to write to /var/tmp/.
          The script needs to have perms to access the mail log files.
          I realise these are all basic troubleshooting steps to the seasoned sysadmin, but beyond logging into the system itself, there’s only so much assistance I can provide from here. But if you can work out what’s different on that one system, you’re 95% of the way to fixing the problem.

  14. Hello,

    The graph works but when i receive a mail the cacti graph marks a line in blue en the line continues untill a next event. so in my graph i have continues lines where as it normaly it should register one peak and then drop to zero again. is this as designed?

  15. Hi Brandon,

    This tool is working perfect for me, but one thing I need to know.
    How frequency this tool overriding email stats. Suppose in a starting 2 hrs I seen some count and after couple of hours count was 0.
    Is there any specific time interval for this deamon to reset stats.

    1. It constantly monitors the mail log files so as soon as something happens in the logs, the stats should update straight away.

  16. Hi Brandon,

    great work! Thanks a lot! I’d like to change two thins:

    1) get the average values in messages/minute instead of messages/5 minutes
    2) get the total values (only as numbers below the graphs) for the mentioned time frame

    Would this be easy to achieve?

    Regards,
    Stephan

  17. Hi Brandon,
    Pretty useful tool, thank you! However, I noticed that when send messages go over 1000, the value is not shown in the graph (it’s zeroed). Any idea?
    Thanks

  18. This is a very useful und flawlessly working EASY tutorial, thank you, I appreciate!
    Just one note: In the graph template is a HR missing after the last value :,-)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top