Hi Tim,<br><br>Your information seems confusing, e.g. you use different parameters (and in a different order) on the command line as in the command definition.<br>Also on the command line you connect to "db1" while on the web interface 192.168.1.32 is returned. <br>
<br>If you just want to do a basic check of mysql, why don't you use check_mysql ?<br><a href="http://nagiosplugins.org/man/check_mysql">http://nagiosplugins.org/man/check_mysql</a><br><br>If you insist of using <a href="http://check_mysqld.pl">check_mysqld.pl</a>, no problem with that, but make additional tests<br>
- with your nagios user (not root)<br>- use the EXACT same way in the command definition as you launch it on the command line<br>- verify that db1 and 192.168.1.32 is the same host<br><br>The return code/output should be the same.<br>
<br><div class="gmail_quote">On Sun, Oct 9, 2011 at 12:15 AM, Tim Dunphy <span dir="ltr"><<a href="mailto:bluethundr@jokefire.com">bluethundr@jokefire.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello list<br>
<br>
I'm trying to get another check running that is again succeding on the command line but not from within the nagios web interface. this time I am trying to setup a basic check for mysql:<br>
<br>
In the nagios web interface I see this:<br>
<br>
<br>
CRITICAL ERROR - Unable to connect to database '' on server '192.168.1.32' on port 3306 with user 'mysqladmin' - Access denied for user 'mysqladmin'@'<a href="http://VIRTCENT11.summitnjhome.com" target="_blank">VIRTCENT11.summitnjhome.com</a>' (using password: YES)<br>
<br>
<br>
The check works ok from the command line<br>
<br>
[root@VIRTCENT11:/usr/local/nagios/etc/objects] #/usr/local/nagios/libexec/<a href="http://check_mysqld.pl" target="_blank">check_mysqld.pl</a> -p secret -f -u admin -a -H db1<br>
MYSQL 5.5.16-log OK<br>
<br>
In the commands.cfg file I have the command defined as such:<br>
<br>
define command{<br>
command_name check_mysqld<br>
command_line $USER1$/<a href="http://check_mysqld.pl" target="_blank">check_mysqld.pl</a> -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$<br>
}<br>
<br>
In the file that defines the database hosts I have the service defined like this:<br>
<br>
define service {<br>
use generic-service<br>
hostgroup_name db-servers<br>
service_description MYSQLD<br>
check_command check_mysqld!mysqladmin!secret<br>
}<br>
<br>
<br>
<br>
I can also connect to the database from the monitoring host as the account listed in the service definition.<br>
<br>
[root@VIRTCENT11:/usr/local/icinga/etc] #mysql -uadmin -psecret -h db1<br>
Welcome to the MySQL monitor. Commands end with ; or \g.<br>
Your MySQL connection id is 4261<br>
Server version: 5.5.16-log MySQL Community Server (GPL) by Remi<br>
<br>
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.<br>
<br>
Oracle is a registered trademark of Oracle Corporation and/or its<br>
affiliates. Other names may be trademarks of their respective<br>
owners.<br>
<br>
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.<br>
<br>
mysql><br>
<br>
<br>
This is the perl script that functions as check_mysqld<br>
<br>
<br>
#!/usr/bin/perl -w<br>
#<br>
# ============================== SUMMARY =====================================<br>
#<br>
# Program : <a href="http://check_mysqld.pl" target="_blank">check_mysqld.pl</a><br>
# Version : 0.902<br>
# Date : Jan 08, 2008<br>
# Author : William Leibzon - <a href="mailto:william@leibzon.org">william@leibzon.org</a><br>
# Licence : GPL - summary below, full text at <a href="http://www.fsf.org/licenses/gpl.txt" target="_blank">http://www.fsf.org/licenses/gpl.txt</a><br>
#<br>
# =========================== PROGRAM LICENSE =================================<br>
#<br>
# This program is free software; you can redistribute it and/or modify<br>
# it under the terms of the GNU General Public License as published by<br>
# the Free Software Foundation; either version 2 of the License, or<br>
# (at your option) any later version.<br>
#<br>
# This program is distributed in the hope that it will be useful,<br>
# but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>
# GNU General Public License for more details.<br>
#<br>
# You should have received a copy of the GNU General Public License<br>
# along with this program; if not, write to the Free Software<br>
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.<br>
#<br>
# ===================== INFORMATION ABOUT THIS PLUGIN =========================<br>
#<br>
# This MYSQL check plugin issues "SHOW STATUS" commmand on mysql server<br>
# and can issue alerts if selected parameters are above/below given number<br>
# It also returns perfomance data for further nagios 2.0 post-processing<br>
#<br>
# This program is written and maintained by:<br>
# William Leibzon - william(at)<a href="http://leibzon.org" target="_blank">leibzon.org</a><br>
# It is partially based on (more like inspired by) previous work by:<br>
# Matthew Kent (matt at <a href="http://bravenet.com" target="_blank">bravenet.com</a>) - added perfomance data from mysqladmin<br>
# Mitch Wright - original <a href="http://check_mysql.pl" target="_blank">check_mysql.pl</a> plugin using mysqladmin command<br>
#<br>
# ============================= SETUP NOTES ====================================<br>
#<br>
# 1. Make sure to check and if necessary adjust the the path to <a href="http://utils.pm" target="_blank">utils.pm</a><br>
# 2. Make sure to install perl DBI and mysql DBD modules<br>
#<br>
# It is recommended you use arguments to pass on all options to this plugin,<br>
# but if necessary you can change default MySQL hostname, port, user, password<br>
# variables at the start of the script code<br>
#<br>
# The attributes/parameters checked are mysql internal variables retuned<br>
# from "SHOW STATUS" or "SHOW GLOBAL STATUS".<br>
# For each attribute to be checked which you specify in '-a' you must<br>
# also specify warning and critical threshold value in the same order<br>
# in the '-w' and '-c' options (see below for more details).<br>
#<br>
# Using '-f' option causes all attribute values to be available for Nagios<br>
# performance processing. You may also directly specify which variables to be<br>
# return as performance data and with '-A' option. Special value of '*' as in<br>
# '-A *' allows to get all variables from 'SHOW STATUS' as performance data.<br>
#<br>
# Warning and critical thresholds are specified with '-w' and '-c' and each<br>
# one must have exact same number of values to be checked (separated by ',')<br>
# as number of variables specified with '-a'. Any values you dont want to<br>
# compare you specify as ~ (or just not specify a value, i.e. ',,').<br>
#<br>
# There are also number of other one-letter modifiers that can be used<br>
# as prefix before actual data value to direct how data is to be checked.<br>
# These prefixes are as follows:<br>
# > : issue alert if data is above this value (default for numeric value)<br>
# < : issue alert if data is below this value (must be followed by number)<br>
# = : issue alert if data is equal to this value (default for non-numeric)<br>
# ! : issue alert if data is NOT equal to this value<br>
#<br>
# Additionally supported are two specifications of range formats:<br>
# number1:number2 issue alert if data is OUTSIDE of range [number1..number2]<br>
# i.e. alert if data<$number1 or data>$number2<br>
# @number1:number2 issue alert if data is WITHIN range [number1..number2]<br>
# i.e. alert if data>=$number and $data<=$number2<br>
#<br>
# A special modifier '^' can also be used to disable checking that warn values<br>
# are less then (or greater then) critical values (it is rarely needed).<br>
# A quick example of specialized use is '--warn=^<100 --crit=>200' which means<br>
# warning alert if value is < 100 and critical alert if its greater then 200.<br>
#<br>
# You can specify more then one type of threshold for the same variable -<br>
# simply repeat the variable more then once in the list in '-a'<br>
#<br>
# --------------------------------------------------------------------------<br>
#<br>
# If you're using version 5.02 or newer of mysql server then this plugin<br>
# will do "SHOW GLOBAL STATUS" rather then "SHOW STATUS". For more information<br>
# on differences see:<br>
# <a href="http://dev.mysql.com/doc/refman/5.0/en/server-status-variables.html" target="_blank">http://dev.mysql.com/doc/refman/5.0/en/server-status-variables.html</a><br>
#<br>
# Note that it maybe the case that you do actually want "SHOW STATUS" even with<br>
# mysqld 5.0.2, then specify query line with '-q' option like<br>
# ./<a href="http://check_mysqld.pl" target="_blank">check_mysqld.pl</a> -p foo -f -u nagios -A uptime,threads_connected,slow_queries,open_tables -H nagios -q 'SHOW STATUS'<br>
#<br>
# Sample command and service definitions:<br>
#<br>
# define command{<br>
# command_name check_mysqld<br>
# command_line $USER1$/<a href="http://check_mysqld.pl" target="_blank">check_mysqld.pl</a> -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$ -a uptime,threads_connected,questions,slow_queries,open_tables -w ",,,," -c ",,,,"<br>
# }<br>
#<br>
# define service {<br>
# use service-critical<br>
# hostgroup_name mysql<br>
# service_description MYSQLD<br>
# check_command check_mysqld!foo!apples<br>
# }<br>
#<br>
# Examples of command-line use:<br>
# /usr/lib/nagios/plugins/<a href="http://check_mysqld.pl" target="_blank">check_mysqld.pl</a> -v<br>
# /usr/lib/nagios/plugins/<a href="http://check_mysqld.pl" target="_blank">check_mysqld.pl</a> -p foo -f -u nagios -a uptime,threads_connected,questions,slow_queries,open_tables -A threads_running,innodb_row_lock_time_avg -w ",,,," -c ",,,,>25" -H nagios -v<br>
#<br>
# ======================= VERSION HISTORY and TODO ========================<br>
#<br>
# This version history is incomplete as I only started keeping track of<br>
# it as such separate section recently. The first public release of<br>
# the plugin was version 0.7 from May 2006<br>
#<br>
# [0.8 DONE - Dec 2007] Update to my latest format & code for parsing<br>
# warning & critical parameters so they can be of the form<br>
# "<value" (alert if value is less then specified),<br>
# ">value" (alert if its greatr then specified, default)<br>
# as well as "1,~,1" or '1,,1' (ignore 2nd)<br>
# Note: for backward compatibility specifying '0' for threshold is<br>
# more or less same as '~', if you really want 0 then prefix<br>
# it with '<' or '>' or '=' to force the check<br>
# Number of other code cleanup and fixes. This includes allowing<br>
# more then one threshold for same variable (repeat the variable in<br>
# the list of variables with 'a') but making sure that in performance<br>
# variables its listed only once and in proper order.<br>
# [0.85 - Dec 2007] Thanks to the suggestion by Mike Lykov plugin<br>
# can now do 'SHOW GLOBAL STATUS' since mysqld newer then 5.0.2<br>
# will only report one session data in 'SHOW STATUS'. Also in<br>
# part to allow override default behavior (choosing which SHOW<br>
# to do) and to extend plugin functionality a new option '-q'<br>
# is added that allows to specify which query to execute. This<br>
# will let plugin be used with others types of mysql 'SHOW' commands<br>
# [0.851 - Dec 2007] Fixed bug: -t (timeout) parameter was ignored before<br>
# [0.9 - Jan 2008] Threshold parsing and check code has been rewritten<br>
# and now supports ranges in similar way to nagios<br>
# plugin specification.<br>
# [0.901 - Jan 2008] Added forced closing of db connection at alarm<br>
# [0.902 - Jan 2008] Bug fixed in comparison of non-numeric data<br>
#<br>
# TODO or consider for future:<br>
# 1. Add support for storing values in a file or reusing old performance<br>
# values passed as command-line parameters (as I did with check_megaraid<br>
# check_megaraid and check_snmp_netint) and allow to check on rate of<br>
# change rather then actual value.<br>
#<br>
# ========================== START OF PROGRAM CODE ===========================<br>
<br>
use strict;<br>
<br>
# default mysql hostname, port, database, user and password, see NOTES above<br>
my $HOSTNAME= 'localhost';<br>
my $PORT= 3306;<br>
my $DATABASE= '';<br>
my $USERNAME= 'mysql';<br>
my $PASSWORD= '';<br>
<br>
# Add path to additional libraries if necessary<br>
use lib '/mysql/modules';<br>
use lib '/usr/lib/nagios/plugins';<br>
use utils qw(%ERRORS $TIMEOUT);<br>
<br>
# uncomment two lines below and comment out two above lines if you do not have nagios' <a href="http://utils.pm" target="_blank">utils.pm</a><br>
# my $TIMEOUT = 20;<br>
# my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);<br>
<br>
# ============= MAIN PROGRAM CODE - DO NOT MODIFY BELOW THIS LINE ==============<br>
<br>
use Getopt::Long qw(:config no_ignore_case);<br>
use DBI;<br>
<br>
my $o_host= undef; # hostname<br>
my $o_port= undef; # port<br>
my $o_dbname= undef; # database<br>
my $o_login= undef; # Database user<br>
my $o_passwd= undef; # Password<br>
my $o_help= undef; # help option<br>
my $o_verb= undef; # verbose mode<br>
my $o_version= undef; # version info option<br>
my $o_variables=undef; # list of variables for warn and critical<br>
my @o_varsL= (); # array from above list<br>
my $o_perfvars= undef; # list of variables to include in perfomance data<br>
my @o_perfvarsL=(); # array from above list<br>
my $o_warn= undef; # warning level option<br>
my @o_warnL= (); # array of warn data processing threshold<br>
my $o_crit= undef; # Critical level option<br>
my @o_critL= (); # array of critical data processing thresholds<br>
my $o_perf= undef; # Performance data option<br>
my $o_timeout= undef; # Timeout to use - note that normally timeout is take from nagios anyway<br>
my $o_replication=undef; # Normal replication status value, if something else then you'll see CRITICAL error<br>
my $o_slave= undef; # Normal slave status, if something else then you'll see CRITICAL error<br>
my $o_query= undef; # Query to execute instead of default SHOW STATUS<br>
<br>
my $Version='0.9';<br>
my $dbh= undef; # DB connection object<br>
<br>
sub p_version { print "check_mysqld version : $Version\n"; }<br>
<br>
sub print_usage {<br>
print "Usage: $0 [-v] [-H <host> [-P <port>]] [-u <username>] [-q <query>] [-p <password>] [-a <mysql variables> -w <variables warning thresholdz> -c <variables critical thresholds>] [-A <performance output variables>] [-s <expected slave status>] [-r <expected replication status>] [-f] [-t <timeout>] [-V]\n";<br>
}<br>
<br>
sub help {<br>
print "\nMySQL Database Monitor for Nagios version ",$Version,"\n";<br>
print " by William Leibzon - william(at)<a href="http://leibzon.org" target="_blank">leibzon.org</a>\n\n";<br>
print "This monitoring script connects to database, does 'SELECT VERSION' and then\n";<br>
print "'SHOW STATUS' or 'SHOW GLOBAL STATUS' (for version 5.0.2 or newer of mysql)\n";<br>
print "or other query you specify with '-q'. It then allows to select variables\n";<br>
print "from 'SHOW STATUS' and check them against critical and warning thresholds(s)\n";<br>
print "Status data can also be used for performance output.\n\n";<br>
print_usage();<br>
print <<EOT;<br>
-v, --verbose<br>
print extra debugging information<br>
-h, --help<br>
Print this detailed help screen<br>
-H, --hostname=ADDRESS<br>
Hostname or IP Address to check<br>
-P, --port=INTEGER<br>
MySQL port number (default: 3306)<br>
-D, --database=STRING<br>
Database name to login to<br>
(Default is none and you almost never need to change this)<br>
-u, --username=STRING<br>
Connect using the indicated username (Default is 'mysql')<br>
-p, --password=STRING<br>
Use the indicated password to authenticate the connection<br>
(Default is empty password)<br>
==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==<br>
Your clear-text password will be visible as a process table entry<br>
-p, --password=PASSWD<br>
Password to use when connecting to database (Default is empty password)<br>
-q, --query=STRING<br>
Specify query to do instead of 'SHOW STATUS'. This is useful to check<br>
some other specialized mysql commands and tables. In order to work<br>
returned data should be similar to one from "SHOW STATUS", i.e.<br>
table with two columns.<br>
-a, --variables=STRING,[STRING,[STRING...]]<br>
List of variables as found in 'SHOW STATUS' which should be monitored.<br>
The list can be arbitrarily long and the default (if option is not used)<br>
is not to monitor any variable. You can repeat same variable if you need<br>
it checked for both below and above thresholds.<br>
-w, --warn=STR[,STR[,STR[..]]]<br>
This option can only be used if '--variables' (or '-a') option above<br>
is used and number of values listed here must exactly match number<br>
of variables specified with '-a'. The values specify warning threshold<br>
for when Nagios should send WARNING alert. These values are usually<br>
numbers and can have the following prefix modifiers:<br>
> - warn if data is above this value (default for numeric values)<br>
< - warn if data is below this value (must be followed by number)<br>
= - warn if data is equal to this value (default for non-numeric values)<br>
! - warn if data is not equal to this value<br>
~ - do not check this data (must not be followed by number)<br>
^ - for numeric values this disables check that warning < critical<br>
Threshold values can also be specified as range in two forms:<br>
num1:num2 - warn if data is outside range i.e. if data<num1 or data>num2<br>
\@num1:num2 - warn if data is in range i.e. data>=num1 && data<=num2<br>
-c, --crit=STR[,STR[,STR[..]]]<br>
This option can only be used if '--variables' (or '-a') option above<br>
is used and number of values listed here must exactly match number of<br>
variables specified with '-a'. The values specify critical threshold<br>
for when Nagios should send CRITICAL alert. The format is exactly same<br>
as with -w option except no '^' prefix.<br>
-s, --slave=status<br>
If slave status (normally it is 'OFF') is anything other then what is<br>
specified with, then CRITICAL alert would be sent. This can also be done<br>
with '=' option so seperate option is kept for backward compatibility<br>
-r, --replication=status<br>
If replication status (normally it is NULL) is anything other then what<br>
is specified with this option, then CRITICAL alert would be sent.<br>
-f, --perfparse<br>
This should only be used with '-a', '-s' or '-r' and causes to output<br>
variable data not only as part of main status line but also as<br>
perfparse compatible output (for graphing, etc).<br>
-A, --perfvars=STRING,[STRING,[STRING...]]<br>
This allows to list variables which values will go only into perfparse<br>
output (and not for threshold checking). A special value of '*' allows<br>
to output all variables from 'SHOW STATUS' or 'SHOW GLOBAL STATUS'.<br>
-V, --version<br>
Prints version number<br>
EOT<br>
print "\nThere are no required arguments. By default, the local 'mysql' database";<br>
print "from a server listening on MySQL standard port 3306 will be checked\n\n";<br>
}<br>
<br>
# For verbose output<br>
sub verb { my $t=shift; print $t,"\n" if defined($o_verb) ; }<br>
<br>
# Return true if arg is a number<br>
sub isnum {<br>
my $num = shift;<br>
if (defined($num) && $num =~ /^[-|+]?((\d+\.?\d*)|(^\.\d+))$/ ) { return 1 ;}<br>
return 0;<br>
}<br>
<br>
# help function used when checking data against critical and warn values<br>
sub check_threshold {<br>
my ($attrib, $data, $th_array) = @_;<br>
my $mod = $th_array->[0];<br>
my $lv1 = $th_array->[1];<br>
my $lv2 = $th_array->[2];<br>
<br>
# verb("debug check_threshold: $mod : ".(defined($lv1)?$lv1:'')." : ".(defined($lv2)?$lv2:''));<br>
return "" if !defined($lv1) || ($mod eq '' && $lv1 eq '');<br>
return " " . $attrib . " is " . $data . " = " . $lv1 if $mod eq '=' && $data eq $lv1;<br>
return " " . $attrib . " is " . $data . " != " . $lv1 if $mod eq '!' && $data ne $lv1;<br>
return " " . $attrib . " is " . $data . " > " . $lv1 if $mod eq '>' && $data>$lv1;<br>
return " " . $attrib . " is " . $data . " >= " . $lv1 if $mod eq '>=' && $data>=$lv1;<br>
return " " . $attrib . " is " . $data . " < " . $lv1 if $mod eq '<' && $data<$lv1;<br>
return " " . $attrib . " is " . $data . " <= " . $lv1 if $mod eq '<=' && $data<=$lv1;<br>
return " " . $attrib . " is " . $data . " outside range $lv1 .. $lv2" if $mod eq ':' && ($data<$lv1 || $data>$lv2);<br>
return " " . $attrib . " is " . $data . " in range $lv1..$lv2" if $mod eq '@' && $data>=$lv1 && $data<=$lv2;<br>
return "";<br>
}<br>
<br>
# this is a help function called when parsing threshold options data<br>
sub parse_threshold {<br>
my ($thin) = @_;<br>
<br>
# link to an array that holds processed threshold data<br>
# array: 1st is type of check, 2nd is value2, 3rd is value2, 4th is option<br>
my $th_array = [ '', undef, undef, '' ];<br>
my $th = $thin;<br>
my $at = '';<br>
<br>
$at = $1 if $th =~ s/^(\^?[@|>|<|=|!]?~?)//; # check mostly for my own threshold format<br>
$th_array->[3]='^' if $at =~ s/\^//; # deal with ^ option<br>
$at =~ s/~//; # ignore ~ if it was entered<br>
if ($th =~ /^\:([-|+]?\d+\.?\d*)/) { # :number format per nagios spec<br>
$th_array->[1]=$1;<br>
$th_array->[0]=($at !~ /@/)?'>':'<=';<br>
}<br>
elsif ($th =~ /([-|+]?\d+\.?\d*)\:$/) { # number: format per nagios spec<br>
$th_array->[1]=$1;<br>
$th_array->[0]=($at !~ /@/)?'<':'>=';<br>
}<br>
elsif ($th =~ /([-|+]?\d+\.?\d*)\:([-|+]?\d+\.?\d*)/) { # nagios range format<br>
$th_array->[1]=$1;<br>
$th_array->[2]=$2;<br>
if ($th_array->[1] > $th_array->[2]) {<br>
print "Incorrect format in '$thin' - in range specification first number must be smaller then 2nd\n";<br>
print_usage();<br>
exit $ERRORS{"UNKNOWN"};<br>
}<br>
$th_array->[0]=($at !~ /@/)?':':'@';<br>
}<br>
if (!defined($th_array->[1])) {<br>
$th_array->[0] = ($at eq '@')?'<=':$at;<br>
$th_array->[1] = $th;<br>
}<br>
if ($th_array->[0] =~ /[>|<]/ && !isnum($th_array->[1])) {<br>
print "Numeric value required when '>' or '<' are used !\n";<br>
print_usage();<br>
exit $ERRORS{"UNKNOWN"};<br>
}<br>
# verb("debug parse_threshold: $th_array->[0] and $th_array->[1]");<br>
$th_array->[0] = '=' if !$th_array->[0] && !isnum($th_array->[1]) && $th_array->[1] ne '';<br>
$th_array->[0] = '>' if !$th_array->[0] && isnum($th_array->[1]);<br>
return $th_array;<br>
}<br>
<br>
# ultimately this function will check if warn threshold is "less" then critical<br>
# (with "less" meaning different things depending on format of data)<br>
sub threshold_specok {<br>
my ($warn_thar,$crit_thar) = @_;<br>
return 1 if isnum($warn_thar->[1]) && isnum($crit_thar->[1]) &&<br>
$warn_thar->[0] eq $crit_thar->[0] &&<br>
$warn_thar->[3] !~ /\^/ &&<br>
(($warn_thar->[1]>=$crit_thar->[1] && $warn_thar->[0] !~ /</) ||<br>
($warn_thar->[1]<=$crit_thar->[1] && $warn_thar->[0] =~ /</));<br>
return 0;<br>
}<br>
<br>
# parse command line options<br>
sub check_options {<br>
Getopt::Long::Configure ("bundling");<br>
GetOptions(<br>
'v' => \$o_verb, 'verbose' => \$o_verb,<br>
'h' => \$o_help, 'help' => \$o_help,<br>
'H:s' => \$o_host, 'hostname:s' => \$o_host,<br>
'P:i' => \$o_port, 'port:i' => \$o_port,<br>
'D:s' => \$o_dbname, 'database:s' => \$o_dbname,<br>
'u:s' => \$o_login, 'username:s' => \$o_login,<br>
'p:s' => \$o_passwd, 'password:s' => \$o_passwd,<br>
't:i' => \$o_timeout, 'timeout:i' => \$o_timeout,<br>
'V' => \$o_version, 'version' => \$o_version,<br>
'r:s' => \$o_replication, 'replication:s' => \$o_replication,<br>
's:s' => \$o_slave, 'slave:s' => \$o_slave,<br>
'a:s' => \$o_variables, 'variables:s' => \$o_variables,<br>
'c:s' => \$o_crit, 'critical:s' => \$o_crit,<br>
'w:s' => \$o_warn, 'warn:s' => \$o_warn,<br>
'f:s' => \$o_perf, 'perfparse:s' => \$o_perf,<br>
'A:s' => \$o_perfvars, 'perfvars:s' => \$o_perfvars,<br>
'q:s' => \$o_query, 'query:s' => \$o_query,<br>
);<br>
if (defined($o_help)) { help(); exit $ERRORS{"UNKNOWN"} };<br>
if (defined($o_version)) { p_version(); exit $ERRORS{"UNKNOWN"} };<br>
<br>
# below code is common for number of my plugins, including check_snmp_?, netstat, etc<br>
# it is mostly compliant with nagios threshold specification (except use of '~')<br>
# and adds number of additional format options using '>','<','!','=' prefixes<br>
my (@ar_warnLv,@ar_critLv);<br>
@o_perfvarsL=split( /,/ , lc $o_perfvars ) if defined($o_perfvars) && $o_perfvars ne '*';<br>
if (defined($o_warn) || defined($o_crit) || defined($o_variables)) {<br>
if (defined($o_variables)) {<br>
@o_varsL=split( /,/ , lc $o_variables );<br>
if (defined($o_warn)) {<br>
$o_warn.="~" if $o_warn =~ /,$/;<br>
@ar_warnLv=split( /,/ , lc $o_warn );<br>
}<br>
if (defined($o_crit)) {<br>
$o_crit.="~" if $o_crit =~ /,$/;<br>
@ar_critLv=split( /,/ , lc $o_crit );<br>
}<br>
}<br>
else {<br>
print "Specifying warning and critical levels requires '-a' parameter with list of STATUS variables\n";<br>
print_usage();<br>
exit $ERRORS{"UNKNOWN"};<br>
}<br>
if (scalar(@ar_warnLv)!=scalar(@o_varsL) || scalar(@ar_critLv)!=scalar(@o_varsL)) {<br>
printf "Number of specified warning levels (%d) and critical levels (%d) must be equal to the number of attributes specified at '-a' (%d). If you need to ignore some attribute do it as ',,'\n", scalar(@ar_warnLv), scalar(@ar_critLv), scalar(@o_varsL);<br>
verb("Warning Levels: ".join(",",@ar_warnLv));<br>
verb("Critical Levels: ".join(",",@ar_critLv));<br>
print_usage();<br>
exit $ERRORS{"UNKNOWN"};<br>
}<br>
for (my $i=0; $i<scalar(@o_varsL); $i++) {<br>
$o_warnL[$i] = parse_threshold($ar_warnLv[$i]);<br>
$o_critL[$i] = parse_threshold($ar_critLv[$i]);<br>
if (threshold_specok($o_warnL[$i],$o_critL[$i])) {<br>
print "All numeric warning values must be less then critical (or greater then when '<' is used)\n";<br>
print "Note: to override this check prefix warning value with ^\n";<br>
print_usage();<br>
exit $ERRORS{"UNKNOWN"};<br>
}<br>
}<br>
}<br>
<br>
# if (scalar(@o_varsL)==0 && scalar(@o_perfvarsL)==0) {<br>
# print "You must specify list of attributes with either '-a' or '-A'\n";<br>
# print_usage();<br>
# exit $ERRORS{"UNKNOWN"};<br>
# }<br>
<br>
$HOSTNAME = $o_host if defined($o_host);<br>
$PORT = $o_port if defined($o_port);<br>
$DATABASE = $o_dbname if defined($o_dbname);<br>
$USERNAME = $o_login if defined($o_login);<br>
$PASSWORD = $o_passwd if defined($o_passwd);<br>
$TIMEOUT = $o_timeout if defined($o_timeout);<br>
}<br>
<br>
# Get the alarm signal (just in case nagios screws up)<br>
$SIG{'ALRM'} = sub {<br>
$dbh->disconnect() if defined($dbh);<br>
print ("ERROR: Alarm signal (Nagios time-out)\n");<br>
exit $ERRORS{"UNKNOWN"};<br>
};<br>
<br>
########## MAIN #######<br>
<br>
check_options();<br>
<br>
# Check global timeout if plugin screws up<br>
if (defined($TIMEOUT)) {<br>
verb("Alarm at $TIMEOUT");<br>
alarm($TIMEOUT);<br>
}<br>
else {<br>
verb("no timeout defined : $o_timeout + 10");<br>
alarm ($o_timeout+10);<br>
}<br>
<br>
verb("connecting to database '$DATABASE' on host '$HOSTNAME' with user '$USERNAME'");<br>
my $dsn = "DBI:mysql:";<br>
$dsn.="database=$DATABASE" if $DATABASE;<br>
$dsn.=":";<br>
$dsn.="host=$HOSTNAME" if $HOSTNAME;<br>
$dsn.=":port=$PORT";<br>
$dbh = DBI->connect($dsn,$USERNAME,$PASSWORD, { PrintError => 0 } );<br>
if (!$dbh) {<br>
print "CRITICAL ERROR - Unable to connect to database '$DATABASE' on server '$HOSTNAME' on port $PORT with user '$USERNAME' - $DBI::errstr\n";<br>
exit $ERRORS{"CRITICAL"};<br>
}<br>
<br>
my $db_command="SELECT VERSION()";<br>
verb ("Mysql Query: $db_command");<br>
my $sth=$dbh->prepare($db_command);<br>
if (!$sth->execute()) {<br>
print "CRITICAL ERROR - Unable to execute '$db_command' on server '$HOSTNAME' connected as user '$USERNAME' - $DBI::errstr\n";<br>
exit $ERRORS{"CRITICAL"};<br>
}<br>
<br>
my ($mysql_version)=$sth->fetchrow_array();<br>
if ($sth->err) {<br>
print "CRITICAL ERROR - Error retrieving data for '$db_command' on server '$HOSTNAME' connected as user '$USERNAME' - $sth->err\n";<br>
exit $ERRORS{"CRITICAL"};<br>
}<br>
$sth->finish();<br>
<br>
my @mvnum=(0,0,0);<br>
@mvnum=($1,$2,$3) if $mysql_version =~ /(\d+)\.(\d+)\.(\d+)/;<br>
verb("Mysql Data: $mysql_version | Numeric: $mvnum[0].$mvnum[1].$mvnum[2]");<br>
if (defined($o_query)) {<br>
$db_command=$o_query;<br>
}<br>
elsif ($mvnum[0]>5 || ($mvnum[0]==5 && ($mvnum[1]>0 || ($mvnum[1]==0 && $mvnum[2]>1)))) {<br>
$db_command = 'SHOW GLOBAL STATUS';<br>
}<br>
else {<br>
$db_command = 'SHOW STATUS';<br>
}<br>
<br>
verb("Mysql Query: $db_command");<br>
$sth=$dbh->prepare($db_command);<br>
if (!$sth->execute()) {<br>
print "CRITICAL ERROR - Unable to execute '$db_command' on server '$HOSTNAME' connected as user '$USERNAME' - $DBI::errstr\n";<br>
exit $ERRORS{"CRITICAL"};<br>
}<br>
<br>
my %dataresults;<br>
my $statuscode = "OK";<br>
my $statusinfo = "";<br>
my $statusdata = "";<br>
my $perfdata = "";<br>
my $mysql_vname;<br>
my $mysql_value;<br>
my $chk = "";<br>
my $i;<br>
<br>
# load all data from mysql into internal hash array<br>
$dataresults{$_} = [undef, 0, 0] foreach(@o_varsL);<br>
$dataresults{$_} = [undef, 0, 0] foreach(@o_perfvarsL);<br>
while (($mysql_vname,$mysql_value)=$sth->fetchrow_array()) {<br>
$mysql_vname =~ tr/[A-Z]/[a-z]/ ;<br>
$mysql_value='NULL' if !defined($mysql_value);<br>
verb("Mysql Data: $mysql_vname = $mysql_value");<br>
$dataresults{$mysql_vname}[0] = $mysql_value if exists($dataresults{$mysql_vname});<br>
if (defined($o_perfvars) && $o_perfvars eq '*') {<br>
$dataresults{$mysql_vname} = [$mysql_value, 0, 0];<br>
push @o_perfvarsL, $mysql_vname;<br>
}<br>
if (defined($o_replication) && $mysql_vname eq 'rpl_status') {<br>
$statuscode = 'CRITICAL' if $o_replication && ($mysql_value eq 'NULL' || $mysql_value ne $o_replication);<br>
$statusinfo .= " rpl_status=" . $mysql_value;<br>
}<br>
if (defined($o_slave) && $mysql_vname eq 'slave_running') {<br>
$statuscode = 'CRITICAL' if $o_slave && ($mysql_value eq 'NULL' || $mysql_value ne $o_slave);<br>
$statusinfo .= " slave_running=" . $mysql_value;<br>
}<br>
}<br>
if ($sth->err) {<br>
$statuscode = 'CRITICAL';<br>
$statusinfo = "error $sth->err during value retrival " . $statusinfo;<br>
}<br>
$sth->finish();<br>
$dbh->disconnect();<br>
<br>
# main loop to check if warning & critical attributes are ok<br>
for ($i=0;$i<scalar(@o_varsL);$i++) {<br>
if (defined($dataresults{$o_varsL[$i]}[0])) {<br>
if ($chk = check_threshold($o_varsL[$i],lc $dataresults{$o_varsL[$i]}[0],$o_critL[$i])) {<br>
$dataresults{$o_varsL[$i]}[1]++;<br>
$statuscode = "CRITICAL";<br>
$statusinfo .= $chk;<br>
}<br>
elsif ($chk = check_threshold($o_varsL[$i],lc $dataresults{$o_varsL[$i]}[0],$o_warnL[$i])) {<br>
$dataresults{$o_varsL[$i]}[1]++;<br>
$statuscode="WARNING" if $statuscode eq "OK";<br>
$statusinfo .= $chk;<br>
}<br>
if ($dataresults{$o_varsL[$i]}[1]==0) {<br>
$dataresults{$o_varsL[$i]}[1]++;<br>
$statusdata .= " " . $o_varsL[$i] . "=" . $dataresults{$o_varsL[$i]}[0];<br>
}<br>
if (defined($o_perf) && $dataresults{$o_varsL[$i]}[2]==0) {<br>
$dataresults{$o_varsL[$i]}[2]++;<br>
$perfdata .= " " . $o_varsL[$i] . "=" . $dataresults{$o_varsL[$i]}[0];<br>
}<br>
}<br>
else {<br>
$statuscode="CRITICAL";<br>
$statusinfo .= " $o_varsL[$i] data is missing";<br>
}<br>
}<br>
<br>
# add data for performance-only attributes<br>
for ($i=0;$i<scalar(@o_perfvarsL);$i++) {<br>
if (defined($dataresults{$o_perfvarsL[$i]}[0]) && $dataresults{$o_perfvarsL[$i]}[2]==0) {<br>
$perfdata .= " " . $o_perfvarsL[$i] . "=" . $dataresults{$o_perfvarsL[$i]}[0];<br>
$dataresults{$o_perfvarsL[$i]}[2]++;<br>
}<br>
}<br>
<br>
# now output the results<br>
print "MYSQL " . $mysql_version . " " . $statuscode . $statusinfo;<br>
print " -" . $statusdata if $statusdata;<br>
print " |" . $perfdata if $perfdata;<br>
print "\n";<br>
<br>
# end exit<br>
exit $ERRORS{$statuscode};<br>
<br>
<br>
Thanks in advance for any advice you might have to share!<br>
<br>
tim<br>
<br>
<br>
------------------------------------------------------------------------------<br>
All of the data generated in your IT infrastructure is seriously valuable.<br>
Why? It contains a definitive record of application performance, security<br>
threats, fraudulent activity, and more. Splunk takes this data and makes<br>
sense of it. IT sense. And common sense.<br>
<a href="http://p.sf.net/sfu/splunk-d2dcopy2" target="_blank">http://p.sf.net/sfu/splunk-d2dcopy2</a><br>
_______________________________________________<br>
Nagiosplug-help mailing list<br>
<a href="mailto:Nagiosplug-help@lists.sourceforge.net">Nagiosplug-help@lists.sourceforge.net</a><br>
<a href="https://lists.sourceforge.net/lists/listinfo/nagiosplug-help" target="_blank">https://lists.sourceforge.net/lists/listinfo/nagiosplug-help</a><br>
::: Please include plugins version (-v) and OS when reporting any issue.<br>
::: Messages without supporting info will risk being sent to /dev/null<br>
</blockquote></div><br>