1、Zabbix监控TCP连接状态
LISTEN:等待从任何远端TCP 和端口的连接请求。
SYN_SENT:发送完一个连接请求后等待一个匹配的连接请求。
SYN_RECEIVED:发送连接请求并且接收到匹配的连接请求以后等待连接请求确认。
ESTABLISHED:表示一个打开的连接,接收到的数据可以被投递给用户。连接的数据传输阶段的正常状态。
FIN_WAIT_1:等待远端TCP 的连接终止请求,或者等待之前发送的连接终止请求的确认。
FIN_WAIT_2:等待远端TCP 的连接终止请求。
CLOSE_WAIT:等待本地用户的连接终止请求。
CLOSING:等待远端TCP 的连接终止请求确认。
LAST_ACK:等待先前发送给远端TCP 的连接终止请求的确认(包括它字节的连接终止请求的确认)
TIME_WAIT:等待足够的时间过去以确保远端TCP 接收到它的连接终止请求的确认。
TIME_WAIT 两个存在的理由:
1.可靠的实现tcp全双工连接的终止;
2.允许老的重复分节在网络中消逝。
CLOSED:不在连接状态(这是为方便描述假想的状态,实际不存在)
获取tcp连接数的两种方法:
netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,state[key]}'
ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}'
netstat是遍历/proc下面每个PID目录,ss直接读/proc/net下面的统计信息。所以ss执行的时候消耗资源以及消耗的时间都比netstat少很多。
规范脚本存放目录:
mkdir /etc/zabbix/scripts
1.编写监控tcp连接数的shell脚本,并添加执行权限
# cat /etc/zabbix/scripts/tcp_status.sh
#!/bin/bash
#
[ $# -ne 1 ] && echo "Usage:CLOSE-WAIT|CLOSED|CLOSING|ESTAB|FIN-WAIT-1|FIN-WAIT-2|LAST-ACK|LISTEN|SYN-RECV SYN-SENT|TIME-WAIT" && exit 1
tcp_status_fun(){
TCP_STAT=$1
#netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,state[key]}' > /tmp/netstat.tmp
ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' > /tmp/ss.tmp
TCP_STAT_VALUE=$(grep "$TCP_STAT" /tmp/ss.tmp | cut -d ' ' -f2)
if [ -z $TCP_STAT_VALUE ];then
TCP_STAT_VALUE=0
fi
echo $TCP_STAT_VALUE
}
tcp_status_fun $1
添加执行权限:
chmod +x tcp_status.sh
2.创建一个自定义的key:
# cat /etc/zabbix/zabbix_agentd.d/tcp.conf
UserParameter=tcp_status[*],/bin/bash /etc/zabbix/scripts/tcp_status.sh "$1"
3.重启zabbix-agent,修改配置文件必须重启
systemctl restart zabbix-agent
4.服务端使用Zabbix_get测试是否能正常获取值
zabbix_get -s 127.0.0.1 -k tcp_status[ESTAB]
5.web界面,导入tcp模板,创建主机并关联tcp模板
方法2、 zabbix监控tcp状态
(1)agent端配置
- agent端脚本获取监控项
#vim /etc/zabbix/zabbix_agentd.d/tcp_status.sh
#bin/bash
[ $# -ne 1 ] && echo "Usage:CLOSE-WAIT|CLOSED|CLOSING|ESTAB|FIN-WAIT-1|FIN-WAIT-2|LAST-ACK|LISTEN|SYN-RECV SYN-SENT|TIME-WAIT" && exit 1
tcp_status_fun(){
TCP_STAT=$1
ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' > /tmp/ss.txt
TCP_STAT_VALUE=$(grep "$TCP_STAT" /tmp/ss.txt | cut -d ' ' -f2)
if [ -z "$TCP_STAT_VALUE" ];then
TCP_STAT_VALUE=0
fi
echo $TCP_STAT_VALUE
}
tcp_status_fun $1;
- 修改脚本权限
#chmod +x /etc/zabbix/zabbix_agentd.d/tcp_status.sh
修改agent配置文件
#vim /etc/zabbix/zabbix_agentd.d/tcp_status.conf
UserParameter=tcp_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/tcp_status.sh "$1"
agentd重启服务
systemctl restart zabbix-agent
注意:/tmp/ss.txt文件的属主是zabbix
(2)server端配置
思路:定义tcp_status模板—>定义应用集nginx_status—>定义监控项---->定义图形—>定义触发器---->添加主机或主机关联这个模板
zabbix-server端zabbix_get测试获取
# zabbix_get -s 192.168.1.32 -p10050 -k tcp_status[ESTAB]
2
- 键值
tcp_status[ESTAB]
tcp_status[CLOSE-WAIT]
tcp_status[CLOSED]
tcp_status[CLOSING]
tcp_status[FIN-WAIT-1]
tcp_status[FIN-WAIT-2]
tcp_status[LAST-ACK]
tcp_status[LISTEN]
tcp_status[SYN-RECV]
tcp_status[SYN-SENT]
tcp_status[TIME-WAIT]
- 定义监控项:只写一个,其它的就是键值不同
- 定义图形
- 验证
参考链接 :
zabbix监控tcp状态 https://www.ixdba.net/archives/2017/11/1085.htm