check_mssql.pl 4.2 KB

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