bbfdm/scripts/serverselection_launch

120 lines
5.6 KiB
Bash
Executable file

#!/bin/sh
# Copyright (C) 2019 iopsys Software Solutions AB
# Author: AMIN Ben Ramdhane <amin.benramdhane@pivasoftware.com>
source /usr/share/bbfdm/bbf_uci_api
serverselection_launch() {
local i proto device res ba stc times sc1 success_count min_time avg_time max_time avg_time_sum min max micros
local fasthost avg_time_host min_time_host max_time_host
local hostlist=`uci_get_bbf_dmmap dmmap_diagnostics.serverselection.HostList`
local cnt=`uci_get_bbf_dmmap dmmap_diagnostics.serverselection.NumberOfRepetitions 3`
local timeout=`uci_get_bbf_dmmap dmmap_diagnostics.serverselection.Timeout 1000`
local port=`uci_get_bbf_dmmap dmmap_diagnostics.serverselection.port`
local interface=`uci_get_bbf_dmmap dmmap_diagnostics.serverselection.interface`
local protoversion=`uci_get_bbf_dmmap dmmap_diagnostics.serverselection.ProtocolVersion Any`
local 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
[ "$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
[ ! -z "$interface" ] && device=`ifstatus $interface | jsonfilter -e @.device` && device="-I $device" || device=""
while [ $i -lt $cnt ]; do
let i++
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; break; }
ba=`echo "$res" | grep "unknown host"`
[ -n "$ba" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBF_DMMAP; break; }
stc=`echo "$res" | grep "received"`
[ -z "$stc" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=Error_Other; $UCI_COMMIT_BBF_DMMAP; break; }
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 $1 }'`
avg_time=`echo $times | awk -F'[=/ ]' '{ print $2 }'`
max_time=`echo $times | awk -F'[=/ ]' '{ print $3 }'`
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
[ "$port" = "" ] && return
[ ! -z "$interface" ] && device=`ifstatus $interface | jsonfilter -e @.device` && device="-e $device" || device=""
while [ $i -lt $cnt ]; do
let i++
res=$(nping $proto -c 1 --udp --dest-port $port --data-length 24 $device $host 2>&1)
ba=`echo "$res" | grep "RCVD"`
[ -z "$ba" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBF_DMMAP; return; }
stc=`echo "$res" | grep "RCVD" | grep "unreachable"`
[ -n "$stc" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.serverselection.DiagnosticState=Error_Other; $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() {
local pids=`ps | grep serverselection_launch | grep -v grep | awk '{print $1}'`
if [ -n "$pids" ]; then
kill -9 $pids &>/dev/null
$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