summaryrefslogtreecommitdiffstats
path: root/sbin/init.d/monitor
blob: 59cbb161b59ae437001e2a53f812b68193827263 (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
42
43
44
45
46
#!/bin/bash 

. `dirname $0`/functions

option default_check_interval	standard_option 300
option check_interval		standard_option 60
option facility			standard_option
option try_restart              boolean_option
option html			standard_option
option bin			reserved_option /opt/exosec/bin/monitor
option cmdline			reserved_option \
       '$bin -p $pidfile ${opt_html:+--html $opt_html} ${opt_facility:+--syslog $opt_facility} ${opt_try_restart:+--restart}'
option pidfile			reserved_option /var/run/monitor.pid

function do_check {
  local run_stat pid next_stat uptime

  if [ $CONFIG -nt $pidfile ] ; then
    status=CONFIG_CHANGED
  else
    status=OK
  fi
  
  if do_status $1 $2 > /dev/null ; then
    run_stat=RUNNING
    uptime=$[ $( date +%s ) - $( date -d "$( ps ho lstart $REPLY)" +%s ) ]

    read pid < $pidfile
    set -- $( ps ahxo ppid,pid,comm,lstart | \
                  awk '{if ($1 == '$pid' && $3 == "sleep") print $0 } ') 
    if [ $# == 0 ] ; then
      status=$status,$opt_check_interval
    else
      pid=$2 ; shift 3 ; start=$( date -d "$*" +%s)
      next_stat=$[ $opt_check_interval - $(date +%s) + $start ]
      status=$status,$next_stat
    fi
  else
    uptime=0
    run_stat=STOPPED
  fi

  echo "$HOSTNAME $PNAME. $(date +%s) $run_stat $uptime $status"
}

load_config