dtr/code/port.sh

34 lines
979 B
Bash
Executable File
Raw Blame History

#!/bin/bash
source "${cfg[namespace]}/code/func.sh"
json="$(jq -r '.[] | select(.type == "port") | "\(.label)<29>\(.data.addr)<29>\(.data.port)"' < 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")"
port="$(awk -F<> '{print $3}' <<< "$i")"
res="$(timeout 1 nc -zv $addr $port)"
status=$?
unset json_res
declare -A json_res
json_res[type]="port"
json_res[label]="$label"
json_res[status]="$status"
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