mirror of
https://dev.iopsys.eu/bbf/bbfdm.git
synced 2025-12-10 07:44:39 +01:00
124 lines
4.3 KiB
Bash
124 lines
4.3 KiB
Bash
#!/bin/sh
|
|
# Copyright (C) 2019 iopsys Software Solutions AB
|
|
# Author: AMIN Ben Ramdhane <amin.benramdhane@pivasoftware.com>
|
|
|
|
UCI_GET_BBFDM="/sbin/uci -q -c /etc/bbfdm get"
|
|
UCI_SET_BBFDM="/sbin/uci -q -c /etc/bbfdm set"
|
|
UCI_ADD_BBFDM="/sbin/uci -q -c /etc/bbfdm add"
|
|
UCI_DELETE_BBFDM="/sbin/uci -q -c /etc/bbfdm delete"
|
|
UCI_COMMIT_BBFDM="/sbin/uci -q -c /etc/bbfdm commit"
|
|
UCI_SHOW_BBFDM="/sbin/uci -q -c /etc/bbfdm show"
|
|
LOG_FILE="/tmp/nslookup.log"
|
|
|
|
nslookup_get() {
|
|
local val=`$UCI_GET_BBFDM $1`
|
|
echo ${val:-$2}
|
|
}
|
|
|
|
nslookup_launch() {
|
|
local i j time1 time2 timeresponse status AnswerType HostNameReturned address dns_server_ip ResponseTime success_count
|
|
local address=""
|
|
[ "$1" == "cwmp" ] && [ "`$UCI_GET_BBFDM dmmap_diagnostics.nslookup.DiagnosticState`" != "Requested" ] && return
|
|
local hostname=`nslookup_get dmmap_diagnostics.nslookup.HostName`
|
|
local dnsserver=`nslookup_get dmmap_diagnostics.nslookup.DNSServer`
|
|
local cnt=`nslookup_get dmmap_diagnostics.nslookup.NumberOfRepetitions 1`
|
|
[ "$hostname" = "" ] && return
|
|
i=0
|
|
j=0
|
|
success_count=0
|
|
[ -e "${LOG_FILE}" ] && rm ${LOG_FILE}
|
|
delete_all_results
|
|
while [ $i -lt $cnt ]; do
|
|
let i++
|
|
time1=`date +%s`
|
|
if [ -z "$dnsserver" ]; then
|
|
nslookup $hostname >>${LOG_FILE} 2>&1
|
|
else
|
|
nslookup $hostname $dnsserver >>${LOG_FILE} 2>&1
|
|
fi
|
|
time2=`date +%s`
|
|
timeresponse=$(($(($time2-$time1))*1000))
|
|
echo "ResponseTime: $timeresponse" >>${LOG_FILE}
|
|
echo "++++++++++++++++++++++++++++++" >>${LOG_FILE}
|
|
done
|
|
|
|
while IFS= read line; do
|
|
[ -z "$line" ] && continue;
|
|
local server=`echo "$line" | tr -d '\t' | tr -d ' ' | grep "Server:" | awk -F':' '{print $2}'`
|
|
|
|
if [[ -n "$server" && "$server" == "0.0.0.0" ]]; then
|
|
status="Error_DNSServerNotAvailable"
|
|
continue
|
|
elif [ -n "$server" ]; then
|
|
dns_server_ip=$server
|
|
continue
|
|
fi
|
|
|
|
var=`echo "$line" | tr -d '\t' | tr -d ' ' | grep "Name:" | awk -F':' '{print $2}'`
|
|
[ -n "$var" ] && { HostNameReturned=$var; status="Success"; AnswerType="Authoritative"; let success_count++; continue; }
|
|
|
|
var=`echo "$line" | grep "Address " | awk -F':' '{print substr($0, index($0,$2))}' | tr -d '\t' | tr -d ' '`
|
|
[ -n "$var" ] && { [ -z "$address" ] && address="$var" || address="$address,$var"; continue; }
|
|
|
|
var=`echo "$line" | tr -d '\t' | tr -d ' ' | grep "ResponseTime:" | awk -F':' '{print $2}'`
|
|
[ -n "$var" ] && { ResponseTime=$var; continue; }
|
|
|
|
echo $line | grep 'Can' >/dev/null 2>&1 && { continue; }
|
|
echo $line | grep 'connection timed out' >/dev/null 2>&1 && { AnswerType="None"; status="Error_Timeout"; continue; }
|
|
echo $line | grep 'Non-authoritative' >/dev/null 2>&1 && { AnswerType="NonAuthoritative"; continue; }
|
|
|
|
if echo $line | grep '++++++++++++++++++++++' >/dev/null 2>&1; then
|
|
$UCI_ADD_BBFDM dmmap_diagnostics NSLookupResult
|
|
$UCI_SET_BBFDM dmmap_diagnostics.@NSLookupResult[$j].Status=$status
|
|
$UCI_SET_BBFDM dmmap_diagnostics.@NSLookupResult[$j].AnswerType=$AnswerType
|
|
$UCI_SET_BBFDM dmmap_diagnostics.@NSLookupResult[$j].HostNameReturned=$HostNameReturned
|
|
$UCI_SET_BBFDM dmmap_diagnostics.@NSLookupResult[$j].IPAddresses=$address
|
|
$UCI_SET_BBFDM dmmap_diagnostics.@NSLookupResult[$j].DNSServerIP=$dns_server_ip
|
|
$UCI_SET_BBFDM dmmap_diagnostics.@NSLookupResult[$j].ResponseTime=$ResponseTime
|
|
let j++
|
|
address=""
|
|
fi
|
|
done <${LOG_FILE}
|
|
rm -f ${LOG_FILE}
|
|
$UCI_SET_BBFDM dmmap_diagnostics.nslookup.SuccessCount=$success_count
|
|
[ "$1" == "cwmp" ] && $UCI_SET_BBFDM dmmap_diagnostics.nslookup.DiagnosticState=Complete
|
|
$UCI_COMMIT_BBFDM
|
|
[ "$1" == "cwmp" ] && event_dignostic
|
|
}
|
|
|
|
delete_all_results() {
|
|
local j
|
|
for j in $($UCI_SHOW_BBFDM dmmap_diagnostics | grep "dmmap_diagnostics.@NSLookupResult.*=NSLookupResult"); do
|
|
$UCI_DELETE_BBFDM dmmap_diagnostics.@NSLookupResult[-1]
|
|
done
|
|
$UCI_COMMIT_BBFDM
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
nslookup_stop_diagnostic() {
|
|
delete_all_results
|
|
local pids=`ps | grep nslookup_launch | grep -v grep | awk '{print $1}'`
|
|
if [ -n "$pids" ]; then
|
|
kill -9 $pids &>/dev/null
|
|
$UCI_SET_BBFDM dmmap_diagnostics.nslookup.DiagnosticState=None
|
|
$UCI_COMMIT_BBFDM
|
|
fi
|
|
}
|
|
|
|
if [ "$1" == "run" ]; then
|
|
nslookup_launch $2
|
|
elif [ "$1" == "stop" ]; then
|
|
nslookup_stop_diagnostic
|
|
else
|
|
return
|
|
fi
|