check_service.sh 8.7 KB

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