Merge pull request #1311 from waja/check_ntp_remove_unused_variables
[monitoring-plugins.git] / plugins-scripts / check_mssql.pl
blobfb3952d47366b3d48b9043bda0e96ed6ffeca39a
1 #!@PERL@ -w
4 # Copyright 2003 Roy Sigurd Karlsbakk
6 # Requires freetds and DBD::Sybase
7 # http://www.freetds.org
8 # http://www.mbay.net/~mpeppler/
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301, USA.
24 # Report bugs to: help@monitoring-plugins.org
29 use DBI;
30 use DBD::Sybase;
31 use Getopt::Long;
32 use FindBin;
33 use lib "$FindBin::Bin";
34 use utils qw($TIMEOUT %ERRORS &print_revision &support);
35 use strict;
37 my $PROGNAME = "check_mssql";
39 $ENV{'PATH'}='@TRUSTED_PATH@';
40 $ENV{'BASH_ENV'}='';
41 $ENV{'ENV'}='';
43 my (
44 $server,$database,$username,$password,$query,$help,$verbose,$timeout,
45 $dbh,$sth,$row,
46 $s,$opt_V,$regex
48 my $exitcode = $ERRORS{'OK'};
50 process_arguments();
52 # Just in case of problems, let's not hang the monitoring system
53 $SIG{'ALRM'} = sub {
54 print ("SQL UNKNOWN: ERROR connection $server (alarm timeout)\n");
55 exit $ERRORS{"UNKNOWN"};
57 alarm($TIMEOUT);
59 unless ($dbh = DBI->connect("dbi:Sybase:server=".uc($server), "$username", "$password")) {
60 printf "SQL CRITICAL: Can't connect to mssql server $DBI::errstr\n";
61 exit($ERRORS{'CRITICAL'});
64 if (defined $database) { # otherwise use default database
65 unless ($dbh->do("use $database")) {
66 printf ("SQL CRITICAL: Can't 'use $database': $dbh->errstr");
67 exit($ERRORS{'CRITICAL'});
70 $sth = $dbh->prepare($query);
71 unless ($sth->execute()) {
72 printf("SQL CRITICAL: Error in query: $dbh->errstr\n");
73 exit($ERRORS{'CRITICAL'});
76 $row = join(";",$sth->fetchrow_array);
78 $sth->finish;
79 $dbh->disconnect;
81 alarm(0);
82 if (defined $regex) {
83 if ($row =~ /$regex/) {
84 printf "SQL CRITICAL: - $row|$row\n";
85 exit $ERRORS{'CRITICAL'};
86 }else{
87 print "SQL OK: $row|$row\n";
88 exit $ERRORS{'OK'};
92 print "SQL OK: $row|$row\n";
93 exit $ERRORS{'OK'};
95 ##################################################
97 sub syntax {
98 $s = shift or $s = 'Unknown';
99 printf("Error: ($s)\n") unless ($help);
100 printf("Runs a query against a MS-SQL server or Sybase server and returns the first row\n");
101 printf("Returns an error if no responses are running. Row is passed to perfdata in\n");
102 printf("semicolon delimited format\n");
103 printf("A simple sql statement like \"select getdate()\" verifies server responsiveness\n\n");
104 printf("Syntax: %s -s <server> -d <database> -u <username> -p <password> -q <query> [-v]\n", $PROGNAME);
105 printf(" --database -d Database name\n");
106 printf(" --Hostname -H Server name\n");
107 printf(" --username -u Username\n");
108 printf(" --password -p Password\n");
109 printf(" --query -q SQL query to run\n");
110 printf(" --timeout -t Plugin timeout (default:$TIMEOUT)\n");
111 printf(" --regex -r regex against SQL response(CRIT if MATCH)\n");
112 printf(" --verbose -v verbose\n");
113 printf("\nThe SQL response is concatenated into a string with a \";\" demarkation\n\n");
114 exit($ERRORS{'UNKNOWN'});
117 sub process_arguments {
118 Getopt::Long::Configure('bundling');
119 my $status = GetOptions
120 ("p=s" => \$password, "password=s" => \$password,
121 "u=s" => \$username, "username=s" => \$username,
122 "H=s" => \$server, "Hostname=s" => \$server,
123 "d=s" => \$database, "database=s" => \$database,
124 "q=s" => \$query, "query=s" => \$query,
125 "t=i" => \$timeout, "timeout=i" => \$timeout,
126 "r=s" => \$regex, "regex=s" => \$regex,
127 "h" => \$help, "help" => \$help,
128 "v" => \$verbose, "verbose" => \$verbose,
129 "V" => \$opt_V, "version" => \$opt_V);
131 if (defined $opt_V) {
132 print_revision($PROGNAME,'@NP_VERSION@');
133 exit $ERRORS{'UNKNOWN'};
136 syntax("Help:") if ($help);
137 syntax("Missing username") unless (defined($username));
138 syntax("Missing password") unless (defined($password));
139 syntax("Missing server") unless (defined($server));
140 syntax("Missing query string") unless (defined($query));
141 $timeout = $TIMEOUT unless (defined($timeout));
143 return;