4
0

check_mssql.pl 4.3 KB

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