check_mysql.pl 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/nyet/bin/perl
  2. #
  3. # (c)1999 Mitch Wright, NetLine Corporation
  4. # Read the GNU copyright stuff for all the legalese
  5. #
  6. # Check to see that our MySQL server(s) are up and running.
  7. # This plugin requires that mysqladmin(1) is installed on the system.
  8. # Since it is part of the MySQL distribution, that should be a problem.
  9. #
  10. # If no parameters are giving, a usage statement is output.
  11. #
  12. # Exit 0 on success, providing some informational output
  13. # Exit 2 on failure, provide what we can...
  14. #
  15. require 5.004;
  16. sub usage;
  17. my $TIMEOUT = 15;
  18. my $MYSQLADMIN = "/usr/local/bin/mysqladmin";
  19. my %ERRORS = ('UNKNOWN' , '-1',
  20. 'OK' , '0',
  21. 'WARNING', '1',
  22. 'CRITICAL', '2');
  23. my $host = shift || &usage(%ERRORS);
  24. my $user = shift || &usage(%ERRORS);
  25. my $pass = shift || "";
  26. my $warn = shift || 60;
  27. my $crit = shift || 100;
  28. my $state = "OK";
  29. # Just in case of problems, let's not hang Nagios
  30. $SIG{'ALRM'} = sub {
  31. print ("ERROR: No response from MySQL server (alarm)\n");
  32. exit $ERRORS{"UNKNOWN"};
  33. };
  34. alarm($TIMEOUT);
  35. open (OUTPUT,
  36. "$MYSQLADMIN -h $host -u $user --password=\"$pass\" version 2>&1
  37. |");
  38. while (<OUTPUT>) {
  39. if (/failed/) { $state="CRITICAL"; s/.*://; $status=$_; last; }
  40. next if /^\s*$/;
  41. if (/^Server version\s+(\d+.*)/) { $version = $1; next; }
  42. if (/^Uptime:\s+(\d.*)/) { $uptime = $1; next; }
  43. if (/^Threads:\s+(\d+)\s+/) { $threads = $1; next; }
  44. }
  45. $status = "Version $version -- $threads Threads <br>Uptime $uptime" if
  46. $state ne "CRITICAL";
  47. if ($threads >= $warn) { $state = "WARNING"; }
  48. if ($threads >= $crit) { $state = "CRITICAL"; }
  49. print $status;
  50. exit $ERRORS{$state};
  51. sub usage {
  52. print "Required arguments not given!\n\n";
  53. print "MySQL status checker plugin for Nagios, V1.01\n";
  54. print "Copyright (c) 1999-2000 Mitch Wright \n\n";
  55. print "Usage: check_mysql.pl <host> <user> [<pass> [<warn>
  56. [<crit>]]]\n\n"; print " <pass> = password to use for <user> at
  57. <host>\n"; print " <warn> = number of threads to warn us
  58. about\n"; print " <crit> = number of threads to scream at us
  59. about\n"; exit $ERRORS{"UNKNOWN"};
  60. }