-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_slave_status.sh
88 lines (68 loc) · 1.81 KB
/
mysql_slave_status.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -----------------------------------------------------------------------------
# Name : mysql_slave_status.sh
# Description : show slave status
# Author : qiaolei
# Date : 2014/10/13
# -----------------------------------------------------------------------------
#!/bin/sh
usage()
{
echo "usage: $0 <username> <password>"
}
if [ $# -ne 2 ];then
usage
exit 1
fi
LOG_FILE=/tmp/mysql_scripts.log
if [ ! -f $LOG_FILE ];then
touch $LOG_FILE
fi
TIMESTAMP=`date +%Y%m%d%H%M%S`
echo "@$TIMESTAMP: {$0 $*}" >$LOG_FILE
USERNAME=$1
PASSWORD=$2
TMP_SHOW_SLAVE_STATUS=/tmp/show_slave_staus.tmp
if [ ! -f $TMP_SHOW_SLAVE_STATUS ];then
touch $TMP_SHOW_SLAVE_STATUS
fi
if [ $PASSWORD != "NULL" ]; then
mysql -u $USERNAME -p$PASSWORD -e "show slave status\G" >$TMP_SHOW_SLAVE_STATUS
else
mysql -u $USERNAME -e "show slave status\G" >$TMP_SHOW_SLAVE_STATUS
fi
TMP_KEYS=/tmp/keys.tmp
cat $TMP_SHOW_SLAVE_STATUS | sed '1d'| sed '$d' | tr -d "[|]" | awk '{print $1}' > $TMP_KEYS
TMP_VALUES=/tmp/values.tmp
cat $TMP_SHOW_SLAVE_STATUS | sed '1d'| sed '$d' | tr -d "[|]" | awk '{print $2}' > $TMP_VALUES
# keys
declare -a KEYS
i=0
while read LINE
do
KEYS[$i]=${LINE%%:}
i=`expr $i + 1`
done < $TMP_KEYS
#values
declare -a VALUES
i=0
while read LINE
do
VALUES[$i]=$LINE
i=`expr $i + 1`
done < $TMP_VALUES
rm -f $TMP_SHOW_SLAVE_STATUS $TMP_KEYS $TMP_VALUES
# store key-value pair in a map
declare -A KVPS
NUM_OF_KEYS=${#KEYS[@]}
for ((i=0;i<$NUM_OF_KEYS;i++));do
KVPS[${KEYS[$i]}]=${VALUES[$i]}
done
# construct a jason string
RESULT=`printf "%s%s" "$RESULT" "{"`
for ((i=0;i<$NUM_OF_KEYS;i++));do
RESULT=`printf "%s%s%s%s" "$RESULT" "\"${KEYS[$i]}\": \"" "${KVPS[${KEYS[$i]}]}" "\","`
# echo "${KEYS[$i]}: ${KVPS[${KEYS[$i]}]}"
done
RESULT=${RESULT%%,}
RESULT=`printf "%s%s" "$RESULT" "}"`
echo $RESULT