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>.間隔一段時間后,查看圖形

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM