Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Server Monitor via Web

by BigJoe (Curate)
on May 23, 2000 at 03:02 UTC ( #14297=sourcecode: print w/replies, xml ) Need Help??
Category: Web admin tools
Author/Contact Info bigJoe
Description: This is a Quick script to keep track of the load on the server. There are 3 files that are needed to be made. The HTML file to access it the history text file and the script. The HTML diplay is very generic. click here to see it work
This html file uses this code
This is the Monitor Script<BR><BR>
<TR><TD WIDTH=50>Start a new log:</TD><TD>
</SELECT></TD><TD>number of rows on the page:</TD><TD>
<SELECT NAME="numofr">
<TD>Time Interval (approx sec(s)):</TD><TD>
<SELECT NAME="timers">

The PERL Script looks like:

require "";



if($in{numofr} >= 1)
if($in{timers} >= 1)

@months = ('Jan','Feb','Mar','Apr','May','June',

@days = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

($sec,$min,$hour,$mday,$mon,$year,$wday) =(localtime(time))[0,1,2,3,4,

$date = " $days[$wday], $months[$mon]/$mday/$year $hour:$min:$sec";
$date2 = " $hour:$min:$sec";

$loada=`more /proc/loadavg`;

open(OLDPAGE, $old);
$filesize = -s OLDPAGE;
read(OLDPAGE, $oldpage, $filesize);

open(OLDPAGE2, $startfile);
$filesize = -s OLDPAGE;
read(OLDPAGE2, $heading, $filesize);

    $heading="This script started $date";
    open(DUMPTIME, ">$startfile");
    print DUMPTIME $heading;


my @list = split /\s+/, $loada;

if ($list[0] >= 80)
}elsif ($list[0] >= 55){
$fontc ="<FONT COLOR=\"$color\">";
$oldpage = "<TR>
<TD>$fontc $list[3]</TD>
<TD>$fontc $list[6]</TD>
<TD>$fontc $list[7]</TD>
<TD>$fontc $date2</TD>

print "Content-type: text/html\n\n";
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"$timing;URL=$script\">";
print "<HTML><BODY><CENTER>$heading<BR><TABLE cols=4 cellpadding=10
print "<TR><TD>% CPU</TD><TD>run/all</TD><TD>last PID</TD><TD>Time</TD
print "$oldpage";
print "</TABLE></BODY></HTML>";

my @bigarray = split /\n+/, $oldpage;
@bigarray=@bigarray[0 .. ($numofrows*6)];
open(FILEOUT2, ">$old");

foreach (@bigarray)
{    print FILEOUT2 "$_ \n";}

And the text file needs to be history.txt and it needs to be read and writable.
Replies are listed 'Best First'.
RE: Server Monitor via Web
by KM (Priest) on May 23, 2000 at 04:35 UTC
    Whoa! Evil! was a 'standard' some time ago, but the best thing to use is to do form processing, and an easy way to create HTML (amung other things). If you want, can be used in 'cgi-lib' mode so you shouldn't have to change your method calls. Read the documentation for to learn more about that.

    Secondly you should really be using the strict pragma (use strict;) and -w. These will really help you learn how to be a better programmer, and you will get to learn about interesting things like scope!

    Some people will disagree with me on this, but I tend to like getting the date using Look at the documentation for POSIX, and the strftime() method. Also look at the man page for strftime(3) to see formats. If you agree or not on this, it is still good to look at the module, since it can do quite a bit. Personally, I don't see the use of the text file saying what time the script began.

    $loada=`more /proc/loadavg`;

    I don't think backticks should be used in CGIs. Although nothing is between them which is passed in from an untrusted source, it is better to use system() in a save manner, like:

    system("/bin/more", "/proc/loadavg");

    Look at perlsec for safe uses of system()

    open(OLDPAGE2, $startfile);
    $filesize = -s OLDPAGE;
    read(OLDPAGE2, $heading, $filesize);

    I'm not really sure why you are not just opening the file and reading it in. Why are you using read()?

      With a system("..", ".."); call does it return the output? I thaught it just returned a 1 or a 0 based on if it worked. Can you just put read(filename, $var, size) ? Without opening?
        Yes, my fsck-up. I forgot what you were doing for a minute when I saw backticks :) You may want to use a pipe, or look at perlsec to see how to safely emulate backticks with fork() and exec(). Now matter how you do it, good to look into all the other possibilities :)


        Re: A negative vote on this post
        Why would someone vote negative on a post where there is not only a correction, but viable options for someone to learn with and raise thier clue factor? C'mon people. Unless you are giving good advice, think before you do this.

RE: Server Monitor via Web
by KM (Priest) on May 23, 2000 at 04:43 UTC
    I either need more room, or have to write less :) I continue..

    You write a lot of things like:

    $fontc ="<FONT COLOR=\"$color\">";

    When you could use qq{} and not have to escape.

    $fontc = qq{<FONT COLOR="$color">};

    You may want to look into using here docs when printing multiple lines, and look at's methods for displaying HTML.

    Just some observations and hints to help you along the Path of Perldom.


        cool, thanks for the tips.

    Log In?

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: sourcecode [id://14297]
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (5)
    As of 2020-09-30 05:58 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (160 votes). Check out past polls.