硬碟與記憶體,在實際的營運環境中是非常重要的警示指標,
但在CloudWatch中沒有針對EC2的硬碟與記憶體的預設Metrics.
(預設有的是CPU, IO, Network,...)
不過AWS有提供另外的辦法-->自定義Metrics.
基本上是讓EC2本身向CloudWatch發送資訊,
所以使用AWS在這部分只有自己來了
(AWS還能針對alarm提供後續的行為,ex:重啟,關機等等,功能算是很不錯的,
但一開始卻缺了這兩樣重要的監控指標= =")
先準備一個IAM使用者,
具有以下操作的權限 (參考 为 Amazon EC2 Linux 实例监控内存和磁盘指标)
cloudwatch:PutMetricData
cloudwatch:GetMetricStatistics
cloudwatch:ListMetrics
ec2:DescribeTags
首先新增一個使用者,記下credentials. (這資訊不能從任何地方再次取得,請保管好)
點選該使用者的Permissions分頁,新增Inline Policy.
Policy內容如圖
對不同的OS其安裝的過程不盡相同,
Aaron的環境上有Centos 6.8, Centos 7.2和Win 2012r2.
windows與linux用來獲取磁碟, 記憶體資訊的工具是不同的.
Linux作業系統,使用Perl Script達到這個目的(需額外下載 Amazon CloudWatch Monitoring Scripts for Linux)
Win作業系統,使用EC2Config Service達到這個目的 (win 2012r2有內建)
Centos 6.8
準備套件安裝
sudo yum install wget unzip perl-core perl-DateTime perl-Sys-Syslog perl-CPAN perl-libwww-perl perl-Crypt-SMIME perl-Crypt-SSLeay openssl openssl-devel -y
啟動CPAN (Comprehensive Perl Archive Network)
sudo perl -MCPAN -e shell
安裝LWP Perl Bundles(過程中都選yes)
install Bundle::LWP6 LWP YAML
離開CPAN
quit
安裝perl https協定
perl -MCPAN -e 'install LWP::Protocol::https'
( 否則報告到cloudwatch時可能遇到 ERROR: Failed to call CloudWatch: HTTP 500. )
取得perl script
wget http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip
解壓縮到/opt
unzip CloudWatchMonitoringScripts-1.2.1.zip -d /opt
刪除zip檔
rm -f CloudWatchMonitoringScripts-1.2.1.zip
複製aws script的授權配置樣版
cp /opt/aws-scripts-mon/awscreds.template /opt/aws-scripts-mon/awscreds.conf
編輯授權配置檔(將建立的IAM使用者的Access Key ID和Secret Access Key輸入
vi /opt/aws-scripts-mon/awscreds.conf
修改配置檔權限
chmod 0400 /opt/aws-scripts-mon/awscreds.conf
驗證
/opt/aws-scripts-mon/mon-put-instance-data.pl --mem-util --verify --verbose
出現以下字樣代表成功,如果將--verbose拿掉,則會report到cloudwatch上,可到aws cloudwatch -> metrics去查看.
Verification completed successfully. No actual metrics sent to CloudWatch.
加入排程 crontab -e,大功告成
*/5 * * * * /opt/aws-scripts-mon/mon-put-instance-data.pl --mem-util --swap-util --disk-space-util --disk-path=/ --from-cron
Centos 7.2
準備套件安裝
sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA zip unzip -y
不需安裝安裝LWP Perl Bundles,直接跳到取得perl script,之後皆與Centos 6.8相同
Win 2012r2
不用安裝套件,
EC2Config Service內建在主機(C:\Program Files\Amazon\Ec2ConfigService)中,
關於設定檔(\Settings\AWS.EC2.Windows.CloudWatch.json),
找到PollInterval改為每5分鐘送一次資料=>00:05:00,
效能指標的設定如下圖紅框,若複製紅框內容可設定多個指標.
設定IAM使用者的Access Key ID和Secret Access Key,並且指定哪些components要傳給cloudwatch.
上圖關於Flows的設定部分有誤,應是來源,目的,當有多個來原或目的時,使用()刮起來,
ex: (PerformanceCounter, PerformanceCounter2, PerformanceCounter3), CloudWatch
啟用cloudwatch Logs整合,執行Ec2ConfigServiceSettings.exe,照下圖打勾,OK,最後去重新啟動EC2Config服務.
這樣就能讓ec2 instance將自定義的指標傳到cloudwatch去,然後就能設定相關警示了.
參考
Setup AWS Cloudwatch Memory and Drive Monitoring on RHEL
Monitor EC2 Memory Usage using AWS CloudWatch