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:#!/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";
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).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>
Copyright © 2002 O'Reilly & Associates. All rights reserved.