版权声明:未经本人允许严禁转载 https://blog.csdn.net/WanJiaBaoBao/article/details/85208422
Tcp的连接状态对于我们web服务器来说是至关重要的,尤其是并发量ESTAB;或者是syn_recv值,假如这个值比较大的话我们可以认为是不是受到了攻击,或是time_wait值比较高的话,我们要考虑看我们内核是否需要调优,太高的time_wait值的话会占用太多端口,要是端口少的话后果不堪设想。
环境介绍
- 已关闭防火墙、selinux;
- 所用zabbix版本4.0.3;
- 采用YUM安装方式;
- 所有IP地址及安装包如下表:
IP地址 | 角色 | 安装包 |
---|---|---|
192.168.91.133 | server、agent | zabbix-server-mysql、zabbix-get、zabbix-agent |
192.168.91.134 | agent | zabbix-agent |
- 安装步骤及前期配置省略。
配置步骤
- 在agent主机上编写脚本,并赋予脚本执行权限
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# vim tcp_status.sh
[root@localhost scripts]# cat 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
function 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 ' ' -f 2)
if [ -z "$TCP_STAT_VALUE" ];then
TCP_STAT_VALUE=0
fi
echo "$TCP_STAT_VALUE"
}
tcp_status_fun $1;
[root@localhost scripts]# chmod +x tcp_status.sh
- 将监控脚本位置写入配置文件,并赋予“key”
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
······以上省略
UnsafeUserParameters=1
UserParameter=tcp_status[*],/bin/bash /scripts/tcp_status.sh "$1"
······以下省略
注:也可以将指定脚本的配置文件写到/etc/zabbix/zabbix.agent.d/目录下
例:
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.d/tcp_status.conf
UserParameter=tcp_status[*],/bin/bash /scripts/tcp_status.sh "$1"
- 重新启动zabbix-agent服务
[root@localhost ~]# systemctl restart zabbix-agent
- 在server通过Zabbix_get(不要直接执行脚本)测试agent是否能获取到值
[root@localhost ~]# zabbix_get -s 192.168.91.134 -k tcp_status[LISTEN]
6
-
创建主机群组
-
创建模板
-
创建监控项,将所有TCP状态全部加入到模板中(此步骤只举一例,具体有哪些TCP状态请查看脚本第2条)
-
定义触发器根据自己的环境进行定义,这里不再列出
-
创建图表
-
在主机中添加模板进行应用
-
查看图表