check_netstat_connectioncount.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/bin/sh
  2. # Petit script custom pour vérifier le nombre de connexions sur netstat
  3. # GPL v3+ (copyright chl-dev@bugness.org)
  4. # Default values
  5. RANGE_WARNING="1:50"
  6. RANGE_CRITICAL="1:100"
  7. # Output
  8. OUTPUT_EXIT_STATUS=0
  9. OUTPUT_DETAIL_WARNING=""
  10. OUTPUT_DETAIL_CRITICAL=""
  11. OUTPUT_PERFDATA=""
  12. PROGPATH=$( echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,' )
  13. REVISION="0.1"
  14. # Stop at the first non-catched error
  15. set -e
  16. # Include check_range()
  17. . $PROGPATH/utils.sh
  18. #
  19. # Fonction d'aide
  20. #
  21. usage() {
  22. cat <<EOF
  23. Usage :
  24. $0 [-w warning_range] [-c critical_range] -p port [[-w...] -p port] ...
  25. Example :
  26. ./check_netstat_connectioncount.sh -w 50 -c 100 -p 80
  27. Note: Since the port is checked against the lastest ranges given, order
  28. of the arguments is important. Ex:
  29. ./check_netstat_connectioncount.sh -w 1:5 -c 1:10 -p 22 -p listen-unix:X11 -w 1:50 -c 1:100 -p 80 -p 443
  30. Special values for 'port' :
  31. all
  32. all-ipv4
  33. all-ipv6
  34. listen
  35. listen-ipv4
  36. listen-ipv6
  37. listen-unix
  38. listen-unix:PATTERN
  39. Default values:
  40. warning_range: $RANGE_WARNING
  41. critical_range: $RANGE_CRITICAL
  42. EOF
  43. }
  44. check_range_syntax() {
  45. check_range 0 "$1" >/dev/null 2>&1
  46. if [ "$?" -eq "2" ]; then
  47. return 1
  48. fi
  49. return 0
  50. }
  51. # Some early checks
  52. for i in netstat ss; do
  53. if which "$i" >/dev/null 2>&1 ; then
  54. COMMAND_SYS="$i"
  55. break
  56. fi
  57. done
  58. if [ -z "$COMMAND_SYS" ]; then
  59. echo "UNKNOWN 'netstat' and 'ss' not found."
  60. exit 1
  61. fi
  62. #
  63. # Gestion des paramètres
  64. #
  65. while getopts hw:c:p: f; do
  66. case "$f" in
  67. 'h')
  68. usage
  69. exit
  70. ;;
  71. 'w')
  72. if check_range_syntax "$OPTARG" >/dev/null; then
  73. RANGE_WARNING="$OPTARG"
  74. else
  75. echo "UNKNOWN: invalid range."
  76. exit 3
  77. fi
  78. ;;
  79. 'c')
  80. if check_range_syntax "$OPTARG" >/dev/null; then
  81. RANGE_CRITICAL="$OPTARG"
  82. else
  83. echo "UNKNOWN: invalid range."
  84. exit 3
  85. fi
  86. ;;
  87. 'p')
  88. # Ce n'est pas très propre, mais on gère tout ici plutôt que de remplir
  89. # un buffer et de le traiter ensuite
  90. # Note : grep renvoie un code d'erreur 1 s'il n'y a pas de résultat,
  91. # d'où l'ajout d'un || true sur lui uniquement.
  92. LABEL="$OPTARG"
  93. case "$OPTARG" in
  94. 'all')
  95. CPT="$( $COMMAND_SYS -taun | tail -n +2 | wc -l )"
  96. PORT_NUMBER='all'
  97. ;;
  98. 'all-ipv4')
  99. CPT="$( $COMMAND_SYS -taun4 | tail -n +2 | wc -l )"
  100. PORT_NUMBER='all-ipv4'
  101. ;;
  102. 'all-ipv6')
  103. CPT="$( $COMMAND_SYS -taun6 | tail -n +2 | wc -l )"
  104. PORT_NUMBER='all-ipv6'
  105. ;;
  106. 'listen')
  107. CPT="$( $COMMAND_SYS -tlun | tail -n +2 | wc -l )"
  108. PORT_NUMBER='listen'
  109. ;;
  110. 'listen-ipv4')
  111. CPT="$( $COMMAND_SYS -tlun4 | tail -n +2 | wc -l )"
  112. PORT_NUMBER='listen-ipv4'
  113. ;;
  114. 'listen-ipv6')
  115. CPT="$( $COMMAND_SYS -tlun6 | tail -n +2 | wc -l )"
  116. PORT_NUMBER='listen-ipv6'
  117. ;;
  118. 'listen-unix')
  119. CPT="$( $COMMAND_SYS -xl | tail -n +2 | wc -l )"
  120. PORT_NUMBER='listen-unix'
  121. ;;
  122. 'listen-unix:'*)
  123. CPT="$( $COMMAND_SYS -xl | tail -n +2 | grep "$( echo "$OPTARG" | sed 's/^listen-unix://' )" | wc -l )"
  124. PORT_NUMBER=$OPTARG # risque de bug côté superviseur ?
  125. ;;
  126. *)
  127. PORT_NUMBER=$( printf "%d" "$OPTARG" )
  128. LABEL="port$PORT_NUMBER"
  129. CPT="$( $COMMAND_SYS -tauen | sed 's/[[:space:]]\+/\t/g' | cut -f 4 | ( grep -c ":$PORT_NUMBER$" || true ) )"
  130. ;;
  131. esac
  132. # mémo : 'label'=value[UOM];[warn];[crit];[min];[max]
  133. OUTPUT_PERFDATA=$( printf "%s'%s'=%d;%s;%s;0;" \
  134. "$( test -n "$OUTPUT_PERFDATA" && echo "$OUTPUT_PERFDATA " )" \
  135. "$LABEL" \
  136. "$CPT" \
  137. "$RANGE_WARNING" \
  138. "$RANGE_CRITICAL" )
  139. if check_range "$CPT" "$RANGE_CRITICAL"; then
  140. OUTPUT_EXIT_STATUS=2
  141. OUTPUT_DETAIL_CRITICAL="$OUTPUT_DETAIL_CRITICAL Port:$PORT_NUMBER($CPT conn.)"
  142. elif check_range "$CPT" "$RANGE_WARNING"; then
  143. if [ "$OUTPUT_EXIT_STATUS" -eq 0 ]; then
  144. OUTPUT_EXIT_STATUS=1
  145. fi
  146. OUTPUT_DETAIL_WARNING="$OUTPUT_DETAIL_WARNING Port:$PORT_NUMBER($CPT conn.)"
  147. fi
  148. ;;
  149. \?)
  150. usage
  151. exit 1
  152. ;;
  153. esac
  154. done
  155. case "$OUTPUT_EXIT_STATUS" in
  156. '0')
  157. printf "OK ($COMMAND_SYS)"
  158. ;;
  159. '1')
  160. printf "WARNING ($COMMAND_SYS) %s" "$OUTPUT_DETAIL_WARNING"
  161. ;;
  162. '2')
  163. printf "CRITICAL ($COMMAND_SYS) %s" "$OUTPUT_DETAIL_CRITICAL"
  164. ;;
  165. *)
  166. printf "UNKNOWN"
  167. ;;
  168. esac
  169. printf "|%s\n" "$OUTPUT_PERFDATA"
  170. # on supprime les retours à la ligne
  171. exit $OUTPUT_EXIT_STATUS