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.1,"
  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. hostname="localhost"
  26. port=80
  27. path_pid=/var/run
  28. name_pid="nginx.pid"
  29. status_page="nginx_status"
  30. pid_check=1
  31. secure=0
  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 whether nginx is running."
  41. echo "It also parses the nginx's status page to get requests and"
  42. echo "connections per second as well as requests per connection. You"
  43. echo "may have to alter your nginx configuration so that the plugin"
  44. echo "can access the server's status page."
  45. echo "The plugin is highly configurable for this reason. See below for"
  46. echo "available options."
  47. echo ""
  48. echo "$PROGNAME -H localhost -P 80 -p /var/run -n nginx.pid "
  49. echo " -s nginx_statut -o /tmp [-w INT] [-c INT] [-S] [-N]"
  50. echo ""
  51. echo "Options:"
  52. echo " -H/--hostname)"
  53. echo " Defines the hostname. Default is: localhost"
  54. echo " -P/--port)"
  55. echo " Defines the port. Default is: 80"
  56. echo " -p/--path-pid)"
  57. echo " Path where nginx's pid file is being stored. You might need"
  58. echo " to alter this path according to your distribution. Default"
  59. echo " is: /var/run"
  60. echo " -n/--name_pid)"
  61. echo " Name of the pid file. Default is: nginx.pid"
  62. echo " -N/--no-pid-check)"
  63. echo " Turn this on, if you don't want to check for a pid file"
  64. echo " whether nginx is running, e.g. when you're checking a"
  65. echo " remote server. Default is: off"
  66. echo " -s/--status-page)"
  67. echo " Name of the server's status page defined in the location"
  68. echo " directive of your nginx configuration. Default is:"
  69. echo " nginx_status"
  70. echo " -S/--secure)"
  71. echo " In case your server is only reachable via SSL, use this"
  72. echo " this switch to use HTTPS instead of HTTP. Default is: off"
  73. echo " -w/--warning)"
  74. echo " Sets a warning level for requests per second. Default is: off"
  75. echo " -c/--critical)"
  76. echo " Sets a critical level for requests per second. Default is:"
  77. echo " off"
  78. exit $ST_UK
  79. }
  80.  
  81. while test -n "$1"; do
  82. case "$1" in
  83. -help|-h)
  84. print_help
  85. exit $ST_UK
  86. ;;
  87. --version|-v)
  88. print_version $PROGNAME $VERSION
  89. exit $ST_UK
  90. ;;
  91. --hostname|-H)
  92. hostname=$2
  93. shift
  94. ;;
  95. --port|-P)
  96. port=$2
  97. shift
  98. ;;
  99. --path-pid|-p)
  100. path_pid=$2
  101. shift
  102. ;;
  103. --name-pid|-n)
  104. name_pid=$2
  105. shift
  106. ;;
  107. --no-pid-check|-N)
  108. pid_check=0
  109. ;;
  110. --status-page|-s)
  111. status_page=$2
  112. shift
  113. ;;
  114. --secure|-S)
  115. secure=1
  116. ;;
  117. --warning|-w)
  118. warning=$2
  119. shift
  120. ;;
  121. --critical|-c)
  122. critical=$2
  123. shift
  124. ;;
  125. *)
  126. echo "Unknown argument: $1"
  127. print_help
  128. exit $ST_UK
  129. ;;
  130. esac
  131. shift
  132. done
  133.  
  134. get_wcdiff() {
  135. if [ ! -z "$warning" -a ! -z "$critical" ]
  136. then
  137. wclvls=1
  138.  
  139. if [ ${warning} -ge ${critical} ]
  140. then
  141. wcdiff=1
  142. fi
  143. elif [ ! -z "$warning" -a -z "$critical" ]
  144. then
  145. wcdiff=2
  146. elif [ -z "$warning" -a ! -z "$critical" ]
  147. then
  148. wcdiff=3
  149. fi
  150. }
  151.  
  152. val_wcdiff() {
  153. if [ "$wcdiff" = 1 ]
  154. then
  155. echo "Please adjust your warning/critical thresholds. The warning \
  156. must be lower than the critical level!"
  157. exit $ST_UK
  158. elif [ "$wcdiff" = 2 ]
  159. then
  160. echo "Please also set a critical value when you want to use \
  161. warning/critical thresholds!"
  162. exit $ST_UK
  163. elif [ "$wcdiff" = 3 ]
  164. then
  165. echo "Please also set a warning value when you want to use \
  166. warning/critical thresholds!"
  167. exit $ST_UK
  168. fi
  169. }
  170.  
  171. check_pid() {
  172. if [ -f "$path_pid/$name_pid" ]
  173. then
  174. retval=0
  175. else
  176. retval=1
  177. fi
  178. }
  179.  
  180. get_status() {
  181. if [ "$secure" = 1 ]
  182. then
  183. wget_opts="-O- -q -t 3 -T 3 --no-check-certificate"
  184. out1=`wget ${wget_opts} http://${hostname}:${port}/${status_page}`
  185. sleep 1
  186. out2=`wget ${wget_opts} http://${hostname}:${port}/${status_page}`
  187. else
  188. wget_opts="-O- -q -t 3 -T 3"
  189. out1=`wget ${wget_opts} http://${hostname}:${port}/${status_page}`
  190. sleep 1
  191. out2=`wget ${wget_opts} http://${hostname}:${port}/${status_page}`
  192. fi
  193.  
  194. if [ -z "$out1" -o -z "$out2" ]
  195. then
  196. echo "UNKNOWN - Local copy/copies of $status_page is empty."
  197. exit $ST_UK
  198. fi
  199. }
  200.  
  201. get_vals() {
  202. tmp1_reqpsec=`echo ${out1}|awk '{print $10}'`
  203. tmp2_reqpsec=`echo ${out2}|awk '{print $10}'`
  204. reqpsec=`expr $tmp2_reqpsec - $tmp1_reqpsec`
  205.  
  206. tmp1_conpsec=`echo ${out1}|awk '{print $9}'`
  207. tmp2_conpsec=`echo ${out2}|awk '{print $9}'`
  208. conpsec=`expr $tmp2_conpsec - $tmp1_conpsec`
  209.  
  210. reqpcon=`echo "scale=2; $reqpsec / $conpsec" | bc -l`
  211. if [ "$reqpcon" = ".99" ]
  212. then
  213. reqpcon="1.00"
  214. fi
  215. }
  216.  
  217. do_output() {
  218. output="nginx is running. $reqpsec requests per second, $conpsec \
  219. connections per second ($reqpcon requests per connection)"
  220. }
  221.  
  222. do_perfdata() {
  223. perfdata="'reqpsec'=$reqpsec 'conpsec'=$conpsec 'conpreq'=$reqpcon"
  224. }
  225.  
  226. # Here we go!
  227. get_wcdiff
  228. val_wcdiff
  229.  
  230. if [ ${pid_check} = 1 ]
  231. then
  232. check_pid
  233. if [ "$retval" = 1 ]
  234. then
  235. echo "There's no pid file for nginx. Is nginx running? Please \
  236. also make sure whether your pid path and name is correct."
  237. exit $ST_CR
  238. fi
  239. fi
  240.  
  241. get_status
  242. get_vals
  243. do_output
  244. do_perfdata
  245.  
  246. if [ -n "$warning" -a -n "$critical" ]
  247. then
  248. if [ "$reqpsec" -ge "$warning" -a "$reqpsec" -lt "$critical" ]
  249. then
  250. echo "WARNING - ${output} | ${perfdata}"
  251. exit $ST_WR
  252. elif [ "$reqpsec" -ge "$critical" ]
  253. then
  254. echo "CRITICAL - ${output} | ${perfdata}"
  255. exit $ST_CR
  256. else
  257. echo "OK - ${output} | ${perfdata} ]"
  258. exit $ST_OK
  259. fi
  260. else
  261. echo "OK - ${output} | ${perfdata}"
  262. exit $ST_OK
  263. fi
  264.