跳到內容

社群 FreedomBox/監控伺服器

來自華夏公益教科書

每個社群的 FreedomBox 伺服器上的監控客戶端需要將收集到的指標傳送到一箇中央位置。這個中央位置,即監控伺服器,需要儲存傳送的資料,將資料視覺化以供管理員檢視,並在出現異常情況時向他們發出警報。本節介紹伺服器上的設定。假設伺服器也執行著 FreedomBox。但是,對說明進行一些小的修改,任何執行 Debian GNU/Linux Buster 或更高版本的伺服器都可以用作伺服器。

設定 Graphite Carbon

[編輯 | 編輯原始碼]

Graphite Carbon 伺服器負責從各個 FreedomBox 伺服器收集資料,並將它們儲存在一個名為 Whisper 的時間序列資料庫中。執行以下命令來設定 Graphite Carbon。

  1. 安裝 Graphite Carbon。
    apt install graphite-carbon
    
  2. 開啟防火牆中用於接收來自 FreedomBox 伺服器資料的埠。
    firewall-cmd --zone=external --add-port=2003/tcp
    firewall-cmd --zone=external --add-port=2003/tcp --permanent
    
  3. 之後,graphite-carbon 伺服器將執行並監聽埠 2003。可以使用以下命令檢查這一點
    systemctl status carbon-cache
    ss -nlpt | grep carbon-cache
    

設定 Graphite Web

[編輯 | 編輯原始碼]

Graphite Web 服務負責將 Graphite Carbon 儲存的資料提供給能夠視覺化資料的消費者,例如 Grafana。執行以下命令來設定 Graphite Web。

  1. 安裝 Graphite Web
    apt install graphite-web libapache2-mode-wsgi-py3
    
  2. 為 Graphite Web 使用的 Django 框架生成一個金鑰。
    python3 -c 'import random; result = "".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)") for i in range(64)]); print(result)'
    
  3. 注意從先前命令中生成的金鑰,並將其設定為檔案 /etc/graphite/local_settings.py 中的金鑰。
    SECRET_KEY="<output from the key generation command>"
    
  4. 為 Graphite Web 建立一個數據庫以儲存其自身資料。
    sudo --user _graphite graphite-manage migrate --run-syncdb
    chown _graphite:_graphite /var/lib/graphite/graphite.db
    chmod 0600 /var/lib/graphite/graphite.db
    
  5. 為 graphite-web 建立 Apache 配置,並開始提供服務。
    cat << EOF > /etc/apache2/sites-available/graphite-web.conf
    Listen 8080
    
    <VirtualHost *:8080>
            WSGIDaemonProcess _graphite processes=5 threads=5 display-name='%{GROUP}' inactivity-timeout=120 user=_graphite group=_graphite
            WSGIProcessGroup _graphite
            WSGIImportScript /usr/share/graphite-web/graphite.wsgi process-group=_graphite application-group=%{GLOBAL}
            WSGIScriptAlias / /usr/share/graphite-web/graphite.wsgi
    
            Alias /static/ /usr/share/graphite-web/static/
            <Location "/static/">
                    SetHandler None
            </Location>
    </VirtualHost>
    EOF
    a2ensite graphite-web
    systemctl restart apache2
    

設定 Grafana

[編輯 | 編輯原始碼]

Grafana 從 Graphite Web 獲取資料並生成漂亮的視覺化效果。它還可以配置為在資料中發生特定事件時發出警報。執行以下命令來設定 Grafana。

  1. 從其專案的 Debian 儲存庫中安裝 Grafana。
    apt-get install -y apt-transport-https
    wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
    cat << EOF > /etc/apt/sources.list.d/grafana.list
    deb https://packages.grafana.com/oss/deb stable main
    EOF
    apt update
    apt install grafana
    
  2. 配置 Grafana 以從現有域的子路徑提供服務。編輯檔案 /etc/grafana/grafana.ini,並確保以下兩個指令按如下所示進行配置。
    root_url = https://<domain_name>/monitor/
    serve_from_sub_path = true
    
  3. 配置 Grafana 在每次機器啟動時啟動,並啟動 Grafana。
    systemctl daemon-reload
    systemctl enable grafana-server.service
    systemctl start grafana-server.service
    
  4. 建立 Apache 配置以從子路徑代理 Grafana,並開始提供服務。
    cat << EOF > /etc/apache2/conf-available/grafana.conf
    <Location /monitor/>
        ProxyPass https://:3000/monitor/
    </Location>
    EOF
    a2enconf grafana
    systemctl reload apache2
    
  5. 設定 Grafana 訪問憑據。訪問 https://<domain_name>/monitor/,並使用以下憑據登入。系統將提示您更改密碼。設定一個強密碼。
    • 使用者名稱: admin
    • 密碼: admin
  6. 為每個將使用監控設定的使用者建立使用者帳戶。在左側選單中,轉到 伺服器管理員使用者。單擊 新建使用者按鈕並提供姓名、電子郵件、使用者名稱和密碼等詳細資訊以建立新的使用者帳戶。
  7. 透過 Graphite Web 將 Grafana 連線到 Graphite Carbon 資料庫,我們在之前的部分中配置了它。轉到 配置資料來源,並選擇 Carbon 資料來源。在顯示的配置對話方塊中,提供以下詳細資訊
    • 名稱: Carbon(或您選擇的任何名稱)
    • URL: https://:8080
    • 訪問: 伺服器
    • 版本: 1.1
    按下 儲存並測試。您應該會看到一條訊息確認資料來源可訪問。

為每個社群建立儀表板

[編輯 | 編輯原始碼]

在 Grafana 介面中,您需要為每個您管理的社群建立儀表板。以下說明概述瞭如何執行此操作

  1. 登入 Grafana 介面。
  2. 在選單中,選擇 建立儀表板。將建立儀表板,並顯示提示以新增新的面板。選擇 圖表
  3. 將建立新的儀表板,其中包含一個簡單的圖表。
  4. 單擊儲存圖示儲存儀表板。在儲存對話方塊中,根據建立儀表板的社群提供儀表板的名稱。單擊 儲存
  5. 要開始在面板中顯示正確的資料,請單擊 面板標題並選擇 編輯。在 指標選項卡中,單擊 選擇指標下拉選單。您應該會看到在 collectd 中配置的社群分配的域的名稱。然後選擇一個指標,例如 cpu-0,然後選擇 cpu-user。您應該開始在圖表中看到指標的繪圖。儲存儀表板。

要監控的引數

[編輯 | 編輯原始碼]

建議您根據自己的喜好建立儀表板,但至少監控每個社群的以下引數

  • CPU
    • 系列:<community> • * • cpu-user
    • 函式:averageSeries() • alias(使用者)
    • 系列:<community> • * • cpu-nice
    • 函式:averageSeries() • alias(正常)
    • 系列:<community> • * • cpu-system
    • 函式:averageSeries() • alias(系統)
    • 系列:<community> • * • cpu-wait
    • 函式:averageSeries() • alias(等待)
  • 記憶體
    • 系列:<community> • memory • percent-used
    • 函式:alias(% 使用)
  • 負載
    • 系列:<community> • load • load • shorterm
    • 函式:alias(5 分鐘)
    • 系列:<community> • load • load • midterm
    • 函式:alias(10 分鐘)
    • 系列:<community> • load • load • longterm
    • 函式:alias(15 分鐘)
  • 磁碟
    • 系列:<community> • <disk> • disk_ops • read
    • 函式:alias(讀取操作)
    • 系列:<community> • <disk> • disk_ops • write
    • 函式:alias(寫入操作)
    • 系列:<community> • <disk> • disk_octets • read
    • 函式:alias(讀取位元組)
    • 系列:<community> • <disk> • disk_octets • write
    • 函式:alias(寫入位元組)
  • 磁碟 I/O 時間
    • 系列:<community> • <disk> • pending_operations
    • 函式:alias(掛起操作)
    • 系列:<community> • <disk> • disk_io_time • io_time
    • 函式:alias(I/O 時間)
    • 系列:<community> • <disk> • disk_io_time • weighted_io_time
    • 函式:alias(加權 I/O 時間)
  • <mountpoint> 分割槽(對於每個分割槽)
    • 系列:<community> • df-<mountpoint> • percent_bytes-used
    • 函式:alias(使用)
    • 系列:<community> • df-<mountpoint> • percent_bytes-reserved
    • 函式:alias(保留)
  • <LAN/WAN> 網路頻寬(對於每個介面)
    • 系列:<community> • interface-<interface> • if_octets • rx
    • 函式:alias(接收)
    • 系列:<community> • interface-<interface> • if_octets • tx
    • 函式:alias(傳輸)
  • 跟蹤連線
    • 系列:<community> • conntrack • conntrack
    • 函式:alias(跟蹤的連線)
  • Wi-Fi 接入點
    • alias(sumSeries(isNonNull(<community>.ping.ping-10_42_0_[1-9][0-9])), 'Wi-Fi 接入點')
  • 網際網路上行鏈路
    • alias(isNonNull(<community>.ping.ping-10_42_0_4), '網際網路路由器')
    • alias(isNonNull(<community>.ping.ping-10_42_0_5), 'PTP 鏈路 - 路由器 A')
    • alias(isNonNull(<community>.ping.ping-10_42_0_6), 'PTP 鏈路 - 路由器 B')
  • 網際網路 Ping
    • 系列:<community> • ping • ping-1_1_1_1
    • 函式:alias(網際網路 Ping)
  • 程序
    • 系列:<community> • processes • ps_state-running
    • 函式:alias(正在執行)
    • 系列:<community> • processes • ps_state-sleeping
    • 函式:alias(休眠)
    • 系列:<community> • processes • ps_state-paging
    • 函式:alias(分頁)
    • 系列:<community> • processes • ps_state-blocked
    • 函式:alias(阻塞)
    • 系列:<community> • processes • ps_state-zombies
    • 函式:alias(殭屍程序)
    • 系列:<community> • processes • ps_state-stopped
    • 函式:alias(已停止)
  • 正常執行時間
    • 系列:<community> • uptime • uptime
    • 函式:alias(自重啟以來的時間)
  • 使用者
    • 系列:<community> • users • users
    • 函式:alias(已登入使用者)
  • collectd 佇列
    • 系列:<community> • collectd-cache • cache_size
    • 函式:alias(快取大小)
    • 系列:<community> • collectd-write_queue • derive-dropped
    • 函式:alias(已放棄的指標)
    • 系列:<community> • collectd-write_queue • queue_length
    • 函式:alias(佇列長度)
華夏公益教科書