本文說明診斷資訊來源,您可以利用這些資訊找出 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 工具是由 Google Cloud 支援團隊建立,可協助您自行診斷 Google Cloud 專案中的常見問題,包括 Ops Agent 的問題。gcpdiag 是開放原始碼工具,並非官方 Google Cloud產品,但由 Google 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
- Cloud Resource Manager API:
VM 管理員:在執行個體上啟用 VM 管理員的免付費層級。如果您在建立 VM 時安裝作業套件代理程式,或是代理程式政策管理 VM 執行個體上的作業套件代理程式安裝作業,就已符合 VM 管理員的規定。
執行 gcpdiag
gcpdiag
指令可搭配 Ops Agent 使用,其基本語法如下:
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 - Google Cloud 平台的診斷資訊」。
舉例來說,如要為專案「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
表示 Ops Agent 正在 VM 上執行,並傳送 VM 的記錄檔和指標。這份報表包含下列狀態說明:
Ops Agent installed on the VM, and is successfully sending logs and metrics.
狀態:FAIL
如果狀態為 FAIL
,表示未安裝 Ops Agent,或是未從 VM 傳送記錄和指標。狀態說明會指出系統偵測到哪些問題:
- 如果未安裝 Ops Agent,您會看到下列狀態說明:
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 沒有相關聯的服務帳戶。如要解決這個問題,請 將服務帳戶附加到 VM,然後再嘗試重新安裝 Ops Agent。
- VM 已 安裝舊版代理程式 (監控代理程式或記錄代理程式)。如要解決這個問題,請解除安裝舊版代理程式,然後再嘗試安裝作業套件代理程式。
如果作業套件代理程式未從 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 Manager。如要解決這個問題,請在執行個體上安裝 VM Manager 免付費層級。安裝完成後,請等待 15 分鐘,然後再嘗試再次執行 gcpdiag
指令。
代理程式健康狀態檢查
2.25.1 版為作業代理程式推出了啟動時間健康狀態檢查。當 Ops 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 缺少 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 Metrics Writer 角色 授予服務帳戶。詳情請參閱「 Agent 缺少 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 已開啟。 詳情請參閱「 無法使用必要的連接埠」。 |
PacApiConnErr
|
連線能力 | 這段健康狀態檢查程式碼不可靠。這段程式碼已在作業套件代理程式 2.46.1 版中停用。 | 更新至作業套件代理程式 2.46.1 以上版本。 |
找出開始時間錯誤
自 2.35.0 版起,Cloud Logging API 會將健康檢查資訊寫入 ops-agent-health
記錄 (2.33.0 和 2.34.0 版使用 ops-agent-health-checks
)。同樣的資訊也會寫入 health-checks.log
檔案,如下所示:
- Linux:
/var/log/google-cloud-ops-agent/health-checks.log
- Windows:
C:\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。如果代理程式無法啟動,但在失敗前已能回報錯誤,您可能也會看到系統回報的啟動時間錯誤。
如要在記錄中查看作業套件代理程式中的執行階段錯誤,請執行下列操作:
-
前往 Google Cloud 控制台的「Logs Explorer」頁面:
如果您是使用搜尋列尋找這個頁面,請選取子標題為「Logging」的結果。
- 輸入以下查詢並點選「執行查詢」:
log_id("ops-agent-health")
如要在 Error Reporting 中查看作業套件代理程式中的執行階段錯誤,請執行下列操作:
-
前往 Google Cloud 控制台的「Error Reporting」頁面:
您也可以透過搜尋列找到這個頁面。
- 如要查看 Ops Agent 的錯誤,請篩選
Ops Agent
的錯誤。
確認是否成功收集記錄
作業代理程式 2.46.0 版推出了資訊性 LogPingOpsAgent
健康狀態檢查。這項檢查會每 10 分鐘將資訊性訊息寫入 ops-agent-health
。您可以透過下列任一操作,確認 Ops Agent 是否正在寫入記錄:
- 使用記錄檔探索工具搜尋特定 VM 的記錄檔,找出 ping 訊息。
- 使用 Metrics Explorer 查看特定 VM 的
log_entry_count
指標值。 - 建立快訊政策,在特定 VM 未更新
log_entry_count
指標時通知您。
如果上述任一選項顯示記錄訊息未經過擷取,您可以採取下列做法:
如要查看特定 VM 上作業套件代理程式的狀態,您需要 VM 的執行個體 ID。如要找出執行個體 ID,請按照下列步驟操作:
-
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面:
如果您是使用搜尋列尋找這個頁面,請選取子標題為「Compute Engine」的結果。
- 按一下 VM 執行個體的名稱。
- 在「Details」分頁中,找到「Basic information」部分。 執行個體 ID 會以數字字串顯示。在後續章節中,請使用這個字串做為 INSTANCE_ID 值。
適用於 VM 的代理程式診斷工具
代理程式診斷工具會從 VM 收集以下所有代理程式的關鍵本機偵錯資訊:作業套件代理程式、舊版記錄代理程式和舊版監控代理程式。偵錯資訊包括專案資訊、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 失敗,您可以使用本節所述的診斷指令碼進行偵錯。舉例來說,您可能會看到下列任一情況:
- 如果您使用「Install Ops Agent for Monitoring and Logging」核取方塊,在建立 VM 時安裝作業套件代理程式,作業套件代理程式安裝作業就會失敗。
Cloud Monitoring VM 執行個體資訊主頁或 Compute Engine VM 詳細資料頁面中的「Observability」可觀測性分頁,代理程式狀態維持在「Pending」狀態超過 10 分鐘。如果狀態持續顯示為「Pending」,可能表示下列任一情況:
- 套用政策時發生問題。
- 作業套件代理程式實際安裝作業發生問題。
- VM 和 Cloud Monitoring 之間的連線問題。
如要執行政策診斷指令碼,請執行下列指令:
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
for 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 的「指標管理」頁面提供的資訊可協助您控制可計費指標的支出金額,且不會影響可觀察性。「指標管理」頁面會回報下列資訊:
- 以位元組和樣本為基礎的計費作業量,跨指標網域和個別指標。
- 指標的標籤和基數資料。
- 每個指標的讀取次數。
- 在警告政策和自訂資訊主頁中使用指標。
- 指標寫入錯誤率。
您也可以使用「指標管理」頁面排除不需要的指標,省下擷取這些指標的成本。
如要查看「指標管理」頁面,請按照下列步驟操作:
-
在 Google Cloud 控制台中,前往
「Metrics management」頁面:如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。
- 在工具列中選取時間範圍。根據預設,「指標管理」頁面會顯示過去一天內收集的指標資訊。
如要進一步瞭解「指標管理」頁面,請參閱「查看及管理指標使用情形」一文。