+ TCP/UDP port checks

master
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

@ -1,7 +1,7 @@
#!/bin/bash
for monitor in storage/data/*; do
if [[ "$(tail -n1 < $monitor | jq -r '.status')" == 1 ]]; then
if [[ "$(tail -n1 < $monitor | jq -r '.status')" == 1 ]]; then
label="$(tail -n1 < $monitor | jq -r '.label')"
label_sha="$(shasum <<< "$label" | cut -c 1-16)"
if [[ ! -f "storage/reports/$label_sha" ]]; then

View File

@ -6,34 +6,38 @@ json="$(jq -r '.[] | select(.type == "ping") | "\(.label)<29>\(.data.addr)"' < st
IFS=$'\n'
for i in $json; do
label="$(awk -F<> '{print $1}' <<< "$i")"
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 ' ')"
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
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")"
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
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

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,38 +6,42 @@ json="$(jq -r '.[] | select(.type == "request") | "\(.label)<29>\(.data.url)<29>\(
IFS=$'\n'
for i in $json; do
label="$(awk -F<> '{print $1}' <<< "$i")"
label_sha="$(shasum <<< "$label" | cut -c 1-16)"
url="$(awk -F<> '{print $2}' <<< "$i")"
method="$(awk -F<> '{print $3}' <<< "$i")"
match="$(awk -F<> '{print $4}' <<< "$i")"
string="$(awk -F<> '{print $5}' <<< "$i")"
data="$(awk -F<> '{print $6}' <<< "$i")"
if [[ "$label" != '' ]]; then
label_sha="$(shasum <<< "$label" | cut -c 1-16)"
url="$(awk -F<> '{print $2}' <<< "$i")"
method="$(awk -F<> '{print $3}' <<< "$i")"
match="$(awk -F<> '{print $4}' <<< "$i")"
string="$(awk -F<> '{print $5}' <<< "$i")"
data="$(awk -F<> '{print $6}' <<< "$i")"
[[ "$method" == '' ]] && method="GET"
if [[ "$match" == "body" ]]; then
res="$(curl -A "${cfg[useragent]}" -s -X "$method" "$url" --data "$data" | grep -E "$string")"
status=$?
elif [[ "$match" == "head" ]]; then
res="$(curl -A "${cfg[useragent]}" -sD - -o /dev/null -X "$method" "$url" --data "$data" | grep -E "$string")"
status=$?
elif [[ "$match" == "status" ]]; then
res="$(curl -A "${cfg[useragent]}" -sD - -o /dev/null -X "$method" "$url" --data "$data" | grep -E "HTTP.*? $string")"
status=$?
fi
[[ "$method" == '' ]] && method="GET"
if [[ "$match" == "body" ]]; then
res="$(curl -A "${cfg[useragent]}" -s -X "$method" "$url" --data "$data" | grep -E "$string")"
status=$?
elif [[ "$match" == "head" ]]; then
res="$(curl -A "${cfg[useragent]}" -sD - -o /dev/null -X "$method" "$url" --data "$data" | grep -E "$string")"
status=$?
elif [[ "$match" == "status" ]]; then
res="$(curl -A "${cfg[useragent]}" -sD - -o /dev/null -X "$method" "$url" --data "$data" | grep -E "HTTP.*? $string")"
status=$?
fi
unset json_res
declare -A json_res
json_res[type]="req"
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/$label_sha"
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
unset json_res
declare -A json_res
json_res[type]="req"
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/$label_sha"
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

@ -14,21 +14,22 @@ 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
@ -36,6 +37,19 @@ elif [[ "$(jq -r '.type' < $data | head -n 1)" == "req" ]]; then
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
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
fi
svg="$(echo -e "$label\n$timedata" | awk -f "${cfg[namespace]}/code/plot.awk")"

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