#!/usr/bin/perl5 # usage: mrtg-getinfo-intr # # # interrupt number to monitor (first field in /proc/interrupts) # # This script polls information from some /proc files and outputs the polled # information in a format usable by mrtg. It can also function as a required # file, in which case it will gather the data from /proc but not output # anything to stdout. # # This script is derived from mrtg-getinfo-mem open(INTRINFO, "/proc/interrupts") || die("/proc/interrupts: $!\nstopped"); while () { if (/^\s*(\d+):\s*(\d+) (.) (.*)\s*\n/) { # This should always be the case $interrupts[$1] = $2; $flag[$1] = $3; # We may have two interrupts with the same name (e.g., 3c509) # Set up a "pointer" so we can iteratively access both if (defined $p{$4}) { $q{$4} += 1; } else { $p{$4} = $q{$4} = 1; } # We will also need to set up a pseudo 2-d array $number{"$4,$q{$4}"} = $1; } } close(INTRINFO); open(UPTIME, "/proc/uptime") || die("/proc/uptime: $!\nstopped"); =~ /^(\d+)/; $uptime = $1; close(UPTIME); open(HOSTNAME, "/proc/sys/kernel/hostname") || die("/proc/sys/kernel/hostname: $!\nstopped"); $hostname = ; chomp $hostname; close(HOSTNAME); # See if we are being require'd from another perl5 program. # If we are, we expect caller(0) to return non-null information; # in particular, $subroutine should be "(eval)" (by empirical observation). ($package, $filename, $line, $subroutine, $hasargs, $wantargs) = caller(0); if (! defined $subroutine) { for ($i = 0; $i < 2; $i += 1) { if ($i <= $#ARGV) { my $j = $ARGV[$i]; if ($j =~ /^\d+$/) { printf("%d\n", $j >= 0? $interrupts[$j]: 0); } else { if (defined $number{"$j,$p{$j}"}) { printf("%d\n", $interrupts[$number{"$j,$p{$j}"}]); $p{$j} += 1; } else { printf("0\n"); } } } else { printf("0\n"); } } if ($uptime >= 86400) { my($days) = int($uptime/86400); printf("%d day%s, ", $days, $days == 1? "": "s"); } printf("%02d:%02d:%02d\n", ($uptime/3600)%24, ($uptime/60)%60, $uptime%60); printf("%s\n", $hostname); } 1;