#!/usr/bin/perl -w
#
# Plugin for watching io-bound traffic (in blocks) on disks.
#
# Usage: Link or copy into /etc/lrrd/client.d/
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by lrrd-config)
#
# $Log: iostat.in,v $
# Revision 1.2  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
# Magic markers (optional - used by lrrd-config and some installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf

use strict;


if ( $ARGV[0] and $ARGV[0] eq "autoconf")
{
	if (system("grep -q '^disk_io: [^ ]' /proc/stat") == 0)
	{
		print "yes\n";
		exit 0;
	}
	else
	{
		print "no\n";
		exit 1;
	}
}

open (IN, "/proc/stat") or die "Could not open /proc/stat for reading: $!\n";

my %devs = ();

while (<IN>)
{
	next unless (/^disk_io:\s*(.+)\s*/);
	foreach my $dev (split /\s+/)
	{
		next unless $dev =~ /\S/;
		next unless ($dev =~ /\((\d+),(\d+)\):\(\d+,\d+,(\d+),\d+,(\d+)\)/);
		$devs{"dev".$1."_".$2."_read"} = $3;
		$devs{"dev".$1."_".$2."_write"} = $4;
	}
}

close (IN);

if ( $ARGV[0] and $ARGV[0] eq "config")
{

	print "graph_title IOstat\n";
	print "graph_args --base 1024 -l 0\n";
	print "graph_vlabel blocks / second\n";
	foreach my $key (sort by_dev keys %devs)
	{
		print "$key.label $key\n";
		print "$key.type COUNTER\n";
		print "$key.max 10000\n";
	}
	exit 0;
}

foreach my $key (sort by_dev keys %devs)
{
	print "$key.value $devs{$key}\n";
}

sub by_dev {
    my @a = $a =~ /^dev(\d+)_(\d+)/;
    my @b = $b =~ /^dev(\d+)_(\d+)/;

    $a[0] <=> $b[0] || $a[1] <=> $b[1];
}

# vim:syntax=perl
