check_service.sh 10 KB

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