4
0

check_mssql.pl 4.3 KB

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