check_netstat_connectioncount.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if [ "$COMMAND_SYS" = "ss" ]; then
  56. OUTPUT_COLUMN=5
  57. else
  58. OUTPUT_COLUMN=4
  59. fi
  60. break
  61. fi
  62. done
  63. if [ -z "$COMMAND_SYS" ]; then
  64. echo "UNKNOWN 'netstat' and 'ss' not found."
  65. exit 1
  66. fi
  67. #
  68. # Gestion des paramètres
  69. #
  70. while getopts hw:c:p: f; do
  71. case "$f" in
  72. 'h')
  73. usage
  74. exit
  75. ;;
  76. 'w')
  77. if check_range_syntax "$OPTARG" >/dev/null; then
  78. RANGE_WARNING="$OPTARG"
  79. else
  80. echo "UNKNOWN: invalid range."
  81. exit 3
  82. fi
  83. ;;
  84. 'c')
  85. if check_range_syntax "$OPTARG" >/dev/null; then
  86. RANGE_CRITICAL="$OPTARG"
  87. else
  88. echo "UNKNOWN: invalid range."
  89. exit 3
  90. fi
  91. ;;
  92. 'p')
  93. # Ce n'est pas très propre, mais on gère tout ici plutôt que de remplir
  94. # un buffer et de le traiter ensuite
  95. # Note : grep renvoie un code d'erreur 1 s'il n'y a pas de résultat,
  96. # d'où l'ajout d'un || true sur lui uniquement.
  97. LABEL="$OPTARG"
  98. case "$OPTARG" in
  99. 'all')
  100. CPT="$( $COMMAND_SYS -taun | tail -n +2 | wc -l )"
  101. PORT_NUMBER='all'
  102. ;;
  103. 'all-ipv4')
  104. CPT="$( $COMMAND_SYS -taun4 | tail -n +2 | wc -l )"
  105. PORT_NUMBER='all-ipv4'
  106. ;;
  107. 'all-ipv6')
  108. CPT="$( $COMMAND_SYS -taun6 | tail -n +2 | wc -l )"
  109. PORT_NUMBER='all-ipv6'
  110. ;;
  111. 'listen')
  112. CPT="$( $COMMAND_SYS -tlun | tail -n +2 | wc -l )"
  113. PORT_NUMBER='listen'
  114. ;;
  115. 'listen-ipv4')
  116. CPT="$( $COMMAND_SYS -tlun4 | tail -n +2 | wc -l )"
  117. PORT_NUMBER='listen-ipv4'
  118. ;;
  119. 'listen-ipv6')
  120. CPT="$( $COMMAND_SYS -tlun6 | tail -n +2 | wc -l )"
  121. PORT_NUMBER='listen-ipv6'
  122. ;;
  123. 'listen-unix')
  124. CPT="$( $COMMAND_SYS -xl | tail -n +2 | wc -l )"
  125. PORT_NUMBER='listen-unix'
  126. ;;
  127. 'listen-unix:'*)
  128. CPT="$( $COMMAND_SYS -xl | tail -n +2 | grep "$( echo "$OPTARG" | sed 's/^listen-unix://' )" | wc -l )"
  129. PORT_NUMBER=$OPTARG # risque de bug côté superviseur ?
  130. ;;
  131. *)
  132. PORT_NUMBER=$( printf "%d" "$OPTARG" )
  133. LABEL="port$PORT_NUMBER"
  134. CPT="$( $COMMAND_SYS -taun | sed 's/[[:space:]]\+/\t/g' | cut -f "$OUTPUT_COLUMN" | ( grep -c ":$PORT_NUMBER$" || true ) )"
  135. ;;
  136. esac
  137. # mémo : 'label'=value[UOM];[warn];[crit];[min];[max]
  138. OUTPUT_PERFDATA=$( printf "%s'%s'=%d;%s;%s;0;" \
  139. "$( test -n "$OUTPUT_PERFDATA" && echo "$OUTPUT_PERFDATA " )" \
  140. "$LABEL" \
  141. "$CPT" \
  142. "$RANGE_WARNING" \
  143. "$RANGE_CRITICAL" )
  144. if check_range "$CPT" "$RANGE_CRITICAL"; then
  145. OUTPUT_EXIT_STATUS=2
  146. OUTPUT_DETAIL_CRITICAL="$OUTPUT_DETAIL_CRITICAL Port:$PORT_NUMBER($CPT conn.)"
  147. elif check_range "$CPT" "$RANGE_WARNING"; then
  148. if [ "$OUTPUT_EXIT_STATUS" -eq 0 ]; then
  149. OUTPUT_EXIT_STATUS=1
  150. fi
  151. OUTPUT_DETAIL_WARNING="$OUTPUT_DETAIL_WARNING Port:$PORT_NUMBER($CPT conn.)"
  152. fi
  153. ;;
  154. \?)
  155. usage
  156. exit 1
  157. ;;
  158. esac
  159. done
  160. case "$OUTPUT_EXIT_STATUS" in
  161. '0')
  162. printf "OK ($COMMAND_SYS)"
  163. ;;
  164. '1')
  165. printf "WARNING ($COMMAND_SYS) %s" "$OUTPUT_DETAIL_WARNING"
  166. ;;
  167. '2')
  168. printf "CRITICAL ($COMMAND_SYS) %s" "$OUTPUT_DETAIL_CRITICAL"
  169. ;;
  170. *)
  171. printf "UNKNOWN"
  172. ;;
  173. esac
  174. printf "|%s\n" "$OUTPUT_PERFDATA"
  175. # on supprime les retours à la ligne
  176. exit $OUTPUT_EXIT_STATUS