]> git.pld-linux.org Git - packages/monitoring-plugin-check_mysql-heartbeat.git/blob - check_mysql-heartbeat.sh
43a1ea241a88921d72e920af008e5b4b763ba983
[packages/monitoring-plugin-check_mysql-heartbeat.git] / check_mysql-heartbeat.sh
1 #!/bin/sh
2 # License: GPL v2
3 # Author: Elan Ruusamäe <glen@delfi.ee>
4 #
5 # Usage: check_mysql-heartbeat
6 #
7
8 PROGRAM=${0##*/}
9 REVISION=$(echo '$Revision$' | sed -e 's/[^0-9.]//g')
10 PROGPATH=${0%/*}
11 . $PROGPATH/utils.sh
12
13 heartbeat=mk-heartbeat
14 hostname=
15 port=
16 username=
17 password=
18 database=
19 warning=200
20 critical=400
21
22 die() {
23         eval local rc=\$STATE_$1
24         [ "$rc" ] || rc=$STATE_UNKNOWN
25         echo "$1: $2"
26         exit $rc
27 }
28
29 usage() {
30         cat >&2 <<EOF
31 Usage: $PROGRAM
32
33     --mk, --maatkit
34        Uses maatkit: mk-heartbeat
35     --pt, --percona-toolkit
36        Uses percona-toolkit: pt-hearbeat
37
38     -w SECONDS, --warning SECONDS
39     -c SECONDS,--critical SECONDS
40     -H HOSTNAME, --host HOSTNAME
41     -P PORT, --port PORT
42     -u USERNAME, --username USERNAME
43     -p PASSWORD, --password PASSWORD
44     -D DATABASE, --database DATABASE
45 EOF
46 }
47
48 # Parse arguments
49 args=$(getopt -o hVw:c:H:P:u:p:D: --long help,version,warning:,critical:,mk,maatkit,pt,percona-tookit,host:,port:,username:,password:,database: -u -n $PROGRAM -- "$@")
50 if [ $? != 0 ]; then
51         usage
52         exit 1
53 fi
54 eval set -- "$args"
55
56 ## Start of main program ##
57 while :; do
58         case "$1" in
59         -h|--help)
60                 usage
61                 exit 0
62                 ;;
63         -V|--version)
64                 echo $PROGRAM $REVISION
65                 exit 0
66                 ;;
67         --mk|--maatkit)
68                 heartbeat=mk-heartbeat
69                 ;;
70         --pt|--percona-toolkit)
71                 heartbeat=pt-heartbeat
72                 ;;
73         -c|--critical)
74                 shift
75                 critical=$1
76                 ;;
77         -w|--warning)
78                 shift
79                 warning=$1
80                 ;;
81         -H|--host)
82                 shift
83                 hostname=$1
84                 ;;
85         -P|--port)
86                 shift
87                 port=$1
88                 ;;
89         -u|--username)
90                 shift
91                 username=$1
92                 ;;
93         -p|--password)
94                 shift
95                 password=$1
96                 ;;
97         -D|--database)
98                 shift
99                 database=$1
100                 ;;
101         --)
102                 shift
103                 break
104                 ;;
105         *)
106                 die UNKNOWN "Internal error: [$1] Not recognized!"
107                 ;;
108         esac
109         shift
110 done
111
112 if [ -z "$hostname" ]; then
113         die UNKNOWN "No hostname given"
114 fi
115
116 # check out config errors
117 if [ $warning -gt $critical ]; then
118         die UNKNOWN "Warning level bigger than critical level"
119 fi
120
121 secs=$($heartbeat ${database:+-D $database} --check -h $hostname ${username:+-u $username} ${password:+-p $password} ${port:+--port $port} 2>&1)
122 rc=$?
123 if [ "$rc" != 0 ]; then
124         die UNKNOWN "$secs"
125 fi
126
127 # strip decimals, shell doesn't process these
128 secs=${secs%[.,]*}
129
130 [ $secs -gt $critical ] && die CRITICAL "$heartbeat on $hostname $secs seconds over critical treshold $critical seconds"
131 [ $secs -gt $warning ] && die WARNING "$heartbeat on $hostname $secs seconds over warning treshold $warning seconds"
132
133 die OK "$heartbeat on $hostname @$secs seconds"
This page took 0.066355 seconds and 2 git commands to generate.