Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 07cf3633b51f3ccc3197ef1037a09615 > files > 20

apache-mod_benchmark-2.0.1-6mdv2010.0.i586.rpm

#!/usr/bin/perl
#
# This program scans the content of the file given as a parameter
# (should be a dump made by 'benchmark_rt')
# It adds at the end of the data alerts when threshold are
# reached. Alerts can be yellow or red.
#

# Global Variables:
$GREEN=0;
$YELLOW=1;
$RED=2;

$DUMPNAME = $ARGV[0];
$STATUS   = $GREEN;	# default status

# -----------------------------------------------------------------------
# Here are the definition you must change to adapt them to the
# URIs and the values you want to scan for.
#
# Each entry of the hash table is made of up a key which can be
# anything since it is not used, and another hash table. This secondary
# hash table gives a regexp that matches URIs and two threshold values
# expressed in milli-seconds.
#
# If the 1st value is reached a 'yellow' status will be generated
# for the macthing URI, if the 2nd threshold is reached, a 'red' status
# will be generated.
# -----------------------------------------------------------------------
%thresholds = (
	static_pages => {
		pattern => ".html\$|.gif\$",	
		th1     => "500",
		th2     => "3000"
	},
	dynamic_pages => {
		pattern => ".jsp\$|.srv\$",
		th1     => "5000",
		th2     => "10000"
	},
);


# -------------------------------------------------------------------
# Open the dump file and scan the lines.
# -------------------------------------------------------------------
$ALERTS = "\n";

if (!defined(open(FD,$DUMPNAME))) {
	# it's dangerous to write in a file whose name is given
	# as a parameter and that cannot be open in read mode !
	print "Could not open file $DUMPNAME\n";
	exit $RED;
}

while (<FD>) {
	# Split lines into columns:
	@cols = split(/\s+/);

	# URIs lines are made up of 7 columns:
	if (scalar(@cols) != 7) { next; }

	$uri = $cols[6];

	# Should we watch this URI ?
	foreach $th (keys %thresholds) {
		$p = $thresholds{$th}{'pattern'};

		if ($uri =~ $p) {
			#print "$uri match $p\n";

			# Test the average time (given in micro-seconds):
			$s1 = $thresholds{$th}{'th1'} * 1000;
			$s2 = $thresholds{$th}{'th2'} * 1000;
			if ($cols[2] > $s2) {
				$STATUS = $RED;
				&AddAlert($uri,$s2,"RED");
			}
			elsif ($cols[2] > $s1) {
				if ($STATUS != $RED) { $STATUS = $YELLOW; }
				&AddAlert($uri,$s1,"YELLOW");
			}

			last;
		}
	}
}

close FD;

# ----------------------------------------------------------------
# Add the alerts as new lines in the original $DUMPNAME file:
# ----------------------------------------------------------------
open(FD,">>$DUMPNAME");
print FD $ALERT;
close FD;

exit $STATUS;

# ------------------------------------
# Add an alert:
# ------------------------------------
sub AddAlert
{
	my ($uri,$th,$status) = @_;

	$th = int($th / 1000);
	$ALERT .= "$status condition for URL: '$uri' (threshold=$th ms)\n";
	#print $ALERT;
}

# EOF