bbfdm/scripts/functions/upload_launch
2019-09-03 15:24:55 +01:00

124 lines
7 KiB
Bash

#!/bin/sh
# Copyright (C) 2019 iopsys Software Solutions AB
# Author: IMEN Bhiri <imen.bhiri@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"
UCI_ADD_VARSTATE="/sbin/uci -q ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state add"
UCI_DELETE_VARSTATE="/sbin/uci -q ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state delete"
CAPTURE_FILE="/tmp/upload_dump"
UPLOAD_DIAGNOSTIC_FILE="/tmp/bbfdm_upload_diagnostic"
CONNECTION_TIMEOUT=20
upload_get() {
local val=`$UCI_GET_VARSTATE $1`
echo ${val:-$2}
}
upload_launch() {
local tx_bytes_before rx_bytes_before time1 tx_bytes_after rx_bytes_after time2 res ba stc periodtime
local url=$2
local interface=$3
local size=$4
[ "$1" == "cwmp" ] && [ "`$UCI_GET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState`" != "Requested" ] && return
[ "$url" = "" ] && { $UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=Error_InitConnectionFailed; return; }
local proto=`upload_get cwmp.@uploaddiagnostic[0].ProtocolVersion Any`
# Disable acceleration on Broadcom devices to capture all packets with tcpdump
[ -e /usr/sbin/fcctl ] && { fcctl disable >/dev/null 2>&1; fcctl flush >/dev/null 2>&1; }
tcpdump -i $interface tcp -w ${CAPTURE_FILE} > /dev/null 2>&1 &
PID=$!
sleep 1
dd if=/dev/zero of=${UPLOAD_DIAGNOSTIC_FILE} bs=${size} count=1 2>/dev/null
if [ ${url:0:7} = http:// ]; then
tx_bytes_before=`ubus call network.device status "{'name':'$interface'}" | jsonfilter -e @.statistics.tx_bytes`
rx_bytes_before=`ubus call network.device status "{'name':'$interface'}" | jsonfilter -e @.statistics.rx_bytes`
time1=`date +%s`
[ "$proto" = "Any" ] && res=$(curl --fail --connect-timeout ${CONNECTION_TIMEOUT} -T ${UPLOAD_DIAGNOSTIC_FILE} $url 2>&1)
[ "$proto" = "IPv4" ] && res=$(curl -4 --fail --connect-timeout ${CONNECTION_TIMEOUT} -T ${UPLOAD_DIAGNOSTIC_FILE} $url 2>&1)
[ "$proto" = "IPv6" ] && res=$(curl -6 --fail --connect-timeout ${CONNECTION_TIMEOUT} -T ${UPLOAD_DIAGNOSTIC_FILE} $url 2>&1)
time2=`date +%s`
tx_bytes_after=`ubus call network.device status "{'name':'$interface'}" | jsonfilter -e @.statistics.tx_bytes`
rx_bytes_after=`ubus call network.device status "{'name':'$interface'}" | jsonfilter -e @.statistics.rx_bytes`
ba=`echo "$res" | grep "bad address"`
[ -n "$ba" ] && { $UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=Error_InitConnectionFailed; kill $PID &> /dev/null; return; }
stc=`echo "$res" | grep "404 Not Found"`
[ -n "$stc" ] && { $UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=Error_NoResponse; kill $PID &> /dev/null; return; }
stc=`echo "$res" |sed -n 3p|awk '{print $13}'`
[ "$stc" != "100" ] && { $UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=Error_TransferFailed; kill $PID &> /dev/null; return; }
elif [ ${url:0:6} = ftp:// ]; then
#add user and pass if they exist
substr="@"
if [ -z "${url##*$substr*}" ] ;then
url=`echo $url |sed -e "s/ftp:\/\/\([^:]*\):\([^:]*\)@\(.*\)/-u \1:\2 ftp:\/\/\3/"`
fi
tx_bytes_before=`ubus call network.device status "{'name':'$interface'}" | jsonfilter -e @.statistics.tx_bytes`
rx_bytes_before=`ubus call network.device status "{'name':'$interface'}" | jsonfilter -e @.statistics.rx_bytes`
time1=`date +%s`
[ "$proto" = "Any" ] && res=$(curl --fail --disable-epsv --ftp-pasv --connect-timeout ${CONNECTION_TIMEOUT} -T ${UPLOAD_DIAGNOSTIC_FILE} $url 2>&1)
[ "$proto" = "IPv4" ] && res=$(curl -4 --fail --disable-epsv --ftp-pasv --connect-timeout ${CONNECTION_TIMEOUT} -T ${UPLOAD_DIAGNOSTIC_FILE} $url 2>&1)
[ "$proto" = "IPv6" ] && res=$(curl -6 --fail --disable-epsv --ftp-pasv --connect-timeout ${CONNECTION_TIMEOUT} -T ${UPLOAD_DIAGNOSTIC_FILE} $url 2>&1)
time2=`date +%s`
tx_bytes_after=`ubus call network.device status "{'name':'$interface'}" | jsonfilter -e @.statistics.tx_bytes`
rx_bytes_after=`ubus call network.device status "{'name':'$interface'}" | jsonfilter -e @.statistics.rx_bytes`
ba=`echo "$res" | grep "Couldn't resolve host"`
[ -n "$ba" ] && { $UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=Error_InitConnectionFailed; kill $PID 2> /dev/null; return; }
stc=`echo "$res" | grep "Access denied"`
[ -n "$stc" ] && { $UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=Error_LoginFailed; kill $PID 2> /dev/null; return; }
stc=`echo "$res" | grep "Failed FTP upload"`
[ -n "$stc" ] && { $UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=Error_NoResponse; kill $PID 2> /dev/null; return; }
stc=`echo "$res" |tail -n 1 |awk '{print $(NF-11)}'`
[ "$stc" != "100" ] && { $UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=Error_TransferFailed; kill $PID 2> /dev/null; return; }
fi
tx_bytes=$((tx_bytes_after-tx_bytes_before))
rx_bytes=$((rx_bytes_after-rx_bytes_before))
periodtime=$(($((time2-time1))*1000000))
$UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=Completed
$UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].TestBytesSent=$size
$UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].TotalBytesReceived=$rx_bytes
$UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].TotalBytesSent=$tx_bytes
$UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].PeriodOfFullLoading=$periodtime
local perconnection=`$UCI_GET_VARSTATE cwmp.@uploaddiagnostic[0].EnablePerConnection`
if ([ "$perconnection" == "true" ] || [ "$perconnection" == "1" ]); then
$UCI_ADD_VARSTATE cwmp UploadPerConnection
$UCI_SET_VARSTATE cwmp.@UploadPerConnection[0].TestBytesSent=$size
$UCI_SET_VARSTATE cwmp.@UploadPerConnection[0].TotalBytesReceived=$rx_bytes
$UCI_SET_VARSTATE cwmp.@UploadPerConnection[0].TotalBytesSent=$tx_bytes
else
$UCI_DELETE_VARSTATE cwmp.@UploadPerConnection[0]
fi
rm ${UPLOAD_DIAGNOSTIC_FILE} &>/dev/null
sleep 3
local pids=`ps | grep $PID`
kill $PID &>/dev/null
# Enable acceleration on Broadcom devices after killing the tcpdump pid
[ -e /usr/sbin/fcctl ] && { fcctl enable >/dev/null 2>&1; fcctl flush >/dev/null 2>&1; }
}
upload_stop_diagnostic() {
$UCI_DELETE_VARSTATE cwmp.@UploadPerConnection[0]
local pids=`ps | grep upload_launch.*run | grep -v grep | awk '{print $1}'`
if [ -n "$pids" ]; then
kill -9 $pids &>/dev/null
$UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=None
fi
local pids=`ps | grep upload_launch.*run | grep -v grep | awk '{print $1}'`
if [ -n "$pids" ]; then
kids=$(grep -l "PPid.*$pids" /proc/*/task/*/status | grep -o "[0-9]*")
for kid in $kids; do
kill -9 $kid &>/dev/null
done
kill -9 $pids &>/dev/null
$UCI_SET_VARSTATE cwmp.@uploaddiagnostic[0].DiagnosticState=None
fi
}
if [ "$1" == "run" ] ; then
upload_launch $2 $3 $4 $5
elif [ "$1" == "stop" ]; then
upload_stop_diagnostic
else
return
fi