Monitoring sendmail with snmp on FreeBSD

I wanted to monitor the amount of emails in sendmail's outbound queue using snmp without any sed/awk/shell/Perl scripts in between. On FreeBSD that's fairly easy:

  1. Make sure you have a valid /etc/mail/sendmail.cf
  2. Install net-mgmt/net-snmp from the ports (for security reasons, you might disable any option and even want to disable both agentx options)
  3. Copy /usr/local/share/snmp/snmpd.conf.example to /usr/local/share/snmp/snmpd.conf
  4. Edit snmpd.conf and set a community, sysLocation, sysContact, etc. as usual
  5. Also, allow the community to read the whole MIB tree by setting
    view   systemonly  included   .1
    
    Remove any other view systemonly ... line
  6. Enable and start snmpd:
    # sysrc snmpd_enable=YES
    # service snmpd start
    
  7. Now query MTA-MIB::mtaGroupStoredMessages.1.10:
    $ snmpget -c public -v 2c localhost .1.3.6.1.2.1.28.2.1.4.1.10
    MTA-MIB::mtaGroupStoredMessages.1.10 = Gauge32: 0
    

Proof

Does it work?

# mailq
/var/spool/mqueue is empty
                Total requests: 0
# snmpget -c public -v 2c localhost .1.3.6.1.2.1.28.2.1.4.1.10
MTA-MIB::mtaGroupStoredMessages.1.10 = Gauge32: 0
# ipfw add 2000 deny tcp from any to any 25
02000 deny tcp from any to any dst-port 25
# sendmail root
subject: test 1

foobar 1
.
# mailq
                /var/spool/mqueue (1 request)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
v2BDihhV003399        9 Sat Mar 11 14:44 <root@where.ever>
                 (Deferred: Permission denied)
                                         user@some.where
                Total requests: 1
# snmpget -c public -v 2c localhost .1.3.6.1.2.1.28.2.1.4.1.10
MTA-MIB::mtaGroupStoredMessages.1.10 = Gauge32: 1
# ipfw delete 02000
# sendmail -qf
# mailq
/var/spool/mqueue is empty
                Total requests: 0
# snmpget -c public -v 2c localhost .1.3.6.1.2.1.28.2.1.4.1.10
MTA-MIB::mtaGroupStoredMessages.1.10 = Gauge32: 0