check_mssql.pl 4.2 KB

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