如何使用 CloudWatch 整合發佈和監控 Amazon EMR 應用程式狀態?
我想將 Amazon CloudWatch 與 Amazon EMR 整合,以發佈和監控安裝在叢集上應用程式的狀態。我希望 CloudWatch 在應用程式關閉時提醒我。
簡短描述
將 CloudWatch 與 Amazon EMR 整合時,您可以追蹤已安裝應用程式 (例如 HiveServer2 和 YARN ResourceManager) 的關鍵狀態。然後,您可以將狀態發佈到 CloudWatch 自訂指標,並設定服務無法使用時的警示。若要追蹤其他應用程式,您可以根據需要修改應用程式清單。
解決方法
先決條件:
- Amazon EMR 版本 5.30.0 或更新版本
- 具有 cloudwatch:PutMetricData 權限的 Amazon EMR 執行個體設定檔角色或 AWS Identity and Access Management (IAM) 使用者角色
建立指令碼來監控 Amazon EMR 應用程式
您可以建立指令碼來監控 Amazon EMR 應用程式。以下名為 check_process.sh 的範例指令碼,可用於監控主節點上的 YARN ResourceManager 和 HiveServer2。該指令碼還可監控核心和任務工作節點上的 YARN NodeManager。若要監控其他應用程式,您可以修改指令碼中 # Monitor specific services 區段下的應用程式。
若要設定以下指令碼以包含其他應用程式,請參閱使用 Amazon EMR 叢集建立啟動動作來安裝其他軟體。
指令碼範例:
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #!/bin/bash # Set up logging LOG_FILE="/var/log/hadoop/service-monitor-detailed.log" LOG_STATUS_FILE="/var/log/hadoop/service-monitor-status.log" TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') CLUSTERID=$(jq -r ".jobFlowId" < /emr/instance-controller/lib/info/extraInstanceData.json) INSTANCEID=$(ec2-metadata -i | cut -d " " -f 2) HOSTIP=$(hostname -i) NODETYPE=$(cat /mnt/var/lib/instance-controller/extraInstanceData.json | jq -r '.instanceRole' | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}') # Function to log messages log_message() { echo "$TIMESTAMP - $1" >> "$LOG_FILE" echo "$TIMESTAMP - $1" } log_status_message() { echo "$TIMESTAMP - $1" >> "$LOG_STATUS_FILE" } # Function to send metric to CloudWatch send_to_cloudwatch() { local host_ip=$1 local service_name=$2 local status=$3 aws cloudwatch put-metric-data \ --namespace "EMR/ServiceStatus" \ --metric-name "ServiceStatus" \ --value "$status" \ --unit "Count" \ --dimensions ClusterId=$CLUSTERID,NodeServiceName=$service_name,InstanceId=$INSTANCEID,NodeType=$NODETYPE \ --timestamp "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \ --region "${AWS_REGION:-us-east-1}" || { log_message "ERROR: Failed to send metric for service $service_name" return 1 } log_message "Successfully sent metric for service: $service_name (Status: $status)" } # Create log file if it doesn't exist touch "$LOG_FILE" touch "$LOG_STATUS_FILE" log_message "Starting service monitoring..." # Monitor specific services services=( "hive-server2" "hadoop-yarn-resourcemanager" "hadoop-yarn-nodemanager" ) service_names=( "HiveServer2" "YARN-ResourceManager" "YARN-NodeManager" ) for i in "${!services[@]}"; do # Check if service is disabled as not all services are running on all nodes if systemctl is-enabled "${services[$i]}" 2>/dev/null | grep -q "disabled"; then log_message "$CLUSTERID $INSTANCEID $HOSTIP $NODETYPE ${service_names[$i]}-Status DISABLED (ignored)" continue fi # Get service status status_output=$(systemctl status "${services[$i]}" 2>/dev/null) # Extract the process status process_status=$(echo "$status_output" | grep "Active:" | sed -E 's/Active: ([^ ]+) .*/\1/' | xargs) # Log message log_message "$CLUSTERID $INSTANCEID $HOSTIP $NODETYPE ${service_names[$i]}-Status $process_status" log_status_message "$CLUSTERID $INSTANCEID $HOSTIP $NODETYPE ${service_names[$i]}-Status $process_status" # Convert status to numeric value for CloudWatch status_value=0 if [ "$process_status" != "active" ]; then status_value=1 # Send to CloudWatch send_to_cloudwatch "$HOSTIP" "${service_names[$i]}" "$status_value" fi done log_message "Service monitoring completed." exit 0
**重要:**在生產環境中執行指令碼之前,最好先在測試環境中測試該指令碼。
上述指令碼會將自訂指標發佈到 CloudWatch。AWS 會依小時按比例計算所有自訂指標的費用,並且僅在指令碼將指標傳送到 CloudWatch 時才開始計費。如需詳細資訊,請參閱 Amazon CloudWatch 定價。
在您的 Amazon EMR 叢集上設定服務監控
**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
若要實作自動化服務監控,請使用啟動動作作指令碼。
請完成下列步驟:
-
若要準備指令碼,請執行以下 cp AWS CLI 命令,將指令碼上傳到 Amazon EMR 叢集可以存取的 Amazon Simple Storage Service (Amazon S3) 儲存貯體:
aws s3 cp check_process.sh s3://your-bucket/monitoring/check_process.sh -
若要將指令碼複製到每個叢集節點,並使用 crontab 來排程指令碼,請建立類似下列範例的啟動動作指令碼:
#!/bin/bash # Copy monitoring script from S3 aws s3 cp s3://your-bucket/monitoring/check_process.sh /home/hadoop/ chmod +x /home/hadoop/check_process.sh # Add to crontab (crontab -l 2>/dev/null; echo "*/5 * * * * /home/hadoop/check_process.sh") | crontab -**注意:**修改 crontab 持續時間以滿足您的需求。
-
將啟動動作指令碼新增到 Amazon EMR 叢集設定檔。
**注意:**Amazon EMR EC2 執行個體必須具有存取指令碼的最低必要權限。 -
啟動叢集。
啟動叢集後,在叢集節點上執行以下命令,以確認 Amazon EMR 正確複製了指令碼:
ls -l /home/hadoop/check_process.sh
若要確認您已正確設定 crontab,請在叢集節點上執行下列命令:
crontab -l
查看日誌
此指令碼會在叢集節點上產生詳細的日誌和狀態日誌。若要確認指令碼是否正常執行,請查看兩個日誌。
詳細日誌
/var/log/hadoop/service-monitor-detailed.log 檔案提供包含時間戳記、叢集 ID、執行個體 ID、主機 IP 位址、節點類型和服務狀態的完整日誌。
檔案範例:
2025-05-06 23:07:01 - Starting service monitoring... 2025-05-06 23:07:01 - j-1O1234567890 i-0a6871234567890 111.xx.xx.92 Master HiveServer2-Status inactive 2025-05-06 23:07:01 - Successfully sent metric for service: HiveServer2 (Status: 1) 2025-05-06 23:07:01 - j-1O1234567890 i-0a6871234567890 111.xx.xx.92 Master YARN-ResourceManager-Status active 2025-05-06 23:07:01 - Service monitoring completed.
狀態日誌
/var/log/hadoop/service-monitor-status.log 檔案包含服務狀態記錄,但不包含其他中繼資料。
檔案範例:
2025-05-06 23:07:01 - j-1O1234567890 i-0a6871234567890 111.xx.xx.92 Master HiveServer2-Status inactive 2025-05-06 23:07:01 - j-1O1234567890 i-0a6871234567890 111.xx.xx.92 Master YARN-ResourceManager-Status active 2025-05-06 23:08:01 - j-1O1234567890 i-0a6871234567890 111.xx.xx.92 Master HiveServer2-Status inactive 2025-05-06 23:08:01 - j-1O1234567890 i-0a6871234567890 111.xx.xx.92 Master YARN-ResourceManager-Status failed 2025-05-06 23:09:01 - j-1O1234567890 i-0a6871234567890 111.xx.xx.92 Master HiveServer2-Status inactive 2025-05-06 23:09:01 - j-1O1234567890 i-0a6871234567890 111.xx.xx.92 Master YARN-ResourceManager-Status failed
使用 CloudWatch 監控應用程式指標
當應用程式關閉時,指令碼會將指標傳送到 CloudWatch。
若要監控指標,請完成以下步驟:
- 開啟 CloudWatch console (CloudWatch 主控台)。
- 在導覽窗格的 Metrics (指標) 下,選擇 All metrics (所有指標)。
- 在 Metrics (指標) 下,選擇 EMR/ServiceStatus,然後選取 ServiceStatus 指標。
- 依可用維度篩選指標: ClusterId、InstanceId、NodeServiceName 和 NodeType。
相關資訊
- 語言
- 中文 (繁體)

相關內容
- 已提問 2 年前