summaryrefslogtreecommitdiffstats
path: root/sbin/init.d/stat
blob: b049145fe38ba269338940470b6dd5994261fe4e (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
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
#!/bin/bash 

. `dirname $0`/functions

option	output		standard_option
option	runas		standard_option stat
option	opt		long_option
option	freq		standard_option
option	method		standard_option
option	statusfile	standard_option
option	check_interval	reserved_option 60
option	bin		reserved_option /opt/exosec/bin/statistics

function fct_begin_section {
    # assign the default instance name from the config file argument
    pidfile=/var/run/stat/$2.pid
    procname=statistics
    opt_statusfile=/var/run/stat/$2.nfo
    opt_output=/var/run/stat/$2.log
}

function do_start {
    local p=$1 ; shift
    local testname=$1
    local message

    opt_method=${opt_method:-$testname}
    if [ "$FORCE_START_STOP" -eq 0 ] && do_status $p $testname > /dev/null ; then
	echo "Already started, stop it before restarting"
	return 1
    fi
 
    message="# Starting Monitoring on $testname for $testname statistics ..."
    echo "$message"

    { local path
	for path in `dirname $pidfile` `dirname $opt_statusfile` `dirname $opt_output`; do
	    mkdir -p $path 2> /dev/null && \
	    { [ -z "$opt_runas" ] || chown $opt_runas $path && chmod 700 $path ; } \
	       || { echo "Bad owner or rights on directory $path ($opt_runas:700), fix it." 
		    return 1 ; }
	done ;
    }

    # note: here, cmdline doesn't include $bin.
    cmdline="-t $opt_method -o $opt_output -s $opt_statusfile -p $pidfile
	    -w $opt_freq -n $testname $opt_opt"

    if [ -z "$opt_runas" ] ; then
	( $bin $cmdline )
    else
	( su - $opt_runas -- $cmdline )
    fi

    if [ "$?" = 0 ] ; then 
	echo "$message done."
    else
	echo "$message error."
    fi
}

function do_check { 
  local run_stat date status service result next_stat
  local service=$1 instance=$2

  if do_status $service $instance > /dev/null ; then
    run_stat=RUNNING
    uptime=$[ $( date +%s ) - $( date -d "$( ps ho lstart $REPLY)" +%s ) ] 
  else
    run_stat=STOPPED
    uptime=0
  fi
  if [ ! -e $opt_statusfile ] ; then
     echo "$HOSTNAME $service.$instance $(date +%s) STOPPED"
     return 1
  fi
   
  read idsvc date status result < $opt_statusfile > /dev/null 2>&1 
  if [ $? != 0 ] ; then 
    echo "$HOSTNAME $2 $(date +%s) STOPPED" ; return 1
  fi

  diff=$[ $(date +%s) - $date ]
  [ $[ $diff > 2 * $opt_freq ] = 1 -a $status = OK ] && 
     status="ALERT"
  [ $[ $diff > $opt_freq ] = 1 -a $status = OK ] && 
     status="WARNING"
 
  if [ $run_stat = RUNNING ] ; then
    read pid < $pidfile
    set -- $( ps ahxo ppid,pid,comm,lstart | \
                  awk '{if ($1 == '$pid' && $3 == "sleep") print $0 } ') 
    if [ $# == 0 ] ; then
      status=$status,$opt_freq
    else
      pid=$2 ; shift 3 ; start=$( date -d "$*" +%s)
      next_stat=$[ $opt_freq - $(date +%s) + $start ]
      status=$status,$next_stat
    fi
  fi
  
  echo "$HOSTNAME $service.$instance $date $run_stat $uptime $status $result"
}

load_config