1.agent端监控脚本
[root@test-agent zabbix_agentd.d]# pwd
/etc/zabbix/zabbix_agentd.d
[root@test-agent zabbix_agentd.d]# vim tcp_status_monitoring_port.sh
#!/bin/bash
#
#********************************************************************
#Author: fengshengli
#Date: 2021-01-11
#param1 CLOSE-WAIT ESTAB LISTEN TIME-WAIT
#param2 server-port
#********************************************************************
TCPS1=$1
TCPP1=$2
export TCPS1
export TCPP1
# 切记双斜杠转义符,将awk的参数转义
TCP_NUM=`ss -ant|awk " \\$4 ~ /:$TCPP1$/ && \\$1 ~ /^$TCPS1/ {print \\$1 \\$4 }" | wc -l `
echo $TCP_NUM
2.配置zabbix-agent配置文件
[root@test-agent zabbix_agentd.d]# vim /etc/zabbix/zabbix_agentd.conf
### Option: UnsafeUserParameters
# Allow all characters to be passed in arguments to user-defined parameters.
# The following characters are not allowed:
# \ ' " ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @
# Additionally, newline characters are not allowed.
# 0 - do not allow
# 1 - allow
#
# Mandatory: no
# Range: 0-1
# Default:
# UnsafeUserParameters=0
UnsafeUserParameters=1
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command>
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
# 服务TCP连接数监控 参数一 状态 参数二 端口
UserParameter=tcp.status.count[*],"/etc/zabbix/zabbix_agentd.d/tcp_status_monitoring_port.sh" $1 $2
3.agent端测试脚本
[root@test-agent zabbix_agentd.d]# sh tcp_status_monitoring_port.sh LISTEN 80
6
4.重启agent
[root@test-agent zabbix_agentd.d]# systemctl restart zabbix-agent
5.server端测试
[root@test-server zabbix]# zabbix_get -s 192.168.1.127 -k tcp.status.count[LISTEN,80]
2