運用 Ops Agent 收集來自 Apache 的記錄檔

瞭解如何使用作業套件代理程式,收集及查看 syslog 記錄檔。這些記錄檔是由安裝在 Compute Engine 虛擬機器 (VM) 執行個體上的 Apache 網路伺服器所收集。您可以使用類似本快速入門導覽課程中的程序,監控其他第三方應用程式

在本快速入門導覽課程中,執行以下操作:

  1. 建立 Compute Engine VM 執行個體,並安裝 Ops Agent
  2. 安裝 Apache 網路伺服器。
  3. 針對 Apache 網路伺服器設定 Ops Agent。
  4. 在 Logs Explorer 中查看記錄檔。
  5. 建立記錄式快訊。
  6. 測試快訊。
  7. 清除所用資源。

如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


事前準備

  1. 貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。

  2. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  3. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  4. Make sure that billing is enabled for your Google Cloud project.

  5. Enable the Compute Engine, Cloud Monitoring, and Cloud Logging APIs.

    Enable the APIs

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Compute Engine, Cloud Monitoring, and Cloud Logging APIs.

    Enable the APIs

  9. 建立 VM 執行個體

    1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

      前往「VM instances」(VM 執行個體) 頁面

      如果您是使用搜尋列尋找這個頁面,請選取子標題為「Compute Engine」的結果

    2. 點選「Create instance」(建立執行個體) 建立 VM。使用導覽選單中的選項,設定執行個體。
    3. 在「Machine configuration」(機器設定) 選項中,執行下列操作:
      1. 在「Name」(名稱) 欄位輸入描述性名稱。
      2. 在「Machine type」(機型) 預設下拉式選單,依序選取「Shared-core」(共用核心) >「e2-small」
    4. 確認「OS and storage」(OS 與儲存空間) 選項顯示「Debian GNU/Linux」。如果沒有,請依序點選「OS and storage」(OS 與儲存空間) 和「Change」(變更)。在「Boot disk」(開機磁碟) 對話方塊中,將「Version」(版本) 設為「Debian GNU/Linux」
    5. 在「Networking」(網路) 選項的「Firewall」(防火牆) 部分,選取「Allow HTTP traffic」(允許 HTTP 流量) 和「Allow HTTPS traffic」(允許 HTTPS 流量)
    6. 確認「Observability」(觀測能力) 選項顯示「Install Ops Agent」(安裝 Ops Agent)。如果沒有,請按一下「Observability」(觀測能力) 選項,然後選取「Install Ops Agent for Monitoring and Logging」(安裝 Ops Agent 來處理監控和記錄工作)
    7. 按一下 [建立]。

    安裝 Apache 網路伺服器

    如要在 Compute Engine VM 執行個體上安裝 Apache 網路伺服器,請執行下列操作:

    1. 在「VM instances」(VM 執行個體) 頁面上找到新的 VM,前往「Connect」(連線) 欄,然後點選「SSH」

      連線時發生問題嗎?請參閱安全殼層疑難排解

    2. 如要更新套件清單,請將下列指令複製到剪貼簿,貼到 SSH 終端機,然後按下 Enter 鍵:

      sudo apt-get update
      
    3. 看到「Reading package lists... Done」(正在讀取套件清單... 完成) 訊息後,請在 SSH 終端機中執行下列指令,安裝 Apache2 網路伺服器:

      sudo apt-get install apache2 php7.0
      

      系統詢問是否繼續安裝時,請輸入 Y。如果安裝指令失敗,請使用 sudo apt-get install apache2 php

    4. 命令提示字元傳回後,請前往「VM instances」(VM 執行個體) 頁面,然後將 VM 的外部 IP 位址複製到下列網址:

      http://EXTERNAL_IP
      
    5. 如要連線至 Apache 網路伺服器,請開啟新的瀏覽器分頁,然後輸入上一步中的網址。

      網路伺服器安裝成功後,瀏覽器分頁就會顯示 Apache2 Debian 預設頁面。

      顯示 Apache2 預設頁面。

    收集 Apache 網路伺服器記錄檔和指標

    在這些步驟中,您會設定 Ops Agent,以便從 Apache 網路伺服器收集記錄和指標:
    1. 前往 VM 執行個體的 SSH 終端機。如果尚未開啟終端機,請執行下列步驟:

      1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

        前往「VM instances」(VM 執行個體) 頁面

        如果您是使用搜尋列尋找這個頁面,請選取子標題為「Compute Engine」的結果

      2. 找到新的 VM,然後點選「SSH」SSH

    2. 複製下列指令,貼到執行個體的終端機,然後按下 Enter 鍵:

      # Configures Ops Agent to collect telemetry from the app. You must restart the agent for the configuration to take effect.
      
      set -e
      
      # Check if the file exists
      if [ ! -f /etc/google-cloud-ops-agent/config.yaml ]; then
        # Create the file if it doesn't exist.
        sudo mkdir -p /etc/google-cloud-ops-agent
        sudo touch /etc/google-cloud-ops-agent/config.yaml
      fi
      
      # Create a back up of the existing file so existing configurations are not lost.
      sudo cp /etc/google-cloud-ops-agent/config.yaml /etc/google-cloud-ops-agent/config.yaml.bak
      
      # Configure the Ops Agent.
      sudo tee /etc/google-cloud-ops-agent/config.yaml > /dev/null << EOF
      metrics:
        receivers:
          apache:
            type: apache
        service:
          pipelines:
            apache:
              receivers:
                - apache
      logging:
        receivers:
          apache_access:
            type: apache_access
          apache_error:
            type: apache_error
        service:
          pipelines:
            apache:
              receivers:
                - apache_access
                - apache_error
      EOF
      

      先前的指令會建立設定,收集和擷取 Apache 網路伺服器中的記錄檔和指標。詳情請參閱「針對 Apache 網路伺服器設定 Ops Agent」一節。

    3. 重新啟動 Ops Agent:
      1. 如要重新啟動代理程式,請在執行個體上執行下列指令:
        sudo systemctl restart google-cloud-ops-agent
        
      2. 如要確定代理程式已重新啟動,請執行下列指令,並驗證「指標代理程式」和「Logging 代理程式」元件是否已啟動:
        sudo systemctl status "google-cloud-ops-agent*"
        

    查看 Apache 網路伺服器記錄檔

    如要在 Google Cloud 控制台中查看記錄,請使用記錄檔探索工具:

    1. 前往 Google Cloud 控制台的「Logs Explorer」頁面:

      前往「Logs Explorer」(記錄檔探索工具)

      如果您是使用搜尋列尋找這個頁面,請選取子標題為「Logging」的結果

      「Query results」(查詢結果) 窗格會顯示最近的記錄檔。

    2. 確認工具列中的「Show query」(顯示查詢) 已啟用。

    3. 如要查看 Apache 網路伺服器記錄檔,請建立並執行查詢:

      1. 在Google Cloud 專案選取器中展開 Google Cloud 專案清單,然後將Google Cloud 專案 ID 複製到剪貼簿。

      2. 在下列運算式中,將複製的 ID 貼到 PROJECT_ID 欄位中,然後將運算式複製到查詢編輯器中:

        resource.type="gce_instance"
        logName=("projects/PROJECT_ID/logs/apache_access" OR "projects/PROJECT_ID/logs/apache_error")
        

        執行先前的查詢時,只會顯示 apache_accessapache_error 記錄項目。

      3. 點選「Run query」(執行查詢)

        「Query results」(查詢結果) 窗格會顯示查詢結果。

    您已設定作業套件代理程式,從 Apache 網路伺服器收集記錄和指標,並查看這些記錄。下一步是建立警告政策,這樣在記錄檔中出現特定模式時,系統就會通知您。

    建立電子郵件通知管道

    建立快訊政策之前,請先設定快訊政策要使用的通知管道。Cloud Monitoring 支援多種不同類型的通知管道,包括電子郵件、Slack、PagerDuty 和 Pub/Sub。詳情請參閱「建立及管理通知管道」。如要透過電子郵件接收通知,請按照下列步驟操作:
    1. 前往 Google Cloud 控制台的 「Alerting」(警告) 頁面

      前往「Alerting」(快訊)

      如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果

    2. 點選工具列中的「Edit Notification Channels」(編輯通知管道)
    3. 在「Notification channels」(通知管道) 頁面中,捲動至「Email」(電子郵件),然後點選「Add new」(新增)
    4. 輸入您的電子郵件地址和顯示名稱 (例如 My email),然後按一下「Save」(儲存)

    建立記錄式警告

    如要在記錄項目中出現特定訊息時收到通知,請建立以記錄為基礎的警告。在本節中,您將建立記錄式快訊,以便在記錄項目中出現 404 Not Found 訊息時收到通知。

    1. 前往 Google Cloud 控制台的「Logs Explorer」頁面:

      前往「Logs Explorer」(記錄檔探索工具)

      如果您是使用搜尋列尋找這個頁面,請選取子標題為「Logging」的結果

    2. 在「Query results」(查詢結果) 工具列中,點選「Create alert」(建立快訊) 。記錄檔快訊政策窗格會隨即開啟。

    3. 在「Alert details」(快訊詳細資料) 的「Alert Policy Name」(快訊政策名稱) 欄位中,輸入 404 Not Found

    4. 在「Choose logs to include in this alert」(選擇要包含在這則快訊中的記錄檔)中,執行下列操作:

      1. 移除記錄檔篩選器文字方塊中的任何內容。
      2. 複製下列查詢並貼到記錄檔篩選器文字方塊中:

        severity>=DEFAULT /help httpRequest.status=404
        

        先前的記錄檔篩選器會搜尋 severity 等級至少為 DEFAULT 的記錄項目,包含 /help 文字且 httpRequest 狀態為 404

    5. 在「Set notification frequency and autoclose duration」(設定通知頻率和自動關閉期限) 區段中,執行下列操作:

      1. 將「Time between notifications」(通知傳送間隔時間) 欄位設為「5 min」(5 分鐘)
      2. 將「Incident autoclose duration」(事件自動關閉期限) 欄位設為「30 min」(30 分鐘)。
    6. 在「Who should be notified?」(應該通知誰?) 中,從「Notification Channels」(通知管道) 選單中選取您的電子郵件,然後點選「Save」(儲存)

    測試警告政策

    如要測試快訊政策,請按照下列步驟操作:

    1. 前往 VM 執行個體的 SSH 終端機。如果尚未開啟終端機,請執行下列步驟:

      1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

        前往「VM instances」(VM 執行個體) 頁面

        如果您是使用搜尋列尋找這個頁面,請選取子標題為「Compute Engine」的結果

      2. 找到新的 VM,然後點選「SSH」
    2. 如要在伺服器上搜尋虛構頁面 localhost/help,請執行下列指令:

      curl localhost/help
      

      終端機顯示「404 Not Found」訊息後,系統會傳送電子郵件通知。這項程序會在幾分鐘內完成。

      您收到的電子郵件通知會類似以下內容:

      這個記錄式警告範例會產生電子郵件通知。

    3. 如要查看新的記錄項目,請執行下列操作:

      1. 前往 Google Cloud 控制台的「Logs Explorer」頁面:

        前往「Logs Explorer」(記錄檔探索工具)

        如果您是使用搜尋列尋找這個頁面,請選取子標題為「Logging」的結果

      2. 點選工具列中的「Jump to now」(跳到現在時間)

    您已設定作業套件代理程式,從 Apache 網路伺服器收集記錄和指標,但您只查看記錄。如要瞭解如何查看 Apache 網路伺服器指標,請參閱「透過作業套件代理程式收集 Apache 網路伺服器指標:產生流量」。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取您在本頁所用資源的費用,請按照下列步驟操作。

如果您建立新專案,但現在已不再需要該專案,請刪除專案

如果您使用的是現有專案,請執行下列操作:

  1. 如果您已建立 VM,請刪除該 VM:

    1. In the Google Cloud console, go to the VM instances page.

      Go to VM instances

    2. Select the checkbox for the instance that you want to delete.
    3. To delete the instance, click More actions, click Delete, and then follow the instructions.
  2. 刪除您建立的警告政策:

    1. 前往 Google Cloud 控制台的 「Alerting」(警告) 頁面

      前往「Alerting」(快訊)

      如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果

    2. 選取您建立的警告政策,然後點選「Delete」(刪除)

後續步驟