linux:
在linux上手动获得tcp连接数的命令为:
1
2
3
4
5
6
7
8
9
10
11
|
netstat
-an|
awk
'/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}'
#ESTABLISHED socket已经建立连接
#CLOSED socket没有被使用,无连接
#CLOSING 服务器端和客户端都同时关闭连接
#CLOSE_WAIT 等待关闭连接
#TIME_WAIT 表示收到了对方的FIN报文,并发送出了ACK报文,等待2MSL后就可回到CLOSED状态
#LAST_ACK 远端关闭,当前socket被动关闭后发送FIN报文,等待对方ACK报文
#LISTEN 监听状态
#SYN_RECV 接收到SYN报文
#SYN_SENT 已经发送SYN报文
|
在zabbix_agent端编辑配置文件:
1
2
|
vi
/etc/zabbix/zabbix_agentd
.conf
Include=
/etc/zabbix/zabbix_agentd
.d/
|
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
|
#编写一个获取tcp状态的脚本:
mkdir
/etc/zabbix/zabbix_agentd
.d
/script
#此处注意,一定不要将脚本直接放到/etc/zabbix/zabbix_agentd.d/目录下,否则zabbix_agent会把它当做自己的配置文件执行,造成重启不成功
vi
/etc/zabbix/zabbix_agentd
.d
/script/tcp_status
.sh
#!/bin/bash
#this script is get tcp status
tmp_file=
/tmp/tcp_status
.txt
/bin/netstat
-an|
awk
'/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}'
> $tmp_file
case
$1
in
closed)
output=$(
awk
'/CLOSED/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
listen)
output=$(
awk
'/LISTEN/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
synrecv)
output=$(
awk
'/SYN_RECV/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
synsent)
output=$(
awk
'/SYN_SENT/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
established)
output=$(
awk
'/ESTABLISHED/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
timewait)
output=$(
awk
'/TIME_WAIT/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
closing)
output=$(
awk
'/CLOSING/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
closewait)
output=$(
awk
'/CLOSE_WAIT/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
lastack)
output=$(
awk
'/LAST_ACK/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
finwait1)
output=$(
awk
'/FIN_WAIT1/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
finwait2)
output=$(
awk
'/FIN_WAIT2/{print $2}'
$tmp_file)
if
[
"$output"
==
""
];
then
echo
0
else
echo
$output
fi
;;
*)
echo
-e
"\e[033mUsage: sh $0 [closed|closing|closewait|synrecv|synsent|finwait1|finwait2|listen|established|lastack|timewait]\e[0m"
esac
|
1
2
|
#赋予脚本可执行权限
chmod
+x
/etc/zabbix/zabbix_agentd
.d
/script/tcp_status
.sh
|
编写zabbix_agent上的自定义key:
1
2
|
vi
/etc/zabbix/zabbix_agentd
.conf
UserParameter=tcp.status[*],
/etc/zabbix/zabbix_agentd
.d
/script/tcp_status
.sh $1
|
重启zabbix_agent:
1
|
/etc/init
.d
/zabbix_agent
restart
|
在服务端测试输入参数返回的值:
1
|
zabbix_get -s 118.190.114.140 -p 10050 -k tcp.status[listen]
|
最后在zabbix上创建监控项和触发器,附件中上传了一个附件,里面是tcp_status模板,直接在配置——模板——导入即可。
附件:http://down.51cto.com/data/2366688
本文转自 王家东哥 51CTO博客,原文链接:http://blog.51cto.com/xiaodongge/1929101