点击(此处)折叠或打开
- #!/bin/bash
- #Written by carl zhang
- #This Nagios plugin can be check your mysql connections status
- USAGE_Method="$(basename $0) [-w|--warning] <Free Percent> [-c|--critical] <Free Percent>"
- USAGE_Value="WARNING value must be small than CRITICAL value: `basename $0` $*"
- MYSQLUSER=nagios
- MYSQLPS=nagiosadmin
- MYSQLBIN=/usr/local/mysql/bin/mysql
- STATE_OK=0
- STATE_WARNING=1
- STATE_CRITICAL=2
- STATE_UNKNOWN=3
- if [ $# -lt 4 ];then
- echo
- echo "Usage: $USAGE_Method"
- echo
- exit 0
- fi
- while [ $# -gt 0 ];do
- case "$1" in
- -w|--warning)
- shift
- WARNING=$1
- ;;
- -c|--critical)
- shift
- CRITICAL=$1
- ;;
- esac
- shift
- done
- if [[ $WARNING -eq $CRITICAL || $WARNING -gt $CRITICAL ]]
- then
- echo
- echo "$USAGE_Value"
- echo
- echo "Usage: $USAGE_Method"
- echo
- exit 0
- fi
- used_connections=$($MYSQLBIN -u$MYSQLUSER -p$MYSQLPS -e "show status like 'Threads_connected'\G" | grep "Value" | awk '{print $2}')
- max_connections=$($MYSQLBIN -u$MYSQLUSER -p$MYSQLPS -e "show variables like 'max_connections'\G" | grep "Value" | awk '{print $2}')
- PERCENT=$(bc <<< "scale=2;$used_connections/$max_connections" | tr '^.' ' ')
- if [[ "$PERCENT" -gt "$CRITICAL" ]]
- then
- echo "CRITICAL - used_connections is : $used_connections"
- exit 2
- fi
- if [[ "$PERCENT" -gt "$WARNING" && "$PERCENT" -lt "$CRITICAL" ]]
- then
- echo "WARNING - used_connections is : $used_connections"
- exit 1
- fi
- if [[ "$PERCENT" -lt "$WARNING" ]]
- then
- echo "OK - used_connections is : $used_connections"
- exit 0
- fi
点击(此处)折叠或打开
- GRANT PROCESS, FILE, SUPER, REPLICATION CLIENT ON *.* TO 'nagios'@'localhost' IDENTIFIED BY 'nagiosadmin';
- FLUSH PRIVILEGES;
点击(此处)折叠或打开
- vim /usr/local/nagios/etc/nrpe.cfg
- command[check_mysql_connections]=/usr/local/nagios/libexec/check_mysql_connections -w 75 -c 85
点击(此处)折叠或打开
- killall nrpe
- /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
点击(此处)折叠或打开
- define service {
- use generic-service
- host_name zhongshan_server_xx_xxx
- service_description check_mysql_connections
- check_period 24x7
- notifications_enabled 1
- max_check_attempts 3
- normal_check_interval 3
- retry_check_interval 2
- contact_groups dbadmins
- notification_interval 10
- notification_period 24x7
- notification_options w,u,c,r
- check_command check_nrpe!check_mysql_connections
- }
点击(此处)折叠或打开
- /etc/rc.d/init.d/nagios checkconfig
- /etc/rc.d/init.d/nagios reload