package Collectd::Plugin::NFS::RPC;

#
# Get the ops and retrans numbers not collected by normal NFS plugin
#


use strict;
use Collectd qw| :all |;


my $dataset = [
	{
		name => 'nfs_ops',
		type => DS_TYPE_COUNTER,
		min  => 0,
		max  => 1099511627776,
	}, 
	{
		name => 'nfs_retrans',
		type => DS_TYPE_COUNTER,
		min  => 0,
		max  => 10000000000,
	}, 
];


# Register plugin
plugin_register( TYPE_READ, "nfs-rpc", "rpc_get_stats" );
plugin_register( TYPE_CONFIG, "nfs-rpc", "rpc_config" );
plugin_register( TYPE_INIT, "nfs-rpc", "rpc_init" );
plugin_register( TYPE_DATASET, "nfs-rpc", $dataset );


# Config
sub rpc_config {
	return 1;
} # end sub


# Init
sub rpc_init {
	# Make sure /proc/net/rpc/nfs exists
	if ( ! -f "/proc/net/rpc/nfs" ) {
		ERROR( "Can't find /proc/net/rpc/nfs" );
		return;
	} # end if

	return 1;
} # end sub


# Read
sub rpc_get_stats {
	my ( @lines, $ops, $retrans, );
	
	eval {
		open( INPUT, "/proc/net/rpc/nfs" ) || do {
			ERROR( "Can't open /proc/net/rpc/nfs: $!" );
			return;
		}; # end do

		@lines = <INPUT>;

		close INPUT;
	}; # end eval

	# Sanity check
	unless ( scalar @lines > 0 ) {
		return;
	} # end unless

	# Get rpc line
	for ( @lines ) {
		next unless substr( $_, 0, 4 ) eq 'rpc ';

		( undef, $ops, $retrans, undef ) = split;
	} # end for
	

	if ( defined $ops && defined $retrans ) {
		plugin_dispatch_values( { plugin => "nfs-rpc", type => "nfs_procedure", type_instance => "ops", plugin_instance => "",
					values => [ $ops, ], 
		} );

		plugin_dispatch_values( { plugin => "nfs-rpc", type => "nfs_procedure", type_instance => "retrans", plugin_instance => "",
					values => [ $retrans ], 
		} );
	} else {
		return;
	} # end if

	return 1;
} # end sub


1;