check_service.sh 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #!/usr/bin/env bash
  2. # Author: Jon Schipp
  3. # 2015-03-09 [Pascal Hegy] - Add sudo for linux
  4. # 2015-03-09 [Pascal Hegy] - Change USER variable to USERNAME to avoid the use and confusion with the USER env variable
  5. # 2017-08-30 [Roberto Leibman] - Reordered checks to make sure dead and inactive get checked first
  6. # 2018-04-25 [Robin Gierse] - Update check via systemctl for Linux with grep to produce better output for systemctl
  7. # 2019-03-15 [nem / liberodark] - Add support for check all failed services in linux
  8. ########
  9. # Examples:
  10. # 1.) List services for osx
  11. # $ ./check_service.sh -l -o osx
  12. #
  13. # 2.) Check status of SSH service on a linux machine
  14. # $ ./check_service.sh -o linux -s sshd
  15. # 3.) Manually select service management tool and service
  16. # $ ./check_service.sh -o linux -t "service rsyslog status"
  17. # Nagios Exit Codes
  18. OK=0
  19. WARNING=1
  20. CRITICAL=2
  21. UNKNOWN=3
  22. # Weather or not we can trust the exit code from the service management tool.
  23. # Defaults to 0, put to 1 for systemd. Otherwise we must rely on parsing the
  24. # output from the service management tool.
  25. TRUST_EXIT_CODE=0
  26. usage()
  27. {
  28. cat <<EOF
  29. Check status of system services for Linux, FreeBSD, OSX, and AIX.
  30. Options:
  31. -s <service> Specify service name
  32. -l List services
  33. -o <os> OS type, "linux/osx/freebsd/aix"
  34. -u <user> User if you need to ``sudo -u'' for launchctl (def: nagios, linux and osx only)
  35. -t <tool> Manually specify service management tool (def: autodetect) with status and service
  36. e.g. ``-t "service nagios status"''
  37. EOF
  38. }
  39. argcheck() {
  40. # if less than n argument
  41. if [ $ARGC -lt $1 ]; then
  42. echo "Missing arguments! Use \`\`-h'' for help."
  43. exit 1
  44. fi
  45. }
  46. os_check() {
  47. if [ "$OS" == null ]; then
  48. unamestr=$(uname)
  49. if [[ $unamestr == 'Linux' ]]; then
  50. OS='linux'
  51. elif [[ $unamestr == 'FreeBSD' ]]; then
  52. OS='freebsd'
  53. elif [[ $unamestr == 'Darwin' ]]; then
  54. OS='osx'
  55. else
  56. echo "OS not recognized, Use \`-o\` and specify the OS as an argument"
  57. exit 3
  58. fi
  59. fi
  60. }
  61. determine_service_tool() {
  62. if [[ $OS == linux ]]; then
  63. if command -v systemctl >/dev/null 2>&1; then
  64. SERVICETOOL="systemctl status $SERVICE | grep -i Active"
  65. LISTTOOL="systemctl"
  66. if [ $USERNAME ]; then
  67. SERVICETOOL="sudo -u $USERNAME systemctl status $SERVICE"
  68. LISTTOOL="sudo -u $USERNAME systemctl"
  69. fi
  70. TRUST_EXIT_CODE=1
  71. elif command -v service >/dev/null 2>&1; then
  72. SERVICETOOL="service $SERVICE status"
  73. LISTTOOL="service --status-all"
  74. if [ $USERNAME ]; then
  75. SERVICETOOL="sudo -u $USERNAME service $SERVICE status"
  76. LISTTOOL="sudo -u $USERNAME service --status-all"
  77. fi
  78. elif command -v initctl >/dev/null 2>&1; then
  79. SERVICETOOL="status $SERVICE"
  80. LISTTOOL="initctl list"
  81. if [ $USERNAME ]; then
  82. SERVICETOOL="sudo -u $USERNAME status $SERVICE"
  83. LISTTOOL="sudo -u $USERNAME initctl list"
  84. fi
  85. elif command -v chkconfig >/dev/null 2>&1; then
  86. SERVICETOOL=chkconfig
  87. LISTTOOL="chkconfig --list"
  88. if [ $USERNAME ]; then
  89. SERVICETOOL="sudo -u $USERNAME chkconfig"
  90. LISTTOOL="sudo -u $USERNAME chkconfig --list"
  91. fi
  92. elif [ -f /etc/init.d/$SERVICE ] || [ -d /etc/init.d ]; then
  93. SERVICETOOL="/etc/init.d/$SERVICE status | tail -1"
  94. LISTTOOL="ls -1 /etc/init.d/"
  95. if [ $USERNAME ]; then
  96. SERVICETOOL="sudo -u $USERNAME /etc/init.d/$SERVICE status | tail -1"
  97. LISTTOOL="sudo -u $USERNAME ls -1 /etc/init.d/"
  98. fi
  99. else
  100. echo "Unable to determine the system's service tool!"
  101. exit 1
  102. fi
  103. fi
  104. if [[ $OS == freebsd ]]; then
  105. if command -v service >/dev/null 2>&1; then
  106. SERVICETOOL="service $SERVICE status"
  107. LISTTOOL="service -l"
  108. elif [ -f /etc/rc.d/$SERVICE ] || [ -d /etc/rc.d ]; then
  109. SERVICETOOL="/etc/rc.d/$SERVICE status"
  110. LISTTOOL="ls -1 /etc/rc.d/"
  111. else
  112. echo "Unable to determine the system's service tool!"
  113. exit 1
  114. fi
  115. fi
  116. if [[ $OS == osx ]]; then
  117. if [ -f /usr/sbin/serveradmin >/dev/null 2>&1 ] && serveradmin list | grep "$SERVICE" 2>&1 >/dev/null; then
  118. SERVICETOOL="serveradmin status $SERVICE"
  119. LISTTOOL="serveradmin list"
  120. elif [ -f /Applications/Server.app/Contents/ServerRoot/usr/sbin/serveradmin >/dev/null 2>&1 ] && \
  121. /Applications/Server.app/Contents/ServerRoot/usr/sbin/serveradmin list | \
  122. grep "$SERVICE" 2>&1 >/dev/null; then
  123. SERVICETOOL="/Applications/Server.app/Contents/ServerRoot/usr/sbin/serveradmin status $SERVICE"
  124. LISTTOOL="/Applications/Server.app/Contents/ServerRoot/usr/sbin/serveradmin list"
  125. elif command -v launchctl >/dev/null 2>&1; then
  126. SERVICETOOL="launchctl list | grep -v ^- | grep $SERVICE || echo $SERVICE not running! "
  127. LISTTOOL="launchctl list"
  128. if [ $USERNAME ]; then
  129. SERVICETOOL="sudo -u $USERNAME launchctl list | grep -v ^- | grep $SERVICE || echo $SERVICE not running! "
  130. LISTTOOL="sudo -u $USERNAME launchctl list"
  131. fi
  132. elif command -v service >/dev/null 2>&1; then
  133. SERVICETOOL="service --test-if-configured-on $SERVICE"
  134. LISTTOOL="service list"
  135. else
  136. echo "Unable to determine the system's service tool!"
  137. exit 1
  138. fi
  139. fi
  140. if [[ $OS == aix ]]; then
  141. if command -v lssrc >/dev/null 2>&1; then
  142. SERVICETOOL="lssrc -s $SERVICE | grep -v Subsystem"
  143. LISTTOOL="lssrc -a"
  144. else
  145. echo "Unable to determine the system's service tool!"
  146. exit 1
  147. fi
  148. fi
  149. }
  150. ARGC=$#
  151. LIST=0
  152. MANUAL=0
  153. OS=null
  154. SERVICETOOL=null
  155. LISTTOOL=null
  156. SERVICE=".*"
  157. #USERNAME=nagios
  158. argcheck 1
  159. while getopts "hls:o:t:u:" OPTION
  160. do
  161. case $OPTION in
  162. h)
  163. usage
  164. exit 0
  165. ;;
  166. l)
  167. LIST=1
  168. ;;
  169. s)
  170. SERVICE="$OPTARG"
  171. ;;
  172. o)
  173. if [[ "$OPTARG" == linux ]]; then
  174. OS="$OPTARG"
  175. elif [[ "$OPTARG" == osx ]]; then
  176. OS="$OPTARG"
  177. elif [[ "$OPTARG" == freebsd ]]; then
  178. OS="$OPTARG"
  179. elif [[ "$OPTARG" == aix ]]; then
  180. OS="$OPTARG"
  181. else
  182. echo "Unknown type!"
  183. exit 1
  184. fi
  185. ;;
  186. t)
  187. MANUAL=1
  188. MANUALSERVICETOOL="$OPTARG"
  189. ;;
  190. u)
  191. USERNAME="$OPTARG"
  192. ;;
  193. \?)
  194. exit 1
  195. ;;
  196. esac
  197. done
  198. os_check
  199. if [ $MANUAL -eq 1 ]; then
  200. SERVICETOOL=$MANUALSERVICETOOL
  201. else
  202. determine_service_tool
  203. fi
  204. # -l conflicts with -t
  205. if [ $MANUAL -eq 1 ] && [ $LIST -eq 1 ]; then
  206. echo "Options conflict: \`\`-t'' and \`\`-l''"
  207. exit 2
  208. fi
  209. if [ $LIST -eq 1 ]; then
  210. if [[ $LISTTOOL != null ]]; then
  211. $LISTTOOL
  212. exit 0
  213. else
  214. echo "OS not specified! Use \`\`-o''"
  215. exit 2
  216. fi
  217. fi
  218. # Check the status of a service
  219. STATUS_MSG=$(eval "$SERVICETOOL" 2>&1)
  220. EXIT_CODE=$?
  221. ## Exit code from the service tool - if it's non-zero, we should
  222. ## probably return CRITICAL. (though, in some cases UNKNOWN would
  223. ## probably be more appropriate)
  224. [ $EXIT_CODE -ne 0 ] && echo "$STATUS_MSG" && exit $CRITICAL
  225. ## For systemd and most systems, $EXIT_CODE can be trusted - if it's 0, the service is running.
  226. ## Ref https://github.com/jonschipp/nagios-plugins/issues/15
  227. [ $TRUST_EXIT_CODE -eq 1 ] && [ $EXIT_CODE -eq 0 ] && echo "$STATUS_MSG" && exit $OK
  228. case $STATUS_MSG in
  229. *stop*)
  230. echo "$STATUS_MSG"
  231. exit $CRITICAL
  232. ;;
  233. *STOPPED*)
  234. echo "$STATUS_MSG"
  235. exit $CRITICAL
  236. ;;
  237. *not*running*)
  238. echo "$STATUS_MSG"
  239. exit $CRITICAL
  240. ;;
  241. *NOT*running*)
  242. echo "$STATUS_MSG"
  243. exit $CRITICAL
  244. ;;
  245. *NOT*RUNNING*)
  246. echo "$STATUS_MSG"
  247. exit $CRITICAL
  248. ;;
  249. #*inactive*)
  250. # echo "$STATUS_MSG"
  251. # exit $CRITICAL
  252. # ;;
  253. *dead*)
  254. echo "$STATUS_MSG"
  255. exit $CRITICAL
  256. ;;
  257. *running*)
  258. echo "$STATUS_MSG"
  259. exit $OK
  260. ;;
  261. *RUNNING*)
  262. echo "$STATUS_MSG"
  263. exit $OK
  264. ;;
  265. *SUCCESS*)
  266. echo "$STATUS_MSG"
  267. exit $OK
  268. ;;
  269. *[eE]rr*)
  270. echo "Error in command: $STATUS_MSG"
  271. exit $CRITICAL
  272. ;;
  273. *[fF]ailed*)
  274. echo "$STATUS_MSG"
  275. exit $CRITICAL
  276. ;;
  277. *[eE]nable*)
  278. echo "$STATUS_MSG"
  279. exit $OK
  280. ;;
  281. *[dD]isable*)
  282. echo "$STATUS_MSG"
  283. exit $CRITICAL
  284. ;;
  285. *[cC]annot*)
  286. echo "$STATUS_MSG"
  287. exit $CRITICAL
  288. ;;
  289. *[aA]ctive*)
  290. echo "$STATUS_MSG"
  291. exit $OK
  292. ;;
  293. *Subsystem*not*on*file)
  294. echo "$STATUS_MSG"
  295. exit $CRITICAL
  296. ;;
  297. [1-9][1-9]*)
  298. echo "$SERVICE running: $STATUS_MSG"
  299. exit $OK
  300. ;;
  301. "")
  302. echo "$SERVICE is not running: no output from service command"
  303. exit $CRITICAL
  304. ;;
  305. *)
  306. echo "Unknown status: $STATUS_MSG"
  307. echo "Is there a typo in the command or service configuration?: $STATUS_MSG"
  308. exit $UNKNOWN
  309. ;;
  310. *0\ loaded*)
  311. echo "$STATUS_MSG"
  312. exit $OK
  313. ;;
  314. esac