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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
cd $(dirname $0)
MAX_SOURCE_OFFSET=3600
MAX_MASTER_OFFSET=300
MAX_SLAVE_OFFSET=300
adei_default_timeout=${default_timeout:-120}
. lib/adei/adei_version.sh
. lib/adei/adei_print.sh
. lib/adei/adei_error.sh
. lib/nagios/nagios.sh
shopt -s nocasematch;
function adei_curl {
if [ -n "$adei_auth" ]; then
curl -u "$adei_auth" "$@"
else
curl "$@"
fi
}
# Auth with redirect (-L/--location-trusted) not working. Credentials are lost on hops.
function adei_query {
local resp
local timeout=${3:-$adei_default_timeout}
local err=0
if [ "$2" == "ecode" ]; then
url="$1&mysql=master"
# resp=$(adei_curl --proxy "" -f -m "$timeout" "$url" 2>&1 | grep "returned error")
resp=$(adei_curl --proxy "" --location-trusted -sf -m "$timeout" -w "%{http_code}" -o /dev/null "$url" 2>&1);
[ -z "$resp" -o "$resp" = "000" ] && resp="10" # error code 1
elif [ "$2" == "emsg" ]; then
url="$1&mysql=master"
resp=$(adei_curl --proxy "" --location-trusted -f -m "$timeout" "$url" 2>&1 | grep -o "curl.*")
[ -z "$resp" ] && resp="No response from"
else
if [ "$2" == "slave" ]; then
url="$1&mysql=slave"
else
url="$1&mysql=master"
fi
resp=$(adei_curl --proxy "" --location-trusted -sf -m "$timeout" "$url"); err=$?
if [ $err -ne 0 ]; then
resp=$(adei_query "$1" "ecode" "$timeout")
err=$(($(($resp / 10)) + $(($resp % 10))))
resp=$(adei_query "$1" "emsg" "$timeout")
[ -n "$debug" ] && echo "$(date) $timeout Failed $url" >> /tmp/adei.log
else
[ -n "$debug" ] && echo "$(date) $timeout OK $url" >> /tmp/adei.log
fi
fi
echo -n $resp
return $err
}
function adei_format_query {
local with_auth="${1:-1}"
local query="$2"
local database=${3:-master} # ignoring here, it will be handled separately by adei_query
local timeout=${4:-$adei_default_timeout}
local source=${5:-"$adei_source"}
local url="${6:-$adei_url}"
auth=""
[ $with_auth -gt 0 ] && auth="${adei_auth}@"
if [[ "$query" =~ \? ]]; then
echo "http://${auth}${url}/${query}${source}"
else
echo "http://${auth}${url}/${query}?xxxxx${source}"
fi
}
function adei_simple_query {
local req=$(adei_format_query 0 "$@")
local database=${2:-master}
adei_query "$req" $database "$timeout"; err=$?
return $err
}
function adei_text_query {
local out # Local on the same string breaks error reporting
out="$(adei_simple_query "$@")"; local err=$?
if [ $err -gt 0 -o -z "$out" ]; then
# echo "Error sending ADEI request: $(adei_format_query 0 "$@")"
[ $err -eq 0 ] && local err=1
[ -n "$out" ] && echo -n "$out"
return $err
elif [[ "$out" =~ "Error:" ]]; then
echo -n "$out"
return 7
else
echo -n "$out"
return 0
fi
}
function adei_xml_query {
local out # Local on the same string breaks error reporting
out="$(adei_simple_query "$@")"; local err=$?
if [ $err -ne 0 -o -z "$out" ]; then
# echo "Error sending ADEI request: $(adei_format_query 0 '$@')"
[ $err -eq 0 ] && err=1
[ -n "$out" ] && echo "$out"
return $err
fi
local xml="$(echo "$out" | xmllint --format - 2>/dev/null)"
if [ $err -ne 0 -o -z "$out" ]; then
echo "$out"
if [[ "$out" =~ "Error:" ]]; then
return 7
else
return 4
fi
fi
error=$(echo "$xml" | grep "<Error>")
if [ -n "$error" ]; then
echo $error | sed -e "s|</\?Error>||g"
return 7
fi
echo "$xml"
return $err
}
function adei_value_query {
local out # Local on the same string breaks error reporting
out="$(adei_xml_query "$@")"; local err=$?
[ $err -ne 0 ] && { echo "$out"; return $err; }
local values
values="$(echo "$out" | grep "Value")"; err=$?
[ $err -ne 0 ] && return 6
echo "$values"
}
function adei_get_databases {
local out
out="$(adei_xml_query "list.php?target=databases" "$@")"
[ $err -gt 0 ] && { echo -n "$out"; return $err; }
echo "$out" | grep "Value" | sed -e "s/^.*db_name=\"\([^\"]*\)\".*$/\\1/" | sed -e "s/ /::space::/"
}
function adei_query_version {
local version # Local on the same string breaks error reporting
version="$(adei_text_query "info.php?target=version&encoding=text" "$@")"; local err=$?
[ $err -gt 0 ] && { echo "$version"; return $err; }
adei_version="$version"
adei_revision=$(echo $adei_version | cut -d '-' -f 1)
if [ "$adei_revision" == "$adei_version" ]; then
adei_date=""
else
adei_date=$(echo $adei_version | cut -d '-' -f 2)
fi
}
function adei_resolve_id {
fn="$1"
id="$2"
host="$3"
setup="$4"
local var=$(cat "$fn" | grep "^$id" | awk '{ print $2 }')
[ -z "$var" -a -n "$host" ] && var=$(cat "$fn" | grep "^$host" | awk '{ print $2 }')
[ -z "$var" -a -n "$setup" ] && var=$(cat "$fn" | grep "^$setup" | awk '{ print $2 }')
[ -n "$debug" ] && echo "$(date) resolved ($var) from $fn (id=$id, host=$host, setup=$setup) pwd ($(pwd))" >> /tmp/resolv.log
echo "$var"
}
function adei_init_ {
local id="$1" && shift # Either URL or [setup]@[host]
# local url="$1" && shift
local server="$1" && shift
local database="$1" && [[ ! "$server" =~ \&|= ]] && shift
adei_args=( "$@" )
[ -z "$id" ] && { echo "ADEI ID is not specified" && exit 8 ; }
local url
local host
local setup
if [[ $id =~ http.*:// ]]; then # url
url="$id"
host="$(echo ${url#http*://} | cut -d '/' -f 1)" # parse port, maybe
unset $setup
elif [[ $id =~ : ]]; then # [setup@]host
local seho
IFS='@' read -ra seho <<< "$id" && shift
host="${seho[1]:-${seho[0]}}" # parse port, for sure
setup="${seho[1]:+${seho[0]}}"
unset $url
else # [setup]@[host] or both, no port
local seho
IFS='@' read -ra seho <<< "$id" && shift # techincally we can try to resolve to decide which is which
host="${seho[1]:-${seho[0]}}" # This could be mixed up, but it is either not important, or correct, or misconfigured anyway
setup="${seho[1]:+${seho[0]}}" # Only error if only setup is provided. But we handle it in resolution code which agnostic and treats them as ids
url=$(adei_resolve_id "setup/adei.txt" "$id" "$host" "$setup")
if [ -n "$url" ]; then # Get URL, now we can determine host correctly and ignore the ids.
host="$(echo ${url#http*://} | cut -d '/' -f 1)"
unset $setup
else # only 'setup' is not allowed if URL is not configured, so it should be host (or error in configuration)
unset $url # so either we have both (correctly) or only host (correctly)
fi
fi
local hopo
IFS=':' read -ra hopo <<< "$host" && shift
host="${hopo[0]}" # only non fqdn if also url not set
port=":${hopo[1]:-80}"
[ $port = ":80" ] && port=""
# Preliminary for the sake of error reporting
adei_host="$host"
adei_port="$port"
local fqdn
if [ -z "$url" ]; then
url=$(adei_resolve_id "setup/adei.txt" "$id" "$host" "$setup")
if [ -n "$url" ]; then
fqdn="$(echo ${url#http*://} | cut -d '/' -f 1)"
IFS=':' read -ra hopo <<< "$fqdn" && shift
host="${hopo[0]}" # again fqdn
port=":${hopo[1]:-80}"
[ $port = ":80" ] && port=""
else
fqdn=$(resolve_fqdn "$host") # this may be not fqdn
[ -n "$fqdn" ] && host="$fqdn"
url="${host}${port}/adei"
fi
fi
# In some case we miss here "setup", but surely then the password is resolved host-based
adei_auth=$(adei_resolve_id "security/adei.txt" "$id" "$host" "$setup")
# Now build ADEI url
adei_setup="$setup"
adei_host="$host"
adei_port="$port"
adei_url="${url#http*://}/services"
# Now check port
adei_online=$(scripts/ping.pl "$host" "$port")
[ $adei_online -ne 1 ] && return 2
adei_query_version; local err=$?
[ $err -gt 0 ] && return $err
if [[ "$server" =~ \&|= ]]; then
adei_source="&$server"
else
adei_source=""
[ -n "$adei_setup" -a "$adei_setup" != "*" ] && adei_source+="&setup=$adei_setup"
[ -n "$server" -a "$server" != "-" ] && adei_source+="&db_server=$server"
if [ "$database" == "#1" ]; then
databases="$(adei_get_databases)"; err=$?
[ $err -gt 0 ] && { echo "Failed to query ADEI databases: $databases"; return $err; }
[ -z "$databases" ] && { echo "No databases reported by ADEI"; return 6; }
database=$(echo "$databases" | head -n 1)
fi
[ -n "$database" -a "$database" != "-" ] && adei_source+="&db_name=$database"
fi
return 0
}
function adei_init {
adei_init_ "$@"; local code=$?
adei_healthy=$(($code == 0))
adei_process_error "$code" "" "*" "$(adei_print_status "$0" $adei_online $adei_healthy)"
}
|