#!/usr/local/bin/perl -w

#
# This script analyzes the results of the who_uses_the_system command
#

use File::stat;

# Basename for log files
$LOGFILE_BASE="/home/panstarrs/ipp/.tmp/log_";
# Hosts on which the 'who_uses_the_system' command was run
@HOSTS=qw/ipp004 ipp005 ipp006 ipp007 ipp008 ipp009
    ipp010 ipp011 ipp012 ipp013 ipp014 ipp015 ipp016 ipp017 ipp018 ipp019
    ipp020 ipp021        ipp023 ipp024 ipp025 ipp026 ipp027 ipp028 ipp029
    ipp030 ipp031 ipp032 ipp033 ipp034 ipp035 ipp036 ipp037 ipp038 ipp039
    ipp040 ipp041 ipp042 ipp043 ipp044 ipp045 ipp046 ipp047 ipp048 ipp049
    ipp050 ipp051 ipp052 ipp053 ipp054 ipp055 ipp056 ipp057 ipp058 ipp059
    ipp060 ipp061 ipp062 ipp063 ipp064 ipp065 ipp066
           ippc01 ippc02 ippc03 ippc04 ippc05 ippc06 ippc07 ippc08 ippc09
    ippc10 ippc11 ippc12 ippc13 ippc14 ippc15 ippc16 ippc17 ippc18 ippc19
    ippc20 ippc21 ippc22 ippc23 ippc24 ippc25 ippc26 ippc27 ippc28 ippc29
    ippc30 ippc31 ippc32 ippc33 ippc34 ippc35 ippc36 ippc37 ippc38 ippc39
    ippc40 ippc41 ippc42 ippc43 ippc44 ippc45 ippc46 ippc47 ippc48 ippc49
    ippc50 ippc51 ippc52 ippc53 ippc54 ippc55 ippc56 ippc57 ippc58 ippc59
    ippc60 ippc61 ippc62 ippc63
    ippdb00 ippdb01 ippdb02 ippdb03 stare00 stare01 stare02 stare03 stare04/;

print "<html>\n";
print "<head><title>Who uses the system?</title></head>\n";
print "<body>\n";
print "<b>To get info, move the mouse pointer over the table cell. !Warning! Command lines can be truncated</b><br/><br/><br/>\n";
print "<table border=\"1\">\n";
# Scan each logfile
foreach $computer (@HOSTS) {
    $LOGFILE=$LOGFILE_BASE.$computer;
    $canopen = open LOG, "<$LOGFILE";
    if ($canopen) { # The logfile exists (host is active)
        # The summary table is indexed by the pid. for each pid, we
        # store the average cpu/memory load, the process name, the
        # user name
        %summary = ();
        for $line (<LOG>) {
            chomp $line;
            if ( !($line =~ /.*top.*/) && ($line =~ /^ ?[1-9].*/) ) {
                # We don't want the 'top' process to be shown
                $line  =~ s/\s+/;/g;
                $line =~ s/^;//g;
                # print "$line\n";
                @values = split ';', $line;
                $pid = $values[0];
                #print $pid, "\n";
                $username = $values[1];
                $proc =$values[8];
                $mem =$values[9];
                $size = scalar(@values);
                #$procname = $values[11];
                $fullprocname = "";
                #print "$procname \n";
                for ($i=11;$i<$size;$i++) {
                        $fullprocname = $fullprocname.$values[$i]." ";
                }
                if (!defined $summary{$pid}) {
                    # The pid is a new one
                    $summary{$pid}{"count"} = 1;
                    $summary{$pid}{"username"} = $username;
                    $summary{$pid}{"proc"} = $proc;
                    $summary{$pid}{"memory"} = $mem;
                    $summary{$pid}{"procname"} = $fullprocname;
                } else {
                    # The pid has already been observed
                    # print "The pid has already been observed\n";
                    #if ($summary{$pid}{"procname"} eq $procname) {
                        $before = $summary{$pid}{"count"};
                        $summary{$pid}{"count"}++;
                        $after = $summary{$pid}{"count"};
                        $ratio = $before / $after;
                        $summary{$pid}{"proc"} = $ratio *
                            $summary{$pid}{"proc"} + $proc / $after;
                        $summary{$pid}{"memory"} = $ratio *
                            $summary{$pid}{"memory"} + $proc / $after;
                    #}
                }
            }
        }
        close LOG;
        # When was the logfile produced? mtime is modification time
        $stats = stat($LOGFILE);
        # Show results
        printf("<tr><td colspan=\"6\" title=\"From %s (%s)\"><b>%s</b></td></tr>\n",
               $LOGFILE, scalar localtime $stats->mtime, $computer);
        $onceShown = 0;
        while ( my ($pid, $value) = each(%summary) ) {
                # print $value->{"count"} , " ", $pid, "\n";
            if ($value->{"count"} > 1) {
                #print "Ok\n";
                $onceShown = 1;
                printf("<tr>\n");
                if ($value->{"username"} eq "ipp" or $value->{"username"} eq "apache" or $value->{"username"} eq "root") {
                    $color = "black";
                } else {
                    # The user us not a regular one... Show it in red.
                    $color = "red";
                }
                $comment = "Whom the process belongs to";
                printf("<td title=\"$comment\"><font color=\"$color\">%s</font></td>\n", $value->{"username"});
                $comment = "Process name";
                printf("<td title=\"$comment\">%s</td>\n", $value->{"procname"});
                if ($value->{"proc"} > 100.) {
                    $color = "red";
                } elsif ($value->{"proc"} > 50.) {
                    $color = "orange";
                } else {
                    $color = "black";
                }
                $comment = "Average cpu load";
                printf("<td title=\"$comment\"><font color=\"$color\">%.1f</font></td>\n", $value->{"proc"});
                if ($value->{"memory"} > 100.) {
                    $color = "red";
                } elsif ($value->{"memory"} > 50.) {
                    $color = "orange";
                } else {
                    $color = "black";
                }
                $comment = "Average memory load";
                printf("<td title=\"$comment\"><font color=\"$color\">%.1f</font></td>\n", $value->{"memory"});
                $comment = "How many times the process was observed every 3 seconds during 15 seconds";
                printf("<td title=\"$comment\">%s</td>\n", $value->{"count"});
                $comment = "Process pid";
                printf("<td title=\"$comment\">%s</td>\n", $pid);
                printf("</tr>\n");
            }
        }
        if ($onceShown == 0) {
            print "<tr><td colspan=\"6\" title=\"No process occurred more than once (in 5 times every 3 seconds)\">Nothing interesting for this one</td></tr>\n";
        }
    } else {
        print "<tr><td title=\"Hostname\" title=\"... maybe $computer is down\"><b>$computer</b></td><td colspan=\"5\">Can't open $LOGFILE</td></tr>\n";
    }
}

print "</table>\n";
print "</body>\n";
print "</html>\n";

