Kickstart has asked for the wisdom of the Perl Monks concerning the following question:
I've got a script (below) that opens an HTTP port (44000) for reporting some stats about the machine so a monitoring machine will call me if there is a problem.
I've got two problems with the script: 1. It always returns a complaint that the partition being checked is full (or BAD, in simple terms the monitor can understand). I don't see why this would be the case. 2. It will respond fine for a few times, then requests will time out. A portscan of the machine sees that port 44000 is still open, but it won't return a report. The HTTP daemon script continues to run. It's not using over-much memory or CPU in this state (normal amounts).
(note: yeah, it's a junk script...I always write like this to start then go back and clean up)
Here is the simple conf file:
Here is the script itself:# Location of reportsock text file $rp_loc = '/tmp/reportsock'; # Command to get information from 'top' $top_cmd = 'top -b -n 1 | head -7'; # Command to get information for disk usage $disk_cmd = 'df -h'; # Max partition usage allowed $max_hd = 95; # Max load allowed $max_load = 1; # Least CPU idle allowed $least_cpuidle = 80;
Here is the script that creates a report:#!/usr/bin/perl use HTTP::Daemon; use HTTP::Status; my $d = new HTTP::Daemon LocalAddr => '10.0.1.1', LocalPort => 44000; print "Starting: <URL:", $d->url, ">\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { print "Got connection\n"; if ($r->method eq 'GET') { $c->send_file_response("/tmp/reportsock"); } else { $c->send_error(RC_FORBIDDEN) } } $c->close; undef($c); }
#!/usr/bin/perl # Read text as block $/ = ''; # Include configuration file do '/home/reportsock/reportsock.conf'; ########## Section one - load, memory and swap ########## # Get results of the top command (from conf file) $_ = `$top_cmd`; # Get load average m/(\d\.\d\d).*?(\d\.\d\d).*?(\d\.\d\d)/; my $loadavg1 = $1; my $loadavg2 = $2; my $loadavg3 = $3; # Get CPU idle m/(\d+\.\d)% idle/; my $cpuidle = $1; # Get memory stats m/Mem:\s+(\d+)K.*?(\d+)K.*?(\d+)K/; my $memtotal = $1; my $memused = $2; my $memfree = $3; # Get Swap stats m/Swap:\s+(\d+)K.*?(\d+)K.*?(\d+)K/; my $swaptotal = $1; my $swapused = $2; my $swapfree = $3; ######### Section two - disk usage ######### @lines = `$disk_cmd`; ########## Section three - report ########## open(REPORT, ">$rp_loc") or die ("Could not open $rp_loc for writing") +; print REPORT "Load\t$loadavg1, $loadavg2, $loadavg3\n"; print REPORT "CPU\t$cpuidle%\n"; print REPORT "Memory\t$memfree + $memused = $memtotal\n"; print REPORT "Swap\t$swapfree + $swapused = $swaptotal\n"; foreach $foo (@lines) { $count++; $foo =~ m/\/dev\/(hd..).*?$/; print REPORT "$1\t"; $partname = $1; $foo =~ m/\/dev\/.*?(\d+)%/; print REPORT "$1%\n"; $part_usage = $1; if ($part_usage > $max_hd) { print REPORT "$partname OK " } else { print REPORT "$partname BAD $1/$max_hd "; } } if ($loadavg1 < $max_load) { print REPORT "Load OK "; } else { print REPORT "Load BAD $loadavg1/$max_load "; } if ($cpuidle > $least_cpuidle) { print REPORT "CPU OK "; } else { print REPORT "CPU BAD $cpuidle "; } if ($swapused < $swapfree) { print REPORT "Swap OK "; } else { print REPORT "Swap BAD $swapfree/$swaptotal "; } close(REPORT);
|
---|
Replies are listed 'Best First'. | |
---|---|
Addendum
by Kickstart (Pilgrim) on Mar 25, 2003 at 19:37 UTC | |
by l2kashe (Deacon) on Mar 25, 2003 at 21:35 UTC |