#!/usr/bin/perl -w
#
# check_mssql_dbsize 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 3 of the License, or
# (at your option) any later version.
#
# check_mssql_dbsize 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, see <http://www.gnu.org/licenses/>.
#
#
# This script will check MSSQL Database size via check_nrpe and NSclient. returns performance data in Nagios format
# Author Pall Sigurdsson <palli@opensource.is>
#

use strict;
use Nagios::Plugin;


my $np = Nagios::Plugin->new(
	usage => "Usage: %s <hostname>" );

$np->add_arg(
	spec => 'debug|d=i',
	help => '-d, --debug=INTEGER',
);

$np->getopts;

my $NRPECMD = "/usr/lib/nagios/plugins/check_nrpe";

if (@ARGV < 1) {
	usage();
	exit 3;
}

my $HOSTNAME=$ARGV[0];


my $databases = nrpeexec("-H $HOSTNAME -t 60 -c listCounterInstances -a 'SQLServer:Databases'");
my @array1 = split(/\,/, $databases);


my $num_databases = 0;
foreach my $database (@array1)
{
	# Strip whitespace
	$database =~ s/^\s*(.*?)\s*$/$1/;

	# Call check_nrpe
	my $dbSize = nrpeexec("-H $HOSTNAME -t 60 -c CheckCounter -a 'Counter:$database=\\SQLServer:Databases($database)\\Data File(s) Size (KB)'");

	# Strip everything but the performance data
	$dbSize =~ s/^.*\|(.*?)$/$1/;
	chomp($dbSize);
	$np->add_perfdata($dbSize);
	$num_databases = $num_databases + 1;
}

$np->nagios_exit( OK, "$num_databases databases found in $HOSTNAME");



sub usage {
	print <<"	EOUSAGE";
Usage $0 <hostname>
	EOUSAGE
}



# Execute NRPE with some error handling
sub nrpeexec {
	my @args = @_;

	my $output = '';
	if (open NRPE, "$NRPECMD " . join(' ',@args) . ' 2>&1 |') {
		$output .= $_ while(<NRPE>);
		close NRPE;
	}
	my $ret = $? >> 8;
	# No such file or directory
	if ($ret == 127) {
		$np->nagios_die("Cannot execute $NRPECMD command missing");
	# Some other error
	} elsif ($ret != 0) {
		$np->nagios_die("Cannot execute $NRPECMD: $!");
	}
	return $output;
}