2re 发表于 2014-6-20 16:02:55

nagios插件-监控tcp状态连接数shell脚本

这是用shell开发的nagios插件,根据Nagios Plugin Development Guidelines和Nagios Plugin API编写,在前人的基础上进行补充,支持官方标准的-V、-c -v -t选项。插件主要思路是通过netstat命令获取tcp的各个状态连接,统计每个状态数量,最后按照标准的nagios插件格式输出检测信息和性能信息。监控那种状态,连接数多少警报,都可以通过脚本参数指定。nagios监控服务器,通过nrpe在被监控端执行检测脚本,并把检测脚本运行的状态返回值和输出信息,返回给nagios监控服务器。其中状态返回值0代表正常,1代表警告,2代表紧急,3代表未知。说明:脚本支持这几个参数
      -V|–version 显示脚本版本信息
      -c|--critical threshold 指定critical的阀值,必须指定
      -w|–warning threshold 指定warning的阀值,必须指定
      -s|–status tcp status 指定tcp连接状态,必须指定
      -h|–help 获取使用帮助
      -t|–timeout time 指定脚本运行超时时间
threshold格式
      ~代表负无穷大
      从0开始可以省略start:即0:end
      前面有start: 没有指明结束值,则为无穷大
      报警会在超出起始值与结束值之间,包括这两个值
      起始值为@,则报警会在起始值与结束值之间,包括这两个值
      10 小于0和大于10则警报
      10: 在10到正无穷大之外,即负无穷大到10警报
      ~:10 在负无穷大到10之外,即10到正无穷大警报
      10:20 10到20之外,即小于10和大于20
      @10:20 在10到20之间警报
      @10 在0到10之间警报
这个脚本可能还有bug,欢迎大家修复。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
print_revision() {
      echo "(nagios-plugins-netstat 0.5)"
}

usage(){
echo -e "Usage: $0 [-V|--version] [-h|--help] <-w|--warning warning threshold>\n<-c|--critical critical threshold> <-s|--status status>\n<-t|--timeout time>"
}

check_range() {
mflag=0
if echo $1 | grep -E -q"^@?((~:*$)|(+:?*$))";then
    if echo $1 |grep -E -q "^@";then
    range=${1#@}
      mflag=1
    else
    range=$1
    fi
    if echo $range |grep -E -q ":";then
       start=${range%%:*}
       if [ "$start" = "~" ];then
         start=-999999
       fi
       if [ "$start" -lt 0 ];then
         return 2
       fi
       end=${range#*:}
       if [[ -z $end ]];then
         end=65535
       fi
    else
       start=0
       end=$range
    fi
    if [ "$start" != "~" ] && [ "$end" != "" ];then
       if[ $start -gt $end ];then
          return 2
       fi
    fi
else
    echo "invalid range"
    return 2
fi
return 0
}

select_arg(){
if [ $# -eq 0 ];then
return 1
fi
wcount=0
ccount=0
scount=0
until [ $# -eq 0 ];do
case $1 in
    -V|--version)
      versionflag=1
      shift 1
      ;;
    -h|--help)
      helpflag=1
      shift 1
      ;;
    -w|--warning)
      [ $# -lt 2 ] && return 1
      check_range $2
      if [ $? -ne 0 ];then
      return 1
      else
      warn_start=$start
      warn_end=$end
      warn_mflag=$mflag
      fi
      shift 2
      let wcount++
      ;;
    -c|--critical)
      [ $# -lt 2 ] && return 1
      check_range $2
      if [ $? -ne 0 ];then
      return 1
      else
      critical_start=$start
      critical_end=$end
      critical_mflag=$mflag
      fi
      shift 2
      let ccount++
      ;;
    -s|--status)
      [ $# -lt 2 ] && return 1
      case $2 in
    established|ESTABLISHED)
      status=established
          ;;
      time_wait|TIME_WAIT)
         status=time_wait
          ;;
      syn_recv|SYN_RECV)
      status=syn_recv
      ;;
      fin_wait1|FIN_WAIT1)
      status=fin_wait1
      ;;
    fin_wait1|FIN_WAIT2)
      status=fin_wait2
      ;;
    last_ack|LAST_ACK)
      status=last_ack
      ;;
    close_wait|CLOSE_WAIT)
          status=close_wait
          ;;
    *)
      return 1
       ;;
      esac
      shift 2
      let scount++
      ;;
    -t|--timeout)
      [ $# -lt 2 ] && return 1
      if ! echo $2 |grep -E -q "^*$";then
      return 1
      fi
      timeout=$2
      ;;
    *)
   return 1
   ;;
esac
done
return 0
}

alarm(){
connect=`netstat -ant | awk '/^tcp/ && !/LISTEN/{S[$NF]++}END{for(i in S) print i,S}'`
established=`echo $connect |awk '/ESTABLISHED/{print $2}'`
[ -z $established ] && established=0
time_wait=`echo $connect |awk '/TIME_WAIT/{print $2}'`
[ -z $time_wait ] && time_wait=0
syn_recv=`echo $connect |awk '/SYN_RECV/{print $2}'`
[ -z $syn_recv ] && syn_recv=0
fin_wait1=`echo $connect |awk '/FIN_WAIT1/{print $2}'`
   [ -z $fin_wait1 ] && fin_wait1=0
fin_wait2=`echo $connect |awk '/FIN_WAIT2/{print $2}'`
   [ -z $fin_wait2 ] && fin_wait2=0
last_ack=`echo $connect |awk '/LAST_ACK/{print $2}'`
   [ -z $last_ack ] && last_ack=0
close_wait=`echo $connect |awk '/CLOSE_WAIT/{print $2}'`
   [ -z $close_wait ] && close_wait=0

if [ $warn_mflag -eq 0 -a $critical_mflag -eq 0 ];then
w1=-gt;w2=-lt;c1=-gt;c2=-lt;wboole=-a;cboole=-a
elif [ $warn_mflag -eq 1 -a $critical_mflag -eq 0 ];then
w1=-lt;w2=-gt;c1=-gt;c2=-lt;wboole=-o;cboole=-a
elif [ $warn_mflag -eq 0 -a $critical_mflag -eq 1 ];then
w1=-gt;w2=-lt;c1=-lt;c2=-gt;wboole=-a;cboole=-o
elif [ $warn_mflag -eq 1 -a $critical_mflag -eq 1 ];then
   w1=-lt;w2=-gt;c1=-lt;c2=-gt;wboole=-o;cboole=-o
fi
if [ ${!status} $w1 $warn_start $wboole ${!status} $w2 $warn_end ] && [ ${!status} $c1 $critical_start $cboole ${!status} $c2 $critical_end ];then
exitcode=0
else
if ! [ ${!status} $w1 $warn_start $wboole ${!status} $w2 $warn_end ];then
    exitcode=1
fi
if ! [ ${!status} $c1 $critical_start $cboole ${!status} $c2 $critical_end ];then
      exitcode=2
fi
if [ ${!status} -le 0 ];then
      exitcode=3
fi
fi

if [ $exitcode -eq 0 ];then
serviceoutput="$status OK - total:${!status}"
elif [ $exitcode -eq 1 ];then
serviceoutput="$status Warning - total:${!status}"
elif [ $exitcode -eq 2 ];then
serviceoutput="$status Critical - total:${!status}"
elif [ $exitcode -eq 3 ];then
   serviceoutput="$status Unknown - total:${!status}"
fi
echo -e "$serviceoutput;| established $established;\ntime_wait $time_wait;\nsyn_recv $syn_recv;\nfin_wait1 $fin_wait1;\nfin_wait2 $fin_wait2;\nlast_ack $last_ack;\nclose_wait $close_wait"
exit $exitcode
}

select_arg $@
[ $? -ne 0 ] && usage && exit 3

if [[ -n $versionflag ]];then
   if [ $versionflag -eq 1 ];then
   print_revision && exit 0
   fi
else
[[ -n $helpflag ]]&& [ $helpflag -eq 1 ] && usage && exit 0
fi
[ $ccount -ne 1 ] || [ $wcount -ne 1 ] || [ $scount -ne 1 ] && usage && exit 3

[ -z $timeout ] && timeout=10
alarm &
commandpid=$!
(sleep $timeout;kill -9 $commandpid &>/dev/null) &
watchdog=$!
wait$commandpid &>/dev/null
pexitcode=$?
[ $pexitcode -gt 3 ] && pexitcode=3
watchdogchild=`ps -eo pid,ppid | awk "\\$2==$watchdog{print \\$1}"`
for a in $watchdogchild;do
kill -9 $a &>/dev/null
done
kill -9 $watchdog &>/dev/null
exit $pexitcode






wuchuan3210g 发表于 2014-8-8 18:17:54

求助啊如何 向200台机器中批量添加   tcp状态连接数呢?
页: [1]
查看完整版本: nagios插件-监控tcp状态连接数shell脚本