summaryrefslogtreecommitdiffstats
path: root/adei/check_adei.sh
blob: 06047b67c45138b7d62e7d3016050e8a0860d94c (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
86
87
88
89
90
91
92
93
94
95
96
97
#! /bin/bash

. $(dirname "$0")/lib/adei/adei.sh

adei_init "$@"; code=$?

online=${adei_online}
healthy=$(( $code == 0 ))

adei_version_check "0.0.10"
[ $? -eq 0 ] && adei10=1

msg=""
[ -n "$adei_version" ] && msg+="ADEI $adei_version"

# Get current database size
[ -n "$adei10" ] && size=$(adei_text_query "info.php?target=size&encoding=text")

[[ "$size" =~ "Error:" ]] && size=""
[ -n "$size" ] && msg+=" \${color gray}/ $((size / 1024 / 1024 / 1024)) GB"

# Check pending administrative scripts
if [ -n "$adei10" -a $healthy -ne 0 ]; then
    scripts=$(adei_xml_query "info.php?target=scripts"); err=$?
    [ $err -eq 0 ] && waiting="$(echo $scripts | grep "Value" | sed -e "s/^\(.*mtime=\"\([^\"]*\)\".*\)$/\\2\\1/" | awk -v date="$(date +%s)" '{duration=date - $1} duration > 3600 { print duration }' | sort -rn)" #"

    if [ $err -ne 0 ]; then
	adei_print_error $err "ScriptInfo" "$waiting"
	healthy=0
    elif [ -n "$waiting" ]; then
        num_waiting="$(echo $waiting | tr ' ' '\n' | wc -l)"
        long_waiting="$(echo $waiting | cut -d ' ' -f 1)"
        if [ $num_waiting -gt 0 ]; then
            healthy=2
            echo "$num_waiting pending scripts, longest for $(format_time $long_waiting)"
        fi
    fi
fi

# Verify offset (for selected database)
if [ -n "$adei10" -a $healthy -ne 0 -a -n "$adei_source" ]; then
    now=$(date +%s)
    sdate="$(adei_text_query "getdata.php?db_mask=0&format=csv&window=-1&rt=1&cache=1&time_format=U" slave)"; err_slave=$?
    [ $err_slave -eq 0 ] && sdate="$(echo "$sdate" | cut -d ',' -f 1 | cut -d '.' -f 1)"
    mdate="$(adei_text_query "getdata.php?db_mask=0&format=csv&window=-1&rt=1&cache=1&time_format=U" master)"; err_master=$?
    [ $err_master -eq 0 ] && mdate="$(echo "$mdate" | cut -d ',' -f 1 | cut -d '.' -f 1)"

    src="$adei_source"
    if [ $err_slave -ne 0 -o $err_master -ne 0  -o -z "$sdate" -o -z "$mdate" ]; then

	echo -n "Error querying (${err_master:+master} ${err_slave:+slave}) data from cache of '$src': "
	if [ $err_master -ne 0 ]; then
	    echo "$(adei_print_error $err_master "$mdate")"
        elif [ $err_slave -ne 0 ]; then
	    echo "$(adei_print_error $err_slave "$sdate")"
	else
	    echo "Unexpected response from getdata service"
	fi
        healthy=0
    else
        master_offset=$(($now - $mdate))
        slave_offset=$(($mdate - $sdate))

        if [ $master_offset -gt $MAX_SOURCE_OFFSET ]; then
            rdate=$(adei_text_query "getdata.php?db_mask=0&format=csv&window=-1&rt=1&time_format=U" master); err=$?
            if [ $err -eq 0 ]; then
		rdate="$(echo "$rdate" | cut -d ',' -f 1 | cut -d '.' -f 1)"
		cache_offset=$(($rdate - $mdate))

		if [ $cache_offset -gt $MAX_MASTER_OFFSET ]; then
		    echo "ADEI cache is off by $(format_time $cache_offset) for '$src'"
		    healthy=2
		else
		    offset=$(($now - $rdate))
#                   echo "Source '$src' is not updated for $(format_time $offset)"
		fi
	    else
		echo -n "Error querying data from '$src' primary database: "
		echo "$(adei_print_error $err_master "$rdate")"
		healthy=0
	    fi
        fi

        if [ $slave_offset -gt $MAX_SLAVE_OFFSET ]; then
            echo "MySQL slave is off by $(format_time $slave_offset) for '$src'"
            healthy=2
        fi

        [ -n "$msg" ] && msg="${msg}, "
        [ -z "$msg" ] && msg="\${color gray}/ "
        msg="${msg}cache $(format_time $master_offset)"
        [ $slave_offset -gt 0 ] && msg="$msg & slave $(format_time $slave_offset)"
        msg="$msg off"
    fi
fi

echo "$(adei_print_status "$0" $online $healthy) $msg"