#! /usr/bin/perl # # # use RRDs; use GDBM_File; $rrddir="/home/wleiden/stats/rrd"; $ddir="$rrddir/data"; $maxloadavg=50; $start=time(); if ($ARGV[0] eq 'DEBUG') {$DEBUG=1;} local $SIG{ALRM} = sub { die "Alarm; been waiting for too long: 600 sec. Exiting now.\n" }; alarm(600); chdir("/home/wleiden/svn/genesis/nodes/") || die; foreach $file (<*/wleiden.conf>) { $file=~s/\/wleiden\.conf//gi; #print "FILE: $file\n"; if (! grep(/^$file$/,@NODES)) { push(@NODES,$file);$nodes.="$file "; # if ($DEBUG) {print STDERR "Adding node $file\n";} } } #if ($DEBUG) {@ALIVE=('jasper');} #else { open (FPING,"/usr/local/sbin/fping $nodes 2> /dev/null |") || die; while () { chomp(); $node=''; if (/^(\S+) is alive/) { $node=$1; push(@ALIVE,$node); } else { if ($DEBUG) {print "$_\n";} } } close(FPING); } foreach $node (sort @ALIVE) { if ($DEBUG) {print "Doing: $node\n";} else {sleep(10);} $counter=0; do { # open (LOADAVG,"/proc/loadavg"); # $loadavg=;close(LOADAVG); $loadavg=`uptime`; #print "Load: $loadavg\n"; if ($loadavg=~/averages:\s+([0-9.]+)/) { $loadavg=100*$1; #print "LOAD: $loadavg\n"; if ($loadavg > $maxloadavg) {sleep (10);next;} else {$counter=999;} } else {print "Loadavg format err: $loadavg\n";} #print "Load: $loadavg\n"; } until ($counter > 3); if ($loadavg > $maxloadavg) {print "CPULOAD $loadavg > $maxloadavg. Skipped: $node\n";next;} #print "Already running?\n"; # if ($DEBUG) {do_it($node);} # else { $P="RRD_FETCH_${node}"; $ps=`ps -auwx|grep $P|grep -v grep`; if ($ps=~/^\S+\s+(\d+).*$P/) { $pid=$1; print "Already running: $node PID: $pid [$P]\n"; ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat("$ddir/${node}/fetch.pl.pid"); if ($mtime > 0) { $age=time()-$mtime; if ($age > 300) {kill(15,$pid);sleep(1);} } else { print "Already running: $node PID: $pid\n"; next; } } #print "Gonna Fork.\n"; if (($pid=fork()) eq 0) { #print "Child for node $node\n"; $0=$PROGRAM_NAME=$P; #print "Doing: $rrddir/fetch.pl $node\n"; exec("$rrddir/fetch.pl $node"); # do_it($node); print "DONE: $node\n"; exit; print "****** $node ******************* $$ ***************\n"; } } }