-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcheck_mysql.sh
executable file
·109 lines (98 loc) · 2.38 KB
/
check_mysql.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
#
# Check MySQL plugin for Nagios
#
# Usage: check_mysql.sh [-u user] [-p password] [-f MySQL defaults-file path]
# -u, --user MySQL user name
# -p, --port MySQL user password
# -f, --defaults-file MySQL defaults-file path
# -w, --warning WARNING Warning value (percent)
# -c, --critical CRITICAL Critical value (percent)
# -h, --help Display this screen
#
# (c) 2014, Benjamin Dos Santos <[email protected]>
# https://github.com/bdossantos/nagios-plugins
#
while [[ -n "$1" ]]; do
case $1 in
--user | -u)
user=$2
shift
;;
--password | -p)
password=$2
shift
;;
--defaults-file | -f)
default_files=$2
shift
;;
--warning | -w)
warning=$2
shift
;;
--critical | -c)
critical=$2
shift
;;
--help | -h)
sed -n '2,10p' "$0" | tr -d '#'
exit 3
;;
*)
echo "Unknown argument: $1"
exec "$0" --help
exit 3
;;
esac
shift
done
if ! hash mysqladmin &>/dev/null; then
echo "CRITICAL - mysqladmin command not found"
exit 2
fi
options=()
warning=${warning:=90}
critical=${critical:=95}
if [[ $warning -ge $critical ]]; then
echo "UNKNOWN - warning ($warning) can't be greater than critical ($critical)"
exit 3
fi
if [[ ! -z $user ]]; then
options=("${options[@]}" "-u ${user}")
fi
if [[ ! -z $password ]]; then
options=("${options[@]}" "-p${password}")
fi
if [[ ! -z $default_files ]]; then
options=("${options[@]}" "--defaults-file=${default_files}")
fi
status=$(mysqladmin "${options[@]}" status)
if [[ $? -ne 0 ]] || [[ -z $status ]]; then
echo "CRITICAL - ${status}"
exit 2
fi
connected_thread=$(echo "$status" | awk '{ print $4 };')
max_connections=$(
mysql -N -s -r -e 'SHOW VARIABLES LIKE "max_connections";' |
awk '{ print $2 };'
)
if [[ $? -ne 0 ]]; then
echo "CRITICAL - could not fetch MySQL max_connections"
exit 2
elif [[ -z $connected_thread ]] || [[ -z $max_connections ]]; then
echo "CRITICAL - 'connected_thread' and 'max_connections' are empty"
exit 2
fi
used=$((connected_thread * 100 / max_connections))
status="${status} - ${connected_thread} of ${max_connections} max_connections"
if [[ $used -gt $critical ]]; then
echo "CRITICAL - ${status}"
exit 2
elif [[ $used -gt $warning ]]; then
echo "WARNING - ${status}"
exit 1
else
echo "OK - ${status}"
exit 0
fi