#!/usr/bin/perl5 # usage: mrtg-getinfo-mem [ -m ] # # -m mem info (mem used, - buffers) # default is to output overview of vm used (mem used, swap used) # # 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. open(MEMINFO, "/proc/meminfo") || die("/proc/meminfo: $!\nstopped"); while () { if (/^Mem: +(\d+) +(\d+) +(\d+) +(\d+) +(\d+) +(\d+)/) { ($mem_total, $mem_used, $mem_free, $mem_shared, $mem_buffers, $mem_cached) = ($1, $2, $3, $4, $5, $6); } elsif (/^Swap: +(\d+) +(\d+) +(\d+)/) { ($swap_total, $swap_used, $swap_free) = ($1, $2, $3); } } close(MEMINFO); 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) { require 'getopts.pl'; Getopts("ms"); if ($opt_m) { printf("%d\n%d\n", $mem_used, $mem_used - $mem_buffers - $mem_cached) } else { printf("%d\n%d\n", $mem_used, $swap_used) } 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;