tinderbox_build: Add --enable-libtap configure option
[monitoring-plugins.git] / plugins-scripts / check_mssql.pl
blob9a8fc5117195457ffca41e5534e8b9e02a96f9c5
1 #!/usr/bin/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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 # Report bugs to: nagiosplug-help@lists.sourceforge.net
29 use DBI;
30 use DBD::Sybase;
31 use Getopt::Long;
32 use lib ".";
33 use utils qw($TIMEOUT %ERRORS &print_revision &support);
34 use strict;
36 my $PROGNAME = "check_mssql";
38 my (
39 $server,$database,$username,$password,$query,$help,$verbose,$timeout,
40 $dbh,$sth,$row,
41 $s,$opt_V,$regex
43 my $exitcode = $ERRORS{'OK'};
45 process_arguments();
47 # Just in case of problems, let's not hang Nagios
48 $SIG{'ALRM'} = sub {
49 print ("SQL UNKNOWN: ERROR connection $server (alarm timeout)\n");
50 exit $ERRORS{"UNKNOWN"};
52 alarm($TIMEOUT);
54 unless ($dbh = DBI->connect("dbi:Sybase:server=".uc($server), "$username", "$password")) {
55 printf "SQL CRITICAL: Can't connect to mssql server $DBI::errstr\n";
56 exit($ERRORS{'CRITICAL'});
59 if (defined $database) { # otherwise use default database
60 unless ($dbh->do("use $database")) {
61 printf ("SQL CRITICAL: Can't 'use $database': $dbh->errstr");
62 exit($ERRORS{'CRITICAL'});
65 $sth = $dbh->prepare($query);
66 unless ($sth->execute()) {
67 printf("SQL CRITICAL: Error in query: $dbh->errstr\n");
68 exit($ERRORS{'CRITICAL'});
71 $row = join(";",$sth->fetchrow_array);
73 $sth->finish;
74 $dbh->disconnect;
76 alarm(0);
77 if (defined $regex) {
78 if ($row =~ /$regex/) {
79 printf "SQL CRITICAL: - $row|$row\n";
80 exit $ERRORS{'CRITICAL'};
81 }else{
82 print "SQL OK: $row|$row\n";
83 exit $ERRORS{'OK'};
87 print "SQL OK: $row|$row\n";
88 exit $ERRORS{'OK'};
90 ##################################################
92 sub syntax {
93 $s = shift or $s = 'Unknown';
94 printf("Error: ($s)\n") unless ($help);
95 printf("Runs a query against a MS-SQL server or Sybase server and returns the first row\n");
96 printf("Returns an error if no responses are running. Row is passed to perfdata in\n");
97 printf("semicolon delimited format\n");
98 printf("A simple sql statement like \"select getdate()\" verifies server responsiveness\n\n");
99 printf("Syntax: %s -s <server> -d <database> -u <username> -p <password> -q <query> [-v]\n", $PROGNAME);
100 printf(" --database -d Database name\n");
101 printf(" --Hostname -H Server name\n");
102 printf(" --username -u Username\n");
103 printf(" --password -p Password\n");
104 printf(" --query -q SQL query to run\n");
105 printf(" --timeout -t Plugin timeout (default:$TIMEOUT)\n");
106 printf(" --regex -r regex against SQL response(CRIT if MATCH)\n");
107 printf(" --verbose -v verbose\n");
108 printf("\nThe SQL response is concatenated into a string with a \";\" demarkation\n\n");
109 exit($ERRORS{'UNKNOWN'});
112 sub process_arguments {
113 Getopt::Long::Configure('bundling');
114 my $status = GetOptions
115 ("p=s" => \$password, "password=s" => \$password,
116 "u=s" => \$username, "username=s" => \$username,
117 "H=s" => \$server, "Hostname=s" => \$server,
118 "d=s" => \$database, "database=s" => \$database,
119 "q=s" => \$query, "query=s" => \$query,
120 "t=i" => \$timeout, "timeout=i" => \$timeout,
121 "r=s" => \$regex, "regex=s" => \$regex,
122 "h" => \$help, "help" => \$help,
123 "v" => \$verbose, "verbose" => \$verbose,
124 "V" => \$opt_V, "version" => \$opt_V);
126 if (defined $opt_V) {
127 print_revision($PROGNAME,'@NP_VERSION@');
128 exit $ERRORS{'OK'};
131 syntax("Help:") if ($help);
132 syntax("Missing username") unless (defined($username));
133 syntax("Missing password") unless (defined($password));
134 syntax("Missing server") unless (defined($server));
135 syntax("Missing query string") unless (defined($query));
136 $timeout = $TIMEOUT unless (defined($timeout));
138 return;