bbfdm/scripts/functions/ipping_launch
2019-12-04 09:52:05 +01:00

118 lines
4.8 KiB
Bash
Executable file

#!/bin/sh
# Copyright (C) 2019 iopsys Software Solutions AB
# Author: MOHAMED Kallel <mohamed.kallel@pivasoftware.com>
# Author: AMIN Ben Ramdhane <amin.benramdhane@pivasoftware.com>
UCI_CONFIG_DIR="/etc/config/"
UCI_GET_VARSTATE="/sbin/uci -q ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state get"
UCI_SET_VARSTATE="/sbin/uci -q ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state set"
ipping_get() {
local val=`$UCI_GET_VARSTATE $1`
echo ${val:-$2}
}
ipping_launch() {
local i res ba stc times sc1 success_count failure_count min_time avg_time max_time avg_time_sum min max micros avg_time_det min_time_det max_time_det avg_time_sum_det min_det max_det
[ "$1" == "cwmp" ] && [ "`$UCI_GET_VARSTATE cwmp.@ippingdiagnostic[0].DiagnosticState`" != "Requested" ] && return
local host=`ipping_get cwmp.@ippingdiagnostic[0].Host`
local cnt=`ipping_get cwmp.@ippingdiagnostic[0].NumberOfRepetitions 3`
local dsize=`ipping_get cwmp.@ippingdiagnostic[0].DataBlockSize 64`
local timeout=`ipping_get cwmp.@ippingdiagnostic[0].Timeout 1000`
local dscp=`ipping_get cwmp.@ippingdiagnostic[0].DSCP 0`
local proto=`ipping_get cwmp.@ippingdiagnostic[0].ProtocolVersion Any`
local tos=$((dscp<<2))
[ "$host" = "" ] && return
timeout=$((timeout/1000))
[ "$timeout" = "0" ] && timeout="1"
micros=1000
success_count=0
avg_time_sum=0
avg_time_sum_det=0
min=9999999
min_det=999999999
max=0
max_det=0
i=0
while [ $i -lt $cnt ]; do
let i++
[ "$proto" = "Any" ] && res=$(fping -o -c 1 -b $dsize -t $timeout -O $tos $host 2>&1)
[ "$proto" = "IPv4" ] && res=$(fping -4 -o -c 1 -b $dsize -t $timeout -O $tos $host 2>&1)
[ "$proto" = "IPv6" ] && res=$(fping -6 -o -c 1 -b $dsize -t $timeout -O $tos $host 2>&1)
ba=`echo "$res" | grep "not known"`
[ -n "$ba" ] && { $UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].DiagnosticState=Error_CannotResolveHostName; return; }
stc=`echo "$res" | grep "loss"`
[ -z "$stc" ] && { $UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].DiagnosticState=Error_Other; return; }
times=`echo "$res" | grep "min/avg/max"`
[ -z "$times" ] && continue
sc1=`echo $stc | awk -F'/' '{ print $4 }'`
sc1=${sc1:-0}
success_count=$((success_count+sc1))
times=`echo $times | awk -F'=' '{ print $4 }'`
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_det=$(echo $min_time $micros | awk '{printf "%3.0f\n",$1*$2}')
avg_time_det=$(echo $avg_time $micros | awk '{printf "%3.0f\n",$1*$2}')
max_time_det=$(echo $max_time $micros | awk '{printf "%3.0f\n",$1*$2}')
min_time=$(awk "BEGIN{print $min_time * 1000}")
avg_time=$(awk "BEGIN{print $avg_time * 1000}")
max_time=$(awk "BEGIN{print $max_time * 1000}")
[ $min_time -lt $min ] && min=$min_time
[ $max_time -gt $max ] && max=$max_time
avg_time_sum=$((avg_time_sum+avg_time))
[ $min_time_det -lt $min_det ] && min_det=$min_time_det
[ $max_time_det -gt $max_det ] && max_det=$max_time_det
avg_time_sum_det=$((avg_time_sum_det+avg_time_det))
done
failure_count=$((cnt-success_count))
[ $success_count -gt 0 ] && avg_time=$((avg_time_sum/success_count)) || avg_time=0
[ $success_count -gt 0 ] && avg_time_det=$((avg_time_sum_det/success_count)) || avg_time_det=0
avg_time=$(awk "BEGIN{print int($avg_time / 1000)}")
min_time=$(awk "BEGIN{print int($min / 1000)}")
max_time=$(awk "BEGIN{print int($max / 1000)}")
min_time_det=$min_det
max_time_det=$max_det
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].DiagnosticState=Complete
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].SuccessCount=$success_count
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].FailureCount=$failure_count
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].AverageResponseTime=$avg_time
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].MinimumResponseTime=$min_time
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].MaximumResponseTime=$max_time
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].AverageResponseTimeDetailed=$avg_time_det
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].MinimumResponseTimeDetailed=$min_time_det
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].MaximumResponseTimeDetailed=$max_time_det
[ "$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
}
ipping_stop_diagnostic() {
local pids=`ps | grep ipping_launch | grep -v grep | awk '{print $1}'`
if [ -n "$pids" ]; then
kill -9 $pids &>/dev/null
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].DiagnosticState=None
fi
}
if [ "$1" == "run" ]; then
ipping_launch $2
elif [ "$1" == "stop" ]; then
ipping_stop_diagnostic
else
return
fi