#!/usr/bin/perl
# Munin plugin for monitoring swapspace usage
#
# FIELDS:
#		Swap Alloc	swap allocated (used)
#		Swap Unalloc	swap reserved but not allocated
#		Swap Avail	swap available for reservation
#
# Core logic developed by Brendan Gregg.
# REFERENCE: http://www.brendangregg.com/k9toolkit.html - the swap diagram.
#
# COPYRIGHT: Copyright (c) 2004 Brendan Gregg.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#  (http://www.gnu.org/copyleft/gpl.html)

# Perldoc

=pod

=head1 NAME

swapspace_info - Plugin to monitor Swapspace usage

=head1 AUTHOR

Christian Braum, chrisi_braum@web.de

Core logic developed by Brendan Gregg. See K9Toolkit:
http://www.brendangregg.com/K9Toolkit/swapinfo

=head1 LICENSE

GPL 2.

=cut

# Main
use strict;
use warnings;

if ( defined $ARGV[0] )
{
	if ( $ARGV[0] eq "config" )
	{
		&config();
	}
	else
	{
		&output();
	}
}
else
{
	&output();
}

sub value
{
	my %h_swapvalue;
	eval 'use Sun::Solaris::Kstat; 1;'
            or die 'Please install Sun::Solaris::Kstat';
	my $Kstat = Sun::Solaris::Kstat->new();

	# --- Fetch Hardware info ---
	### pagesize
	$ENV{PATH} = "/usr/bin";
	chomp(my $PAGESIZE = `pagesize`);
	my $PAGETOMB = $PAGESIZE / (1024 * 1024);
	my $PAGETOBYTE = $PAGESIZE;
	my $BLOCKTOP = 512 / $PAGESIZE;
	my %VMnow;
	my %VMold;
	my %VMinfo;

	# --- Fetch VM info ---
	foreach my $count (0..12)
	{
		#
		#  The values are counters that increment each second, here we
		#  check them several times and look for the value changing.
		#  (reading them once then again a second later was not reliable).
		#
		foreach my $var ("swap_avail","swap_alloc","swap_free")
		{
			$VMnow{$var} = $Kstat->{unix}->{0}->{vminfo}->{$var};
			unless ($count)
			{
				$VMold{$var} = $VMnow{$var};
				next;
			}
			if (($VMnow{$var} != $VMold{$var}) && (! $VMinfo{$var}))
			{
				$VMinfo{$var} = $VMnow{$var} - $VMold{$var};
			}
		}
		select(undef, undef, undef, 0.1);
		$Kstat->update();
	}

	# --- Calculations ---

	### Swap
	my $swap_free = $VMinfo{swap_free};
	my $swap_avail = $VMinfo{swap_avail};
	my $swap_alloc = $VMinfo{swap_alloc};
	my $swap_unalloc = $swap_free - $swap_avail;

        my $swap_unalloc_B   = sprintf( "%d ", $swap_unalloc * $PAGETOBYTE );
        my $swap_avail_B     = sprintf( "%d ", $swap_avail * $PAGETOBYTE );
        my $swap_alloc_B     = sprintf( "%d ", $swap_alloc * $PAGETOBYTE );
        my $swap_free_B      = sprintf( "%d ", $swap_free * $PAGETOBYTE );

	$h_swapvalue{"Alloc.value"} = "$swap_alloc_B";
        $h_swapvalue{"Unalloc.value"} = "$swap_unalloc_B";
        $h_swapvalue{"Avail.value"} = "$swap_avail_B";

	return %h_swapvalue;
}

sub output
{
	my %h_swapvalues=value();
	print "Alloc.value " .   $h_swapvalues{"Alloc.value"} . " \n";
	print "Unalloc.value " . $h_swapvalues{"Unalloc.value"} . " \n";
	print "Avail.value " .   $h_swapvalues{"Avail.value"} . "\n";
}

sub config
{
	print "graph_args --base 1024 -l 0 \n";
        print "graph_vlabel Bytes\n";
        print "graph_title Swapspace usage\n";
        print "graph_category memory\n";
        print "graph_info This graph shows what the machine uses Swapspace for.\n";
        print "graph_order ";

	print "Alloc ",
	      "Unalloc ",
       	      "Avail ",
	      "\n";

	print "Alloc.label Alloc \n";
        print "Alloc.draw \n";
        print "Alloc.info Swap used.\n";
        print "Unalloc.label Unalloc \n";
        print "Unalloc.draw \n";
        print "Unalloc.info Swap reserved but not allocated.\n";
        print "Avail.label Avail \n";
        print "Avail.draw \n";
        print "Avail.info Swap available.\n";
}
