Napsty / check_mysql_slavestatus

Monitoring plugin to check the Status of a MySQL/MariaDB replication slave
15 stars 15 forks source link

support for mysql 5.6+ login paths #11

Open samva259 opened 5 years ago

samva259 commented 5 years ago

Thank you very much for making this available. I have a small improvement request. Could support for the --login-path parameter for the mysql client be added to this script ?

For example if a new "-l" switch was added the affected lines of code might be something like these (plus relevant documentation changes):

... while getopts "H:P:u:p:S:s:w:c:o:m:h:l" Input; ... l) loginpath="--login-path=${OPTARG}";; ... then for the case we receive the -l switch the invoke of the mysql client would be

ConnectionResult=$(mysql ${loginpath} ${optfile} ... -e "show slave ${connection} status\G" 2>&1) ...

Thank you again for considering this

samva259 commented 4 years ago

sample code to add support for --login-path parameter as an alternative to --user and --password parameters, and to add some logic to check for conflicting parameter(s): ... #########################################################################

Usage: ./check_mysql_slavestatus.sh (-o file|(-H dbhost [-P port]|-S socket) (-u dbuser -p dbpass|-l loginpath)) [-s connection] [-w integer] [-c integer] [-m integer]

######################################################################### help="\ncheck_mysql_slavestatus.sh (c) 2008-2019 GNU GPLv2 licence Usage: $0 (-o file|(-H dbhost [-P port]|-S socket) (-u username -p password|-l loginpath)) [-s connection] [-w integer] [-c integer] [-m]\n Options:\n-o Path to option file containing connection settings (e.g. /home/nagios/.my.cnf). Note: If this option is used, -H, -u, -p, -l parameters will become optional\n-H Hostname or IP of slave server\n-P MySQL Port of slave server (optional, defaults to 3306)\n-u Username of DB-user\n-p Password of DB-user\n-l Login Path (used instead of Username and Password)\n-S database socket\n-s Connection name (optional, with multi-source replication)\n-w Replication delay in seconds for Warning status (optional)\n-c Replication delay in seconds for Critical status (optional)\n-m Threshold in seconds since when replication did not move (compares the slaves log position)\n Attention: The DB-user you type in must have CLIENT REPLICATION rights on the DB-server. Example:\n\tGRANT REPLICATION CLIENT on . TO 'nagios'@'%' IDENTIFIED BY 'secret';" ... while getopts "H:P:u:p:S:s:w:c:o:m:l:h" Input; ... l) loginpath_p="y";loginpath="--login-path=${OPTARG}";; ...

Check whether all required arguments were passed in (either option file or full connection settings)

if [[ -z "${optfile}" && -z "${host}" && -z "${socket}" && -z "${loginpath}" ]]; then echo -e "Missing required parameter(s)"; exit ${STATE_UNKNOWN} elif [[ -z "${optfile}" && ((-z "${host}" && -z "${socket}") || (-z "${loginpath}" && (-z "${user}" || -z "${password}"))) ]]; then echo -e "Missing required parameter(s)"; exit ${STATE_UNKNOWN} elif [[ -z "${optfile}" && (-n "${host}" && -n "${socket}") ]]; then echo -e "Conflicting parameter(s)"; exit ${STATE_UNKNOWN} elif [[ -z "${optfile}" && (-n "${loginpath}" && -n "${user}") ]]; then echo -e "Conflicting parameter(s)"; exit ${STATE_UNKNOWN} elif [[ -z "${optfile}" && (-n "${loginpath}" && -n "${password}") ]]; then echo -e "Conflicting parameter(s)"; exit ${STATE_UNKNOWN} fi ...

Connect to the DB server and store output in vars

if [[ -n $loginpath ]]; then if [[ -n $socket ]]; then ConnectionResult=$(mysql ${loginpath} ${optfile} ${socket} -e "show slave ${connection} status\G" 2>&1) else ConnectionResult=$(mysql ${loginpath} ${optfile} ${host} ${port} -e "show slave ${connection} status\G" 2>&1) fi else if [[ -n $socket ]]; then ConnectionResult=$(mysql ${optfile} ${socket} ${user} -e "show slave ${connection} status\G" 2>&1) else ConnectionResult=$(mysql ${optfile} ${host} ${port} ${user} -e "show slave ${connection} status\G" 2>&1) fi fi ...