查看作業套件代理程式疑難排解資訊

本文說明診斷資訊來源,可協助您找出 Ops Agent 安裝或執行時的問題。

使用 gcpdiag 驗證代理程式狀態

您可以使用 gcpdiag 產生個別 VM 或專案中整個 VM 叢集的 Ops Agent 狀態報告,確認代理程式已安裝,並主動傳送記錄和指標。gcpdiag 可以產生多種格式的狀態報告,包括 CSV 或 JSON 格式。

以下是 CSV 格式的車隊代理程式狀態報告:

🔎 gce/ERR/2024_004: Verify Ops Agent is installed on GCE VMs and is sending logs and metrics.

gce/ERR/2024_004,projects/my-project/zones/my-location/instances/instance-1,failed,Ops Agent not installed on the VM,https://gcpdiag.dev/rules/gce/ERR/2024_004
gce/ERR/2024_004,projects/my-project/zones/my-location/instances/instance-2,skipped,VM Manager is needed for the ops agent detection. Please enable it at: https://cloud.google.com/compute/docs/manage-os#automatic and run this check again.,https://gcpdiag.dev/rules/gce/ERR/2024_004
gce/ERR/2024_004,projects/my-project/zones/my-location/instances/instance-3,skipped,VM Manager is needed for the ops agent detection. Please enable it at: https://cloud.google.com/compute/docs/manage-os#automatic and run this check again.,https://gcpdiag.dev/rules/gce/ERR/2024_004
gce/ERR/2024_004,projects/my-project/zones/my-location/instances/instance-4,ok,"Ops Agent installed on the VM, and is successfully sending logs and metrics.",https://gcpdiag.dev/rules/gce/ERR/2024_004
gce/ERR/2024_004,projects/my-project/zones/my-location/instances/instance-5,ok,"Ops Agent installed on the VM, and is successfully sending logs and metrics.",https://gcpdiag.dev/rules/gce/ERR/2024_004

支援團隊建立 gcpdiag 工具,協助您自行診斷專案中的常見問題,包括 Ops Agent 的問題。gcpdiag 是開放原始碼工具,並非官方產品,但支援團隊會積極開發及維護這項工具。 Google Cloud Google Cloud Google CloudGoogle Cloud 前往 GitHub 貢獻心力或回報問題。

事前準備

gcpdiag 已整合至 Cloud Shell,因此您不必安裝即可執行。如要執行 gcpdiag,請先符合下列先決條件:

  • 授權:gcpdiag 會在 Cloud Shell 中使用應用程式預設憑證 (ADC) 進行驗證。
    確認用於 gcpdiag 的憑證在 Ops Agent 執行的 Google Cloud 專案中,至少具備下列角色:

    • 檢視者 (roles/viewer)
    • 服務使用情形消費者 (roles/serviceusage.serviceUsageConsumer)
  • API:如要在專案中使用 gcpdiag,必須啟用下列 API:

    • Cloud Resource Manager API:cloudresourcemanager.googleapis.com
    • Identity and Access Management API:iam.googleapis.com
    • Cloud Logging API:logging.googleapis.com
    • 服務用量:serviceusage.googleapis.com

    如要啟用這些 API,請在 Cloud Shell 或其他指令列中執行下列指令:

     gcloud --project=PROJECT_ID services enable \
       cloudresourcemanager.googleapis.com \
       iam.googleapis.com \
       logging.googleapis.com \
       serviceusage.googleapis.com
    
  • VM 管理員:在執行個體的免費層級啟用 VM 管理員。如果您在建立 VM 時安裝作業套件代理程式,或是代理程式政策管理 VM 執行個體上的作業套件代理程式安裝作業,您就已符合 VM 管理員的需求。

執行 gcpdiag

搭配 Ops Agent 使用的 gcpdiag 指令基本語法如下:

gcpdiag lint --project=PROJECT_ID [--name=INSTANCE_NAME] --show-skipped --include=gce/err/2024_004 [--output=[CSV|JSON]]
  • PROJECT_ID 是 Google Cloud 專案的 ID。
  • INSTANCE_NAME 是 Compute Engine VM 的名稱。使用 --name 選項查詢特定 VM;如要查詢整個機群,請省略這個選項。
  • 使用 --output 選項將報表儲存為 CSV 或 JSON 格式的檔案;如果不想輸出檔案,請省略這個選項。

--include 選項的值會指定一或多個 gcpdiag 規則;gce/ERR/2024-004 是 Ops Agent 的規則。如要進一步瞭解 gcpdiag 指令的語法,請參閱「gcpdiag - Diagnostics for Google Cloud Platform」。

舉例來說,如要為「my-project」專案產生全車隊報告,請執行下列指令:

gcpdiag lint --project=my-project --show-skipped --include=gce/ERR/2024_004

輸出結果會與下列內容相似:

🔎 gce/ERR/2024_004: Verify Ops Agent is installed on GCE VMs and is sending logs and metrics.
  - my-project/instance-1                                                 [FAIL] Ops Agent not installed on the VM
  - my-project/instance-2                                                 [FAIL] Ops Agent not installed on the VM
  - my-project/instance-3                                                 [SKIP] Unable to confirm Ops Agent installation
  VM Manager is needed for the ops agent detection. Please enable it at: https://cloud.google.com/compute/docs/manage-os#automatic and run this check again.
  - my-project/instance-4                                            [FAIL] Ops Agent is installed, but it's failing to send both logs and metrics to Google Cloud.
  Is Ops Agent sending logs? (Yes)  Is Ops Agent sending metrics? (No)
  - my-project/instance-5                                                 [FAIL] Ops Agent is installed, but it's failing to send both logs and metrics to Google Cloud.
  Is Ops Agent sending logs? (No)   Is Ops Agent sending metrics? (No)
  - my-project/instance-6                                                 [ OK ] Ops Agent installed on the VM, and is successfully sending logs and metrics.
  - my-project/instance-7                                                 [ OK ] Ops Agent installed on the VM, and is successfully sending logs and metrics.

如要為專案「my-project」中的執行個體「my-instance」產生報表,請執行下列指令:

gcpdiag lint  --project=my-project --name=my-instance --show-skipped --include=gce/err/2024_004

輸出結果會與下列內容相似:

🔎 gce/ERR/2024_004: Verify Ops Agent is installed on GCE VMs and is sending logs and metrics.
  - my-project/my-instance   [ OK ] Ops Agent installed on the VM, and is successfully sending logs and metrics.

如果 gcpdiag 指令失敗,且輸出內容如下所示,請確認您已符合執行工具的所有必要條件

WARNING:googleapiclient.http:Encountered 403 Forbidden with reason "PERMISSION_DENIED"
[ERROR]:can't access project my-project: Cloud Resource Manager API has not been used in project my-project before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudresourcemanager.googleapis.com/overview?project=my-project then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry..
[DEBUG]: An Http Error occurred whiles accessing projects.get

HttpError 403 when requesting https://cloudresourcemanager.googleapis.com/v3/projects/my-project?alt=json returned Cloud Resource Manager API has not been used in project my-project before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudresourcemanager.googleapis.com/overview?project=my-project then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. Details: ...
[ERROR]:exiting program...
error getting project details

解讀 gcpdiag 輸出內容

本節提供相關資訊,協助您瞭解及使用 gcpdiag 產生的報表。

gcpdiag 指令會產生包含下列資訊的報表:

  • 用於叫用指令的 gcpdiag 規則指標。 作業套件代理程式的相關資訊如下所示:
    🔎 gce/ERR/2024_004: Verify Ops Agent is installed on GCE VMs and is sending logs and metrics
  • 特定 VM 的結果 ID,格式為 PROJECT_ID/INSTANCE_ID。如果是全車隊報告,輸出內容會包含每個 VM 的項目。如果是單一 VM 報表,輸出內容會包含單一項目。
  • 支票的狀態。狀態會是下列其中一個值:
  • 狀態值的說明。

以下各節將進一步說明狀態值及其意義。

狀態:OK

OK 狀態表示作業套件代理程式正在 VM 上執行,並傳送 VM 的記錄檔和指標。報表包含下列狀態說明:
Ops Agent installed on the VM, and is successfully sending logs and metrics.

狀態:FAIL

FAIL 狀態表示作業套件代理程式未安裝,或未從 VM 傳送記錄和指標。狀態說明會指出偵測到的問題:

  • 如果未安裝作業套件代理程式,您會看到下列狀態說明:
    Ops Agent not installed on the VM
  • 如果作業套件代理程式未從 VM 傳送記錄和指標,您會看到類似下列的狀態說明:
    Agent is installed, but it's failing to send both logs and metrics to Google Cloud.
    Is Ops Agent sending logs? (Yes) Is Ops Agent sending metrics? (No)

如果尚未安裝作業套件代理程式,請安裝作業套件代理程式。如果安裝失敗,問題可能出在下列其中一個方面:

如果作業套件代理程式未從 VM 傳送記錄或指標,請使用代理程式健康檢查功能找出啟動時間錯誤,並修正問題。

狀態:SKIP

SKIP 狀態表示 gcpdiag 無法判斷 VM 是否已安裝作業套件代理程式。報表包含下列狀態說明:
Unable to confirm Ops Agent installation
  VM Manager is needed for the ops agent detection.
  Please enable it at: https://cloud.google.com/compute/docs/manage-os#automatic and run this check again.

如要測試代理程式的狀態,gcpdiag 必須在 VM 上執行 VM 管理工具。如要解決這個問題,請在執行個體上安裝VM 管理員免費層級。安裝完成後,請等待 15 分鐘,然後再次嘗試執行 gcpdiag 指令。

代理程式健康狀態檢查

2.25.1 版導入了作業代理程式的啟動時間健康狀態檢查。Ops Agent 啟動時,會執行一系列檢查,確認是否有任何狀況會導致 Agent 無法正常運作。如果代理程式偵測到其中一個條件,就會記錄描述問題的訊息。作業套件代理程式會檢查下列項目:

  • 連線問題
  • 代理程式用來回報自身指標的通訊埠是否可用
  • 權限問題
  • 代理程式用來寫入記錄或指標的 API 是否可用
  • 健康狀態檢查常式本身有問題。
如要瞭解如何找出開始時間錯誤,請參閱「找出開始時間錯誤」。

2.37.0 版導入了作業代理程式的執行階段健康狀態檢查。這些錯誤會回報給 Cloud Logging 和 Error Reporting。 如要瞭解如何找出執行階段錯誤,請參閱「找出執行階段錯誤」。

2.46.0 版導入了資訊LogPingOpsAgent代碼。這個代碼不代表發生錯誤。 詳情請參閱「 確認記錄檔收集作業是否成功」。

下表依字母順序列出各個健康狀態檢查代碼,並說明每個代碼的意義。結尾為字串「Err」的代碼表示錯誤,其他代碼則為資訊代碼。

健康檢查代碼 類別 意義 建議
DLApiConnErr 連線能力 對下載子網域 (dl.google.com) 的要求失敗。 檢查網際網路連線和防火牆規則。 詳情請參閱 網路連線問題
FbMetricsPortErr 通訊埠可用性 作業套件代理程式自我指標所需的 20202 連接埠無法使用。 確認通訊埠 20202 已開啟。詳情請參閱「 必要連接埠無法使用」。
HcFailureErr 一般 作業套件代理程式健康狀態檢查常式發生內部錯誤。 透過 Google Cloud 控制台提交支援案件。 詳情請參閱「 取得支援」一文。
LogApiConnErr 連線能力 對 Logging API 的要求失敗。 檢查網際網路連線和防火牆規則。 詳情請參閱 網路連線問題
LogApiDisabledErr API 目前 Google Cloud 專案已停用 Logging API。 啟用 Logging API
LogApiPermissionErr 權限 服務帳戶缺少記錄檔寫入者角色 (roles/logging.logWriter)。 記錄檔寫入者角色指派給服務帳戶。詳情請參閱「 Agent lacks API permissions」(代理程式缺少 API 權限)。
LogApiScopeErr 權限 VM 缺少 https://www.googleapis.com/​auth/​logging.write 存取範圍。 將 https://www.googleapis.com/​auth/​logging.write 範圍新增至 VM。 詳情請參閱「 驗證存取範圍」。
LogApiUnauthenticatedErr API 目前的 VM 無法向 Logging API 驗證。 確認憑證檔案、VM 存取範圍和權限設定正確無誤。詳情請參閱「 授權 Ops Agent」。
LogPingOpsAgent   每 10 分鐘寫入 ops-agent-health 記錄的資訊酬載訊息。您可以根據產生的記錄項目,確認代理程式是否正在傳送記錄。這不是錯誤訊息。 這則訊息預計每 10 分鐘會顯示一次。如果訊息在 20 分鐘以上未顯示,則代理程式可能發生問題。如需疑難排解資訊,請參閱「 排解 Ops Agent 問題」。
LogParseErr 執行階段 Ops Agent 無法剖析一或多個記錄。 檢查您建立的任何記錄處理器設定。 詳情請參閱「記錄剖析錯誤」。
LogPipeLineErr 執行階段 Ops Agent 的記錄管道失敗。 確認代理程式是否可存取緩衝區檔案、檢查磁碟是否已滿,並確認 Ops Agent 設定正確無誤。詳情請參閱「 管道錯誤」。
MetaApiConnErr 連線能力 向 GCE 中繼資料伺服器查詢 VM 存取範圍、OAuth 權杖和資源標籤時發生錯誤。 檢查網際網路連線和防火牆規則。 詳情請參閱 網路連線問題
MonApiConnErr 連線能力 Monitoring API 要求失敗。 檢查網際網路連線和防火牆規則。 詳情請參閱 網路連線問題
MonApiDisabledErr API 目前的 Google Cloud 專案已停用 Monitoring API。 啟用 Monitoring API
MonApiPermissionErr 權限 服務帳戶缺少「監控指標寫入者」角色 (roles/monitoring.metricWriter)。 將「Monitoring 指標寫入者」角色 指派給服務帳戶。詳情請參閱「 Agent lacks API permissions」(代理程式缺少 API 權限)。
MonApiScopeErr 權限 VM 缺少 https://www.googleapis.com/​auth/​monitoring.write 存取範圍。 將 https://www.googleapis.com/​auth/​monitoring.write 範圍新增至 VM。 詳情請參閱「 驗證存取範圍」。
MonApiUnauthenticatedErr API 目前的 VM 無法向 Monitoring API 驗證。 確認憑證檔案、VM 存取範圍和權限設定正確無誤。詳情請參閱「 授權 Ops Agent」。
OtelMetricsPortErr 通訊埠可用性 作業套件代理程式自我指標所需的 20201 連接埠無法使用。 確認通訊埠 20201 已開啟。 詳情請參閱「 A required port is unavailable」。
PacApiConnErr 連線能力 這項健康檢查代碼不可靠。作業套件代理程式 2.46.1 版已停用這項程式碼。 更新至作業代理程式 2.46.1 以上版本。

找出開始時間錯誤

從 2.35.0 版開始,健康檢查資訊會由 Cloud Logging API (2.33.0 版和 2.34.0 版使用 ops-agent-health-checks) 寫入 ops-agent-health 記錄檔。相同資訊也會寫入 health-checks.log 檔案,如下所示:

  • Linux/var/log/google-cloud-ops-agent/health-checks.log
  • WindowsC:\ProgramData\Google\Cloud Operations\Ops Agent\log\health-checks.log

您也可以查詢 Ops Agent 服務的狀態,藉此查看任何健康狀態檢查訊息,方法如下:

  • 在 Linux 中,執行下列指令:
       sudo systemctl status google-cloud-ops-agent"*"
       

    尋找類似「[Ports Check] Result: PASS」的訊息。 其他結果包括「ERROR」和「FAIL」。

  • 在 Windows 中,請使用 Windows 事件檢視器。尋找與 google-cloud-ops-agent 服務相關的「資訊」、「錯誤」或「失敗」訊息。

解決所有問題後,請務必重新啟動代理程式。健康狀態檢查會在代理程式啟動時執行,因此如要重新執行檢查,必須重新啟動代理程式。

找出執行階段錯誤

系統會將執行階段健康狀態檢查結果回報給 Cloud Logging 和 Error Reporting。如果代理程式無法啟動,但能在失敗前回報錯誤,您也可能會看到回報的啟動時間錯誤。

如要在 Logging 中查看作業套件代理程式的執行階段錯誤,請執行下列操作:

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

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

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

  2. 輸入下列查詢並點按「執行查詢」
    log_id("ops-agent-health")

如要在 Error Reporting 中查看作業套件代理程式的執行階段錯誤,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Error Reporting」頁面:

    前往 Error Reporting

    您也可以透過搜尋列找到這個頁面。

  2. 如要查看 Ops Agent 的錯誤,請篩選 Ops Agent 的錯誤。

確認記錄檔收集作業是否成功

作業代理程式 2.46.0 版導入了資訊 LogPingOpsAgent 健康狀態檢查。這項檢查每 10 分鐘會將資訊訊息寫入 ops-agent-health。您可以透過下列任一方式,根據這些訊息的存在與否,確認 Ops Agent 是否正在寫入記錄:

如果上述任一選項顯示系統未擷取記錄訊息,請按照下列步驟操作:

如要檢查特定 VM 的作業套件代理程式狀態,您需要 VM 的執行個體 ID。如要找出執行個體 ID,請按照下列步驟操作:

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

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

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

  • 按一下 VM 執行個體的名稱。
  • 在「詳細資料」分頁中,找到「基本資訊」區段。 執行個體 ID 會顯示為數值字串。在後續章節中,請將這個字串做為 INSTANCE_ID 值。

VM 的代理程式診斷工具

代理程式診斷工具會從 VM 收集所有下列代理程式的重要本機偵錯資訊:作業套件代理程式、舊版 Logging 代理程式和舊版 Monitoring 代理程式。偵錯資訊包括專案資訊、VM 資訊、代理程式設定、代理程式記錄、代理程式服務狀態等,通常需要手動收集。這項工具也會檢查本機 VM 環境,確保符合代理程式正常運作的特定需求,例如網路連線和必要權限。

為 VM 上的代理程式提出客戶案件時,請執行代理程式診斷工具,並將收集到的資訊附加到案件中。提供這項資訊可縮短支援案件的疑難排解時間。將資訊附加至支援案件之前,請先遮蓋密碼等機密資訊。

代理程式診斷工具必須在 VM 內執行,因此您通常需要先透過 SSH 連線至 VM。下列指令會擷取並執行代理程式診斷工具:

Linux

curl -sSO https://dl.google.com/cloudagents/diagnose-agents.sh
sudo bash diagnose-agents.sh

Windows

(New-Object Net.WebClient).DownloadFile("https://dl.google.com/cloudagents/diagnose-agents.ps1", "${env:UserProfile}\diagnose-agents.ps1")
Invoke-Expression "${env:UserProfile}\diagnose-agents.ps1"

按照指令碼執行的輸出內容,找出包含所收集資訊的檔案。一般來說,您可以在 Linux 的 /var/tmp/google-agents 目錄和 Windows 的 $env:LOCALAPPDATA/Temp 目錄中找到這些檔案,除非您在執行指令碼時自訂了輸出目錄。

如需詳細資訊,請檢查 Linux 上的 diagnose-agents.sh 指令碼或 Windows 上的 diagnose-agents.ps1 指令碼。

自動安裝政策的代理程式診斷工具

如果嘗試使用 Ops Agent OS 政策安裝 Ops Agent 失敗,可以使用本節所述的診斷指令碼進行偵錯。舉例來說,您可能會看到下列其中一種情況:

如要執行政策診斷指令碼,請執行下列指令:

curl -sSO https://dl.google.com/cloudagents/diagnose-ui-policies.sh
bash diagnose-ui-policies.sh VM_NAME VM_ZONE

這項指令碼會顯示受影響的 VM 和相關自動安裝政策資訊。

為 VM 上的代理程式提出客戶案件時,請執行代理程式診斷工具,並將收集到的資訊附加至案件。提供這項資訊可縮短支援案件的疑難排解時間。將資訊附加至支援案件之前,請先遮蓋密碼等機密資訊。

代理程式自我記錄

如果代理程式無法將記錄檔擷取至 Cloud Logging,您可能必須在本機 VM 上檢查代理程式的記錄檔,以進行疑難排解。您也可以使用記錄檔輪替來管理代理程式的自我記錄。

Linux

如要檢查寫入 Journald 的自我記錄,請執行下列指令:

journalctl -u google-cloud-ops-agent*

如要檢查記錄模組寫入磁碟的自我記錄,請執行下列指令:

vim -M /var/log/google-cloud-ops-agent/subagents/logging-module.log

Windows

如要檢查寫入 Windows Event Logs 的自我記錄,請執行下列指令:

Get-WinEvent -FilterHashtable @{ Logname='Application'; ProviderName='google-cloud-ops-agent*' } | Format-Table -AutoSize -Wrap

如要檢查記錄模組寫入磁碟的自我記錄,請執行下列指令:

notepad "C:\ProgramData\Google\Cloud Operations\Ops Agent\log\logging-module.log"

如要檢查 Windows Service Control Manager 中 Ops Agent 服務的記錄,請執行下列指令:

Get-WinEvent -FilterHashtable @{ Logname='System'; ProviderName='Service Control Manager' } | Where-Object -Property Message -Match 'Google Cloud Ops Agent' | Format-Table -AutoSize -Wrap

在 Cloud Monitoring 中查看指標用量和診斷資訊

Cloud Monitoring 的「指標管理」頁面提供相關資訊,可協助您控管可計費指標的支出金額,同時不影響可觀測性。「指標管理」頁面會回報下列資訊:

  • 以位元組和樣本為準的計費方式,在指標網域和個別指標的擷取量。
  • 指標的標籤和基數相關資料。
  • 每個指標的讀取次數。
  • 在警告政策和自訂資訊主頁中使用指標。
  • 指標寫入錯誤率。

您也可以使用「指標管理」頁面排除不必要的指標,藉此省下擷取這些指標的費用。

如要查看「指標管理」頁面,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的 「指標管理」頁面:

    前往「指標管理」

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

  2. 在工具列中選取時間範圍。根據預設,「指標管理」頁面會顯示前一天收集到的指標資訊。

如要進一步瞭解「指標管理」頁面,請參閱「查看及管理指標用量」。