-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathads-monitor
executable file
·349 lines (312 loc) · 8.53 KB
/
ads-monitor
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/bash
# Formatting date and time
date_msg()
{
echo "$(date +'%F %T') | $(date --date=@$1 +'%F %T') $2"
}
# Calculating transactions per second
tps() {
actual_txs=
tac $1 | grep CLOCK | head -2 | while read line
do
if [[ ${line} =~ \[([0-9]+)\]\ CLOCK:\ ([0-9A-F]+).*txs:([0-9]+) ]]; then
if [ -z "$actual_txs" ]; then
actual_time=${BASH_REMATCH[1]}
actual_clock="0x000${BASH_REMATCH[2]}"
actual_txs=${BASH_REMATCH[3]}
else
last_time=${BASH_REMATCH[1]}
last_clock="0x000${BASH_REMATCH[2]}"
last_txs=${BASH_REMATCH[3]}
if [ $(( $actual_clock )) -ge "0" ] && [ $(( $last_clock )) -lt "0" ]; then
txs=${actual_txs}
else
txs="$(($actual_txs - $last_txs))"
fi
if [ -n "$2" ]; then
date_msg ${actual_time} "0x${actual_clock: -3} $txs"
else
echo ${txs}
fi
break
fi
fi
done
}
# Parsing CLOCK log
clock() {
line=`tac $2 | grep -m 1 -E "^\[[0-9]+\]"`
regex="^\[([0-9]+)\]\ (\[.*\]\ )?([A-Z ]+:\ )?Awaiting for genesis block time:\ ([0-9]+\ s)"
if [[ ${line} =~ ${regex} ]]; then
if [ -n "$4" ]; then
if [ -n "$3" ]; then
date_msg ${BASH_REMATCH[1]} "The node will start in ${BASH_REMATCH[4]}"
else
echo 1
fi
return
else
>&2 date_msg ${BASH_REMATCH[1]} "The node will start in ${BASH_REMATCH[4]}"
echo 0
return 1
fi
fi
item=$1
if [[ "$1" = "time" || "$1" = "net" ]]; then
item=peers
fi
sync_regex="^([0-9]+):\[([0-9]+)\]\ (\[.*\]\ )?([A-Z\ ]+:\ )?START\ syncing\ header\ ([0-9A-F]+)$"
clock_regex="^([0-9]+):\[([0-9]+)\]\ (\[.*\]\ )?([A-Z\ ]+:\ )?CLOCK:\ ([0-9A-F]+).*\ ${item}:([0-9]+).*\ \[,?([,0-9]*)\]"
sync_line=`tac $2 | grep -n "START syncing header" | head -1`
clock_line=`tac $2 | grep -n CLOCK | head -1`
if [[ ${sync_line} =~ ${sync_regex} ]]; then
sync_num=${BASH_REMATCH[1]}
sync_time=${BASH_REMATCH[2]}
sync_block=${BASH_REMATCH[5]}
fi
if [[ ${clock_line} =~ ${clock_regex} ]]; then
clock_num=${BASH_REMATCH[1]}
clock_time=${BASH_REMATCH[2]}
clock_count=${BASH_REMATCH[5]}
clock_val=${BASH_REMATCH[6]}
if [ "$1" = "time" ]; then
clock_val=${clock_time}
elif [ "$1" = "net" ]; then
clock_val=${BASH_REMATCH[7]}
fi
fi
if [ -n "${sync_num}" ] && ([ -z "${clock_num}" ] || [ ${sync_num} -le ${clock_num} ]); then
left_blocks=$(( (`date +%s` / 512) - ("0x${sync_block}" / 512) ))
if [ -n "$4" ]; then
if [ -n "$3" ]; then
date_msg ${sync_time} "Database synchronizing; left ${left_blocks} blocks"
else
echo 2
fi
return
else
>&2 date_msg ${sync_time} "Database synchronizing; left ${left_blocks} blocks"
echo 0
return 2
fi
elif [ -n "${clock_num}" ]; then
clock="0x000${clock_count}"
if [ -n "$3" ]; then
date_msg ${clock_time} "0x${clock: -3} ${clock_val}"
elif [ -n "$4" ]; then
echo $(( 100 + ${clock_val} ))
else
echo ${clock_val}
fi
else
if [ -n "$4" ]; then
if [ -n "$3" ]; then
echo "Error 22: Cannot find CLOCK log"
else
echo 22
fi
exit
else
>&2 echo "Error 22: Cannot find CLOCK log"
echo 0
exit 22
fi
fi
}
# Number of awaiting transactions
txs() {
clock "txs" $1 $2
}
# Number of active peers
peers() {
clock "peers" $1 $2
}
# Checks if the node is alive
status() {
clock "peers" $1 "$2" 1
}
# Number of currently open connections
connections() {
clock "ticket" $1 $2
}
# JSON with all metrics
all_metrics() {
time=$(clock "time" $1)
time=$((( time / 512 ) * 512))
block=$(printf "%X\n" $time)
network=$(clock "net" $1)
echo "\
{\
\"date\":\"$(date --iso-8601=seconds)\",\
\"block\":\"${block}\",\
\"status\":$(status $1),\
\"transactions\":$(txs $1),\
\"peers\":$(peers $1),\
\"connections\":$(connections $1),\
\"network\":[${network}]\
}"
}
# Check node configuration
check_stderr() {
err=0
if [ ! -d ${working_dir} ]; then
message="The node has not been configured - cannot find working directory \"${working_dir}\""
err=11
elif [ ! -f ${stderr_path} ]; then
message="The node has not been started - cannot find STDERR file \"${stderr_path}\""
err=12
else
line=`tac ${stderr_path} | grep -m 1 .`
regex="^\[([0-9]+)\]"
if [[ ${line} =~ ${regex} ]]; then
if [ $(date +"%s") -ge $((BASH_REMATCH[1] + 10)) ]; then
message="The node seems to be dead"
err=13
fi
else
message="Cannot parse STDERR file \"${stderr_path}\""
err=21
fi
fi
if [ ${err} -gt 0 ] ; then
if [ "${cmd}" = "status" ]; then
if [ -n "${verbose}" ]; then
echo "Error ${err}: ${message}"
else
echo ${err}
fi
exit
else
>&2 echo "Error ${err}: ${message}"
echo 0
exit ${err}
fi
fi
}
# Display error message
show_error() {
>&2 echo "Error: $1"
>&2 echo
>&2 show_usage
exit 1
}
# Display help message
show_help() {
echo "Monitor the ADS network."
echo
show_usage
}
default_working_dir=~/.adsd
default_stderr="error.log"
# Display usage message
show_usage() {
echo "Usage"
echo " $0 [options] <command>"
echo
echo "Commands"
echo " status checks if the node is alive"
echo " tps transactions per second"
echo " txs the number of awaiting transactions"
echo " peers the number of active peers"
echo " conns the number of currently open connections"
echo " all JSON with all metrics"
echo
echo "Options"
echo " -w, --working-dir <DIR> working directory (default '${default_working_dir}')"
echo " -e, --stderr-path <FILE> absolute path to STDERR (default '${default_stderr}' in working directory)"
echo " -v, --verbose verbose mode"
echo " -c, --continuous continuous monitoring"
echo " -h, --help display this help and exit"
}
# Parsing options
PARAMS=""
while (( "$#" )); do
case "$1" in
-v|--verbose)
verbose=1
shift 1
;;
-c|--continuous)
continuous=1
shift 1
;;
-[vc][vc])
if [[ $1 = *"v"* ]]; then verbose=1; fi
if [[ $1 = *"c"* ]]; then continuous=1; fi
shift 1
;;
-w|--working-dir)
if [ -z "$2" ]; then
show_error "Working directory is required"
fi
working_dir=$2
shift 2
;;
-e|--stderr-path)
if [ -z "$2" ]; then
show_error "Path to stderr is required"
fi
stderr_path=$2
shift 2
;;
-h|-\?|--help)
show_help
exit 0
;;
--) # End argument parsing
shift
break
;;
-*|--*=) # Unsupported flags
show_error "Unsupported flag $1"
;;
*) # Preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# Set positional arguments in their proper place
eval set -- "${PARAMS}"
if [ -z "$1" ]; then
show_error "Command is required"
fi
if [ -n "$2" ]; then
show_error "To many arguments"
fi
cmd=$1
# Checking paths and dirs
if [ -z "$working_dir" ]; then
working_dir=${default_working_dir}
fi
working_dir=`readlink -m ${working_dir}`
if [ -z "$stderr_path" ]; then
stderr_path="${working_dir}/${default_stderr}"
fi
stderr_path=`readlink -m ${stderr_path}`
# Parsing command
command=
case "${cmd}" in
status) command="status ${stderr_path} ${verbose}" ;;
tps) command="tps ${stderr_path} ${verbose}" ;;
txs) command="txs ${stderr_path} ${verbose}" ;;
peers) command="peers ${stderr_path} ${verbose}" ;;
conns) command="connections ${stderr_path} ${verbose}" ;;
all) command="all_metrics ${stderr_path}" ;;
*) show_error "Unsupported command \"${cmd}\"" ;;
esac
# Runs the monitor in a loop
if [ -n "${continuous}" ]; then
while true; do
check_stderr
${command}
sleep 1
done
else
check_stderr
${command};
err=$?
if [ ${err} -gt 0 ]; then
exit ${err}
fi
fi