#!/usr/bin/perl

######################################################
#
# BIG BROTHER - EXT SCRIPT TO GENERATE MAP
#
# This program is Copyright (c) 2001
# Patrick Nixon - pnixon @ ci . somerville . ma . us
# All Rights Reserved
######################################################
# VERSION: 
# 1.0  Patrick Nixon - Created 
######################################################
#  This script will generate a map based on the conditions of groupings
#    stored in the bb-hosts file.
#
#  General Premise taken from BigSister's map feature
#    and GD.pm's examples.
######################################################
#  INSTALLATION
# 1) make sure this script is in $BBHOME/ext
# 2) make sure you install the GD.pm perl module
# 3) modify bb-hosts for the entries you want displayed on the map
#    ie: 10.1.1.1 bbgod # BBNET map:main:1:1
#	 10.1.2.1 bbtest # BBPAGER map:test:10:10
#	 10.1.3.1 bbtest # smtp map:main:1:1
# NOTE: For hosts in the same group, the coordinates need to match
# 4) copy your template in png format into $BBHOME/ext
# 5) add to your BB setup to run as an external script
######################################################
#  DISCLAIMER
#  This software was written by me, for me.  It does 
#  what I need it to.  You use it at your own risk.  
#  If bad things happen (loss of data, load spike,
#  swarm of locusts, raining frogs), don't blame me.
#  You've been warned.
######################################################

#  Requires GD.pm
use GD;

######################################################
#  User Configuration Area
######################################################
$BBHOME="/home/gart/bb";
$BBVAR="/home/gart/bbvar";
$BBMAP="/home/gart/bb/ext/template.png";
# Size in Pixels for Status Circle
$BBSIZE=11;

######################################################
# DON"T CHANGE THIS!
######################################################
# create a new image
$im = GD::Image->newFromPng($BBMAP);

######################################################
# Defined Colors - Modifiable
######################################################
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$red = $im->colorAllocate(255,0,0);
$yellow = $im->colorAllocate(255,255,0);
$purple = $im->colorAllocate(153,51,153);
$blue = $im->colorAllocate(0,0,255);
$green = $im->colorAllocate(0,255,0);
$gray = $im->colorAllocate(128,128,128);
@color=(green,clear,blue,purple,yellow,red);
@colors=($green,$clear,$blue,$purple,$yellow,$red);

######################################################
######################################################
#
#  You should not have to change anything below this line
#
######################################################
######################################################

######################################################
##### Subroutine to get host status out of bbvar/logs
######################################################
sub checkhost {
	$chtarget = "@_";
	$status="0";
	if ( `grep -l -w "red" $BBVAR/logs/$chtarget.* | wc -l` > 0 ) { 
		$status = "5";
	} elsif ( `grep -l -w "yellow" $BBVAR/logs/$chtarget.* | wc -l` > 0 ) { 
		$status = "4";
	} elsif ( `grep -l -w "purple" $BBVAR/logs/$chtarget.* | wc -l` > 0 ) { 
		$status = "3";
	} else { $status = "0"; };
}

######################################################
##### Subroutine to check host status against existing status
######################################################
sub checkstatus {
	$newstat = "@_";
	if ($newstat > $status) { $status = $newstat }
}

######################################################
##### Subroutine to check building status against existing status
######################################################
sub checkbuilding {
	$chtarget = "@_";
	$BCOORDS{$chtarget} = "$mapx".","."$mapy";
	$bstatus = $BSTAT{$chtarget};
	if ($status > $bstatus) { $BSTAT{$chtarget} = $status; }	
}

######################################################
# Generate Map from BB-HOSTS Entries
######################################################
foreach $line (`grep "map:" $BBHOME/etc/bb-hosts`) {
	chomp $line;
	@items = split(/\s+/,$line);
	$maphost = $items[1];
	$mapip = $items[0];
	$_ = $line;
	/map:(\w+):(\d+):(\d+)/;
	$mapb = $1;
	$mapx = $2;
	$mapy = $3;
	checkhost($maphost);
	checkbuilding($mapb);
}

######################################################
# Draw Status Circles on Map
######################################################
foreach $building (keys(%BCOORDS)) {
	($xx,$yy)=split(/,/,$BCOORDS{$building});
	$im->arc($xx,$yy,$BBSIZE,$BBSIZE,0,360,$color[$BSTAT{$building}]);
	$im->fill($xx,$yy,$colors[$BSTAT{$building}]);
}

######################################################
# I like my background to be black (you can take this out)
######################################################
$im->fill(1,1,$black);

######################################################
# make sure we are writing to a binary stream
######################################################
binmode STDOUT;

######################################################
# Convert the image to PNG and print it on standard output
######################################################
open (FILE,">$BBHOME/www/map.png") || die "Error Output";
print FILE $im->png;
close FILE;
