forked from arikogan/mysql-gelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-status.sh
executable file
·68 lines (52 loc) · 1.65 KB
/
mysql-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
#!/bin/bash
#
# For variables description, see
# https://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html
#
# TODO
# * Chunk messages to allow more variables as part of the payload
VERSION="1.1"
HOST=`hostname --long`
MESSAGE="MySQL Status"
TIMESTAMP=`date +%s`
LEVEL=1
# Use ~/.my.cnf
#MYSQL_USER=root
#MYSQL_PASS=admin
#MYSQL_SOCKET=/var/run/mysqld.sock
GRAYLOG_SERVER=graylog.fxempiredev.com
GRAYLOG_PORT=12305
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
#STATUS_CMD="echo 'SHOW GLOBAL STATUS;' | mysql -u $MYSQL_USER -p$MYSQL_PASS -S $MYSQL_SOCKET"
STATUS_CMD="echo 'SHOW GLOBAL STATUS;' | mysql"
cd $DIR
if [ ! -e status.last ]; then
eval $STATUS_CMD > status.last
else
eval $STATUS_CMD > status.current
MSG="{\"version\": \"$VERSION\""
MSG="$MSG,\"host\":\"$HOST\""
MSG="$MSG,\"short_message\":\"$MESSAGE\""
# MSG="$MSG,\"full_message\":\"\""
MSG="$MSG,\"timestamp\":$TIMESTAMP"
MSG="$MSG,\"level\":$LEVEL"
UPTIME_LAST=`grep "Uptime\s" status.last | cut -f 2`
UPTIME_CURR=`grep "Uptime\s" status.current | cut -f 2`
SECONDS=$((UPTIME_CURR - UPTIME_LAST))
for variable in `cat variables-diff`; do
LAST=`grep "$variable\s" status.last | cut -f 2`
CURRENT=`grep "$variable\s" status.current | cut -f 2`
DIFF=$((CURRENT - LAST))
DIFF_PER_SECOND=`printf "%0.5f\n" $(bc <<< "scale=5; $DIFF/$SECONDS")`
MSG="$MSG,\"_${variable}_per_second\":$DIFF_PER_SECOND"
done
for variable in `cat variables-abs`; do
VALUE=`grep "$variable\s" status.current | cut -f 2`
MSG="$MSG,\"_$variable\":$VALUE"
done
MSG="$MSG}"
# echo $MSG
echo $MSG | gzip -cf | nc -w 1 -u $GRAYLOG_SERVER $GRAYLOG_PORT
mv status.current status.last
fi
cd -