mirror of
https://dev.iopsys.eu/bbf/bbfdm.git
synced 2025-12-10 07:44:39 +01:00
131 lines
5.5 KiB
Bash
Executable file
131 lines
5.5 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_error()
|
|
{
|
|
local cnt=`ipping_get cwmp.@ippingdiagnostic[0].NumberOfRepetitions 3`
|
|
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].SuccessCount=0
|
|
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].FailureCount=$cnt
|
|
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].AverageResponseTime=0
|
|
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].MinimumResponseTime=9999
|
|
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].MaximumResponseTime=0
|
|
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].AverageResponseTimeDetailed=0
|
|
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].MinimumResponseTimeDetailed=999999999
|
|
$UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].MaximumResponseTimeDetailed=0
|
|
}
|
|
|
|
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 proto=`ipping_get cwmp.@ippingdiagnostic[0].ProtocolVersion Any`
|
|
[ "$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=$(ping -q -c 1 -s $dsize -W $timeout $host 2>&1)
|
|
[ "$proto" = "IPv4" ] && res=$(ping -q -4 -c 1 -s $dsize -W $timeout $host 2>&1)
|
|
[ "$proto" = "IPv6" ] && res=$(ping -q -6 -c 1 -s $dsize -W $timeout $host 2>&1)
|
|
ba=`echo "$res" | grep "bad address"`
|
|
[ -n "$ba" ] && { $UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].DiagnosticState=Error_CannotResolveHostName; ipping_error; return; }
|
|
ba=`echo "$res" | grep "unknown host"`
|
|
[ -n "$ba" ] && { $UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].DiagnosticState=Error_CannotResolveHostName; ipping_error; return; }
|
|
stc=`echo "$res" | grep "received"`
|
|
[ -z "$stc" ] && { $UCI_SET_VARSTATE cwmp.@ippingdiagnostic[0].DiagnosticState=Error_Other; ipping_error; return; }
|
|
times=`echo "$res" | grep "min/avg/max"`
|
|
[ -z "$times" ] && continue
|
|
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_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
|