+ TCP/UDP port checks

This commit is contained in:
Dominika Liberda 2021-06-10 22:57:41 +02:00
parent 8dbd07bb12
commit bf4dadd8d6
7 changed files with 121 additions and 59 deletions

View file

@ -61,3 +61,7 @@ function notify() {
fi
done
}
function cleanup() {
unset label label_sha json_res json addr res status res_parsed
}

View file

@ -6,6 +6,7 @@ json="$(jq -r '.[] | select(.type == "ping") | "\(.label)<29>\(.data.addr)"' < st
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=.//'))"
@ -36,4 +37,7 @@ for i in $json; do
notify="$(jq -r '.[] | select(.label == "'"$label"'").notify[]' < storage/appconfig/$1.json)"
notify "$notify" 0
fi
fi
done
cleanup

33
code/port.sh Executable file
View file

@ -0,0 +1,33 @@
#!/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

View file

@ -6,6 +6,7 @@ json="$(jq -r '.[] | select(.type == "request") | "\(.label)<29>\(.data.url)<29>\(
IFS=$'\n'
for i in $json; do
label="$(awk -F<> '{print $1}' <<< "$i")"
if [[ "$label" != '' ]]; then
label_sha="$(shasum <<< "$label" | cut -c 1-16)"
url="$(awk -F<> '{print $2}' <<< "$i")"
method="$(awk -F<> '{print $3}' <<< "$i")"
@ -40,4 +41,7 @@ for i in $json; do
notify="$(jq -r '.[] | select(.label == "'"$label"'").notify[]' < storage/appconfig/$1.json)"
notify "$notify" 0
fi
fi
done
cleanup

View file

@ -14,21 +14,35 @@ label="$(jq -r '.label' < $data | head -n 1 | tr ' ' '_')"
start_date="$(jq -r '.date' < $data | tail -n $lim | head -n1)"
end_date="$(jq -r '.date' < $data | tail -n1)"
tmp="$(mktemp)"
type="$(jq -r '.type' < $data | head -n 1)"
if [[ "$(jq -r '.type' < $data | head -n 1)" == "ping" ]]; then
if [[ "$type" == "ping" ]]; then
timedata="$(jq -r '.time' < $data | tail -n $lim)"
while read line; do
echo -n 's@'"'"'></circle@'"'"'><title>'"$line"'</title></circle@;'
done <<< "$(jq -r '"Name: \(.label)\\\\nPing time: \(.time)ms\\\\nDate: \(.date)"' < "$data" | tail -n $lim)" > $tmp
elif [[ "$(jq -r '.type' < $data | head -n 1)" == "req" ]]; then
elif [[ "$type" == "req" ]]; then
timedata="$(jq -r '.status' < $data | tail -n $lim)"
a=""
for i in $timedata; do
if [[ "$i" -gt 0 ]]; then
a="${a}0#"
elif [[ "$i" == "0" ]]; then
elif [[ "$i" == 0 ]]; then
a="${a}1#"
fi
done
timedata="$(tr '#' '\n' <<< "$a")"
while read line; do
echo -n 's@'"'"'></circle@'"'"'><title>'"$line"'</title></circle@;'
done <<< "$(jq -r '"Name: \(.label)\\\\nDate: \(.date)"' < "$data" | tail -n $lim)" > $tmp
elif [[ "$type" == "port" ]]; then
timedata="$(jq -r '.status' < $data | tail -n $lim)"
for i in $timedata; do
if [[ "$i" -gt 0 ]]; then
a="${a}0#"
elif [[ "$i" == 0 ]]; then
a="${a}1#"
fi
done

View file

@ -12,15 +12,18 @@ fi
source "${cfg[namespace]}/code/ping.sh" every_1min
source "${cfg[namespace]}/code/req.sh" every_1min
source "${cfg[namespace]}/code/port.sh" every_1min
if [[ $counter30 == 30 ]]; then
source "${cfg[namespace]}/code/ping.sh" every_30min
source "${cfg[namespace]}/code/req.sh" every_30min
source "${cfg[namespace]}/code/port.sh" every_30min
counter30=0
fi
if [[ $counter5 == 5 ]]; then
source "${cfg[namespace]}/code/ping.sh" every_5min
source "${cfg[namespace]}/code/req.sh" every_5min
source "${cfg[namespace]}/code/port.sh" every_5min
counter5=0
fi