From c8c277200169dfadf88dd176e5c056967550e059 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Sun, 25 Aug 2019 03:49:35 +0200 Subject: Add remote services --- remote/lib/status.sh | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 remote/lib/status.sh (limited to 'remote/lib/status.sh') diff --git a/remote/lib/status.sh b/remote/lib/status.sh new file mode 100644 index 0000000..3c7fbec --- /dev/null +++ b/remote/lib/status.sh @@ -0,0 +1,177 @@ +. lib/parameters.sh +. lib/print.sh +. lib/report.sh + +function check_server_traffic { + local result="$1" + local yellow=${2:-0} + local red=${3:-0} + + local name=("in" "out" "fwd") + + local traf + read -ra traf <<< $result + if [ ${#traf[@]} -ne 3 ]; then + [ -z "$result" ] && result="No info received" + print "$result" "red" $fold 1 + return + fi + + local output="" + for i in $(seq 0 2); do + if ! [[ ${traf[$i]} =~ ^[0-9]+$ ]]; then + print "$result" "red" $fold 1 + return + fi + + local val=$(printf "% 4u" $((${traf[$i]} / 1024 / 1024 / 1024))) + if [ $red -gt 0 -a $val -gt $red ]; then + val=$(decorate "$val GB" "red") + elif [ $yellow -gt 0 -a $val -gt $yellow ]; then + val=$(decorate "$val GB" "yellow") + else + val="$val GB" + fi + +# output="$output${output:+, }${name[$i]}: $val" + output="$output${output:+, } $val" + done + +# print "$output" + print "in/out/fwd: $output" +} + +function check_server_ { + local service=$1 && shift + + local hopo + IFS=':' read -ra hopo <<< $1 && shift + local host=${hopo[0]} + local port=${hopo[1]:-22} + + local result=$(ssh $ssh_params -p $port root@$host /opt/scripts/check_server_$service.sh 2>&1 ) + + case "$service" in + 'traffic') + check_server_traffic "$result" "$@" + ;; + *) + print "$result" + esac + +} + + +function check_server_status { + local hopo + IFS=':' read -ra hopo <<< $1 && shift + local host=${hopo[0]} + local port=${hopo[1]:-22} + local services="$@" + + local ping_output=$(scripts/ping.pl $host $port $timeout) + print_status "$ping_output" + + if [ "$services" == "-" ]; then return; fi + + if [ "$ping_output" == "1" ]; then + local output=$(ssh $ssh_params -p $port root@$host /opt/scripts/check_server_status.sh 2>&1) + + local status=1 + [ -n "$output" ] && status=0 && output="\n$(decorate "$host:$port" "cyan" "u")\n$output\n" + print_status $status + + # Check Additional services, error is reported if anything provides information + local headers="" + if [ -n "$services" ]; then + for service in "$services"; do + if [[ "$service" =~ ^vpn/(.*)$ ]]; then + local ip=${BASH_REMATCH[1]} + [ $(hostname) == "styx" ] && check_server_status "$ip" "-" + else + local service_status=1 + local service_output=$(ssh $ssh_params -p $port root@$host /opt/scripts/check_${service}_status.sh 2>&1 | sed 's/^[[:space:]]*//;/^$/d') + if [ -n "$service_output" ]; then + first_line=$(echo "$service_output" | head -n 1) + if [[ "$first_line" =~ ^([0-9]+)[[:space:]](.*)$ ]]; then + service_status=${BASH_REMATCH[1]} + service_output=$(sed "1 d" <<< "$service_output") + if [ -n "${BASH_REMATCH[2]}" ]; then + service_header=$(sed -r "s/\\$\{color\s+([^}]*)\}/$(set_color \\1)/g" <<< "${BASH_REMATCH[2]}") #" + headers="$headers $service_header" + fi + else + service_status=0 + fi + + if [ -n "$service_output" ]; then + [ -z "$output" ] && output="\n$(decorate "$host:$port" "cyan" "u")" + output="${output}\n $(decorate "$service" "gray" "u")\n$(echo ${service_output} | sed 's/^/ /')" + fi + fi + fi + + done + fi + + print " ::: ${headers}" + #report="$report
$output" + echo "${output}" >&12 + else + print_status "x" + for service in "$services"; do + print_status "x" + done + fi +} + +function check_service { + local service="$1" && shift + local id="$1" && shift + local args="$@" + + local output=$(service/check_${service}.sh ${id} "$@" 2>&1 | sed 's/^[[:space:]]*//;/^$/d') + + local info=$(echo "$output" | tail -n 1) + + local online=0 + local health=0 + local header="" + if [[ "$info" =~ ^([0-9]+)[[:space:]]+([0-9]+)[[:space:]]+(.*)$ ]]; then + online=${BASH_REMATCH[1]} + health=${BASH_REMATCH[2]} + header=${BASH_REMATCH[3]} + + output=$(sed '$ d' <<< $ouput) + fi + + print_status "$online" + print_status "$health" + + headers=$(sed -r "s/\\$\{color\s+([^}]*)\}/$(set_color \\1)/g" <<< "$header") # " + print " ::: ${headers}" + + + important=$(grep "^\*" <<< $output) + messages=$(grep -v "^\*" <<< $output) + [ -n "$output" ] && output="\n$(decorate "$service:$id" "cyan" "u")\n$(set_color white)$important\n$(reset_color)$messages\n" +} + + + + +function check { + local args + local title="$1" && shift + read -ra args <<< "$1" && shift + local host=${args[0]}; + + print "$(decorate $title "purple") ::: " | sed -r "s/\\$\{color\s+([^}]*)\}/$(set_color \\1)/g; " + + local service + for service in "$@"; do + service=$(sed -r "s/<([0-9]+)>/\${args[\\1]}/g; s/<([^>]+)>/\${\\1}/g" <<< "$service") # " + ( eval "$service" ) + done + print_eol +} -- cgit v1.2.3