当前位置:网站首页>zabbix监控TCP连接个数
zabbix监控TCP连接个数
2022-06-30 11:25:00 【weixin_44989941】
zabbix版本: 5.0
1.首先用netstat命令 打印所有 TCP 连接
基于netstat命令输出的TCP连接信息,统计各连接状态的数量。
主要使用的命令为netstat -tan,涉及三个选项:
-t,显示TCP连接。
-u,显示UDP连接。
-a,Show both listening and non-listening sockets. 连接状态总的分为两大类,listening和non-listening,即监听/非监听,也就是连接状态为LISTEN/非LISTEN。
-a就是显示所有状态的连接。
-l,仅显示LISTEN状态的连接。
-n,Show numerical addresses instead of trying to determine symbolic host, port or user names. 关闭名称解析,以数字方式显示IP和端口。 这个在这次统计过程中作用不大,但一般都习惯性地加上。
[[email protected] ~]# netstat -tan
2.awk 格式化输出统计结果
从netstat命令中可以看出,我们需要取的是第6列的State,但忽略第一行,将各State各自循环+1,得到各种状态的连接数量。
根据需求,得到格式化输出命令:
[[email protected] ~]# netstat -tan | awk 'NR>1 {++v[$6]} END {for(k in v) print k,v[k]}'
这样就得到了全面的基础数据,接下来通过grep来匹配连接状态,取到对应的行,经过剪切就能取到对应连接状态的数量了。
[[email protected] ~]# netstat -tan | awk 'NR>1 {++v[$6]} END {for(k in v) print k,v[k]}' | grep ESTABLISHED | cut -d' ' -f2
3.编写脚本
vi /etc/zabbix/zabbix_agentd.d/Tcp_State_Statistic.sh
#!/bin/bash
# Description: 统计并获取TCP连接状态
# Author: yqc
# Define variables
Tcp_State=$1
# Define function "Tcp_State_Statistic"
Tcp_State_Statistic(){
State_Num=$(netstat -tan | awk 'NR>1 {++v[$6]} END {for(k in v) print k,v[k]}' | grep ${Tcp_State} | cut -d' ' -f2)
if [[ -z $State_Num ]]; then
State_Num=0
fi
echo $State_Num
}
# Notification or Use function "Tcp_State_Statistic"
if [[ $# != 1 ]]; then
echo "Usage: bash $0 <LISTEN|SYN_SENT|SYN_RCVD|ESTABLISHED|FIN_WAIT1|CLOSE_WAIT|FIN_WAIT2|LAST_ACK|TIME_WAIT>"
else
Tcp_State_Statistic $1
fi
4.zabbix agent配置文件中添加 UserParameter
vi /etc/zabbix/zabbix_agentd.conf
[[email protected] ~]# grep '^[a-Z]' /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1,192.168.85.148
ServerActive=127.0.0.1,192.168.85.148
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf启动:
[[email protected] ~]# systemctl enable zabbix-agent && systemctl restart zabbix-agent
5.添加自定义监控项
在/etc/zabbix/zabbix_agentd.d/目录下新建配置文件来添加自定义监控项,配置文件模块化有利于后期维护。
vim /etc/zabbix/zabbix_agentd.d/tcp_state_statistic.conf
UserParameter=tcp_state_statistic[*],/etc/zabbix/zabbix_agentd.d/Tcp_State_Statistic.sh "$1" #新文件,只添加这一行
chmod +x /etc/zabbix/zabbix_agentd.d/Tcp_State_Statistic.sh
/etc/zabbix/zabbix_agentd.d/Tcp_State_Statistic.sh LISTEN
重启zabbix-agentd
[[email protected] ~]# systemctl restart zabbix-agent6.zabbix Server测试手动获取监控项数据
安装zabbix-get:
yum -y install zabbix-get手动获取监控数据
[[email protected] ~]# zabbix_get -s 192.168.85.148 -p 10050 -k "tcp_state_statistic["LISTEN"]"
13
[[email protected] ~]# zabbix_get -s 192.168.85.148 -p 10050 -k "tcp_state_statistic["EASTAB"]"
0
7.Zabbix Server 制作 TCP 连接状态监测模板
创建模板
配置-模板-创建模板:

填写模板信息:

创建应用集
找到对应模板,点击应用集:



创建监控项
找到相应模板,点击监控项:
配置--->主机--->监控项


依次克隆并修改,将TCP连接各状态的监控项都创建好。

可以根据需要继续创建图形、触发器等,这里的目的是为了熟悉了解TCP连接状态,以及如何通过Zabbix来监测到数据,不做过多个性化的操作。
8.Zabbix Server 添加主机并关联模板


验证监控数据

图形显示

边栏推荐
- Pointdistiller: structured knowledge distillation for efficient and compact 3D detection
- 100 important knowledge points that SQL must master: using table aliases
- Oracle netsuite helps TCM bio understand data changes and make business development more flexible
- Uncover the whole link communication process of customer service im
- How to 'gracefully' avoid MySQL login prompt information in scripts
- Webview,ScrollView滑动冲突咋整
- MCU firmware packaging Script Software
- Line generation (Gauss elimination method, linear basis)
- Le talent scientifique 丨 dessins animés qu'est - ce qu'erdma?
- Customize an annotation to get a link to the database
猜你喜欢

Record the memory leak of viewpager + recyclerview once

国内首批!阿里云云原生数据湖产品通过信通院评测认证

Win10 R package installation error: not installed in arch=i386

以PolarDB为代表的阿里云数据库以跻身全球第一阵营

If it is not listed again, Kuangshi technology will not be able to endure

来聊聊怎么做硬件兼容性检测,快速迁移到openEuler?

Uncover the whole link communication process of customer service im

Our company has used this set of general solutions for 7 years, and has opened up dozens of systems, a stable batch!

Mathematics (fast power)

A theoretical defect of relative position coding transformer and Its Countermeasures
随机推荐
How to 'gracefully' avoid MySQL login prompt information in scripts
盘点那些具有特色的写作软件
Compression state DP bit operation
线代(高斯消元法、线性基)
Discussion on the essence of "FPGA mining" from open source projects
"New digital technology" completed tens of millions of yuan of a + round financing and built an integrated intelligent database cloud management platform
What is a wechat applet that will open the door of the applet
科普达人丨漫画图解什么是eRDMA?
暑假学习记录
Oracle netsuite helps TCM bio understand data changes and make business development more flexible
dplyr 中的filter报错:Can‘t transform a data frame with duplicate names
Methods and usage of promise async and await
他是上海两大产业的第一功臣,却在遗憾中默默离世
[xi'anjiaotonguniversity] information sharing of the first and second postgraduate entrance examinations
shell第一个命令结果传入第二个命令删除
QT embeds the sub QT program window into the current program
Boost研究:Boost Log
Train an image classifier demo in pytorch [learning notes]
EMC-浪涌
Database cascading operation