check_service.sh 8.4 KB

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