host_history.pl 0.0.2 released

Discussion in 'Host History' started by Ian.H, Jul 10, 2004.

  1. Ian.H

    Ian.H Administrator Staff Member

    Hi all,

    After some config changes on the server a while ago, I realised one major downfall to 0.0.1, the number of max drivers was hardcoded to 8 for what I run on my server 99% of the time. Obviously, this wouldn't suit all admins as they run different configs. This new version takes care of that now my creating the max drivers axis dynamically from the hosts file.

    I've tested this up to 32 users (I forced this value) and it works fine, although the axis numbers become a little "compacted".. I'll address this part in a later version as I can't see this being an issue for a while (I hear S2 may hold 20 driver grids for which this script will cover just fine).


    Code:
    #!/usr/bin/perl
    ###########################################################################
    #            host_history.pl
    #
    #  10/07/2004 15:34
    #
    #  Copyright (c) 2004,  Ian.H <ian.h@dsrc.digiserv.net>
    #  Web    : <http://digiserv.net/>
    #         : <http://dsrc.digiserv.net/>
    #
    # License : BSD
    ###########################################################################
    #
    # A simple script that will use the host{port}.txt file to generate an
    # overall log of your server activity and then generate 2 graphs (small
    # and large ) representing 24 hours usage for drivers count.
    #
    ###########################################################################
    #
    # NOTE: This was coded to run on a *nix platform but it should also run
    #       on a windoze box.
    #
    ###########################################################################
    #
    # PRE-REQUISITES:
    # ---------------
    #
    # Perl5 ( <http://www.perl.com/> | <http://www.activestate.com/> )
    # GD1 or GD2 libs ( <http://www.boutell.com/gd/> )
    # tail ( windoze version at: <http://unxutils.sourceforge.net/> )
    #
    #
    # Please DO NOT ask how to install these if you do not currently have
    # them, see their accompanying README files for installation information.
    #
    ###########################################################################
    #
    # CONFIGURATION:
    # --------------
    #
    # Change the details below in the 'my %CONF = (...)' section to match
    # your requirements / filesystem. PLEASE NOTE: the *_file values MUST BE
    # fully-qualified paths / filename. The output_file and hourly_log_file
    # will be created upon execution. The host_operator should be the name of
    # your server connection that displays in the 'connections list' in LFS.
    #
    # The graph entries are the fully-qualified path / filename of where the
    # graphs should be created
    #
    # The tail entry should be a fully-qualified path/ filename to your
    # tail or tail.exe binary
    #
    ###########################################################################
    
    
    use strict;
    use warnings;
    use GD;
    
    my %CONF = (
        host_file               => '/path/to/LFS/host63392.txt',
        output_file             => '/path/to/host_history.log',
        hourly_log_file         => '/path/to/host_history_hour.log',
        host_operator           => 'Your host name (/host in server cfg file)',
        large_graph_output_file => '/path/to/driver_history_large.png',
        small_graph_output_file => '/path/to/driver_history_small.png',
        tail                    => '/path/to/tail(.exe)',
        server_name             => 'Your server name'
    );
    
    
    my @drivers = ();
    my $track = '';
    my $cars = '';
    my $max_guests = 0;
    
    my $time = time;
    
    open(HOST, "<$CONF{host_file}");
        while (<HOST>) {
            if (/^maxguests=(.+)$/s) {
                $max_guests = $1;
            }
    
            if (/^trackcfg=(.+)$/s) {
                $track = $1;
            }
    
            if (/^cars=(.+)$/s) {
                $cars = $1;
            }
    
            push @drivers, /^conn=(.+)$/s;
        }
    
        $max_guests =~ s/[\n|\r|[\r\n]$//g;
        $track =~ s/[\n|\r|[\r\n]$//g;
        $cars =~ s/[\n|\r|[\r\n]$//g;
    close(HOST);
    
    open(LOG, ">>$CONF{output_file}");
        print LOG "$time|$track|$cars|";
        foreach (@drivers) {
            s/[\n|\r|[\r\n]$//g;
            print LOG "$_\x00" unless $_ eq $CONF{host_operator} or $_ eq '';
        }
        print LOG "\n";
    close(LOG);
    
    system("$CONF{tail} -n 1440 $CONF{output_file} > $CONF{hourly_log_file}");
    
    
    create_small_driver_history_graph();
    create_large_driver_history_graph();
    
    
    sub create_small_driver_history_graph {
        my @data = ();
        my @hourly_log_contents = ();
    
        open(LOG, "<$CONF{hourly_log_file}");
            @hourly_log_contents = <LOG>;
        close(LOG);
    
        foreach (@hourly_log_contents) {
            /^[0-9]+\|[A-Z1-9]+\|[A-Z46]+\|(.*)$/s;
            my $driver_list = $1;
            my @drivers = split("\x00", $driver_list);
            pop @drivers;
            push @data, scalar @drivers;
        }
    
        $max_guests += 1 unless (($max_guests % 2) == 0);
    
        my $width = 1440;
        my $height = 80;
        my $out_width = 170;
        my $out_height = 95;
        my $graph = new GD::Image($width, $height);
        my $out_graph = new GD::Image($out_width, $out_height);
    
        #~ Colours
        my $white = $graph->colorAllocate(255, 255, 255);
        my $black = $graph->colorAllocate(0, 0, 0);
        my $light_blue = $graph->colorAllocate(134, 155, 191);
        my $background = $graph->colorAllocate(214, 224, 235);
        my $int_lines = $graph->colorAllocate(184, 194, 205);
        my $int_lines_light = $graph->colorAllocate(204, 214, 225);
    
        my $out_white = $out_graph->colorAllocate(255, 255, 255);
        my $out_black = $out_graph->colorAllocate(0, 0, 0);
        $out_graph->filledRectangle(0, 0, $width, $height, $out_white);
    
        $out_graph->string(gdTinyFont, 123, 82, get_time() . '>', $out_black);
        $out_graph->string(gdTinyFont, 1, 82, $CONF{server_name}, $out_black);
    
        $graph->filledRectangle(0, 0, $width, $height, $background);
    
        my $s = 0;
        for (my $i = $max_guests; $i > 0; $i--) {
            if (($i % 2) == 0) {
                $graph->line(0, $s, $width, $s, $int_lines) unless $s == 0;
            } else {
                $graph->line(0, $s, $width, $s, $int_lines_light) unless $s == 0;
            }
            $s += ($height / $max_guests);
        }
    
        my $i = 0;
        foreach (@data) {
            $graph->line($i, $height - ($_ * ($height / $max_guests)), $i, $height, $light_blue);
            $i++;
        }
    
        $s = 0;
        for (my $i = $max_guests; $i > 0; $i--) {
            $out_graph->string(gdTinyFont, 157, $s, $i, $out_black) unless (($i % 2) != 0);
            $s += ($height / $max_guests);
        }
    
        $out_graph->copyResized($graph, 0, 0, 0, 0, 155, 80, $width, $height);
        $out_graph->setAntiAliased($light_blue);
    
        $out_graph->interlaced('true');
    
        open(GRAPH, ">$CONF{small_graph_output_file}");
            binmode GRAPH;
            print GRAPH $out_graph->png;
        close(GRAPH);
    
       undef $graph;
        undef $out_graph;
    }
    
    
    sub create_large_driver_history_graph {
        my @data = ();
        my @hourly_log_contents = ();
    
        open(LOG, "<$CONF{hourly_log_file}");
            @hourly_log_contents = <LOG>;
        close(LOG);
    
        foreach (@hourly_log_contents) {
            /^[0-9]+\|[A-Z1-9]+\|[A-Z46]+\|(.*)$/s;
            my $driver_list = $1;
            my @drivers = split("\x00", $driver_list);
            pop @drivers;
            push @data, scalar @drivers;
        }
    
        $max_guests += 1 unless (($max_guests % 2) == 0);
    
        my $width = 1440;
        my $height = 160;
        my $out_width = 360;
        my $out_height = 200;
        my $graph = new GD::Image($width, $height);
        my $out_graph = new GD::Image($out_width, $out_height);
    
        #~ Colours
        my $white = $graph->colorAllocate(255, 255, 255);
        my $black = $graph->colorAllocate(0, 0, 0);
        my $light_blue = $graph->colorAllocate(134, 155, 191);
        my $background = $graph->colorAllocate(214, 224, 235);
        my $int_lines = $graph->colorAllocate(184, 194, 205);
        my $int_lines_light = $graph->colorAllocate(204, 214, 225);
    
        my $out_white = $out_graph->colorAllocate(255, 255, 255);
        my $out_black = $out_graph->colorAllocate(0, 0, 0);
        $out_graph->filledRectangle(0, 0, $width, $height, $out_white);
    
        $out_graph->string(gdSmallFont, 1, 5, 'Server activity over the past 24 hours', $out_black);
        $out_graph->string(gdSmallFont, 300, 182, get_time() . '>', $out_black);
        $out_graph->string(gdSmallFont, 1, 182, $CONF{server_name}, $out_black);
    
        $graph->filledRectangle(0, 0, $width, $height, $background);
    
        my $s = 0;
        for (my $i = $max_guests; $i > 0; $i--) {
            if (($i % 2) == 0) {
                $graph->line(0, $s, $width, $s, $int_lines) unless $s == 0;
            } else {
                $graph->line(0, $s, $width, $s, $int_lines_light) unless $s == 0;
            }
            $s += ($height / $max_guests);
        }
    
        my $i = 0;
        foreach (@data) {
            $graph->line($i, $height - ($_ * ($height / $max_guests)), $i, $height, $light_blue);
            $i++;
        }
    
        $s = 20;
        for (my $i = $max_guests; $i > 0; $i--) {
            $out_graph->string(gdTinyFont, 342, $s, $i, $out_black) unless (($i % 2) != 0);
            $s += ($height / $max_guests);
        }
    
        $out_graph->copyResized($graph, 0, 20, 0, 0, 340, 160, $width, $height);
        $out_graph->setAntiAliased($light_blue);
    
        $out_graph->interlaced('true');
    
        open(GRAPH, ">$CONF{large_graph_output_file}");
            binmode GRAPH;
            print GRAPH $out_graph->png;
        close(GRAPH);
    
        undef $graph;
        undef $out_graph;
    }
    
    
    sub get_time {
    	my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time);
    	return sprintf("%02d:%02d", $hour, $min);
    }

    Downloadable version also attached.



    Regards,

    Ian
     

    Attached Files:

Share This Page