#!/usr/bin/perl
#
# Plugin to monitor connections per second, for LVS loadbalancers.
#
# Magic name:
#
# 	cps_<port>
# 	cps_<vip>_<port>
#
# Examples:
#
# 	cps_smtp
# 	cps_mail.foo.boo_smtp
# 	cps_pop3
# 	cps_www.foo.boo_www
# 	cps_vvv.foo.boo_www
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
# 	suggest  (optional - used by munin-config)
#
# $Log$
# Revision 1.8  2004/12/10 18:55:14  jimmyo
# Removed hardcoded host_name in linux/cps_.
#
# Revision 1.7  2004/12/10 18:51:44  jimmyo
# linux/apt* has been forced to LANG=C, to get predictable output.
#
# Revision 1.6  2004/12/10 11:48:42  jimmyo
# linux/cps_ plugin now groks high numbers.
#
# Revision 1.5  2004/12/10 10:47:49  jimmyo
# Change name from ${scale} to ${graph_period}, to be more consistent.
#
# Revision 1.4  2004/12/09 22:12:56  jimmyo
# Added "graph_period" option, to make "graph_sums" usable.
#
# Revision 1.3  2004/11/21 00:16:57  jimmyo
# Changed a lot of plugins so they use DERIVE instead of COUNTER.
#
# Revision 1.2  2004/05/20 19:02:37  jimmyo
# Set categories on a bunch of plugins
#
# Revision 1.1  2004/01/02 18:50:01  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.4  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
# Magic markers - optional - used by installation scripts and munin-config:
#
#%# family=manual
#%# capabilities=autoconf suggest
#

use strict;

if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" )
{
	&autoconf ();
}
if ( defined $ARGV[0] and $ARGV[0] eq "suggest" )
{
	my $sipvs;
	$sipvs = &ipvs (".", ".", $sipvs);
	exit 0 if $sipvs == undef;
	&suggest ($sipvs);
}

unless ($0 =~ /cps(?:_([^_]+)|)_(.+)\s*$/)
{
	die "Could not parse name $0.\n";
}
my $vip  = $1;
my $port = $2;
my $ipvs;

#print "Name: $0\nPort: $port\nVip : $vip\n";

# Read ipvsadm-output

$ipvs = &ipvs ($vip, $port, $ipvs);

if ( defined $ARGV[0] and $ARGV[0] eq "dump" )
{
	use Data::Dumper;
	print Dumper($ipvs);
}

if ( defined $ARGV[0] and $ARGV[0] eq "config" )
{
        &config ($vip, $port, $ipvs);
}

$vip = $vip || "";
if (exists ($ipvs->{$vip}) and exists ($ipvs->{$vip}->{$port}))
{
	foreach my $host (sort keys %{$ipvs->{$vip}->{$port}})
	{
		(my $fname = $host) =~ s/[.-]/_/g;
		print "$fname.value ", $ipvs->{$vip}->{$port}->{$host}, "\n";;
	}
}
else
{
	foreach my $vip (sort keys %{$ipvs})
	{
		foreach my $host (sort keys %{$ipvs->{$vip}->{$port}})
		{
			(my $fname = ($vip . "-" . $host)) =~ s/[.-]/_/g;
			print "$fname.value ", $ipvs->{$vip}->{$port}->{$host}, "\n";;
		}
	}
}

sub autoconf
{
	system ("/sbin/ipvsadm -L --stats >/dev/null 2>/dev/null");

	if ($? == 0)
	{
		print "yes\n";
		exit 0;
	}
	elsif (($?>>8) == 2)
	{
		print "no (permission denied)\n";
		exit 1;
	}
	elsif ($? == 127)
	{
		print "no (ipvsadm not found)\n";
		exit 1;
	}
	else
	{
		print "no\n";
		exit 1;
	}
}
sub suggest
{
	my $ipvs = shift;
	exit 0 unless $ipvs;

	foreach my $vip (sort keys %{$ipvs})
	{
		foreach my $port (sort keys %{$ipvs->{$vip}})
		{
			print "cps_${vip}_$port\n";
		}
	}
	exit 0;
}

sub config
{
	my $vip  = shift;
	my $port = shift;
	my $ipvs = shift;

	print "graph_title Loadbalanced ",($vip?$vip:"*"),"->",$port," connections\n";
	print "graph_args -l 0\n";
	print "graph_total total\n";
	print "graph_vlabel connections / \${graph_period}\n";
	print "graph_category network\n";
	my $first=1;
	$vip = $vip || "";
	if (exists ($ipvs->{$vip}) and exists ($ipvs->{$vip}->{$port}))
	{
		foreach my $host (sort keys %{$ipvs->{$vip}->{$port}})
		{
			(my $fname = $host) =~ s/[.-]/_/g;
			if ( $first == 1 )
			{
				print "$fname.draw AREA\n";
				$first=0
			}
			else
			{
				print "$fname.draw STACK\n";
			}
			print "$fname.type DERIVE\n";
			$host =~ s/-bak//;
			print "$fname.label $host\n";
			print "$fname.max 1000\n";
			print "$fname.min 0\n";
		}
	}
	else
	{
		foreach my $vip (reverse sort keys %{$ipvs})
		{
			foreach my $host (sort keys %{$ipvs->{$vip}->{$port}})
			{
				(my $fname = ($vip . "-" . $host)) =~ s/[.-]/_/g;
				if ( $first == 1 )
				{
					print "$fname.draw AREA\n";
					$first=0
				}
				else
				{
					print "$fname.draw STACK\n";
				}
				print "$fname.type DERIVE\n";
				$host =~ s/-bak//;
				my $label = "$vip -> $host";
				$label =~ s/\.MIT\.EDU//g;
				print "$fname.label $label\n";
				print "$fname.max 1000\n";
				print "$fname.min 0\n";
			}
		}
	}
	exit 0;
}

sub ipvs
{
	my $vip  = shift;
	my $port = shift;
	my $ipvs = shift;
	open (IPVS, "/sbin/ipvsadm -L --stats 2>/dev/null|") or return undef;
	my $cvip  = "";
	my $cport = "";
	while (<IPVS>)
	{
		next if /^IP Virtual Server/;
		next if /^Prot\s+LocalAddress/;
		if (/^(\w+)\s+([\w\.-]+):([\w\d]+)\s+(\d+)[KMG]?\s+/)
		{
			$cvip  = $2;
			$cport = $3;
		}
		elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)G\s+/)
		{
			$ipvs->{$cvip}->{$cport}->{$1} += ($3*1000000000);
		}
		elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)M\s+/)
		{
			$ipvs->{$cvip}->{$cport}->{$1} += ($3*1000000);
		}
		elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)K\s+/)
		{
			$ipvs->{$cvip}->{$cport}->{$1} += ($3*1000);
		}
		elsif (/^\s+->\s+([^:]+):(\S+)\s+(\d+)\s+/)
		{
			$ipvs->{$cvip}->{$cport}->{$1} += $3;
		}
	}
	close (IPVS) or return undef;
	return $ipvs;
}
# vim:syntax=perl
