bbfdm/scripts/udpecho_launch
Amin Ben Ramdhane 080f7b4f85 Ticket refs #7333: fix warnings, code analysis and clean up the code
- Fix all errors catched by cppcheck threadsafety (cppcheck --error-exitcode=0 --addon=threadsafety bbf)
 - Fix some errors catched by cppcheck cert (cppcheck --error-exitcode=0 --addon=cert bbf)
 - Add new str-protected macros instead of using str functions directly to avoid crashes
2022-02-16 10:33:59 +00:00

84 lines
3.6 KiB
Bash
Executable file

#!/bin/sh
# Copyright (C) 2022 iopsys Software Solutions AB
# Author: AMIN Ben Ramdhane <amin.benramdhane@pivasoftware.com>
. /usr/share/bbfdm/bbf_uci_api
udpecho_launch() {
host=$(uci_get_bbf_dmmap dmmap_diagnostics.udpechodiag.Host)
port=$(uci_get_bbf_dmmap dmmap_diagnostics.udpechodiag.port)
cnt=$(uci_get_bbf_dmmap dmmap_diagnostics.udpechodiag.NumberOfRepetitions 1)
dsize=$(uci_get_bbf_dmmap dmmap_diagnostics.udpechodiag.DataBlockSize 24)
dscp=$(uci_get_bbf_dmmap dmmap_diagnostics.udpechodiag.DSCP 0)
interface=$(uci_get_bbf_dmmap dmmap_diagnostics.udpechodiag.interface)
protocol=$(uci_get_bbf_dmmap dmmap_diagnostics.udpechodiag.ProtocolVersion Any)
inter_time=$(uci_get_bbf_dmmap dmmap_diagnostics.udpechodiag.InterTransmissionTime 1000)
[ -n "$interface" ] && device=$(ifstatus "$interface" | jsonfilter -e @.device) && device="-e $device" || device=""
if [ "$protocol" = "IPv4" ]; then proto="-4"; elif [ "$protocol" = "IPv6" ]; then proto="-6"; else proto=""; fi
tos=$((dscp<<2))
inter_time=$((inter_time/1000))
[ "$inter_time" = "0" ] && inter_time="1"
[ "$host" = "" ] && return
[ "$port" = "" ] && return
micros=1000
success_count=0
avg_time_sum=0
min=9999999
max=0
i=0
while [ $i -lt "$cnt" ]; do
i=$((i+1))
res=$(nping "$proto" -c 1 --tos $tos --udp --dest-port "$port" --data-length "$dsize" "$device" "$host" 2>&1)
ba=$(echo "$res" | grep "RCVD")
[ -z "$ba" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.udpechodiag.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBF_DMMAP; return; }
stc=$(echo "$res" | grep "RCVD" | grep "unreachable")
[ -n "$stc" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.udpechodiag.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" != 0 ] && sc1=1 || 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))
sleep $inter_time
done
failure_count=$((cnt-success_count))
[ $success_count -gt 0 ] && avg_time=$((avg_time_sum/success_count)) || avg_time=0
min_time=$min
max_time=$max
$UCI_SET_BBF_DMMAP dmmap_diagnostics.udpechodiag.DiagnosticState=Complete
$UCI_SET_BBF_DMMAP dmmap_diagnostics.udpechodiag.SuccessCount="$success_count"
$UCI_SET_BBF_DMMAP dmmap_diagnostics.udpechodiag.FailureCount="$failure_count"
$UCI_SET_BBF_DMMAP dmmap_diagnostics.udpechodiag.AverageResponseTime="$avg_time"
$UCI_SET_BBF_DMMAP dmmap_diagnostics.udpechodiag.MinimumResponseTime="$min_time"
$UCI_SET_BBF_DMMAP dmmap_diagnostics.udpechodiag.MaximumResponseTime="$max_time"
$UCI_COMMIT_BBF_DMMAP
}
udpecho_stop_diagnostic() {
pids=$(pgrep -f udpecho_launch)
if [ -n "$pids" ]; then
kill -9 "$pids" >/dev/null 2>&1
$UCI_SET_BBF_DMMAP dmmap_diagnostics.udpechodiag.DiagnosticState=None
$UCI_COMMIT_BBF_DMMAP
fi
}
if [ "$1" = "run" ]; then
udpecho_launch
elif [ "$1" = "stop" ]; then
udpecho_stop_diagnostic
else
return
fi