作者: 薄荷冰           Linux基础  |  网站架构  |  数据库  |  监控报警  |  shell系列  |  常用工具  |  常见问题


 

导航

 
#!/bin/sh
# The wrapper for Cacti PHP script.
# It runs the script every 5 min. and parses the cache file on each following run.
# Version: 1.1.8
#
# This program is part of Percona Monitoring Plugins
# License: GPL License (see COPYING)
# Copyright: 2018 Percona
# Authors: Roman Vynar

ITEM=$1
HOST=localhost
DIR=`dirname $0`
CMD="/usr/bin/php -q $DIR/ss_get_mysql_stats.php --host $HOST --items gg"
# CACHEFILE="/tmp/$HOST-mysql_cacti_stats.txt"
CACHEFILE="`ls /tmp/$HOST-mysql_cacti_stats.txt*`"
[ `echo $CACHEFILE|xargs -n 1|wc -l` -gt 1 ] && echo $CACHEFILE|xargs rm -f
DIR_CNF="$DIR/ss_get_mysql_stats.php.cnf"
mysql_user="$(awk -F"[$ =';]*" '$2~"mysql_user"{print $3}' $DIR_CNF)"
pass_test="$(awk -F"[$ =';]*" '$2~"mysql_pass"{print $3}' $DIR_CNF)"
[ -n "$pass_test" ] && mysql_pass="-p$pass_test"
mysql_port="$(awk -F"[$ =';]*" '$2~"mysql_port"{print $3}' $DIR_CNF)"
mysql_socket="$(awk -F"[$ =';]*" '$2~"mysql_socket"{print $3}' $DIR_CNF)"
CACHEFILE=${CACHEFILE:-"/tmp/$HOST-mysql_cacti_stats.txt:$mysql_port"}

if [ "$ITEM" = "running-slave" ]; then
    # Check for running slave
    # RES=`HOME=~zabbix mysql -e 'SHOW SLAVE STATUS\G' | egrep '(Slave_IO_Running|Slave_SQL_Running):' | awk -F: '{print $2}' | tr '\n' ','`
    RES=`mysql -u$mysql_user $mysql_pass -S $mysql_socket -e 'SHOW SLAVE STATUS\G' | egrep '(Slave_IO_Running|Slave_SQL_Running):' | awk -F: '{print $2}' | tr '\n' ','`
    if [ "$RES" = " Yes, Yes," ]; then
        echo 1
    else
        echo 0
    fi
    exit
elif [ -e $CACHEFILE ]; then
    # Check and run the script
    # TIMEFLM=`stat -c %Y /tmp/$HOST-mysql_cacti_stats.txt`
    TIMEFLM=`stat -c %Y $CACHEFILE`
    TIMENOW=`date +%s`
    if [ `expr $TIMENOW - $TIMEFLM` -gt 300 ]; then
        rm -f $CACHEFILE
        $CMD 2>&1 > /dev/null
    fi
else
    $CMD 2>&1 > /dev/null
fi

# Parse cache file
if [ -e $CACHEFILE ]; then
    cat $CACHEFILE | sed 's/ /\n/g; s/-1/0/g'| grep $ITEM | awk -F: '{print $2}'
else
    echo "ERROR: run the command manually to investigate the problem: $CMD"
fi

 

posted on 2018-12-27 20:40  薄荷冰  阅读(269)  评论(0)    收藏  举报