SNMP

In the first part, I covered building and installing Cacti from scratch on an OS X machine. Now I will guide you through setting up SNMP on your other OS X machines. I put together a package of some scripts I made to extract CPU usage, disk activity, and temperature (for OS X Server), which will be executed and read when Cacti polls the SNMP server.

Ruby

Ruby is awesome (It comes with OS X, by the way). The scripts I use to parse data from commands are written in Ruby. If you don’t know it, and if you’re looking for an all-purpose scripting and text processing language, I would highly suggest learning it. Other than the fact that it is a powerful and innovative language, it has clean syntax, that in my opinion, is easier to follow than Perl.

Scripts for OS X

We will now set up scripts to parse output from iostat and vm_stat. These commands will be executed to gather statistics on disk activity and CPU usage. If you’re running OS X Server, it will also parse output from servermgrd to get cpu temperatures.

I’ve bundled the necessary scripts and launchd jobs together into a single archive. Download here.

sudo tar -C / -xzvf snmp_scripts.tar.gz

The necessary files will be extracted to /usr/share/snmp, /opt/net-snmp, and /Library/LaunchDaemons. A configuration file is extracted to /usr/share/snmp/snmpd.conf (more about this later). The two launchd jobs will be responsible for keeping snmp running and for averaging statistics over a five minute interval every five minutes. The contents in /opt/net-snmp will contain scripts to collect data from commands and write the output to /opt/net-snmp/data.

Adding a User

Before we can continue, we need to add a user to query SNMP. You’ll need to remember this user and password when configuring Cacti. In this example, we’ll add a user with the name ’snmpstats’ and the password ’snmp10pass’.

sudo net-snmp-config --create-snmpv3-user -ro -a 'snmp10pass' snmpstats

SNMP Configuration

If you look at the file we extracted earlier, /usr/share/snmp/snmpd.conf, you’ll see several lines beginning with ‘exec’. This tells SNMP that when we’re querying a certain part of the MIB tree, to execute a command and use its output as a response to the query.

For executing commands in SNMP, we use the .1.3.6.1.4.1.2021 section of the MIB tree. The command will execute and stdout line #N will be accessible by accessing .101.N For example, if .1.3.6.1.4.1.2021.8.101 executes a command, the sixth line of output can be retrieved from .1.3.6.1.4.1.2021.8.101.101.6. If this seems confusing, read the snmpd.conf man page or look at the stock configuration file in /etc/snmpd.conf.

Firing it Up

We are now ready to start the launchd jobs, which will start the daemon and the data collection.

sudo launchctl load /Library/LaunchDaemons/org.machx.snmp-data.plist /Library/LaunchDaemons/org.machx.snmp.plist

Wait ten minutes and you should start seeing output in /opt/net-snmp/data. To test, run

snmpget -v3 -u snmpstats -l authNoPriv -a MD5 -A 'snmp10pass' localhost .1.3.6.1.4.1.2021.8.1.101.101.1

This should yield the CPU user usage.

Back to the Cacti Side

Log into Cacti and import templates from this XML file: cacti_host_template_mac_os_x.xml.

Add the SNMP host you just configured under the new ‘Mac OS X’ template and pick the graphs you want.