#!/bin/sh # Copyright (C) 2022 iopsys Software Solutions AB # Author: AMIN Ben Ramdhane . /usr/share/bbfdm/bbf_api serverselection_launch() { hostlist=$(uci_get_bbf_dmmap dmmap_diagnostics.serverselection.HostList) cnt=$(uci_get_bbf_dmmap dmmap_diagnostics.serverselection.NumberOfRepetitions 3) timeout=$(uci_get_bbf_dmmap dmmap_diagnostics.serverselection.Timeout 1000) port=$(uci_get_bbf_dmmap dmmap_diagnostics.serverselection.port 7) interface=$(uci_get_bbf_dmmap dmmap_diagnostics.serverselection.interface) protoversion=$(uci_get_bbf_dmmap dmmap_diagnostics.serverselection.ProtocolVersion Any) protocol=$(uci_get_bbf_dmmap dmmap_diagnostics.serverselection.Protocol ICMP) if [ "$protoversion" = "IPv4" ]; then proto="-4"; elif [ "$protoversion" = "IPv6" ]; then proto="-6"; else proto=""; fi ip_addr_used=$(get_ip_addr_used $protoversion $interface) $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.IPAddressUsed="$ip_addr_used" [ "$hostlist" = "" ] && return timeout=$((timeout/1000)) [ "$timeout" = "0" ] && timeout="1" micros=1000 success_count=0 avg_time_sum=0 avg_time_host=9999999 min=9999999 max=0 i=0 for host in $(echo "$hostlist" | tr "," "\n"); do if [ "$protocol" = "ICMP" ]; then [ -n "$interface" ] && device=$(ifstatus "$interface" | jsonfilter -e @.device) && device="-I $device" || device="" while [ $i -lt "$cnt" ]; do i=$((i+1)) res=$(ping -q $proto -c 1 -W $timeout $device $host 2>&1) ba=$(echo "$res" | grep "bad address") [ -n "$ba" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBF_DMMAP; return; } ba=$(echo "$res" | grep "unknown host") [ -n "$ba" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBF_DMMAP; return; } stc=$(echo "$res" | grep "received") [ -z "$stc" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=Error_Other; $UCI_COMMIT_BBF_DMMAP; return; } times=$(echo "$res" | grep "min/avg/max") [ -z "$times" ] && break sc1=$(echo "$stc" | awk '{print $4}') sc1=${sc1:-0} success_count=$((success_count+sc1)) times=$(echo "$times" | awk -F'=' '{ print $2 }') min_time=$(echo "$times" | awk -F'[=/ ]' '{ print $2 }') avg_time=$(echo "$times" | awk -F'[=/ ]' '{ print $3 }') max_time=$(echo "$times" | awk -F'[=/ ]' '{ print $4 }') min_time=${min_time:-0} avg_time=${avg_time:-0} max_time=${max_time:-0} min_time=$(echo "$min_time" "$micros" | awk '{printf "%3.0f\n",$1*$2}') avg_time=$(echo "$avg_time" "$micros" | awk '{printf "%3.0f\n",$1*$2}') max_time=$(echo "$max_time" "$micros" | awk '{printf "%3.0f\n",$1*$2}') [ "$min_time" -lt "$min" ] && min="$min_time" [ "$max_time" -gt "$max" ] && max="$max_time" avg_time_sum=$((avg_time_sum+avg_time)) done else [[ "$host" == *"."* ]] && proto="4" || proto="6" [ -n "$interface" ] && device=$(ifstatus "$interface" | jsonfilter -e @.device) && device="-i $device" || device="" while [ $i -lt "$cnt" ]; do i=$((i+1)) res=$(udpechoclientd -c 1 -t $timeout --host "$host" --port "$port" --protocol "$proto" $device 2>&1) ba=$(echo "$res" | grep "Can't Resolve Host Name") [ -n "$ba" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBF_DMMAP; return; } ba=$(echo "$res" | grep "RCVD") [ -z "$ba" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBF_DMMAP; return; } times=$(echo "$res" | grep "rtt") [ -z "$times" ] && continue sc1=$(echo "$res" | grep "Rcvd" | awk -F': ' '{print $3}' | awk -F'(' '{ print $1 }') sc1=${sc1:-0} success_count=$((success_count+sc1)) max_time=$(echo "$times" | awk -F': ' '{ print $2 }' | awk -F'ms' '{ print $1 }') min_time=$(echo "$times" | awk -F': ' '{ print $3 }' | awk -F'ms' '{ print $1 }') avg_time=$(echo "$times" | awk -F': ' '{ print $4 }' | awk -F'ms' '{ print $1 }') min_time=${min_time:-0} avg_time=${avg_time:-0} max_time=${max_time:-0} min_time=$(echo "$min_time" $micros | awk '{printf "%3.0f\n",$1*$2}') avg_time=$(echo "$avg_time" $micros | awk '{printf "%3.0f\n",$1*$2}') max_time=$(echo "$max_time" $micros | awk '{printf "%3.0f\n",$1*$2}') [ "$min_time" -lt "$min" ] && min="$min_time" [ "$max_time" -gt "$max" ] && max="$max_time" avg_time_sum=$((avg_time_sum+avg_time)) done fi [ $success_count -gt 0 ] && avg_time=$((avg_time_sum/success_count)) || avg_time=0 [ "$avg_time" != "0" ] && [ $avg_time -lt $avg_time_host ] && avg_time_host="$avg_time" && min_time_host="$min" && max_time_host="$max" && fasthost="$host" success_count=0 avg_time_sum=0 min=9999999 max=0 i=0 done $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=Complete $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.FastestHost="$fasthost" $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.AverageResponseTime="$avg_time_host" $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.MinimumResponseTime="$min_time_host" $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.MaximumResponseTime="$max_time_host" $UCI_COMMIT_BBF_DMMAP } serverselection_stop_diagnostic() { pids=$(pgrep -f serverselection_launch) if [ -n "$pids" ]; then kill -9 "$pids" >/dev/null 2>&1 $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=None $UCI_COMMIT_BBF_DMMAP fi } if [ "$1" = "run" ]; then serverselection_launch elif [ "$1" = "stop" ]; then serverselection_stop_diagnostic else return fi