1#!/usr/bin/perl 2# This small utility is used to collect the available memory and 3# used memory. For free memory, it sums the free, cached and buffers. 4 5# You must enable the "gauge" feature to have a proper graph 6# as those numbers do not grow 7# 8# Colin Tinker 9# g1gsw@titanic.demon.co.uk 10 11# setup local vars 12 13my($machine); 14 15# Enter default macine name here 16 17$machine = "g1gsw.ampr.org"; 18 19# This allows command args to override defaults listed above 20 21if (scalar(@ARGV) > 1 ) 22 { 23 print("USAGE: memory.pl {machine}\n"); 24 exit(-1); 25 } 26 27 if ($ARGV[0] ne '' && $ARGV[0] ne '#') 28 { 29 30 $machine = $ARGV[0]; 31 } 32 33# Calculate free memory 34 35 $getfree = `cat /proc/meminfo | grep Mem:`; 36 $getfree =~ /^Mem:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/; 37 38# Each (\D+) = $1 or $2 or $3 etc 39 40 $outfree = $3; 41 $outbuffers = $5; 42 $outcached = $6; 43 44# Total free memory 45 46 $gettotal = $outfree+$outcached+$outbuffers; 47 print $gettotal."\n"; 48 49# Get used memory free 50 51 $getused = `cat /proc/meminfo | grep Swap:`; 52 $outused = $1-$gettotal; 53 print $outused."\n"; 54 55# Get uptime 56 57 $getuptime = `/usr/bin/uptime`; 58 59# Parse though getuptime and get data 60 $getuptime =~ /^\s+(\d{1,2}:\d{2}..)\s+up\s+(\d+)\s+(\w+),/; 61 62#Print getuptime data for mrtg 63 64 print $2." ".$3."\n"; 65 66# Print machine name for mrtg 67 68 print $machine."\n"; 69