Guest User

opennic munin script

a guest
Sep 16th, 2022
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.64 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. domain=example.geek  # domain name to resolve
  4. n_nameservers=2      # /etc/resolv.conf must list 2 OpenNIC resolvers at the top of the file
  5. timeout=1            # seconds
  6. n_iterations=2       # n samples to average
  7.  
  8. action=$1
  9. nss=()
  10.  
  11. # read only first n_nameservers
  12. function get_nameservers {
  13.     local re='^nameserver[[:space:]]+(.+)'
  14.     local n=0
  15.  
  16.     while read -r line; [ $n -lt $n_nameservers ]; do
  17.         [[ "$line" =~ $re ]]
  18.  
  19.         if [ -n "${BASH_REMATCH[1]}" ]; then
  20.             let ++n
  21.             echo "${BASH_REMATCH[1]}"
  22.         fi
  23.     done </etc/resolv.conf
  24. }
  25.  
  26. nss=( $(get_nameservers) )
  27.  
  28. case "$action" in
  29.     config)
  30.         echo "graph_title OpenNIC DNS response time"
  31.         echo "graph_category network"
  32.         echo "graph_vlabel % fail (-) / time (ms) (+)"
  33.         echo "graph_info This plugin monitors OpenNIC DNS response time. A single look-up for the target domain" \
  34.              "is sent and the time to get a valid response is shown in milliseconds. The query is sent" \
  35.              "to the first $n_nameservers resolvers in /etc/resolv.conf. The query timeout is $timeout seconds."
  36.  
  37.         for i in $(eval echo {0..$((${#nss[@]} - 1))}); do
  38.             ns=${nss[$i]}
  39.  
  40.             echo "time${i}.label $ns time"
  41.             echo "time${i}.min 0"
  42.             echo "time${i}.max $((1000 * timeout))"
  43.             echo "time${i}.warning 250"
  44.             echo "time${i}.info DNS response time for $ns"
  45.             echo "loss${i}.label $ns fail"
  46.             echo "loss${i}.min -100"
  47.             echo "loss${i}.max 0"
  48.             echo "loss${i}.warning -50:"
  49.             echo "loss${i}.info Failed requests for $ns (percent)"
  50.         done
  51.         ;;
  52.  
  53.     "")
  54.         for i_ns in $(eval echo {0..$((${#nss[@]} - 1))}); do
  55.             ns=${nss[$i_ns]}
  56.             time=0
  57.             lost=0
  58.  
  59.             for i_it in $(eval echo {0..$((n_iterations - 1))}); do
  60.                 out=$(dig +notcp +ignore +noall +stats +time=$timeout +tries=1 +retry=0 +fail @${ns} ${domain}. A)
  61.  
  62.                 re='Query time: ([[:digit:]]+) msec'
  63.                 if [[ "$out" =~ $re ]]; then
  64.                     let time+=${BASH_REMATCH[1]}
  65.                 fi
  66.  
  67.                 re='no servers could be reached'
  68.                 if [[ "$out" =~ $re ]]; then
  69.                     let ++lost
  70.                 fi
  71.             done
  72.  
  73.             let time/=n_iterations
  74.             let lost=-100*lost/n_iterations
  75.  
  76.             echo "time${i_ns}.value ${time}"
  77.             echo "loss${i_ns}.value ${lost}"
  78.         done
  79.         ;;
  80.     *)
  81.         echo "unrecognized action: '$action'"
  82.         exit 1
  83.         ;;
  84. esac
Advertisement
Add Comment
Please, Sign In to add comment