summaryrefslogtreecommitdiff
path: root/puppet/modules/site_nagios/files/plugins/check_last_regex_in_log
blob: 475693882342980b938d9f03c664437778d311b9 (plain)
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
#!/bin/sh
#
# depends on nagios-plugins-common for /usr/lib/nagios/plugins/utils.sh
# this package is installed using leap_platform by the Site_check_mk::Agent::Mrpe
# class

set -e

usage()
{
cat << EOF
usage: $0 -w <sec> -c <sec> -r <regexp> -f <filename>

OPTIONS:
  -h         Show this message
  -r <regex> regex to grep for
  -f <file>  logfile to search in
  -w <sec>   warning  state after X seconds
  -c <sec>   critical state after x seconds

example: $0 -f /var/log/syslog -r 'tapicero' -w 300 -c 600
EOF
}


. /usr/lib/nagios/plugins/utils.sh


warn=0
crit=0
log=''
regex=''

set -- $(getopt hr:f:w:c: "$@")
while [ $# -gt 0 ]
do
    case "$1" in
    (-h) usage; exit 0 ;;
    (-f) log="$2"; shift;;
    (-r) regex="$2"; shift;;
    (-w) warn="$2"; shift;;
    (-c) crit="$2"; shift;;
    (--) shift; break;;
    (-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
    (*)  break;;
    esac
    shift
done

[ $warn -eq 0 -o $crit -eq 0 -o -z "$regex" -o -z "$log" ] && ( usage; exit $STATE_UNKNOWN)
[ -f "$log" ] || (echo "$log doesn't exist"; exit $STATE_UNKNOWN)

lastmsg=$(tac $log | grep -i $regex | head -1 |  sed 's/  / /g' | cut -d' ' -f 1-3)

if [ -z "$lastmsg" ]
then
  summary="\"$regex\" in $log was not found"
  state=$STATE_CRITICAL
  state_text='CRITICAL'
  diff_sec=0
else
  lastmsg_sec=$(date '+%s' -d "$lastmsg")
  now_sec=$(date '+%s')

  diff_sec=$(($now_sec - $lastmsg_sec))

  if [ $diff_sec -lt $warn ]; then
    state=$STATE_OK
    state_text='OK'
  elif [ $diff_sec -lt $crit ]; then
    state=$STATE_WARNING
    state_text='WARNING'
  else
    state=$STATE_CRITICAL
    state_text='CRITICAL'
  fi

  summary="Last occurrence of \"$regex\" in $log was $diff_sec sec ago"
fi

# check_mk_agent output
# echo "$state Tapicero_Heatbeat sec=$diff_sec;$warn;$crit;0; $state_text - $summary"

echo "${state_text}: $summary | seconds=${diff_sec};$warn;$crit;0;"
exit $state