命令测试#
Tcp的连接状态对于我们web服务器来说是至关重要的,从TCP的连接状态中可以看出网络的连接情况,服务器的压力情况,对服务器的并发有很好的直观反映;尤其是并发量ESTAB;或者是syn_recv值,假如这个值比较大的话我们可以认为是不是受到了攻击,或是是time_wait值比较高的话,我们要考虑看我们内核是否需要调优,太高的time_wait值的话会占用太多端口,要是端口少的话后果不堪设想。
我们可以利用下面命令来查询11中状态的连接数,下面我们也可以利用这个命令来写脚本。
[root@access01 ~]# netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}'
LISTEN 57
ESTABLISHED 1197
TIME_WAIT 309
zabbix 客户端配置#
1.编写tcp状态获取脚本
# vim /etc/zabbix/scripts/tcp_conn_status.sh
#!/bin/bash
#this script is used to get tcp and udp connetion status
#tcp status
metric=$1
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 $metric 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
2.给脚本赋予执行权限并执行脚本进行测试,给tcp_status.txt文本zabbix权限
# chmod +x /etc/zabbix/scripts/tcp_conn_status.sh
# bash /etc/zabbix/scripts/tcp_conn_status.sh established
1239
# chown zabbix.zabbix /tmp/tcp_status.txt
3.编写zabbix子配置文件
# vim /etc/zabbix/zabbix_agentd.d/userparameter_tcp_conn.conf
UserParameter=tcp.status[*],/etc/zabbix/scripts/tcp_conn_status.sh $1
4.启动zabbix-agent
# systemctl restart zabbix-agent.service
zabbix 服务端配置#
✏️ 本文所使用模板链接:https://pan.baidu.com/s/1pq-2Swff1XzeHwVkvABQEQ 提取码:2iab
首先在服务器上面测试能否获取到值
# zabbix_get -s 172.24.115.5 -k tcp.status[established]
826
1.创建模板
2.创建该模板下的11个监控项,下面示例其中一个
3.创建该模板下的图形
4.关联该模板到客户端主机
5.进行数据查看
6.在grafana上面进行绘图展示