Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Server Monitor via Web

by BigJoe (Curate)
on May 23, 2000 at 03:02 UTC ( [id://14297]=sourcecode: print w/replies, xml ) Need Help??
Category: Web admin tools
Author/Contact Info bigJoe Big_Joe1008@linuxstart.com
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 monitor.pl script. The HTML diplay is very generic. click here to see it work
This html file uses this code
<BODY>
This is the Monitor Script<BR><BR>
<FORM NAME="myForm" ACTION=monitor.pl METHOD="POST">
<TABLE CELLPADDING=2 CELLSPACING=0>
<TR><TD WIDTH=50>Start a new log:</TD><TD>
<SELECT NAME="new">
    <OPTION>TRUE
    <OPTION>FALSE
</SELECT></TD><TD>number of rows on the page:</TD><TD>
<SELECT NAME="numofr">
    <OPTION>4
    <OPTION>5
    <OPTION>6
    <OPTION>7
    <OPTION>8
    <OPTION>9
    <OPTION>10
    <OPTION>20
    <OPTION>50
    <OPTION>100
</SELECT></TD>
<TD>Time Interval (approx sec(s)):</TD><TD>
<SELECT NAME="timers">
    <OPTION>1
    <OPTION>2
    <OPTION>4
    <OPTION>5
    <OPTION>7
    <OPTION>9
    <OPTION>10
    <OPTION>20
    <OPTION>50
    <OPTION>100
</SELECT></TD>

</TR>
</TR>
<TR><TD COLSPAN=2><INPUT TYPE=SUBMIT VALUE="GO"></TD></TR>
</TABLE>
</FORM>   
</BODY></HTML>
The PERL Script looks like:
#!/usr/bin/perl

require "cgi-lib.pl";

ReadParse(*in);

$goodcolor="GREEN";
$medcolor="ORANGE";
$badcolor="RED";
$old="history.txt";
$numofrows=8;
$startfile="time.txt";
$timing=5;

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


@months = ('Jan','Feb','Mar','Apr','May','June',
         'July','Aug','Sept','Oct','Nov','Dec');

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

($sec,$min,$hour,$mday,$mon,$year,$wday) =(localtime(time))[0,1,2,3,4,
+5,6];
$year=$year+1900;

$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);
close(OLDPAGE);

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

if($in{new})
{
    $heading="This script started $date";
    open(DUMPTIME, ">$startfile");
    print DUMPTIME $heading;
    $oldpage="";
    close(DUMPTIME);
    $numofrows--;
}

$script="monitor.pl?numofr=$numofrows&timers=$timing";

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

if ($list[0] >= 80)
{
    $color=$badcolor;
}elsif ($list[0] >= 55){
    $color=$medcolor;
}else{
    $color=$goodcolor;
}
$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>
</TR></FONT>\n$oldpage";

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
border=1>";
print "<TR><TD>% CPU</TD><TD>run/all</TD><TD>last PID</TD><TD>Time</TD
+></TR>";
print "$oldpage";
print "</TABLE></BODY></HTML>";


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

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

close(FILEOUT2);
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! cgi-lib.pl was a 'standard' some time ago, but the best thing to use is CGI.pm to do form processing, and an easy way to create HTML (amung other things). If you want, CGI.pm can be used in 'cgi-lib' mode so you shouldn't have to change your method calls. Read the documentation for CGI.pm 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 POSIX.pm. 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);
    close(OLDPAGE2);
    

    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 :)

        Cheers,
        KM

        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 CGI.pm's methods for displaying HTML.

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

    Cheers,
    KM

        cool, thanks for the tips.

    Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others learning in the Monastery: (6)
    As of 2024-03-19 08:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found