summaryrefslogtreecommitdiffstats
path: root/plugins-scripts/check_mssql.pl
diff options
context:
space:
mode:
Diffstat (limited to 'plugins-scripts/check_mssql.pl')
-rwxr-xr-xplugins-scripts/check_mssql.pl141
1 files changed, 141 insertions, 0 deletions
diff --git a/plugins-scripts/check_mssql.pl b/plugins-scripts/check_mssql.pl
new file mode 100755
index 0000000..fa15476
--- /dev/null
+++ b/plugins-scripts/check_mssql.pl
@@ -0,0 +1,141 @@
1#!/usr/bin/perl -w
2
3#
4# Copyright 2003 Roy Sigurd Karlsbakk
5#
6# Requires freetds and DBD::Sybase
7# http://www.freetds.org
8# http://www.mbay.net/~mpeppler/
9#
10# This program is free software; you can redistribute it and/or
11# modify it under the terms of the GNU General Public License
12# as published by the Free Software Foundation; either version 2
13# of the License, or (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23#
24# Report bugs to: nagiosplug-help@lists.sourceforge.net
25#
26# $Id$
27#
28
29
30use DBI;
31use DBD::Sybase;
32use Getopt::Long;
33use lib ".";
34use utils qw($TIMEOUT %ERRORS &print_revision &support);
35use strict;
36
37my $PROGNAME = "check_mssql";
38
39my (
40 $server,$database,$username,$password,$query,$help,$verbose,$timeout,
41 $dbh,$sth,$row,
42 $s,$opt_V,$regex
43);
44my $exitcode = $ERRORS{'OK'};
45
46process_arguments();
47
48# Just in case of problems, let's not hang Nagios
49$SIG{'ALRM'} = sub {
50 print ("SQL UNKNOWN: ERROR connection $server (alarm timeout)\n");
51 exit $ERRORS{"UNKNOWN"};
52};
53alarm($TIMEOUT);
54
55unless ($dbh = DBI->connect("dbi:Sybase:server=".uc($server), "$username", "$password")) {
56 printf "SQL CRITICAL: Can't connect to mssql server $DBI::errstr\n";
57 exit($ERRORS{'CRITICAL'});
58}
59
60if (defined $database) { # otherwise use default database
61 unless ($dbh->do("use $database")) {
62 printf ("SQL CRITICAL: Can't 'use $database': $dbh->errstr");
63 exit($ERRORS{'CRITICAL'});
64 }
65}
66$sth = $dbh->prepare($query);
67unless ($sth->execute()) {
68 printf("SQL CRITICAL: Error in query: $dbh->errstr\n");
69 exit($ERRORS{'CRITICAL'});
70}
71
72$row = join(";",$sth->fetchrow_array);
73
74$sth->finish;
75$dbh->disconnect;
76
77alarm(0);
78if (defined $regex) {
79 if ($row =~ /$regex/) {
80 printf "SQL CRITICAL: - $row|$row\n";
81 exit $ERRORS{'CRITICAL'};
82 }else{
83 print "SQL OK: $row|$row\n";
84 exit $ERRORS{'OK'};
85 }
86}
87
88print "SQL OK: $row|$row\n";
89exit $ERRORS{'OK'};
90
91##################################################
92
93sub syntax {
94 $s = shift or $s = 'Unknown';
95 printf("Error: ($s)\n") unless ($help);
96 printf("Runs a query against a MS-SQL server or Sybase server and returns the first row\n");
97 printf("Returns an error if no responses are running. Row is passed to perfdata in\n");
98 printf("semicolon delimited format\n");
99 printf("A simple sql statement like \"select getdate()\" verifies server responsiveness\n\n");
100 printf("Syntax: %s -s <server> -d <database> -u <username> -p <password> -q <query> [-v]\n", $PROGNAME);
101 printf(" --database -d Database name\n");
102 printf(" --Hostname -H Server name\n");
103 printf(" --username -u Username\n");
104 printf(" --password -p Password\n");
105 printf(" --query -q SQL query to run\n");
106 printf(" --timeout -t Plugin timeout (default:$TIMEOUT)\n");
107 printf(" --regex -r regex against SQL response(CRIT if MATCH)\n");
108 printf(" --verbose -v verbose\n");
109 printf("\nThe SQL response is concatenated into a string with a \";\" demarkation\n\n");
110 exit($ERRORS{'UNKNOWN'});
111}
112
113sub process_arguments {
114 Getopt::Long::Configure('bundling');
115 my $status = GetOptions
116 ("p=s" => \$password, "password=s" => \$password,
117 "u=s" => \$username, "username=s" => \$username,
118 "H=s" => \$server, "Hostname=s" => \$server,
119 "d=s" => \$database, "database=s" => \$database,
120 "q=s" => \$query, "query=s" => \$query,
121 "t=i" => \$timeout, "timeout=i" => \$timeout,
122 "r=s" => \$regex, "regex=s" => \$regex,
123 "h" => \$help, "help" => \$help,
124 "v" => \$verbose, "verbose" => \$verbose,
125 "V" => \$opt_V, "version" => \$opt_V);
126
127 if (defined $opt_V) {
128 print_revision($PROGNAME,'$Revision$');
129 exit $ERRORS{'OK'};
130 }
131
132 syntax("Help:") if ($help);
133 syntax("Missing username") unless (defined($username));
134 syntax("Missing password") unless (defined($password));
135 syntax("Missing server") unless (defined($server));
136 syntax("Missing query string") unless (defined($query));
137 $timeout = $TIMEOUT unless (defined($timeout));
138
139 return;
140
141}