44 lines
1.2 KiB
Bash
Executable File
44 lines
1.2 KiB
Bash
Executable File
#!/bin/bash
|
||
source "${cfg[namespace]}/code/func.sh"
|
||
|
||
json="$(jq -r '.[] | select(.type == "ping") | "\(.label)<29>\(.data.addr)"' < storage/appconfig/$1.json)"
|
||
|
||
IFS=$'\n'
|
||
for i in $json; do
|
||
label="$(awk -F<> '{print $1}' <<< "$i")"
|
||
if [[ "$label" != '' ]]; then
|
||
label_sha="$(shasum <<< "$label" | cut -c 1-16)"
|
||
addr="$(awk -F<> '{print $2}' <<< "$i")"
|
||
res="$(ping -c 1 $addr > >(grep icmp_seq | sed -E 's/.*icmp_seq=.//'))"
|
||
status=$?
|
||
|
||
if [[ "$status" == 0 ]]; then
|
||
if [[ "$res" == *"time"* ]]; then
|
||
res_parsed="$(grep -Eoh "time=.*ms" <<< "$res" | sed -E 's/time=//g;s/ms//g' | tr -d ' ')"
|
||
else
|
||
res_parsed='#'
|
||
fi
|
||
else
|
||
res_parsed='#'
|
||
fi
|
||
|
||
unset json_res
|
||
declare -A json_res
|
||
json_res[type]="ping"
|
||
json_res[label]="$label"
|
||
json_res[status]="$status"
|
||
json_res[time]="$res_parsed"
|
||
json_res[res]="$res"
|
||
json_res[date]="$(date "+%T %d.%m.%y")"
|
||
|
||
json_object json_res >> "storage/data/$(shasum <<< "$label" | cut -c 1-16)"
|
||
if [[ "$status" == 0 && -f "storage/reports/$label_sha" ]]; then
|
||
rm "storage/reports/$label_sha"
|
||
notify="$(jq -r '.[] | select(.label == "'"$label"'").notify[]' < storage/appconfig/$1.json)"
|
||
notify "$notify" 0
|
||
fi
|
||
fi
|
||
done
|
||
|
||
cleanup
|