終結 AWS 網路監控悲歌|一步到位 Collectd + Ping, CloudWatch plugin for collectd

Roman Tsai
BlendVision
Published in
13 min readSep 20, 2019

長期以來 AWS 在網路監控 (Network Monitoring) 上一直最受爭議的地方,尤其常被與傳統地端資料中心 (on-premise DC) 的網管工具 (如:PRTG) 相比下,AWS CloudWatch 顯得功能不足且陽春,無法有效滿足大型專案及客戶追求完美品質的監控要求;即便找 AWS 原廠技術人員來幫忙,往往得到的答案是:「AWS 網路管理僅限 AWS 內部使用,若有任何疑慮,可以發 ticket請求協助找問題,但 AWS 無法提供監控畫面給客戶看」,這樣的結果讓監控 AWS 網路對用戶而言是最令人傷腦筋的黑盒子。

而為什麼 AWS 無法提供呢?下圖為 AWS 蜂巢式資料中心架構設計下,形成數千個網路節點所構成巨大雲端網路環境,這也是為什麼進入一個虛擬私人雲端服務 (Virtual Private Cloud — VPC) 無法讓客戶有即時網路環境監控,也不可能讓客戶去監控這數千個網路節點。

圖1. AWS cellular data center architecture design

因此,在沒有即時網路監控下 ,當網路發生狀況時,第一時間下無法辨別是那一端點出了問題。尤其當發生於地端資料中心 (on-premise DC) 與 AWS DirectConnect 串接至 AWS 雲端服務時,沒有即時監控兩端間的資料傳送及確保有狀況錯誤警示更加困難。

本篇文章採用 collectd 工具來解決目前 AWS VPC 網路監控上的不足,此外collectd 可以從蒐集各種資料來源,例如操作系統,應用程序,日誌文件和外部設備,並儲存資料至指定監控系統資料區中,這次也是利用此特性把所有監控數據資料透過 CloudWatch for collectd plugin 送至 AWS CloudWatch 監控服務中。

圖2. AWS CloudWatch 整合 collectd 架構設計

強化 AWS CloudWatch 的監控預警平台架構 — CloudWatch plugin for collectd

AWS CloudWatch 提供給使用者自行定義需監控的系統、平台及網路之健康狀況,CloudWatch plugin for collectd 這個開源專案能夠讓 Cloudwatch 外掛整合一套強大蒐集各種系統數據能力的工具 - collectd。這套工具是一個以 C 語言為主的開發程式,主要在蒐集各種統計資訊,並提供不同種類的儲存方式來存放不同資訊的機制,其支援超過 100 種以上的外掛服務(collectd.org 官網上條列就超過了 90 多種以上的外掛服務)。常見的 collectd plugin 分類有:

  • 系統:cpu, disk, df, memory, swap, uptime, vmem
  • 網路:openvpn, ping, tcpconns, network, snmp
  • 資料庫:MySQL, Oracle, PostgreSQL, memcached, redis, drbd
  • Web:apache, nigix
  • 其它:email, monitorus, rrdtool, zookeeper, bind, iptables, dns, openldap, netapp

本次在 AWS Cloudwatch 要實現的監控目標為:從地端機房到 AWS 雲端服務之中,所有端點的服務狀況及各節點之網路延遲 (network latency) 情形,都可預警監控並在 CloudWatch matrix dashboard 上呈現。圖示如下圖藍色圓圈部份將利用 collectd 及相關外掛 (plugin) 進行各節點之資料蒐集,再來由 CloudWatch 外掛程式把蒐集的資料傳送至 CloudWatch 服務上,製作監控資訊儀表板,以供系統維運人員使用。

圖3. collectd 可涵蓋監控範圍說明

讓我們一起來進行實做吧。

1.安裝 collectd

安裝 collectd 可以自行到 https://collectd.org/ 官網下載最新版本,下列範例為 5.8.1 版本:

% wget https://storage.googleapis.com/collectd-tarballs/collectd-5.8.1.tar.bz2
% tar xf collectd-5.8.1.tar.bz2
% cd collectd-5.8.2
% ./configure

請注意看 % ./configure 執行後的結果內容;

若 ping 結果顯示為 (oping.h no found) 時,表示 gcc library 並沒有oping.h 存在,因此必須要額外再去安裝 liboping,安裝說明請見下方步驟 2.

反之若是顯示 ok,則表示可以繼續安裝;

% make install

執行 make install 後,若沒有任何問題就表示己經成功完成安裝!

2.安裝liboping

這僅限在 % ./configure 中顯示 oping.h no found 結果,才需要安裝。

安裝步驟如下:
% git clone https://github.com/octo/liboping.git
% cd liboping
% ./autogen.sh
% ./configure ; make
% make install
% cd /opt/oping
% cd include
% cp oping.h /usr/lib/gcc/x86_64-redhat-linux/7/include/
% cd ../lib
% cp * /usr/lib/gcc/x86_64-redhat-linux/7/

若發覺 gcc library 不在 /usr/lib/gcc/x86_64-redhat-linux/7/ 目錄位置時,請改成你系統上的 gcc library目錄位址;改好後,重新執行及安裝便可以完成 collectd 的系統安裝。

% ./configure
% make install

3.安裝 CloudWatch plugin for collectd

CloudWatch plugin for collectd 外掛程式可以從 github 中下載安裝

% git clone https://github.com/awslabs/collectd-cloudwatch.git
% cd collectd-cloudwatch/
% wget -bq
https://github.com/awslabs/collectd-cloudwatch/blob/master/src/setup.py
% chmod u+x setup.py
% sudo ./setup.py
% python setup.py
Installing dependencies … OK
Installing python dependencies … OK
Copying plugin tar file … OK
Extracting plugin … OK
Moving to collectd plugins directory … OK
Copying CloudWatch plugin include file … OK

Choose authentication method:
1. IAM Role [Collectd_PutMetricData]
2. IAM User
Enter choice [1]: 1

Enter proxy server name:
1. None
2. Custom
Enter choice [1]: 1

Enter proxy server port:
1. None
2. Custom
Enter choice [1]: 1

Include the Auto-Scaling Group name as a metric dimension:
1. No
2. Yes
Enter choice [1]: 1

Include the FixedDimension as a metric dimension:
1. No
2. Yes
Enter choice [1]: 1

Enable high resolution:
1. Yes
2. No
Enter choice [2]: 2

Enter flush internal:
1. Default 60s
2. Custom
Enter choice [1]: 1

Choose how to install CloudWatch plugin in collectd:
1. Do not modify existing collectd configuration
2. Add plugin to the existing configuration
3. Use CloudWatch recommended configuration (4 metrics)
Enter choice [3]: 2

Plugin configuration written successfully.
Stopping collectd process … NOT OK
Starting collectd process … OK

4.AWS IAM Policy 設定允許 Collectd 可寫 metrics data 至 Cloudwatch

在完成安裝 collectd 及 CloudWatch 外掛程式後,接下來要去設定 AWS IAM Policy 允許把蒐集到的 metrics 資料寫入到 Cloudwatch 裡,所以需建立下圖 Policy 設定內容:

然後,可以把該 Policy 及 Role 加在 collectd 安裝的 EC2 instance上,如下:

設定完畢後,就完成 IAM Policy & Role 設定作業。

5.CloudWatch Blocked & Whitelist metrics 設定

當 collectd 開始蒐集資料時,為了避免太多不必要的資訊進入Cloudwatch裡,因此可以利用blocked & whitelist (封鎖及白名單)的設定,有效去控制資料的內容蒐集。而設定檔案的目錄位置於:/opt/collectd-plugins/cloudwatch/config/:

以下是設定範例:

% cat /opt/collectd-plugins/cloudwatch/config/blocked_metrics
# This file is automatically generated — do not modify this file.
# Use this file to find metrics to be added to the whitelist file instead.
cpu-0-cpu-user
cpu-0-cpu-nice
cpu-0-cpu-system
cpu-0-cpu-idle
cpu-0-cpu-wait
cpu-0-cpu-interrupt
cpu-0-cpu-softirq
cpu-0-cpu-steal
interface-lo-if_octets-
interface-lo-if_packets-
interface-lo-if_errors-
interface-eth0-if_octets-
interface-eth0-if_packets-
interface-eth0-if_errors-
memory — memory-used
load — load-
memory — memory-buffered
memory — memory-cached

% cat /opt/collectd-plugins/cloudwatch/config/whitelist.conf
memory — memory-.*
ping — ping.*

值得一提的是,在 collectd version 5.5 或更新的版本中己經預設蒐集 server resources utilization 資源用量狀況的話,下面設定可以不需加入在whitelist.conf 白名單裡;

  • df-root-percent_bytes-used — disk utilization
  • memory–percent-used — memory utilization
  • swap–percent-used — swap utilization
  • cpu–percent-active — cpu utilization

6. Collectd (collectd.conf) 設定

collectd 主要設定檔預設是在 /opt/collectd/etc/collectd.conf 中,可以針對所需要蒐集的 service metrics 進行設定,以下是設定範例

需要載入的外掛項目只需要把 # 拿掉即可,不想蒐集的項目記得要加上#

接下來就是外掛項目的細部設定,下面是 ping 設定範例

只加入需要監控的 Host 名稱即可,SourceAddress 可不用理它,可以直接加 # 註解掉;

當設定完畢後,只需要重啟 collectd 即可;

% sudo service collectd restart

接來下可以檢視日誌內容,以確保是否有正確執行資料的蒐集

% sudo tail -f /var/log/messages

6.CloudWatch Metrics 設定

當所有 collectd 設定完畢後,最終就是要把蒐集進來的資訊呈現在CloudWatch Dashboard 上,設定步驟如下:

進入CloudWatch 後,點選 Metrics,接著輸入 collectd 後,便可以看到多少metrics指標被蒐集進來:

接著點選要顯示的 Metrics index 指標及圖表畫面設定;

檢查確認設定無誤後,點選 Add to dashboard 選項,便完成設定;

最後就能進入 CloudWatch 檢視剛剛設定好的 dashboard,之後就可隨時查看監控目標的網路連結狀況囉!

最後分享實際圖例為地端資訊中心與 AWS 雲端之間,透過DirectConnect管道連結兩端伺服器的網路監控狀況:

--

--