#!/usr/bin/perl -w
# -*- perl -*-

=head1 NAME

dar_swap - Munin plugin to monitor darwin swap in/out.

=head1 APPLICABLE SYSTEMS

Should work on any darwin (Mac OS X) system with the 'vm_stat' command.

=head1 CONFIGURATION

None needed

=head1 INTERPRETATION

The plugin runs the vm_stat command a shows the number of pages swapped
in/out per second.

=head1 BUGS

None right now.

=head1 MAGIC MARKERS

  #%# family=auto
  #%# capabilities=autoconf

=head1 VERSION

  v.0.0.1

=head1 AUTHOR

Copyright (C) 2010.

Original version by J.T.Sage (at) gmail (dot) com.

=head1 LICENSE

GPLv2

=cut

use Munin::Plugin;

if ( defined($ARGV[0])) {
    if ($ARGV[0] eq 'autoconf') {
	$uname = `uname`;
	if ( not ( $uname =~ /Darwin/ ) ) { print "no (not a Darwin System)\n"; }
	else {
		if ( not -x "/usr/bin/vm_stat" ) { print "no (vm_stat not found)\n"; }
		else {
			print "yes\n";
		}
	}
	exit 0;
    }

    if ( $ARGV[0] eq "config" ) {
	print "graph_title Swap in/out\n";
	print "graph_args -l 0 --base 1000\n";
	print "graph_vlabel pages per \${graph_period} in (-) / out (+)\n";
	print "graph_category system\n";
	print "swap_in.label swap\nswap_in.type DERIVE\nswap_in.max 100000\nswap_in.min 0\nswap_in.graph no\n";
	print "swap_out.label swap\nswap_out.type DERIVE\nswap_out.max 100000\nswap_out.min 0\nswap_out.negative swap_in\n";
	exit 0;
    }
}

@vmstat = `vm_stat`;
for ( $i = 1;  $i < $#vmstat; $i++ ) {
	$line = $vmstat[$i];
	$label = $line;
	$label =~ s/"//g;
	$label =~ s/(\w+)\:.+$/$1/;
	$label =~ s/\n//g;
	$name = $label;
	$name =~ tr/A-Z/a-z/;
	$name =~ s/[^A-Za-z0-9_]/_/g;
	$data = $line;
	$data =~ s/.+?(\d+)\./$1/;
	$data =~ s/\n//g;
	$data = int(($data / 1000) + .5);
	if ( $name eq "pageins" ) {
		print "swap_in.value " . $data . "\n";
	}
	if ( $name eq "pageouts" ) {
		print "swap_out.value " . $data . "\n";
	}

}



# vim:syntax=perl
