diff --git a/code/func.sh b/code/func.sh index 6dfae33..73ad456 100644 --- a/code/func.sh +++ b/code/func.sh @@ -61,3 +61,7 @@ function notify() { fi done } + +function cleanup() { + unset label label_sha json_res json addr res status res_parsed +} diff --git a/code/notify.sh b/code/notify.sh index 9fdb9f0..33c61ac 100755 --- a/code/notify.sh +++ b/code/notify.sh @@ -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 diff --git a/code/ping.sh b/code/ping.sh index 61784d6..de84062 100755 --- a/code/ping.sh +++ b/code/ping.sh @@ -6,34 +6,38 @@ json="$(jq -r '.[] | select(.type == "ping") | "\(.label)�\(.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 diff --git a/code/port.sh b/code/port.sh new file mode 100755 index 0000000..5dc667c --- /dev/null +++ b/code/port.sh @@ -0,0 +1,33 @@ +#!/bin/bash +source "${cfg[namespace]}/code/func.sh" + +json="$(jq -r '.[] | select(.type == "port") | "\(.label)�\(.data.addr)�\(.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 diff --git a/code/req.sh b/code/req.sh index fe786f3..e8881a3 100755 --- a/code/req.sh +++ b/code/req.sh @@ -6,38 +6,42 @@ json="$(jq -r '.[] | select(.type == "request") | "\(.label)�\(.data.url)�\( 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 diff --git a/webroot/uwu.shs b/webroot/uwu.shs index 80fd63a..1523901 100644 --- a/webroot/uwu.shs +++ b/webroot/uwu.shs @@ -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@'"'"'>'"$line"' $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@'"'"'>'"$line"' $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@'"'"'>'"$line"' $tmp fi svg="$(echo -e "$label\n$timedata" | awk -f "${cfg[namespace]}/code/plot.awk")" diff --git a/workers/checker/worker.sh b/workers/checker/worker.sh index 5ba582f..a286fcb 100755 --- a/workers/checker/worker.sh +++ b/workers/checker/worker.sh @@ -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