zabbix监控linux和windows的tcp连接数

时间:2022-01-29 03:35:30

在linux上手动获得tcp连接数的命令为:

netstat -an|awk ‘/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}‘ #ESTABLISHED       socket已经建立连接   #CLOSED            socket没有被使用,无连接   #CLOSING           服务器端和客户端都同时关闭连接   #CLOSE_WAIT        等待关闭连接   #TIME_WAIT         表示收到了对方的FIN报文,,并发送出了ACK报文,等待2MSL后就可回到CLOSED状态   #LAST_ACK          远端关闭,当前socket被动关闭后发送FIN报文,等待对方ACK报文   #LISTEN            监听状态   #SYN_RECV          接收到SYN报文   #SYN_SENT          已经发送SYN报文


在zabbix_agent端编辑配置文件:

vi /etc/zabbix/zabbix_agentd.conf Include=http://www.mamicode.com/etc/zabbix/zabbix_agentd.d/#编写一个获取tcp状态的脚本: mkdir /etc/zabbix/zabbix_agentd.d/script     #此处注意,一定不要将脚本直接放到/etc/zabbix/zabbix_agentd.d/目录下,否则zabbix_agent会把它当做自己的配置文件执行,造成重启不成功 vi /etc/zabbix/zabbix_agentd.d/script/tcp_status.sh #!/bin/bash #this script is get tcp status metric=$1 tmp_file=http://www.mamicode.com/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#赋予脚本可执行权限 chmod +x /



编写zabbix_agent上的自定义key:

vi /etc/zabbix/zabbix_agentd.conf UserParameter=tcp.status[*],/etc/zabbix/zabbix_agentd.d/script/tcp_status.sh $1


重启zabbix_agent:

/etc/init.d/zabbix_agent restart


在服务端测试输入参数返回的值:

zabbix_get -s 118.190.114.140 -p 10050 -k tcp.status[listen]


最后在zabbix上创建监控项和触发器,附件中上传了一个附件,里面是tcp_status模板,直接在配置——模板——导入即可。





windows:

在windows上手动获得tcp连接数的命令为: