#!/bin/sh # Copyright (C) 2019 iopsys Software Solutions AB # Author: AMIN Ben Ramdhane UCI_GET_BBFDM="/sbin/uci -q -c /etc/bbfdm get" UCI_SET_BBFDM="/sbin/uci -q -c /etc/bbfdm set" UCI_COMMIT_BBFDM="/sbin/uci -q -c /etc/bbfdm commit" serverselection_get() { local val=`$UCI_GET_BBFDM $1` echo ${val:-$2} } 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 [ "$1" == "cwmp" ] && [ "`$UCI_GET_BBFDM dmmap_diagnostics.serverselection.DiagnosticState`" != "Requested" ] && return local hostlist=`serverselection_get dmmap_diagnostics.serverselection.HostList` local cnt=`serverselection_get dmmap_diagnostics.serverselection.NumberOfRepetitions 3` local timeout=`serverselection_get dmmap_diagnostics.serverselection.Timeout 1000` local port=`serverselection_get dmmap_diagnostics.serverselection.port` local interface=`serverselection_get dmmap_diagnostics.serverselection.interface` local protoversion=`serverselection_get dmmap_diagnostics.serverselection.ProtocolVersion Any` local protocol=`serverselection_get 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_BBFDM dmmap_diagnostics.serverselection.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBFDM; break; } ba=`echo "$res" | grep "unknown host"` [ -n "$ba" ] && { $UCI_SET_BBFDM dmmap_diagnostics.serverselection.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBFDM; break; } stc=`echo "$res" | grep "received"` [ -z "$stc" ] && { $UCI_SET_BBFDM dmmap_diagnostics.serverselection.DiagnosticState=Error_Other; $UCI_COMMIT_BBFDM; 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_BBFDM dmmap_diagnostics.serverselection.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBFDM; return; } stc=`echo "$res" | grep "RCVD" | grep "unreachable"` [ -n "$stc" ] && { $UCI_SET_BBFDM dmmap_diagnostics.serverselection.DiagnosticState=Error_Other; $UCI_COMMIT_BBFDM; 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_BBFDM dmmap_diagnostics.serverselection.DiagnosticState=Complete $UCI_SET_BBFDM dmmap_diagnostics.serverselection.FastestHost=$fasthost $UCI_SET_BBFDM dmmap_diagnostics.serverselection.AverageResponseTime=$avg_time_host $UCI_SET_BBFDM dmmap_diagnostics.serverselection.MinimumResponseTime=$min_time_host $UCI_SET_BBFDM dmmap_diagnostics.serverselection.MaximumResponseTime=$max_time_host $UCI_COMMIT_BBFDM [ "$1" == "cwmp" ] && event_dignostic } event_dignostic() { local e=1 local i=0 while [ "$e" != 0 -a $i -lt 200 ]; do ubus -t 1 call tr069 inform '{"event":"8 DIAGNOSTICS COMPLETE"}' &>/dev/null e=$? [ "$e" != "0" ] && sleep 1; let i++ done } 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_BBFDM dmmap_diagnostics.serverselection.DiagnosticState=None $UCI_COMMIT_BBFDM fi } if [ "$1" == "run" ]; then serverselection_launch $2 elif [ "$1" == "stop" ]; then serverselection_stop_diagnostic else return fi