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.
This is a great solution for monitoring OS X servers. There is another script floating around with templates of os x as well, but the cpumon.pl for instance, is by far more acurate.
Thank you.
Hello
everything is working fine for me with cacti but these scripts. I’m on a Macbook Pro 10.4.10 and it is not working fine with this ‘exec’ thing.
I’m getting this:
leptifilou:~/Sites/cacti/rra LePtiFilou$ snmpd -H
No log handling enabled - turning on stderr logging
/etc/snmpd.conf: line 204: Warning: Unknown token: exec.
/etc/snmpd.conf: line 205: Warning: Unknown token: exec.
/etc/snmpd.conf: line 206: Warning: Unknown token: exec.
/etc/snmpd.conf: line 207: Warning: Unknown token: exec.
Here’s the part of my /etc/snmpd.conf:
exec 1.3.6.1.4.1.2021.8.1.101 cpu /bin/cat /opt/net-snmp/data/cpu
exec 1.3.6.1.4.1.2021.8.2.101 temp /bin/cat /opt/net-snmp/data/temp
exec 1.3.6.1.4.1.2021.8.3.101 disk /bin/cat /opt/net-snmp/data/disk
exec 1.3.6.1.4.1.2021.8.4.101 mem /bin/cat /opt/net-snmp/data/mem
With this:
leptifilou:~/Sites/cacti/rra LePtiFilou$ snmpd -v
NET-SNMP version: 5.2.1
I’m stuck and it is very annoying
Common disk usage, processes, logged users… are working fine, but everything with this ‘exec’ thing is not. On top of that, data are gathered into /opt/net-snmp/data but it looks like my snmpd agent is not willing to pick them up and put into an OID with ‘exec’ as it is unrecognized.
I’d appreciate your help
Your advices were useful to install everything, I hope the saga will continue.
Issue solved while reinstalling net-snmp 5.4