#! /usr/bin/perl -w ############################################################################# # # makeplot.pl # # Creates a plotfile of the load of the machine. The plot ranges 12 hours # back in time and is sampled at 2 minutes interval. # # Changes: # 29.11.1998, chakie - first version. # # $Id: makeplot.pl,v 1.1 1998-11-30 18:02:48+02 chakie Exp $ ############################################################################# # # declare the stuff we'll use use CGI; # # set some global variables $OutDir = '/home/chakie/www/load'; $OutWWW = '~chakie/load'; $BaseName = `date '+%Y.%m.%d-%H.%M'`; $OutName = $OutDir . '/plot-' . $BaseName; $OutNameWWW = '/plot-' . $BaseName; $DataDir = '/home/chakie/var/loadavg'; # define the name of the datafile $Options = $OutDir . "/plotoptions"; # define the intervals $Hours = 24; # get the current time $CurrentYear = `date '+%Y'`; $CurrentMonth = `date '+%m'`; $CurrentDay = `date '+%d'`; $CurrentHour = `date '+%H'`; $CurrentMinute = `date '+%M'`; # gut away the trailing \n chomp $CurrentYear; chomp $CurrentMonth; chomp $CurrentDay; chomp $CurrentHour; chomp $CurrentMinute; chomp $OutName; chomp $OutNameWWW; ############################################################################# # ############################################################################# sub readFile { my $File = $_[0]; my $Data; # does the file exist? if ( ! -e $File ) { # no such file, ignore } else { # file exists, so open it open FILE, $File || die "Can't open " . $File . ", aborting.\n"; # read from the file while ( ) { # parse the data /(\d\d) (\d\.\d\d) (\d*)/; # now scale the time from [0..60] to [0..100] push @Data, $3; push @Data, $2; } # we're done, close the file close FILE; } # return the data return @Data; } ############################################################################# # Writes the data in one hour to the output-file ############################################################################# sub writeHour { my $File = shift; my $Hour = shift; my @Data = @_; $Total = ($#Data + 1) / 2; # loop over all values in the file for ( $Index = 0; $Index < $Total; $Index++ ) { # create the hour $Time = sprintf "%02d.%02d", $Hour, ($Index / 30) * 100; $Users = shift @Data; $Load = shift @Data; # write the entry print $File $Time . " " . $Load . " " . $Users . "\n"; } } ############################################################################# # ############################################################################# sub createFileName { # create the fileame from passed strings my $File = sprintf "%d.%02d.%02d-%02d", $_[0], $_[1], $_[2], $_[3]; # return it return $File; } ############################################################################# # Main program ############################################################################# # create a new CGI object $Query = new CGI; # create the header print $Query->header; print $Query->start_html ( -title => 'infa.abo.fi load', -bgcolor => 'white' ); # # print s nice header print "
"; print $Query->h1 ( 'infa.abo.fi load' ); print "
"; # # print the actual information print "
Local time is: " . `date` . "

"; # open the destination-file open TARGET, '>' . $OutName || die "Can't open output file '" . $OutName . "', aborting.\n"; # # ok, now loop for the proper number of hours for ( $Hour = 0; $Hour < $Hours - 1; $Hour++ ) { # create the file for this hour $File = $DataDir . '/' . createFileName ($CurrentYear, $CurrentMonth, $CurrentDay, $Hour); # clear the array first and then get data from the file undef @Data; @Data = readFile ( $File ); # write out the data to out file writeHour ( \*TARGET, $Hour, @Data ); } # we're done with the file close TARGET; # open the options-file open OPTIONS, '> ' . $Options || die "Can't open options file " . $Options . "\n"; # write our options to the file print OPTIONS << 'EOF'; set term png small color set nolog set nopolar set border set xrange [0:24] set y2range [0:] set xtics 0, 1, 24 set xtics 0, 1, 24 set y2tics 2 set grid set title "Load average and users" set xlabel "Time (h)" set ylabel "Load" set y2label "Users" set size 1, 1 set origin 0, 0 EOF # print the actual 'plot' command print OPTIONS 'plot "' . $OutName . '" using 1:2 title "Load" with line 1, '; print OPTIONS '"' . $OutName . '" using 1:3 axes x1y2 title "Users" with line 3'; # close the file close OPTIONS; # run 'gnuplot' over the file system 'gnuplot ' . $OutDir . '/plotoptions > ' . $OutName . '.png'; # print out the image print '

'; # create the footer print $Query->hr; print '

'; print '

For more information contact '; print 'Chakie
'; print $Query->end_html;