Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Reporting script

by Kickstart (Pilgrim)
on Mar 25, 2003 at 19:35 UTC ( [id://245764]=perlquestion: print w/replies, xml ) Need Help??

Kickstart has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, thanks for your help.

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:

# 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 itself:
#!/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); }
Here is the script that creates a report:
#!/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
    Looks like it does crash...initial testing indicates that it takes a long time to close the port, and it appears to crash after two requests have been made.

    Thanks,

    KS

      I have not tested the code, but if your port is taking too long to close, you might look at your OS. On Solaris notably prior to tweaking the network stack connections can take a long time to clear, even if closed cleanly. I would start there. If after tweaking your stack, you are still having issues, you might try something along the lines of spawning say 5 kids, then as requests come in pass the work to a child, and if you have less than 5 kids, spawn enough to get back up to 5. Then in the children have them keep track of how many times they have received requests, and die after fulfilling X requests.

      Just some random thoughts..

      /* And the Creator, against his better judgement, wrote man.c */

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://245764]
Approved by sauoq
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-23 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found