ss -ant | awk ‘NR>1 {++s[$1]} END {for(k in s) print k,s[k]}‘
1,配置文件
cat > /etc/zabbix/zabbix_agentd.d/tcp_status.conf <<end
## Monitor tcp status
UserParameter=tcp[*],/etc/zabbix/scripts/tcp_status.sh \$1
end
2,脚本
cat >/etc/zabbix/scripts/tcp_status.sh <<end
#!/bin/bash
#scripts for tcp status
function SYNRECV {
/usr/sbin/ss -ant | awk ‘{++s[\$1]} END {for(k in s) print k,s[k]}‘ | grep ‘SYN-RECV‘ | awk ‘{print \$2}‘
}
function ESTAB {
/usr/sbin/ss -ant | awk ‘{++s[\$1]} END {for(k in s) print k,s[k]}‘ | grep ‘ESTAB‘ | awk ‘{print \$2}‘
}
function FINWAIT1 {
/usr/sbin/ss -ant | awk ‘{++s[\$1]} END {for(k in s) print k,s[k]}‘ | grep ‘FIN-WAIT-1‘ | awk ‘{print \$2}‘
}
function FINWAIT2 {
/usr/sbin/ss -ant | awk ‘{++s[\$1]} END {for(k in s) print k,s[k]}‘ | grep ‘FIN-WAIT-2‘ | awk ‘{print \$2}‘
}
function TIMEWAIT {
/usr/sbin/ss -ant | awk ‘{++s[\$1]} END {for(k in s) print k,s[k]}‘ | grep ‘TIME-WAIT‘ | awk ‘{print \$2}‘
}
function LASTACK {
/usr/sbin/ss -ant | awk ‘{++s[\$1]} END {for(k in s) print k,s[k]}‘ | grep ‘LAST-ACK‘ | awk ‘{print \$2}‘
}
function LISTEN {
/usr/sbin/ss -ant | awk ‘{++s[\$1]} END {for(k in s) print k,s[k]}‘ | grep ‘LISTEN‘ | awk ‘{print \$2}‘
}
\$1
end
chmod a+x /etc/zabbix/scripts/tcp_status.sh