範例:從 Apache 日誌擷取欄位並指派維度 - Amazon CloudWatch 日誌

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

範例:從 Apache 日誌擷取欄位並指派維度

有時,不使用計數,而是在指標值的個別日誌事件中使用值很有用。此範例顯示如何建立擷取規則來建立指標,該指標會量測 Apache Web 伺服器傳出的位元組數。

這個擷取規則會比對日誌事件的 7 個欄位。指標值為第七個比對字符的值。您可以查看對擷取規則 metricValue 欄位中字符為「$7」的參考。

此範例也會說明如何將維度指派給您要建立的指標。

使用 CloudWatch 主控台建立量度篩選
  1. 請在以下位置開啟 CloudWatch 主控台。 https://console.aws.amazon.com/cloudwatch/

  2. 在導覽窗格中,選擇 Log groups (日誌群組)。

  3. 選擇 Apache 伺服器的日誌群組名稱。

  4. 選擇 Actions > Create metric filter (建立指標篩選條件)

  5. 針對 Filter Pattern (篩選條件模式),輸入 [ip, id, user, timestamp, request, status_code, size]

  6. (選用) 若要測試篩選條件模式,請在 Test Pattern (測試模式) 下方,輸入一個或多個日誌事件,用以測試模式。每個日誌事件都必須在一行內,因為 Log event messages (日誌事件訊息) 方塊中使用換行來分隔日誌事件。

  7. 選擇 Next (下一步),然後針對 Filter Name (篩選條件名稱),輸入 size

  8. Metric (指標詳細資訊) 下的 Metric Namespace (指標命名空間) 中,輸入 MyNameSpace。因為這是新的命名空間,請務必選取 Create new (新建)。

  9. 針對 Metric name (指標名稱),輸入 BytesTransferred

  10. 針對 Metric value (指標值),輸入 $size

  11. 針對 Unit (單位),選取Bytes (位元組)。

  12. 針對 Dimension Name (維度名稱),輸入 IP

  13. 針對 Dimension Value (維度值),輸入 $ip,然後選擇 Next (下一步)。

  14. 選擇 Create metric filter (建立指標篩選條件)

使用建立此測量結果篩選 AWS CLI

在命令提示中,執行下列命令

aws logs put-metric-filter \ --log-group-name MyApp/access.log \ --filter-name BytesTransferred \ --filter-pattern '[ip, id, user, timestamp, request, status_code, size]' \ --metric-transformations \ metricName=BytesTransferred,metricNamespace=MyNamespace,metricValue='$size'
aws logs put-metric-filter \ --log-group-name MyApp/access.log \ --filter-name BytesTransferred \ --filter-pattern '[ip, id, user, timestamp, request, status_code, size]' \ --metric-transformations \ metricName=BytesTransferred,metricNamespace=MyNamespace,metricValue='$size',unit=Bytes,dimensions='{{IP=$ip}}'
注意

在此命令中,使用此格式指定多個維度。

aws logs put-metric-filter \ --log-group-name my-log-group-name \ --filter-name my-filter-name \ --filter-pattern 'my-filter-pattern' \ --metric-transformations \ metricName=my-metric-name,metricNamespace=my-metric-namespace,metricValue=my-token,unit=unit,dimensions='{dimension1=$dim,dimension2=$dim2,dim3=$dim3}'

您可以在 put-log-event 呼叫中使用下列資料來測試此規則。如果您沒有在之前的範例中移除監控規則,此將會產生兩種不同的指標。

127.0.0.1 - - [24/Sep/2013:11:49:52 -0700] "GET /index.html HTTP/1.1" 404 287 127.0.0.1 - - [24/Sep/2013:11:49:52 -0700] "GET /index.html HTTP/1.1" 404 287 127.0.0.1 - - [24/Sep/2013:11:50:51 -0700] "GET /~test/ HTTP/1.1" 200 3 127.0.0.1 - - [24/Sep/2013:11:50:51 -0700] "GET /favicon.ico HTTP/1.1" 404 308 127.0.0.1 - - [24/Sep/2013:11:50:51 -0700] "GET /favicon.ico HTTP/1.1" 404 308 127.0.0.1 - - [24/Sep/2013:11:51:34 -0700] "GET /~test/index.html HTTP/1.1" 200 3