aboutsummaryrefslogtreecommitdiff
path: root/plugins-scripts/check_mssql.pl
blob: bf3a651fde94e7f141b3caff1c382751d587a36e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!@PERL@ -w

#
# Copyright 2003 Roy Sigurd Karlsbakk
#
# Requires freetds and DBD::Sybase 
# http://www.freetds.org 
# http://www.mbay.net/~mpeppler/
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301, USA.
#
# Report bugs to: help@monitoring-plugins.org
# 
#


use DBI;
use DBD::Sybase;
use Getopt::Long;
use FindBin;
use lib "$FindBin::Bin";
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use strict;

my $PROGNAME = "check_mssql";

$ENV{'PATH'}='@TRUSTED_PATH@';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';

my (
	$server,$database,$username,$password,$query,$help,$verbose,$timeout,
	$dbh,$sth,$row,
	$s,$opt_V,$regex
);
my $exitcode = $ERRORS{'OK'};

process_arguments();

# Just in case of problems, let's not hang the monitoring system
$SIG{'ALRM'} = sub {
     print ("SQL UNKNOWN: ERROR connection $server (alarm timeout)\n");
     exit $ERRORS{"UNKNOWN"};
};
alarm($timeout);

unless ($dbh = DBI->connect("dbi:Sybase:server=".uc($server), "$username", "$password")) {
	printf "SQL CRITICAL: Can't connect to mssql server $DBI::errstr\n";
	exit($ERRORS{'CRITICAL'});
}

if (defined $database) {  # otherwise use default database
	unless ($dbh->do("use $database")) {
		printf ("SQL CRITICAL: Can't 'use $database': $dbh->errstr");
		exit($ERRORS{'CRITICAL'});
	}
}
$sth = $dbh->prepare($query);
unless ($sth->execute()) {
	printf("SQL CRITICAL: Error in query: $dbh->errstr\n");
	exit($ERRORS{'CRITICAL'});
}

$row = join(";",$sth->fetchrow_array);

$sth->finish;
$dbh->disconnect;

alarm(0);
if (defined $regex) {
	if ($row =~ /$regex/) {
		printf "SQL CRITICAL: - $row|$row\n";
		exit $ERRORS{'CRITICAL'};
	}else{
		print "SQL OK: $row|$row\n";
		exit $ERRORS{'OK'};
	}
}

print "SQL OK: $row|$row\n";
exit $ERRORS{'OK'};

##################################################

sub syntax {
	$s = shift or $s = 'Unknown';
	printf("Error: ($s)\n") unless ($help);
	printf("Runs a query against a MS-SQL server or Sybase server and returns the first row\n");
	printf("Returns an error if no responses are running. Row is passed to perfdata	in\n");
	printf("semicolon delimited format\n");
	printf("A simple sql statement like \"select getdate()\" verifies server responsiveness\n\n");
	printf("Syntax: %s -s <server> -d <database> -u <username> -p <password> -q <query>	[-v]\n", $PROGNAME);
	printf("  --database -d		Database name\n");
	printf("  --Hostname -H		Server name\n");
	printf("  --username -u		Username\n");
	printf("  --password -p		Password\n");
	printf("  --query -q		SQL query to run\n");
	printf("  --timeout -t		Plugin timeout (default:$TIMEOUT)\n");
	printf("  --regex -r		regex against SQL response(CRIT if MATCH)\n");
	printf("  --verbose -v		verbose\n");
	printf("\nThe SQL response is concatenated into a string with a \";\" demarkation\n\n");
	exit($ERRORS{'UNKNOWN'});
}

sub process_arguments {
	Getopt::Long::Configure('bundling');
	my $status = GetOptions
		("p=s" => \$password, "password=s" => \$password,
		 "u=s" => \$username, "username=s" => \$username,
		 "H=s" => \$server, "Hostname=s"    => \$server,
		 "d=s" => \$database, "database=s" => \$database,
		 "q=s" => \$query, "query=s" => \$query,
		 "t=i" => \$timeout, "timeout=i" => \$timeout,
		 "r=s" => \$regex, "regex=s" => \$regex,
		 "h" => \$help, "help" => \$help,
		 "v" => \$verbose, "verbose" => \$verbose,
		 "V" => \$opt_V, "version" => \$opt_V);

	if (defined $opt_V) {
		print_revision($PROGNAME,'@NP_VERSION@');
		exit $ERRORS{'UNKNOWN'};
	}

	syntax("Help:") if ($help);
	syntax("Missing username") unless (defined($username));
	syntax("Missing password") unless (defined($password));
	syntax("Missing server") unless (defined($server));
	syntax("Missing query string") unless (defined($query));
	$timeout = $TIMEOUT unless (defined($timeout));
	
	return;

}