check_service.sh 8.8 KB

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