Whatever

index.cgi

#!/usr/bin/perl

# 2006-06-01 15:57;

use CGI::Safe;
my $c = new CGI;

sub mksl {
	my $h = shift;
	my $t = shift;
	my $x = new CGI($c);
	$x->delete_all;
	$x->param('host',$h) if ($h && $h ne '@');
	$x->param('type',$t) if ($t && $t ne '@');
	my $u = $x->self_url;
	return $u;
}

my @hosts = ();
my %hosts = ();
if (open(DF,'<','hosts')) {
	while (my $l = <DF>) {
		$l =~ s/[\r\n]//gs;
		if ($l =~ /^(.*?)\s*:\s*(.*?)$/) {
			next unless ($1 && $2);
			$hosts{$1} = $2;
			push @hosts, $1;
		}
	}
	close(DF);
}
unless (@hosts) {
	print	$c->header(-status=>404,-type=>'text/html'),
		$c->start_html('Error'),
		$c->h1('Error'),
		$c->p('No hosts!'),
		$c->end_html;
	exit;
}

my $hosti = $c->param('host');
$hosti = $hosts[0] if (@hosts == 1);
$hosti = '@' unless ($hosti);

my $typei = $c->param('type');
$typei = '' unless ($typei);
$typei = lc($typei);
$typei = 'con' unless ($typei =~ /^(sys|con)$/);

unless ($hosts{$hosti}) {
	print	$c->header(-status=>404,-type=>'text/html'),
		$c->start_html('Error'),
		$c->h1('Error'),
		$c->p('Missing or unknown host!'),
		$c->end_html;
	exit;
}

my @graphs = ();
my %osgraphs = ();
my @osgrahs = ();
if (opendir(DD,'.')) {
	while (my $f = readdir(DD)) {
		next if ($f =~/^\./);
		if ($f =~ /^$hosti\_-_$typei\_(.*)\.png$/) {
			my $ff = $1;
			if ($ff =~ /^osver_-_(.*)$/) {
				my %i;
				$i{f} = $f;
				$i{a} = $ff;
				$osgraphs{$1} = \%i;
			} else {
				my %i;
				if ($ff eq 'dist') {
					$i{n} = 'Distance';
				} elsif ($ff eq 'link') {
					$i{n} = 'Link Type';
				} elsif ($ff eq 'os') {
					$i{n} = 'System/Vendor';
				} else {
					$i{n} = ucfirst($ff);
				}
				$i{f} = $f;
				$i{a} = $ff;
				push @graphs, \%i;
			}
		}
	}
	closedir(DD);
}
if (open(DF,'<',"$hosti\_-_$typei\_os_order")) {
	while (my $l = <DF>) {
		$l =~ s/[\r\n]//gs;
		if ($l =~ /^(.*?)\s*:\s*(.*?)$/) {
			next unless ($1 && $2);
			next unless ($osgraphs{$1});
			$osgraphs{$1}->{n} = $2;
			push @osgraphs, $1;
		}
	}
	close(DF);
}

my $ht = $hosts{$hosti};
my $hm = '';
if ($hosti eq '@') {
	$ht = '';
	for (my $i=0;$i<@hosts;$i++) {
		if ($ht) {
			if ($i==$#hosts) {
				$ht .= ' &amp; ';
			} else {
				$ht .= ', ';
			}
		}
		$ht .= $hosts{$hosts[$i]};
	}
	$hm .= '&nbsp;| ' if ($hm);
	$hm .= $c->a({href=>mksl($hosts[$i],$typei)},$hosts{$hosts[$i]});
}
$hm = $c->p({align=>'center'},$hm) if ($hm);

my $timerun = '';
my $timeint = '';
if (open(DF,'<',"$hosti\_-_data_interval")) {
	$timeint = <DF>;
	$timeint =~ s/\s[-+]\d+$//;
	my $timex = <DF>;
	$timex =~ s/\s[-+]\d+$//;
	$timeint .= " to $timex";
	$timerun = <DF>;
	$timerun =~ s/\s[-+]\d+$//;
	close(DF);
} elsif (open(DF,'<','data_interval')) {
	$timeint = <DF>;
	$timeint =~ s/\s[-+]\d+$//;
	my $timex = <DF>;
	$timex =~ s/\s[-+]\d+$//;
	$timeint .= " to $timex";
	$timerun = <DF>;
	$timerun =~ s/\s[-+]\d+$//;
	close(DF);
}
$timerun =~ s/[\r\n]//gs;
$timeint =~ s/[\r\n]//gs;
$timerun = "Last run at $timerun" if ($timerun);
$timeint = "Calculated on data from $timeint" if ($timeint);

my $gm = '';
my @gp = ();
foreach my $g (sort {lc($a->{n}) cmp lc($b->{n})} @graphs) {
	$gm .= '&nbsp;| ' if ($gm);
	$gm .= $c->a({href=>'#'.$g->{a}},$g->{n});
	push @gp, $c->hr({width=>'75%',align=>'center',id=>$g->{a}}).$c->img({src=>$g->{f},alt=>$g->{n},align=>'center'});
}
my $ovt = '';
my @ogp = ();
if (%osgraphs) {
	my $tm = '';
	for (my $i=0;$i<@osgraphs;$i++) {
		$tm .= '&nbsp;| ' if ($tm);
		$tm .= $c->a({href=>'#'.$osgraphs{$osgraphs[$i]}->{a}},$osgraphs{$osgraphs[$i]}->{n});
		push @ogp, $c->hr({width=>'75%',align=>'center',id=>$osgraphs{$osgraphs[$i]}->{a}}).
				$c->img({src=>$osgraphs{$osgraphs[$i]}->{f},alt=>$osgraphs{$osgraphs[$i]}->{n},align=>'center'});
	}
	if ($tm) {
		$ovt .= $c->hr({align=>'center',id=>'osver'}) if ($gm);
		$ovt .= $c->h2({align=>'center'},'Systems/Versions');
		$gm .= '&nbsp;| ' if ($gm);
		$gm .= $c->a({href=>'#osver'},'Systems/Versions');
		$gm .= $c->br;
		$gm .= $tm;
	}
}

my $typeh = '';
my $typet = '';
if ($typei eq 'con') {
	$typeh = $c->a({href=>mksl($hosti,'sys')},'By System');
	$typet = $c->i('by Connection').$c->br;
} elsif ($typei eq 'sys') {
	$typeh = $c->a({href=>mksl($hosti,'con')},'By Connection');
	$typet = $c->i('by System').$c->br;
}
my $head = $c->p({align=>'right'},$typeh,'&nbsp;| ',$c->a({href=>'../'},'Statistics'),'&nbsp;| ',$c->a({href=>'http://whatever.frukt.org'},'Whatever'));
if (open(DF,'<','head')) {
	$head = <DF>;
	close(DF);
	$head =~ s/\$\{tl\}/$typeh/gs;
}
my $foot = $c->hr({align=>'center'}).
		$c->p({align=>'left'},'This stuff was created with',
			$c->a({href=>'http://whatever.frukt.org/p0fstats.text.shtml'},'a couple of perl scripts'),'and',
			$c->a({href=>'http://lcamtuf.coredump.cx/p0f.shtml'},'p0f 2'));
if (open(DF,'<','foot')) {
	$foot = <DF>;
	close(DF);
}

print	$c->header(),
	$c->start_html($ht),
	$head,$c->center(
		$c->h1({align=>'center'},$ht),$hm,$c->hr({align=>'center'}),
		$c->p({align=>'center'},$gm),$c->hr({align=>'center'}),
		$c->p({align=>'center'},$typet,$timerun,$c->br,$timeint),
		@gp,$ovt,@ogp,
	),
	$foot,
	$c->end_html;

(2008-01-11)