bbfdm/scripts/nslookup_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

100 lines
3.5 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
LOG_FILE="/tmp/nslookup.log"
nslookup_launch() {
address=""
hostname=$(uci_get_bbf_dmmap dmmap_diagnostics.nslookup.HostName)
dnsserver=$(uci_get_bbf_dmmap dmmap_diagnostics.nslookup.DNSServer)
cnt=$(uci_get_bbf_dmmap 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
i=$((i+1))
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;
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"; success_count=$((success_count+1)); 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_BBF_DMMAP dmmap_diagnostics NSLookupResult
$UCI_SET_BBF_DMMAP dmmap_diagnostics.@NSLookupResult[$j].Status="$status"
$UCI_SET_BBF_DMMAP dmmap_diagnostics.@NSLookupResult[$j].AnswerType="$AnswerType"
$UCI_SET_BBF_DMMAP dmmap_diagnostics.@NSLookupResult[$j].HostNameReturned="$HostNameReturned"
$UCI_SET_BBF_DMMAP dmmap_diagnostics.@NSLookupResult[$j].IPAddresses="$address"
$UCI_SET_BBF_DMMAP dmmap_diagnostics.@NSLookupResult[$j].DNSServerIP="$dns_server_ip"
$UCI_SET_BBF_DMMAP dmmap_diagnostics.@NSLookupResult[$j].ResponseTime="$ResponseTime"
j=$((j+1))
address=""
fi
done <${LOG_FILE}
rm -f ${LOG_FILE}
$UCI_SET_BBF_DMMAP dmmap_diagnostics.nslookup.SuccessCount=$success_count
$UCI_SET_BBF_DMMAP dmmap_diagnostics.nslookup.DiagnosticState=Complete
$UCI_COMMIT_BBF_DMMAP
}
delete_all_results() {
for j in $($UCI_SHOW_BBF_DMMAP dmmap_diagnostics | grep "dmmap_diagnostics.@NSLookupResult.*=NSLookupResult"); do
$UCI_DELETE_BBF_DMMAP dmmap_diagnostics.@NSLookupResult[-1]
done
$UCI_COMMIT_BBF_DMMAP
}
nslookup_stop_diagnostic() {
delete_all_results
pids=$(pgrep -f nslookup_launch)
if [ -n "$pids" ]; then
kill -9 "$pids" >/dev/null 2>&1
$UCI_SET_BBF_DMMAP dmmap_diagnostics.nslookup.DiagnosticState=None
$UCI_COMMIT_BBF_DMMAP
fi
}
if [ "$1" = "run" ]; then
nslookup_launch
elif [ "$1" = "stop" ]; then
nslookup_stop_diagnostic
else
return
fi