设为首页 收藏本站
查看: 1024|回复: 0

[经验分享] zabbix监控tcp连接状态

[复制链接]

尚未签到

发表于 2019-1-23 07:17:23 | 显示全部楼层 |阅读模式
  原理:
netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}'
TIME_WAIT 79
ESTABLISHED 6
LISTEN 3
  mkdir -p /usr/local/zabbix/share/zabbix/alertscripts
cd !$
vim check_tcp.sh
  #!/bin/bash
-------------------------------------------------------------------------------
FileName:    check_tcp.sh
Revision:    1.0
Date:        2017/04/25
-------------------------------------------------------------------------------
  metric=$1
tmp_file=/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 a+x check_tcp.sh
定义key  vim /usr/local/zabbix/etc/zabbix_agentd.conf 最后添加(如果agent的配置文件 /etc/zabbix/zabbix_agentd.conf 中定义了其他key的包含目录 Include=/etc/zabbix/zabbix_agentd.d/, 可以在 /etc/zabbix/zabbix_agentd.d/ 目录新建一个文件 tcp-status-params.conf, 再写入如下内容,注意修改位置)
  #获取tcp链接状态,注意脚本位置
UserParameter=tcp.status
  • ,/usr/local/zabbix/share/zabbix/alertscripts/check_tcp.sh $1
      直接传参数的话命令行使用
    echo "UserParameter=tcp.status
  • ,/usr/local/zabbix/share/zabbix/alertscripts/check_tcp.sh \$1" >> /usr/local/zabbix/etc/zabbix_agentd.conf
      重启zabbix客户端
    cd /usr/local/zabbix/sbin/
    service zabbix_agentd restart
    systemctl restart zabbix_agentd.service  centos 7命令
      此时可以在服务器验证取值是否正常   -s 指定客户端IP,获值正确就添加模板
    /usr/local/zabbix/bin/zabbix_get -s 10.10.61.3 -p10050 -k "tcp.status[established]"
      登录zabbix   web页面配置
    登录Zabbix 的web界面,选择 Configuration > Templates , 在主界面的右上角有个 Import 按钮,用来导入模板模板如下(可以在Windows下新建一个文本文档,写入如下内容,修改后缀,模板导入提示txt错误时点开计算机-组织-文件夹和搜索选项-查看-去掉隐藏已知文件类型的扩展名以后再重命名模板名
      )


    2.0
    2014-12-04T09:41:57Z


    Templates




    Template TCP Connection Status
    Template TCP Connection Status


    Templates




    TCP Status




    CLOSED
    0

    0

    tcp.status[closed]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status





    CLOSE_WAIT
    0

    0

    tcp.status[closewait]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status





    CLOSING
    0

    0

    tcp.status[closing]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status





    ESTABLISHED
    0

    0

    tcp.status[established]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status





    FIN_WAIT1
    0

    0

    tcp.status[finwait1]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status





    FIN_WAIT2
    0

    0

    tcp.status[finwait2]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status





    LAST_ACK
    0

    0

    tcp.status[lastack]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status





    LISTEN
    0

    0

    tcp.status[listen]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status





    SYN_RECV
    0

    0

    tcp.status[synrecv]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status





    SYN_SENT
    0

    0

    tcp.status[synsent]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status





    TIME_WAIT
    0

    0

    tcp.status[timewait]
    60
    90
    365
    0
    3


    0


    0
    0

    0

    1



    0
    0






    0


    TCP Status













    {Template TCP Connection Status:tcp.status[timewait].last()}>10000
    There are too many TCP TIME_WAIT status

    0
    4

    0





    TCP Status
    900
    200
    0.0000
    100.0000
    1
    1
    0
    1
    0
    0.0000
    0.0000
    0
    0
    0
    0


    0
    0
    C80000
    0
    2
    0

    Template TCP Connection Status
    tcp.status[closed]



    1
    0
    00C800
    0
    2
    0

    Template TCP Connection Status
    tcp.status[closewait]



    2
    0
    0000C8
    0
    2
    0

    Template TCP Connection Status
    tcp.status[closing]



    3
    0
    C800C8
    0
    2
    0

    Template TCP Connection Status
    tcp.status[established]



    4
    0
    00C8C8
    0
    2
    0

    Template TCP Connection Status
    tcp.status[finwait1]



    5
    0
    C8C800
    0
    2
    0

    Template TCP Connection Status
    tcp.status[finwait2]



    6
    0
    C8C8C8
    0
    2
    0

    Template TCP Connection Status
    tcp.status[lastack]



    7
    0
    960000
    0
    2
    0

    Template TCP Connection Status
    tcp.status[listen]



    8
    0
    009600
    0
    2
    0

    Template TCP Connection Status
    tcp.status[synrecv]



    9
    0
    000096
    0
    2
    0

    Template TCP Connection Status
    tcp.status[synsent]



    10
    0
    960096
    0
    2
    0

    Template TCP Connection Status
    tcp.status[timewait]






      再把模板添加到需要监控的主机(粉红色是已经添加后显示的模板名)
      添加完成查看图形,等待出图
      部分图片未上传,请查看笔记
    http://note.youdao.com/noteshare?id=4692b94dd32bf879b40a78faffbf902a&sub=95F2E92DCC354278832781E6D1F3FB48




  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-666464-1-1.html 上篇帖子: Zabbix Server appliance 4.0.2一键安装(第一篇) 下篇帖子: ZABBIX 存在高危漏洞,尽快修补
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表