Other Data-Gathering Applications

What if you need to monitor devices on your network that don’t support SNMP? MRTG is up to the task. For example, you may have a Perl script that gathers usage statistics from some device that doesn’t support SNMP. How can you collect and graph this data? Let’s make this more concrete. Assume that you have the following script, /usr/local/scripts/hostinfo.pl, which reports the number of users and the number of processes on the system:

#!/usr/bin/perl

$who = "/usr/bin/who | wc -l";
$ps = "/bin/ps -ef | wc -l";

chomp($numUsers = int(`$who`));
# We subtract two because ps generates a header and the ps process
# is counted as running.
chomp($numProcesses = int(`$ps`) - 2); 
print "$numUsers\n";
print "$numProcesses\n";

#
# The following code prints the system uptime and the hostname. These two 
# items need to be included in every script that you write and should be the
# very last thing that is printed.
#
chomp($uptime = `/usr/bin/uptime`);
print "$uptime\n";

chomp($hostname = `/bin/hostname`);
print "$hostname\n";

This script prints four variables: the number of users and the number of processes (the data we want MRTG to collect) and the system uptime and hostname (required by MRTG). To get MRTG to run this script, we’ll have to edit mrtg.cfg by hand. The modification is actually simpler than our previous example. Here’s the new entry to mrtg.cfg, with the changes shown in bold:

Target[linuxserver.users]: `/usr/bin/perl /usr/local/bin/hostinfo.pl` ...

Get Essential SNMP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.