Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- domain=example.geek # domain name to resolve
- n_nameservers=2 # /etc/resolv.conf must list 2 OpenNIC resolvers at the top of the file
- timeout=1 # seconds
- n_iterations=2 # n samples to average
- action=$1
- nss=()
- # read only first n_nameservers
- function get_nameservers {
- local re='^nameserver[[:space:]]+(.+)'
- local n=0
- while read -r line; [ $n -lt $n_nameservers ]; do
- [[ "$line" =~ $re ]]
- if [ -n "${BASH_REMATCH[1]}" ]; then
- let ++n
- echo "${BASH_REMATCH[1]}"
- fi
- done </etc/resolv.conf
- }
- nss=( $(get_nameservers) )
- case "$action" in
- config)
- echo "graph_title OpenNIC DNS response time"
- echo "graph_category network"
- echo "graph_vlabel % fail (-) / time (ms) (+)"
- echo "graph_info This plugin monitors OpenNIC DNS response time. A single look-up for the target domain" \
- "is sent and the time to get a valid response is shown in milliseconds. The query is sent" \
- "to the first $n_nameservers resolvers in /etc/resolv.conf. The query timeout is $timeout seconds."
- for i in $(eval echo {0..$((${#nss[@]} - 1))}); do
- ns=${nss[$i]}
- echo "time${i}.label $ns time"
- echo "time${i}.min 0"
- echo "time${i}.max $((1000 * timeout))"
- echo "time${i}.warning 250"
- echo "time${i}.info DNS response time for $ns"
- echo "loss${i}.label $ns fail"
- echo "loss${i}.min -100"
- echo "loss${i}.max 0"
- echo "loss${i}.warning -50:"
- echo "loss${i}.info Failed requests for $ns (percent)"
- done
- ;;
- "")
- for i_ns in $(eval echo {0..$((${#nss[@]} - 1))}); do
- ns=${nss[$i_ns]}
- time=0
- lost=0
- for i_it in $(eval echo {0..$((n_iterations - 1))}); do
- out=$(dig +notcp +ignore +noall +stats +time=$timeout +tries=1 +retry=0 +fail @${ns} ${domain}. A)
- re='Query time: ([[:digit:]]+) msec'
- if [[ "$out" =~ $re ]]; then
- let time+=${BASH_REMATCH[1]}
- fi
- re='no servers could be reached'
- if [[ "$out" =~ $re ]]; then
- let ++lost
- fi
- done
- let time/=n_iterations
- let lost=-100*lost/n_iterations
- echo "time${i_ns}.value ${time}"
- echo "loss${i_ns}.value ${lost}"
- done
- ;;
- *)
- echo "unrecognized action: '$action'"
- exit 1
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment