check_ossec.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/env bash
  2. # Author: Jon Schipp
  3. ########
  4. # Examples:
  5. # 1.) Check status of OSSEC services excluding active response i.e. execd
  6. # $ ./check_ossec.sh -s execd
  7. # 2.) Check status of OSSEC agent
  8. # $ ./check_ossec.sh -a server1
  9. # 3.) Check status of multiple OSSEC agents
  10. # $ ./check_ossec.sh -a "server1,server2,station3"
  11. # 4.) Report critical if more than 3 agents are offline and warning if at least 1 is offline.
  12. # $ ./check_ossec.sh -c 3 -w 1
  13. # Nagios Exit Codes
  14. OK=0
  15. WARNING=1
  16. CRITICAL=2
  17. UNKNOWN=3
  18. # Default Location of the binaries
  19. # Set these to the proper location if your installation differs
  20. AGENT_CONTROL=/var/ossec/bin/agent_control
  21. OSSEC_CONTROL=/var/ossec/bin/ossec-control
  22. if [ ! -f $AGENT_CONTROL ] && [ ! -f $OSSEC_CONTROL ];
  23. then
  24. echo "ERROR: OSSEC binaries not found. If you installed OSSEC in a location other than the default update the AGENT_CONTROL and OSSEC_CONTROL variables in $0."
  25. exit 1
  26. fi
  27. usage()
  28. {
  29. cat <<EOF
  30. Check for status of OSSEC agents and server.
  31. This script should be run on the OSSEC server.
  32. Options:
  33. -a <name> Check status of agent or list of comma separated agents, "agent1,agent2".
  34. -c <int> Critical threshold for number of inactive agents
  35. -l List all agents
  36. -s <service> Check status of OSSEC server processes. Use ``-s all'' to check all.
  37. To exclude a service(s) e.g pass as comma separated argument i.e. ``-s "execd,maild''
  38. -w Warning threshold for number of inactive agents
  39. Usage: $0 -a "server1,server2,station3"
  40. EOF
  41. }
  42. argcheck() {
  43. # if less than n argument
  44. if [ $ARGC -lt $1 ]; then
  45. echo "Missing arguments! Use \`\`-h'' for help."
  46. exit 1
  47. fi
  48. }
  49. # Initialize variables
  50. CRIT=0
  51. WARN=0
  52. CHECK_AGENT=0
  53. CHECK_THRESHOLD=0
  54. LIST_AGENTS=0
  55. SERVER_CHECK=0
  56. EXCLUDE=all
  57. ACTIVE=0
  58. INACTIVE=0
  59. NEVER=0
  60. TOTAL=0
  61. DISCONNECTED=0
  62. CONNECTED=0
  63. NEVERCONNECTED=0
  64. UNKNOWN=0
  65. ARGC=$#
  66. argcheck 1
  67. while getopts "ha:c:ls:v:w:" OPTION
  68. do
  69. case $OPTION in
  70. h)
  71. usage
  72. ;;
  73. a)
  74. CHECK_AGENT=1
  75. AGENT="$OPTARG"
  76. ;;
  77. c)
  78. CHECK_THRESHOLD=1
  79. CRIT="$OPTARG"
  80. ;;
  81. l)
  82. LIST_AGENTS=1
  83. ;;
  84. s)
  85. SERVER_CHECK=1
  86. EXCLUDE=$(echo $OPTARG | sed 's/,/|/g')
  87. ;;
  88. v)
  89. CHECK_THRESHOLD=1
  90. VOL="$OPTARG"
  91. ;;
  92. w)
  93. WARN="$OPTARG"
  94. ;;
  95. \?)
  96. exit 1
  97. ;;
  98. esac
  99. done
  100. if [ $LIST_AGENTS -eq 1 ]; then
  101. $AGENT_CONTROL -l
  102. exit 0
  103. fi
  104. if [ $SERVER_CHECK -eq 1 ]; then
  105. $OSSEC_CONTROL status | grep -v -E "$EXCLUDE" | grep "not running"
  106. if [ $? -eq 0 ]; then
  107. echo "An OSSEC service is not running!"
  108. exit $CRITICAL
  109. else
  110. echo "All OSSEC services running"
  111. exit $OK
  112. fi
  113. fi
  114. if [ $CHECK_AGENT -eq 1 ]; then
  115. for host in $(echo $AGENT | sed 's/,/ /g');
  116. do
  117. RESULT=$($AGENT_CONTROL -l | grep ${host},)
  118. case $RESULT in
  119. *Disconnected)
  120. echo "Agent $host is not connected!"
  121. DISCONNECTED=$((DISCONNECTED+1))
  122. ;;
  123. *Active)
  124. echo "Agent $host is connected"
  125. CONNECTED=$((CONNECTED+1))
  126. ;;
  127. *Never*)
  128. echo "Agent $host has never connected to the server: $RESULT"
  129. NEVERCONNECTED=$((NEVERCONNECTED+1))
  130. ;;
  131. *)
  132. echo "Unknown status or agent: $host"
  133. UNKNOWN=$((UNKNOWN+1))
  134. ;;
  135. esac
  136. done
  137. if [ $DISCONNECTED -gt 0 ] || [ $NEVERCONNECTED -gt 0 ] || [ $UNKNOWN -gt 0 ]; then
  138. echo "-> $DISCONNECTED disconnected agent(s), $NEVERCONNECTED never connected agent(s), and $UNKNOWN agent(s) with unknown status (possible agent name typo?)."
  139. exit $CRITICAL
  140. else
  141. echo "All requested ($CONNECTED) agents are connected to the server!"
  142. exit $OK
  143. fi
  144. fi
  145. if [ $CHECK_THRESHOLD -eq 1 ]; then
  146. ACTIVE=$($AGENT_CONTROL -l | grep Active | wc -l)
  147. INACTIVE=$($AGENT_CONTROL -l | grep Disconnected | wc -l)
  148. NEVER=$($AGENT_CONTROL -l | grep Never | wc -l)
  149. TOTAL=$($AGENT_CONTROL -l | wc -l)
  150. if [ $INACTIVE -gt $CRIT ]; then
  151. echo "$INACTIVE of $TOTAL agents inactive! Active: $ACTIVE"
  152. exit $CRITICAL
  153. elif [ $INACTIVE -gt $WARN ]; then
  154. echo "$INACTIVE of $TOTAL agents inactive! Active: $ACTIVE"
  155. exit $WARNING
  156. else
  157. echo "Active: $ACTIVE - Inactive: $INACTIVE - Never connected:$NEVER - Total: $TOTAL"
  158. exit $OK
  159. fi
  160. fi