check_service.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. auto_os_detect() {
  143. echo test
  144. }
  145. ARGC=$#
  146. LIST=0
  147. MANUAL=0
  148. OS=null
  149. SERVICETOOL=null
  150. LISTTOOL=null
  151. SERVICE=".*"
  152. #USERNAME=nagios
  153. argcheck 1
  154. while getopts "hls:o:t:u:" OPTION
  155. do
  156. case $OPTION in
  157. h)
  158. usage
  159. exit 0
  160. ;;
  161. l)
  162. LIST=1
  163. ;;
  164. s)
  165. SERVICE="$OPTARG"
  166. ;;
  167. o)
  168. if [[ "$OPTARG" == linux ]]; then
  169. OS="$OPTARG"
  170. elif [[ "$OPTARG" == osx ]]; then
  171. OS="$OPTARG"
  172. elif [[ "$OPTARG" == freebsd ]]; then
  173. OS="$OPTARG"
  174. elif [[ "$OPTARG" == aix ]]; then
  175. OS="$OPTARG"
  176. else
  177. echo "Unknown type!"
  178. exit 1
  179. fi
  180. ;;
  181. t)
  182. MANUAL=1
  183. MANUALSERVICETOOL="$OPTARG"
  184. ;;
  185. u)
  186. USERNAME="$OPTARG"
  187. ;;
  188. \?)
  189. exit 1
  190. ;;
  191. esac
  192. done
  193. os_check
  194. if [ $MANUAL -eq 1 ]; then
  195. SERVICETOOL=$MANUALSERVICETOOL
  196. else
  197. determine_service_tool
  198. fi
  199. # -l conflicts with -t
  200. if [ $MANUAL -eq 1 ] && [ $LIST -eq 1 ]; then
  201. echo "Options conflict: \`\`-t'' and \`\`-l''"
  202. exit 2
  203. fi
  204. if [ $LIST -eq 1 ]; then
  205. if [[ $LISTTOOL != null ]]; then
  206. $LISTTOOL
  207. exit 0
  208. else
  209. echo "OS not specified! Use \`\`-o''"
  210. exit 2
  211. fi
  212. fi
  213. # Check the status of a service
  214. STATUS_MSG=$(eval "$SERVICETOOL" 2>&1)
  215. case $STATUS_MSG in
  216. *stop*)
  217. echo "$STATUS_MSG"
  218. exit $CRITICAL
  219. ;;
  220. *STOPPED*)
  221. echo "$STATUS_MSG"
  222. exit $CRITICAL
  223. ;;
  224. *not*running*)
  225. echo "$STATUS_MSG"
  226. exit $CRITICAL
  227. ;;
  228. *running*)
  229. echo "$STATUS_MSG"
  230. exit $OK
  231. ;;
  232. *RUNNING*)
  233. echo "$STATUS_MSG"
  234. exit $OK
  235. ;;
  236. *SUCCESS*)
  237. echo "$STATUS_MSG"
  238. exit $OK
  239. ;;
  240. *[eE]rr*)
  241. echo "Error in command: $STATUS_MSG"
  242. exit $CRITICAL
  243. ;;
  244. *[fF]ailed*)
  245. echo "$STATUS_MSG"
  246. exit $CRITICAL
  247. ;;
  248. *[eE]nable*)
  249. echo "$STATUS_MSG"
  250. exit $OK
  251. ;;
  252. *[dD]isable*)
  253. echo "$STATUS_MSG"
  254. exit $CRITICAL
  255. ;;
  256. *[cC]annot*)
  257. echo "$STATUS_MSG"
  258. exit $CRITICAL
  259. ;;
  260. *inactive*)
  261. echo "$STATUS_MSG"
  262. exit $CRITICAL
  263. ;;
  264. *dead*)
  265. echo "$STATUS_MSG"
  266. exit $CRITICAL
  267. ;;
  268. *[aA]ctive*)
  269. echo "$STATUS_MSG"
  270. exit $OK
  271. ;;
  272. *Subsystem*not*on*file)
  273. echo "$STATUS_MSG"
  274. exit $CRITICAL
  275. ;;
  276. [1-9][1-9]*)
  277. echo "$SERVICE running: $STATUS_MSG"
  278. exit $OK
  279. ;;
  280. "")
  281. echo "$SERVICE is not running: no output from service command"
  282. exit $CRITICAL
  283. ;;
  284. *)
  285. echo "Unknown status: $STATUS_MSG"
  286. echo "Is there a typo in the command or service configuration?: $STATUS_MSG"
  287. exit $UNKNOWN
  288. ;;
  289. esac