zoukankan      html  css  js  c++  java
  • zabbix 监控linux tcp连接数

                zabbix 监控linux tcp连接数

                                         作者:尹正杰

    版权声明:原创作品,谢绝转载!否则将追究法律责任。

    一.TCP的状态概述

    1>.端口状态转换

    2>.TCP 三次握手

    3>.四次断开

    二.zabbix agent端配置监控TCP的脚本

    1>.自定义脚本

    [root@node201.yinzhengjie.org.cn ~]# vim /etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh 
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# cat /etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh 
    #!/bin/bash
    #
    #********************************************************************
    #Author:        yinzhengjie
    #QQ:             1053419035
    #Date:             2019-11-23
    #FileName:        shell/menu.sh
    #URL:             http://www.cnblogs.com/yinzhengjie
    #Description:        The test script
    #Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
    #********************************************************************
    
    
    function tcp_conn_status(){
        TCP_STAT=$1
        ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' > /tmp/tcp_status_monitoring.txt
        TCP_NUM=$(grep "$TCP_STAT" /tmp/tcp_status_monitoring.txt | cut -d ' ' -f2)
        if [ -z $TCP_NUM ];then
            TCP_NUM=0
        fi
        echo $TCP_NUM
    }
    
    function main(){
        case $1 in
            tcp_status)
                tcp_conn_status $2;
                ;;
        esac
    }
    
    main $1 $2
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# 

    2>.为脚本添加执行权限

    [root@node201.yinzhengjie.org.cn ~]# ll /etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh 
    -rw-r--r-- 1 root root 887 Feb 28 07:15 /etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# chmod +x /etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh                 #为自定义监控脚本添加执行权限
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# ll /etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh 
    -rwxr-xr-x 1 root root 887 Feb 28 07:15 /etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# /etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh tcp_status LISTEN            #测试脚本的可用性,测试有数据说明脚本是可用的
    7
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# /etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh tcp_status TIME-WAIT
    48
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# ll /tmp/
    total 4
    drwx------ 3 root root 17 Feb 27 20:20 systemd-private-911f89b1c3ee4635acad4ca6cdb6b581-chronyd.service-XgssdE
    -rw-r--r-- 1 root root 41 Feb 28 07:17 tcp_status_monitoring.txt
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# rm -f /tmp/tcp_status_monitoring.txt                                  #使用root用户昨做完删除测试后记得删除文件,否则默认的zabbix用户是没有写入权限的哟~
    [root@node201.yinzhengjie.org.cn ~]# 

    3>. 修改zabbix agent的配置文件并重启服务

    [root@node201.yinzhengjie.org.cn ~]# grep "# UserParameter=" /etc/zabbix/zabbix_agentd.conf 
    # UserParameter=
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# sed -r -i 's@# (UserParameter=)@1tcp_status_monitoring[*],/etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh $1 $2@' /etc/zabbix/zabbix_agentd.conf 
    [root@node201.yinzhengjie.org.cn ~]# 
    [root@node201.yinzhengjie.org.cn ~]# grep ^UserParameter= /etc/zabbix/zabbix_agentd.conf 
    UserParameter=tcp_status_monitoring[*],/etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh $1 $2
    [root@node201.yinzhengjie.org.cn ~]#
    [root@node201.yinzhengjie.org.cn ~]# systemctl restart zabbix-agent.service 
    [root@node201.yinzhengjie.org.cn ~]#

    三.zabbix webUI进行配置

    1>.在zabbix server测试是否可以获取数据

    [root@zabbix203.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/zabbix/bin/zabbix_get -s node201.yinzhengjie.org.cn -p 10050 -k "tcp_status_monitoring[tcp_status SYN-SENT]"
    8
    [root@zabbix203.yinzhengjie.org.cn ~]# 
    [root@zabbix203.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/zabbix/bin/zabbix_get -s node201.yinzhengjie.org.cn -p 10050 -k "tcp_status_monitoring[tcp_status TIME-WAIT]"
    47
    [root@zabbix203.yinzhengjie.org.cn ~]# 

    2>.在zabbix WebUI上自定义TCP的监控模板

      其实自定义监控模板过程和之前自定义Nginx的监控模板类似,此处我就懒得截图啦,成功添加监控项后如下图所示。
    
      博主推荐阅读:
        自定义Nginx监控项模板(https://www.cnblogs.com/yinzhengjie2020/p/12340063.html)

    3>.将TCP的11个监控项画在同一个图像中

    4>.将自定义模板和对应的agent主机进行关联

    5>.间隔一段时间后,查看图形

  • 相关阅读:
    Android开源框架——Volley
    Android中的事件传递机制
    @ViewDebug.ExportedProperty的使用
    字符间距——扩展
    读取assets文件夹下图片(ods_interview)
    Android消息推送——JPush极光推送
    深入模块
    正则表达式和re模块
    初识模块
    迭代器和生成器
  • 原文地址:https://www.cnblogs.com/yinzhengjie2020/p/12347349.html
Copyright © 2011-2022 走看看