summaryrefslogtreecommitdiffstats
path: root/adei/check_adei_source.sh
blob: dcec5bf55aea3a01bcfdea591f194b2b95051423 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash

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


adei_init "$@"; code=$?
adei_process_error $code

filter="${adei_args[0]}"

#echo "$adei_url"
#echo "$adei_source"
#echo "$filter"
#exit

status=1
n_groups=0
cache_size=0
cur=`date -u +%s`

group_mode=0
#groups=`curl -s -m 3 --proxy "" "$adei_url/services/list.php?target=groups&info=1&setup=$2&db_server=$3&db_name=$db_name" | xmllint --format - | grep "Value" | sed -e "s/^.*db_group=\"\([^\"]*\)\".*$/\\1/" | sed -e "s/ /::space::/"`
groups=$(adei_value_query "list.php?target=groups&info=1" 12); err=$?
if [ $err -ge 1 -a $err -le 3  ]; then
    groups=$(adei_value_query "list.php?target=groups" 5); err=$?
    adei_process_error $err "$groups" "GroupList"

    echo "Data queries are too slow, check database indexes"
    group_mode=1
    status=0
elif [ $err -gt 0 ]; then
    adei_process_error $err "$groups" "GroupList"
fi

if [ $group_mode -eq 0 ]; then
    groups=`echo "$groups" | sed -e "s/ /::space::/g"`
else
    groups=`echo "$groups" | sed -e "s/^.*db_group=\"\([^\"]*\)\".*$/\\1/" | sed -e "s/ /::space::/"`
fi

for group in $groups; do
    n_groups=$(($n_groups + 1))

    nodata=0
    if [ $group_mode -eq 0 ]; then
	group=`echo "$group" | sed -e "s/::space::/ /g"`
	gid=`echo "$group" | sed -e "s/^.*db_group=\"\([^\"]*\)\".*$/\\1/"`
	[ "$gid" == "$group" ] && gid=`echo "$group" | sed -e "s/^.*gid=\"\([^\"]*\)\".*$/\\1/"`

	if [ "$gid" == "$group" ]; then
	    echo "Invalid group specification received from ADEI server: $group"
	    status=0
	    continue
	fi
	urlgid=`echo $gid | sed -e "s/ /+/g"`
	last=$group
    else
	urlgid=`echo $group | sed -e "s/::space::/+/"`
	gid=`echo $group | sed -e "s/::space::/ /"`
	last=$(adei_simple_query "list.php?target=groups&info=1&db_group=$urlgid" 3)
	if [  $? -ne 0  ]; then
	    echo "Queries to $gid are too slow, check database indexes"
	    status=0
	    nodata=1
	else 
    	    error=${last#ERROR: }
	    if [ "$error" != "$last" ]; then
		echo "$gid: $error"
		status=0
		nodata=1
	    fi
	    last=`echo $last | grep "Value"`
	fi
	
    fi

    if [ $nodata -eq 0 ]; then
        echo "$last" | grep "last=" &> /dev/null
	if [ $? -eq 0 ]; then
    	    ts=`echo $last | sed -e "s/^.*last=\"\([^\"]*\)\".*$/\\1/" | cut -d '.' -f 1`
    	    if [ -z "$ts" ]; then
    		nodata=1
#		echo "$gid: contains no data"
#		if [ $status -eq 1 ]; then status=2; fi
    	    fi
	else 
	    ts=""
	    nodata=1
#	    echo "$gid: contains no data"
#	    if [ $status -eq 1 ]; then status=2; fi
	fi
    else
	ts=$cur
    fi
    
    
    if [ $nodata -eq 0 ]; then
	since=$((($cur - $ts) / 3600))
	if [ $since -gt 0 ]; then
	    if [ -n "$filter" ]; then
		echo "$gid" | grep -P "$filter" &>/dev/null
		accept=$?
	    else
		accept=1
	    fi
	
	    if [ $accept -ne 0 ]; then
        	last=`date -u -d "@$ts" "+%F %R:%S"`
		echo "$gid: Last updated on $last"
		if [ $status -eq 1 ]; then
		    status=2
		fi
#	    	continue
	    fi
	fi
    fi


    info=$(adei_value_query "info.php?target=cache&db_group=$urlgid"); err=$?
    [ $err -eq 0 ] && dbsize=`echo $info | sed -e "s/^.*dbsize=\"\([^\"]*\)\".*$/\\1/"`

    if [ $err -ne 0 ]; then
	echo "$gid: Cache query has failed"
	status=0
    elif [ ${#dbsize} -gt 16 ]; then
	# Otherwise both cache & data are missing. We think it is tolerable
	if [ $nodata -eq 0 ]; then 
	    echo "$gid: Cache is empty"
	    if [ $status -eq 1 ]; then status=2; fi
	fi
	continue
    else
	cache_size=$(($cache_size + `echo $info | sed -e "s/^.*dbsize=\"\([^\"]*\)\".*$/\\1/"`))

	last_cache=`echo $info | sed -e "s/^.*last=\"\([^\"]*\)\".*$/\\1/"`
	if [ "$info" == "$last_cache" ]; then
	    # There is data in the source, but cache is empty (but we should detect this earlier)
	    # Otherwise, - no cache, no data (which is OK) or we already reported about troublesome source
	    if [ $nodata -eq 0 ]; then
		last_cache=0			
		echo "$gid: Cache is empty (but non-zero size is reported)"
		if [ $status -eq 1 ]; then status=2; fi
	    fi
	    continue
	else 
	    if [ -z "$ts" ]; then
		echo -n "$gid: contains no data, but cached until "
	        date -u -d "@$last_cache" "+%F %R:%S"
		if [ $status -eq 1 ]; then status=2; fi
		continue
	    else
		since=$((($ts - $last_cache) / 3600))
	    fi
	fi
	
	if [ $since -gt 0 ]; then
	    real_width=$(adei_xml_query "list.php?target=items&db_group=$urlgid"); err=$?

	    if [ $err -ne 0 ]; then
		status=0
		echo "$gid: item list service failed"
	    else
		real_width=$((`echo "$real_width" | wc -l` - 3))
	    fi

	    if [ $real_width -le 0 ]; then
		echo "$gid: Invalid number of items reported by list sevice ($real_width)"
		status=0
		continue
	    fi

	    width=`echo $info | grep width=`
	    if [ -z "$width" ]; then 
		echo "$gid: Invalid cache information from ADEI server, width parameter missing"
		status=0
		continue
	    fi
	    
	    width=`echo $info | sed -e "s/^.*width=\"\([^\"]*\)\".*$/\\1/"`
	    if [ $width -eq 0 ]; then
		echo "$gid: Invalid cache information from ADEI server, width parameter is zero"
		status=0
		continue
	    fi
	    
	    found_reason=0

	    if [ "$real_width" -ne "$width" ]; then
		found_reason=1
		status=0
	        echo "$gid: Channel mistmatch $width (cache) and $real_width (reader)"
	    fi
		
	
#	    if [ $nodata -eq 0 ]; then 
#		if [ $found_reason -eq 0 ]; then
#		    # Using current version, we are not able to handle properly limit & filtering together
#		    adei_version_check "0.0.10"
#		    if [ $? -eq 0 ]; then
#			last_data=$(adei_simple_query "getdata.php?db_group=$urlgid&window=0,1")
#			if [ $? -ne 0 ]; then
#			    found_reason=1
#			    status=0
#			    echo -n "$gid: No valid data in database, but there is (filtered?) records. Last one from "
#			    date -u -d "@$ts" "+%F %R:%S"
#			fi
#		    fi
#		fi
#	    fi

	    if [ $found_reason -eq 0 ]; then
		status=0
	        echo "$gid: Last $since hours are not cached"
	    fi
	fi
    fi
done

if [ $n_groups -eq 0 ]; then
    status=0
fi

cache_size=$(($cache_size / 1073741824))
echo "$status $n_groups groups, $cache_size GB"