home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeEssential SNMPSearch this book

13.4. 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`
MaxBytes[linuxserver.users]: 512
Options[linuxserver.users]: gauge
Title[linuxserver.users]: linuxserver (linuxserver): Number of 
users and processes
YLegend[linuxserver.users]: Users/Processes
LegendI[linuxserver.users]:  Users:
LegendO[linuxserver.users]:  Processes:
PageTop[linuxserver.users]: <H1>Number of users and processes
 </H1>
 <TABLE>
   <TR><TD>System:</TD><TD>linuxserver<TD></TR>
   <TR><TD>Maintainer:</TD><TD></TD></TR>
   <TR><TD>IP:</TD><TD>linuxserver(  )</TD></TR>
  </TABLE>
Note the addition of `/usr/bin/perl /usr/local/bin/hostinfo.pl` to the Target command. This line tells MRTG to run the script or program between the backticks. The rest should be familiar. MRTG interprets the first value that the script prints (the number of users) as its input data; the second value (the number of processes) is the output data. When it generates graphs, it applies the appropriate input and output legends (LegendI and LegendO).



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.