使用 Monit 監視已建立的套接字數量?

使用 Monit 監視已建立的套接字數量?

我不知道如何讓 Monit 監視伺服器上開啟/建立的 TCP/IP 連線的數量,以便在開啟「太多」時發送警報。你知道如何設定嗎?

答案1

這是另一個解決方案

定義以下配置監控:

check program OpenSocket with path "/bin/checkn_socket.sh"
    if status > 0 then alert
                group admin

腳本:checkn_socket.sh

#!/bin/bash

Threshold=4 # Set Threshold

TotalEstSocket=$(netstat -t | awk '{/ESTABLISHED/ && n++} END{ print n }')

if (( TotalEstSocket >= Threshold ))
then
        echo >&2 "Too Many OpenSocket"
        exit $TotalEstSocket
else
        exit 0
fi

監控日誌

[IST Sep 12 22:32:14] error    : 'OpenSocket' status failed (4) for /bin/checkn_socket.sh. Error: Too Many OpenSocket
..
[IST Sep 12 22:32:17] info     : 'OpenSocket' status succeeded
[IST Sep 12 22:32:26] error    : 'OpenSocket' status failed (4) for /bin/checkn_socket.sh. Error: Too Many OpenSocket
..
[IST Sep 12 22:32:29] error    : 'OpenSocket' status failed (4) for /bin/checkn_socket.sh. Error: Too Many OpenSocket
..
[IST Sep 12 22:32:32] error    : 'OpenSocket' status failed (4) for /bin/checkn_socket.sh. Error: Too Many OpenSocket
..
[IST Sep 12 22:32:35] info     : 'OpenSocket' status succeeded

答案2

它似乎沒有被直接支持,但我想出了一個技巧。

決定每分鐘 ESTABLISHED 連線的數量,並寫入具有相同數量的零位元組的檔案。

然後,設定 Monit 來檢查該 Zeros 檔案的檔案大小。如果它收到“太大”警報。

在某些使用者的 crontab 中:

* * * * * /bin/sh -c '/bin/dd if=/dev/zero of=/tmp/tcp_connections.monit count=$(/bin/netstat -t | /bin/grep ESTABLISHED | /usr/bin/wc -l) bs=1 >/dev/null 2>&1'

在監控配置中:

check file tcp_connections with path /tmp/tcp_connections.monit
    if size > 16KB then alert

相關內容