/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
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
106
107
108
109
110
111
112
113
114
115
#!/bin/bash

if [ -z "$1" ]; then
    . /opt/scripts/adei_config.sh
else
    echo $1 | grep -E "[^a-z0-9_\-]" &> /dev/null
    if [ $? -eq 0 ]; then exit; fi
    if [ ! -f /opt/scripts/${1}_config.sh ]; then exit; fi
    . /opt/scripts/${1}_config.sh
fi

status=1

#adei=`ps xa | grep cache.php | grep -v grep | wc -l`
#if [ $adei -gt 1 ]; then
#    echo "Too many ADEI caching scripts are running, currently: $adei"
#    status=0
#fi

mails=`mailutil check | cut -d ' ' -f 6`
if [ "$mails" == "in" ]; then
    mails=`mailutil check | cut -d ' ' -f 4`
fi

if [ $mails -gt 0 ]; then
    lastmail=`echo "type $mails" | mailx -R "" -N`
    dt=`echo "$lastmail" | sed -ne '/^Date:/ {s/^Date:\s*// p;q};'`
    cur=`date -u +%s`
    last=`date -u --date "$dt" +%s`
    since=$((($cur - $last) / 60))
    if [ $since -le 60 ]; then
	echo "There is $mails message in root mailbox, the last message is from $dt"
	if [ $since -le 10 ]; then
	    if [ $mails -le 30 ]; then
		if [ $status -eq 1 ]; then status=2 ; fi
	    else
		status=0
	    fi
	    echo "$lastmail" | sed -e '1,/^$/d' | head -n 10
	elif [ $since -le 1440 ]; then
	    if [ $status -eq 1 ]; then status=2 ; fi
	fi
    fi
fi

apache=`ps xa | grep apache | grep -v grep | wc -l`
if [ $apache -eq 0 ]; then
    echo "Apache server is stopped"
    status=0
fi

mysql=`ps xa | grep mysqld | grep -v grep | wc -l`
if [ $mysql -eq 0 ]; then
    echo "MySQL server is stopped"
    status=0
fi

tmp_size=$((`du -sm $adei_path/tmp/ | cut -f 1` / 1024))
if [ $tmp_size -gt 10 ]; then
    echo "Temporary folder has grown over $tmp_size GB"
    if [ $status -eq 1 ]; then status=2 ; fi
fi

if [ $apache -eq 0 ]; then
    adei_version="x.x"
else
    adei_version=`curl -s --proxy "" "$adei_url/services/info.php?target=version&encoding=text"`
    echo $adei_version | grep '<html' &> /dev/null
    if [ $? -eq 0 ]; then
	echo $adei_version | sed -e 's/.*<h2>Error[[:space:]]\+\([[:digit:]]\+\).*/Unable to detect ADEI version. Server Error \1/'
	adei_version="x.x"
	apache=0
	status=0
    fi
fi

if [ $apache -eq 0 ]; then
    n_errors=0
else
    errors=`curl -s --proxy "" "$adei_url/services/info.php?target=log&interval=3600&encoding=text&setup="`
    if [ -n "$errors" ]; then 
	n_errors=`echo "$errors" | wc -l`
    else
	n_errors=0
    fi
fi
if [ $n_errors -gt 0 ]; then
    echo "ADEI log contains errors, $n_errors events for last hour"
    echo $errors | grep '<html' &> /dev/null
    if [ $? -eq 0 ]; then
	echo $errors | sed -e 's/.*<h2>Error[[:space:]]\+\([[:digit:]]\+\).*/Unable to parse ADEI logs. Server Error \1/'
	status=0
    else
	echo "$errors" | head -n 1
        if [ $status -eq 1 ]; then status=2 ; fi
    fi
fi

long=`grep "Maximum execution time" $apache_log | tail -n 1 | sed -e "s/^\[\([^\[]\+\)\].*$/\1/"`
if [ -n "$long" ]; then
    ts=`date --date "$long" +%s`
    cur=`date +%s`
    if [ $(($cur - $ts)) -le 86400 ]; then
	echo "Execution time exceeding limits set by Apache (last message $long)"
	if [ $status -eq 1 ]; then status=2 ; fi
    fi
fi

if [ -n "$adei_db" ]; then
    adei_size=`du -sm $adei_db | cut -f 1`
    adei_size=`expr $adei_size / 1024`
    echo "$status ADEI $adei_version $adei_size GB"
else
    echo "$status ADEI $adei_version"
fi