1. #!/bin/sh
  2.  
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16.  
  17. PROGNAME=`basename $0`
  18. VERSION="Version 1.3,"
  19. AUTHOR="2009, Mike Adolphs (http://www.matejunkie.com/)"
  20.  
  21. ST_OK=0
  22. ST_WR=1
  23. ST_CR=2
  24. ST_UK=3
  25. path_pid="/var/run/named"
  26. name_pid="named.pid"
  27. path_rndc="/usr/sbin"
  28. path_stats="/var/bind"
  29. path_tmp="/tmp"
  30. version=9.4
  31. pid_check=1
  32.  
  33. print_version() {
  34. echo "$VERSION $AUTHOR"
  35. }
  36.  
  37. print_help() {
  38. print_version $PROGNAME $VERSION
  39. echo ""
  40. echo "$PROGNAME is a Nagios plugin to check the bind daemon whether it's"
  41. echo "running via its pid file and then gets the statistics via rndc stats."
  42. echo "The user that run the script needs the ability to 'sudo rndc stats'!"
  43. echo "The timeframe in which the rndc stats output is updated is controlled"
  44. echo "by the check interval. The output shows amount of requests of various"
  45. echo "types occured during the last check interval."
  46. echo "The script itself is written sh-compliant and free software under the"
  47. echo "terms of the GPLv2 (or later)."
  48. echo ""
  49. echo "$PROGNAME -p/--path_pid /var/run/named -n/--name_pid named.pid"
  50. echo " -r/--path-rndc /usr/sbin -s/--path-stats /var/bind"
  51. echo " -V/--bind-version 9.3/9.4/9.5 -N"
  52. echo ""
  53. echo "Options:"
  54. echo " -p/--path-pid)"
  55. echo " Path where the pid file for bind is stored. You might need to"
  56. echo " alter this to your distribution's way of dealing with pid files."
  57. echo " Default is: /var/run/named"
  58. echo " -n/--name_pid)"
  59. echo " Name of the pid file. Default is: named.pid"
  60. echo " -r/--path-rndc)"
  61. echo " Path where the rndc binary is located. Default is: /usr/sbin"
  62. echo " -s/--path-stats)"
  63. echo " Path where the named.stats file is stored. Default is:"
  64. echo " /var/bind"
  65. echo " -t/--path-tmp)"
  66. echo " Path where the temporary named.stats excerpt is being stored."
  67. echo " Default is: /tmp"
  68. echo " -V/--bind-version)"
  69. echo " Specifies the bind version you're running. Currently there's"
  70. echo " BIND 9.3, 9.4 and 9.5 supported. Default is: 9.4"
  71. echo " -N/--no-pid-check)"
  72. echo " If you don't want that the script checks for the pid file,"
  73. echo " use this option. Default is: off"
  74. echo " -C/--chroot)"
  75. echo " If you're running BIND in a chroot environment, use this"
  76. echo " option to define a path to be chrooted. Please adjust also"
  77. echo " your sudo configuration to enable the Nagios user to chroot!"
  78. echo " Default is: /opt/chroot-bind"
  79. exit $ST_UK
  80. }
  81.  
  82. while test -n "$1"; do
  83. case "$1" in
  84. -help|-h)
  85. print_help
  86. exit $ST_UK
  87. ;;
  88. --version|-v)
  89. print_version $PROGNAME $VERSION
  90. exit $ST_UK
  91. ;;
  92. --path-pid|-p)
  93. path_pid=$2
  94. shift
  95. ;;
  96. --name-pid|-n)
  97. name_pid=$2
  98. shift
  99. ;;
  100. --path-rndc|-r)
  101. path_rndc=$2
  102. shift
  103. ;;
  104. --path-stats|-s)
  105. path_stats=$2
  106. shift
  107. ;;
  108. --path-tmp|-t)
  109. path_tmp=$2
  110. shift
  111. ;;
  112. --bind-version|-V)
  113. version=$2
  114. shift
  115. ;;
  116. --no-check-pid|-N)
  117. pid_check=0
  118. ;;
  119. --chroot|-C)
  120. path_chroot=$2
  121. shift
  122. ;;
  123. *)
  124. echo "Unknown argument: $1"
  125. print_help
  126. exit $ST_UK
  127. ;;
  128. esac
  129. shift
  130. done
  131.  
  132. check_pid() {
  133. if [ -f "$path_pid/$name_pid" ]
  134. then
  135. retval=0
  136. else
  137. retval=1
  138. fi
  139. }
  140.  
  141. trigger_stats() {
  142. if [ -n "$path_chroot" ]
  143. then
  144. sudo chroot $path_chroot $path_rndc/rndc stats
  145. else
  146. sudo $path_rndc/rndc stats
  147. fi
  148. }
  149.  
  150. copy_to_tmp() {
  151. tac $path_stats/named.stats | awk '/--- \([0-9]*\)/{p=1} p{print} /\+\+\+ \([0-9]*\)/{p=0;if (count++==1) exit}' > $path_tmp/named.stats.tmp
  152. }
  153.  
  154. get_vals() {
  155. case ${version} in
  156. 9.3)
  157. succ_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^success [0-9]*' | sort -n | awk '{print $2}'`
  158. succ_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^success [0-9]*' | sort -n | grep -m1 '^success [0-9]*' | awk '{print $2}'`
  159. ref_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^referral [0-9]*' | sort -n | awk '{print $2}'`
  160. ref_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^referral [0-9]*' | sort -n | grep -m1 '^referral [0-9]*' | awk '{print $2}'`
  161. nxrr_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^nxrrset [0-9]*' | sort -n | awk '{print $2}'`
  162. nxrr_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^nxrrset [0-9]*' | sort -n | grep -m1 '^nxrrset [0-9]*' | awk '{print $2}'`
  163. nxdom_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^nxdomain [0-9]*' | sort -n | awk '{print $2}'`
  164. nxdom_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^nxdomain [0-9]*' | sort -n | grep -m1 '^nxdomain [0-9]*' | awk '{print $2}'`
  165. rec_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^recursion [0-9]*' | sort -n | awk '{print $2}'`
  166. rec_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^recursion [0-9]*' | sort -n | grep -m1 '^recursion [0-9]*' | awk '{print $2}'`
  167. fail_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^failure [0-9]*' | sort -n | awk '{print $2}'`
  168. fail_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^failure [0-9]*' | sort -n | grep -m1 '^failure [0-9]*' | awk '{print $2}'`
  169. ;;
  170. 9.4)
  171. succ_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^success [0-9]*' | sort -n | awk '{print $2}'`
  172. succ_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^success [0-9]*' | sort -n | grep -m1 '^success [0-9]*' | awk '{print $2}'`
  173. ref_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^referral [0-9]*' | sort -n | awk '{print $2}'`
  174. ref_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^referral [0-9]*' | sort -n | grep -m1 '^referral [0-9]*' | awk '{print $2}'`
  175. nxrr_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^nxrrset [0-9]*' | sort -n | awk '{print $2}'`
  176. nxrr_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^nxrrset [0-9]*' | sort -n | grep -m1 '^nxrrset [0-9]*' | awk '{print $2}'`
  177. nxdom_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^nxdomain [0-9]*' | sort -n | awk '{print $2}'`
  178. nxdom_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^nxdomain [0-9]*' | sort -n | grep -m1 '^nxdomain [0-9]*' | awk '{print $2}'`
  179. rec_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^recursion [0-9]*' | sort -n | awk '{print $2}'`
  180. rec_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^recursion [0-9]*' | sort -n | grep -m1 '^recursion [0-9]*' | awk '{print $2}'`
  181. fail_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^failure [0-9]*' | sort -n | awk '{print $2}'`
  182. fail_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^failure [0-9]*' | sort -n | grep -m1 '^failure [0-9]*' | awk '{print $2}'`
  183. dup_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^duplicate [0-9]*' | sort -n | awk '{print $2}'`
  184. dup_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^duplicate [0-9]*' | sort -n | grep -m1 '^duplicate [0-9]*' | awk '{print $2}'`
  185. drop_1st=`cat $path_tmp/named.stats.tmp | grep -m1 '^dropped [0-9]*' | sort -n | awk '{print $2}'`
  186. drop_2nd=`cat $path_tmp/named.stats.tmp | grep -o '^dropped [0-9]*' | sort -n | grep -m1 '^dropped [0-9]*' | awk '{print $2}'`
  187. ;;
  188. 9.5)
  189. succ_1st=`grep 'resulted in successful answer' $path_tmp/named.stats.tmp | awk '{ print $1 }' | grep -m1 ''`
  190. succ_2nd=`grep 'resulted in successful answer' $path_tmp/named.stats.tmp | awk '{ print $1 }' | sort -n | grep -m1 ''`
  191. ref_1st=`grep 'resulted in referral' $path_tmp/named.stats.tmp | awk '{ print $1 }' | grep -m1 ''`
  192. ref_2nd=`grep 'resulted in referral' $path_tmp/named.stats.tmp | awk '{ print $1 }' | sort -n | grep -m1 ''`
  193. nxrr_1st=`grep 'resulted in nxrrset' $path_tmp/named.stats.tmp | awk '{ print $1 }' | grep -m1 ''`
  194. nxrr_2nd=`grep 'resulted in nxrrset' $path_tmp/named.stats.tmp | awk '{ print $1 }' | sort -n | grep -m1 ''`
  195. nxdom_1st=`grep 'resulted in NXDOMAIN' $path_tmp/named.stats.tmp | awk '{ print $1 }' | grep -m1 ''`
  196. nxdom_2nd=`grep 'resulted in NXDOMAIN' $path_tmp/named.stats.tmp | awk '{ print $1 }' | sort -n | grep -m1 ''`
  197. rec_1st=`grep 'caused recursion' $path_tmp/named.stats.tmp | awk '{ print $1 }' | grep -m1 ''`
  198. rec_2nd=`grep 'caused recursion' $path_tmp/named.stats.tmp | awk '{ print $1 }' | sort -n | grep -m1 ''`
  199. fail_1st=`grep 'resulted in SERVFAIL' $path_tmp/named.stats.tmp | awk '{ print $1 }' | grep -m1 ''`
  200. fail_2nd=`grep 'resulted in SERVFAIL' $path_tmp/named.stats.tmp | awk '{ print $1 }' | sort -n | grep -m1 ''`
  201. dup_1st=`grep 'duplicate queries received' $path_tmp/named.stats.tmp | awk '{ print $1 }' | grep -m1 ''`
  202. dup_2nd=`grep 'duplicate queries received' $path_tmp/named.stats.tmp | awk '{ print $1 }' | sort -n | grep -m1 ''`
  203. ;;
  204. esac
  205.  
  206. if [ "$succ_1st" == '' ]
  207. then
  208. success=0
  209. else
  210. success=`expr $succ_1st - $succ_2nd`
  211. fi
  212. if [ "$ref_1st" == '' ]
  213. then
  214. referral=0
  215. else
  216. referral=`expr $ref_1st - $ref_2nd`
  217. fi
  218. if [ "$nxrr_1st" == '' ]
  219. then
  220. nxrrset=0
  221. else
  222. nxrrset=`expr $nxrr_1st - $nxrr_2nd`
  223. fi
  224. if [ "$nxdom_1st" == '' ]
  225. then
  226. nxdomain=0
  227. else
  228. nxdomain=`expr $nxdom_1st - $nxdom_2nd`
  229. fi
  230. if [ "$rec_1st" == '' ]
  231. then
  232. recursion=0
  233. else
  234. recursion=`expr $rec_1st - $rec_2nd`
  235. fi
  236. if [ "$fail_1st" == '' ]
  237. then
  238. failure=0
  239. else
  240. failure=`expr $fail_1st - $fail_2nd`
  241. fi
  242. if [ "$dup_1st" == '' ]
  243. then
  244. duplicate=0
  245. else
  246. duplicate=`expr $dup_1st - $dup_2nd`
  247. fi
  248. if [ "$drop_1st" == '' ]
  249. then
  250. dropped=0
  251. else
  252. dropped=`expr $drop_1st - $drop_2nd`
  253. fi
  254. }
  255.  
  256. get_perfdata() {
  257. case ${version} in
  258. 9.3)
  259. perfdata=`echo "'success'=$success 'referral'=$referral 'nxrrset'=$nxrrset 'nxdomain'=$nxdomain 'recursion'=$recursion 'failure'=$failure"`
  260. ;;
  261. *)
  262. perfdata=`echo "'success'=$success 'referral'=$referral 'nxrrset'=$nxrrset 'nxdomain'=$nxdomain 'recursion'=$recursion 'failure'=$failure 'duplicate'=$duplicate 'dropped'=$dropped"`
  263. ;;
  264. esac
  265. }
  266.  
  267. if [ ${pid_check} == 1 ]
  268. then
  269. check_pid
  270. if [ "$retval" = 1 ]
  271. then
  272. echo "There's no pid file for bind9. Is it actually running?"
  273. exit $ST_CR
  274. fi
  275. fi
  276.  
  277. trigger_stats
  278. copy_to_tmp
  279. get_vals
  280. get_perfdata
  281.  
  282. echo "Bind9 is running. $success successfull requests, $referral referrals, $nxdomain nxdomains since last check. | $perfdata"
  283. exit $ST_OK
  284.