Remove support for specifying the port number using "-H example.com:80".
[monitoring-plugins.git] / plugins-scripts / check_mssql.pl
blobfa15476b754babae1d507f072d5d32fe060fc1d3
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
26 # $Id$
30 use DBI;
31 use DBD::Sybase;
32 use Getopt::Long;
33 use lib ".";
34 use utils qw($TIMEOUT %ERRORS &print_revision &support);
35 use strict;
37 my $PROGNAME = "check_mssql";
39 my (
40 $server,$database,$username,$password,$query,$help,$verbose,$timeout,
41 $dbh,$sth,$row,
42 $s,$opt_V,$regex
44 my $exitcode = $ERRORS{'OK'};
46 process_arguments();
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"};
53 alarm($TIMEOUT);
55 unless ($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'});
60 if (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'});
66 $sth = $dbh->prepare($query);
67 unless ($sth->execute()) {
68 printf("SQL CRITICAL: Error in query: $dbh->errstr\n");
69 exit($ERRORS{'CRITICAL'});
72 $row = join(";",$sth->fetchrow_array);
74 $sth->finish;
75 $dbh->disconnect;
77 alarm(0);
78 if (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'};
88 print "SQL OK: $row|$row\n";
89 exit $ERRORS{'OK'};
91 ##################################################
93 sub 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'});
113 sub 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);
127 if (defined $opt_V) {
128 print_revision($PROGNAME,'$Revision$');
129 exit $ERRORS{'OK'};
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));
139 return;