Code:
#!/usr/bin/perl
###########################################################################
# host_history.pl
#
# 15/06/2004 14:18
#
# 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 => 'B.O.F.H',
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 $time = time;
open(HOST, "<$CONF{host_file}");
while (<HOST>) {
if (/^trackcfg=(.+)$/s) {
($track = $1);
}
if (/^cars=(.+)$/s) {
($cars = $1);
}
push @drivers, /^conn=(.+)$/s;
}
$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;
}
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);
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, 162, 60, '2', $out_black);
$out_graph->string(gdTinyFont, 162, 40, '4', $out_black);
$out_graph->string(gdTinyFont, 162, 20, '6', $out_black);
$out_graph->string(gdTinyFont, 162, 1, '8', $out_black);
$out_graph->string(gdTinyFont, 128, 82, get_time() . '>', $out_black);
$out_graph->string(gdTinyFont, 1, 82, $CONF{server_name}, $out_black);
$graph->filledRectangle(0, 0, $width, $height, $background);
$graph->line(0, 70, $width, 70, $int_lines_light);
$graph->line(0, 60, $width, 60, $int_lines);
$graph->line(0, 50, $width, 50, $int_lines_light);
$graph->line(0, 40, $width, 40, $int_lines);
$graph->line(0, 30, $width, 30, $int_lines_light);
$graph->line(0, 20, $width, 20, $int_lines);
$graph->line(0, 10, $width, 10, $int_lines_light);
my $i = 0;
foreach (@data) {
$graph->line($i, $height - ($_ * 10), $i, $height, $light_blue);
$i++;
}
$out_graph->copyResized($graph, 0, 0, 0, 0, 160, 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;
}
my $width = 1440;
my $height = 160;
my $out_width = 350;
my $out_height = 200;
my $graph = new GD::Image($width, $height);
my $out_graph = new GD::Image($out_width, $out_height);
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, 342, 140, '2', $out_black);
$out_graph->string(gdSmallFont, 342, 100, '4', $out_black);
$out_graph->string(gdSmallFont, 342, 60, '6', $out_black);
$out_graph->string(gdSmallFont, 342, 20, '8', $out_black);
$out_graph->string(gdSmallFont, 300, 182, get_time() . '>', $out_black);
$out_graph->string(gdTinyFont, 1, 82, $CONF{server_name}, $out_black);
$graph->filledRectangle(0, 0, $width, $height, $background);
$graph->line(0, 140, $width, 140, $int_lines_light);
$graph->line(0, 120, $width, 120, $int_lines);
$graph->line(0, 100, $width, 100, $int_lines_light);
$graph->line(0, 80, $width, 80, $int_lines);
$graph->line(0, 60, $width, 60, $int_lines_light);
$graph->line(0, 40, $width, 40, $int_lines);
$graph->line(0, 20, $width, 20, $int_lines_light);
my $i = 0;
foreach (@data) {
$graph->line($i, $height - ($_ * 20), $i, $height, $light_blue);
$i++;
}
$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 attached.