aboutsummaryrefslogtreecommitdiff
path: root/modules/private/monitoring/plugins/check_mysql_replication
blob: 1ee5de1470c71da3c2eaa3fc4054a5b776f05e07 (plain) (blame)
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
#!/bin/bash

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

socket=$1
config_file=$2
info=$(mysql --defaults-file=${config_file} -S $socket -e "show slave status" --vertical)
exit_code=$?

lag=$(echo "$info" | grep "\bSeconds_Behind_Master\b" | cut -d':' -f2 | sed -e "s/\s//g")

IO_running=$(echo "$info" | grep "\bSlave_IO_Running\b" | cut -d':' -f2 | sed -e "s/\s//g")
SQL_running=$(echo "$info" | grep "\bSlave_SQL_Running\b" | cut -d':' -f2 | sed -e "s/\s//g")

if [[ $exit_code -ne 0 ]]; then
  echo "UNKNOWN - Impossible to run mysql command"
  exit $STATE_UNKNOWN
elif [[ -z "$lag" ]]; then
  echo "UNKNOWN - No replication found for mysql"
  exit $STATE_UNKNOWN
elif [[ "$IO_running" != "Yes" || "$SQL_running" != "Yes" ]]; then
  echo "UNKNOWN - Replication is not running"
  exit $STATE_UNKNOWN
else
  output="Replication lag for mysql is ${lag}s"
  LC_ALL=C lag=$(printf "%.*f" 0 $lag)

  if [[ $lag -lt 5 ]]; then
    echo "OK - $output | time=${lag}s;5;10;;"
    exit $STATE_OK
  elif [[ $lag -lt 10 ]]; then
    echo "WARNING - $output | time=${lag}s;5;10;;"
    exit $STATE_WARNING
  else
    echo "CRITICAL - $output | time=${lag}s;5;10;;"
    exit $STATE_CRITICAL
  fi
fi