查看活動記錄


Compute Engine 活動記錄已淘汰。請改用稽核記錄。詳情請參閱從活動記錄遷移至稽核記錄

Compute Engine 活動記錄與稽核記錄不同。稽核記錄包含舊版活動記錄的所有資訊,且另外包含其他資訊。建議您使用稽核記錄,而非活動記錄。如果您已使用活動記錄,請參閱從活動記錄遷移至稽核記錄一文。

Compute Engine 的活動記錄功能可讓您追蹤影響專案的特定事件,例如 API 呼叫和系統事件。具體來說,活動記錄會提供以下項目的相關資訊:

  • Compute Engine API 呼叫:GCE_API_CALL 事件是會變更資源狀態的 API 呼叫。舉例來說,系統會把建立磁碟、更新執行個體中繼資料、建立執行個體群組、變更機器類型的 API 呼叫記錄在活動記錄中。不會更新資源的 API 呼叫 (例如 getlist 要求) 則不會記錄於活動記錄中。
  • 作業記錄:變更資源狀態的 API 呼叫完成時,Compute Engine 會傳回一個已完成的作業事件,該事件會記錄在您活動記錄中,這時系統就會記錄 GCE_OPERATION_DONE 事件。
  • 系統記錄:Compute Engine 執行系統事件時,系統會在活動記錄中記錄 GCE_SYSTEM_EVENT 事件。比方說,系統會把資訊公開的維護事件記錄為系統事件。

舉例來說,在 API 事件中,活動記錄會提供詳細資料,例如 API 要求的開始和結束時間、要求主體的細節、發出 API 要求的授權使用者,以及要求的端點。您可以下載活動記錄來搜尋特定的 API 要求,也可以檢閱由 Compute Engine 發起的系統事件。

活動記錄不會提供專案的帳單或用量資訊,例如虛擬機器執行個體執行了多久時間或產生多少費用。如要瞭解帳單記錄,請參閱匯出帳單功能的相關說明。如要瞭解用量記錄,請參閱查看用量報表

Cloud Logging 服務提供活動記錄功能。如要進一步瞭解 Logging 的一般資訊,請參閱 Cloud Logging 說明文件。

事前準備

  • 熟悉 Cloud Logging
  • 如果尚未設定驗證,請先完成設定。 「驗證」是指驗證身分的程序,確認您有權存取 Google Cloud 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列其中一個選項,向 Compute Engine 進行驗證:

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

      1. After installing the Google Cloud CLI, initialize it by running the following command:

        gcloud init

        If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      2. Set a default region and zone.
      3. REST

        如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

          After installing the Google Cloud CLI, initialize it by running the following command:

          gcloud init

          If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

        詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。

這項工作需要的權限

如要執行這項工作,您必須具備以下權限

  • 專案的 logging.logServiceIndexes.list 權限
  • 專案的 logging.logServices.list 權限

查看記錄

所有 Compute Engine 專案依預設都會啟用活動記錄功能。

您可以在 Google Cloud console中透過記錄檔總管查看專案的活動記錄,步驟如下:

  1. 前往 Google Cloud 控制台的「Logging」頁面。

    前往「Logging」(記錄) 頁面

  2. 在記錄檔總管中,從第一個下拉式清單中選取並篩選出您的資源類型。
  3. 從 [All logs] (所有記錄) 下拉式清單中,選取 [compute.googleapis.com/activity_log],即可查看 Compute Engine 活動記錄。

路由記錄

如要瞭解如何匯出活動記錄,請參閱 Cloud Logging 說明文件中的「設定及管理接收器」一文。

識別記錄檔

當您將記錄匯出至 Cloud Storage 時,系統會把記錄檔儲存在記錄項目物件說明文件所述的結構中。

系統會使用下列目錄結構來儲存 Compute Engine 記錄檔:

<bucket>/compute.googleapis.com/activity_log/<year>/<month>/<day>

以下是儲存在名為 my-bucket 的 Cloud Storage 值區中的範例記錄檔案名稱:

my-bucket/compute.googleapis.com/activity_log/2014/10/31/20:00:00_20:59:59_S0.json

在 BigQuery 中,活動記錄會儲存在一組資料表中,其中每個記錄類型每一天都有一個資料表,且系統會使用以下格式為資料表命名:

<dataset>.compute_googleapis_com_activity_log_YYYYMMDD

如要瞭解如何在 BigQuery 中查詢活動記錄,請參閱 BigQuery 中的記錄項目

閱讀活動記錄

活動記錄的結構在 LogEntry 類型說明文件中提供相關說明,歡迎參閱。

Compute Engine 活動記錄有:

  • compute.googleapis.com/activity_log:此項目會做為 log 欄位的值
  • metadata:此項目說明時間戳記等一般資訊
  • structPayload:此項目包含記錄項目的特定內容

您可以查看 LogEntry 說明,進一步瞭解每個記錄項目提供的共同欄位,但活動記錄項目的酬載內容則是專屬於 Compute Engine。以下提供相關說明。

酬載內容

記錄項目的內容是以 JSON 物件格式提供,並且會儲存在 structPayload 欄位中。structPayload 欄位包含下列資訊:

欄位 類型 說明
actor 字串 執行作業的使用者或服務帳戶電子郵件地址,這與 user_id 相同。
error 字串 如果在此事件期間發生錯誤,則提供任何錯誤詳細資料。如果沒有任何錯誤,則會省略。錯誤通常會導致要求無法順利完成。您可以使用此欄位為失敗的要求進行偵錯。
event_subtype 字串 將特定子類型描述為 API 方法。

舉例來說,插入新執行個體的要求會顯示為 compute.instances.insert刪除位址資源的要求會顯示為 compute.addresses.delete

如需 API 方法的完整清單,請參閱 API 參考資料

event_timestamp_us 時間戳記 記錄事件的時間戳記,以標準紀元後的時間表示,以微秒為單位。這與 metadata.timestamp 相同。
event_type 字串 說明一般事件類型,

可為下列其中一個值:

  • GCE_API_CALL:指出更新資源的 REST API 呼叫。
  • GCE_OPERATION_DONE:API 要求完成時,無論是否成功,都會記錄此事件類型。
  • GCE_SYSTEM_EVENT:由 Compute Engine 啟動的系統事件。
info 字串 包含額外資訊的選用欄位 (如果適用)。如果沒有可顯示的額外資訊,則會省略此欄位。
operation 字串 發出 API 要求以更新或變更任何資源時,系統將會建立相應的作業物件以追蹤要求,直到完成為止。這個屬性描述此事件的作業物件,並提供作業名稱、作業的區域或地區及作業 ID 等資訊。

視作業修改的資源為何,作業可以是區域作業地區作業全球作業

request JSON 包含原始 API 要求主體。

resource JSON 說明此事件正在修改的特定資源。比方說,一個虛擬機器 (VM) 執行個體會被視為一個資源,而 VM 的資源屬性範例如下:
"resource": {
  "type": "instance",
  "name": "example-instance",
  "id": "0",
  "zone": "us-central1-f"
}

API 參考資料中列出並詳述資源類型,歡迎參閱。

注意:如果一個動作會影響多個資源,則可能有多個記錄項目具有相同的 trace_id

trace_id 字串 系統提供的追蹤項目 ID 可用於將由單一動作觸發的相關記錄進行分組。例如:
trace_id: "operation-1442436581415-51fe3700bd85a-7fd317e3-f1a3555e"
user_agent 字串 說明執行此要求的用戶端。例如,若您使用 Java 適用的 Cloud 用戶端程式庫發出要求,使用者代理程式即為 Google-API-Java-Client
version 字串 目前的記錄格式版本會指出 Compute Engine 記錄結構定義,目前的版本為 1.2 版。

注意:Compute Engine API 版本管理與記錄格式版本管理並無關聯。

warning 字串 如果在此事件期間發生任何警告,則會提供相關警告的詳情。警告與錯誤不同;警告會提供資訊,而且不會影響要求。

記錄項目範例

舉例來說,說明建立 VM 之 API 要求的範例記錄項目看起來與以下內容類似:

{
  "log": "compute.googleapis.com/activity_log",
  "insertId": "2015-09-16|13:49:42.532185-07|10.106.9.208|335899593",
  "metadata": {
    "severity": "INFO",
    "projectId": "835469197146",
    "serviceName": "compute.googleapis.com",
    "zone": "us-central1-f",
    "labels":{
      "compute.googleapis.com/resource_id":"0",
      "compute.googleapis.com/resource_name":"example-instance",
      "compute.googleapis.com/resource_type":"instance",
      "compute.googleapis.com/resource_zone":"us-central1-f"
    },
    "timestamp": "2015-09-16T20:49:42.423637Z"
  },
  "structPayload": {
    "version": "1.2",
    "trace_id": "operation-1442436581415-51fe3700bd85a-7fd317e3-f1a3555e",
    "event_timestamp_us": "1442436582423637",
    "event_type": "GCE_API_CALL",
    "event_subtype": "compute.instances.insert",
    "resource": {
      "type": "instance",
      "name": "example-instance",
      "id": "0",
      "zone": "us-central1-f"
    },
    "actor": {
      "user": "user@example.com"
    },
    "ip_address": "",
    "user_agent": "apitools-client/1.0",
    "request": {
      "url": "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances",
      "body": "{
        \"canIpForward\":false,
        \"description\":\"\",
        \"disks\":[{
          \"autoDelete\":true,
          \"boot\":true,
          \"deviceName\":\"example-instance\",
          \"initializeParams\":{
            \"diskSizeGb\":\"10\",
            \"diskType\":\"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/diskTypes/pd-standard\",
            \"sourceImage\":\"https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20150818\"
          },
          \"mode\":\"READ_WRITE\",
          \"type\":\"PERSISTENT\"
        }],
        \"machineType\":\"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/machineTypes/n1-standard-1\",
        \"metadata\":{
          \"items\":[]
        },
        \"name\":\"example-instance\",
        \"networkInterfaces\":[{
            \"accessConfigs\":[{
              \"name\":\"External NAT\",
              \"type\":\"ONE_TO_ONE_NAT\"

            }],
            \"network\":\"https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default\"
          }],
        \"scheduling\":{
          \"automaticRestart\":true,
          \"onHostMaintenance\":\"MIGRATE\",
          \"preemptible\":false
        },
        \"serviceAccounts\":[{
          \"email\":\"default\",
          \"scopes\":[
            \"https://www.googleapis.com/auth/devstorage.read_only\",
            \"https://www.googleapis.com/auth/logging.write\"
          ]
        }],
        \"tags\":{
          \"items\":[]
        },
        \"zone\":\"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f\"
      }"
    },
    "operation": {
      "type": "operation",
      "name": "operation-1442436581415-51fe3700bd85a-7fd317e3-f1a3555e",
      "id": "291347737657178184",
      "zone": "us-central1-f"
    }
  }
}

已淘汰的活動記錄項目

下列活動記錄項目將於 2020 年 6 月 1 日淘汰,不會有替代項目:

受控資源類型 事件子類型
gce_backend_service BackendServiceConfigProgramming
gce_instance RE_ENCRYPT_SSL_CERTIFICATE_FLOW
gce_instance addFirewallRuleToSecurityPolicy
gce_instance attachCloudLink
gce_instance attachFirewallSecurityPolicy
gce_instance compute.instanceGroupManagers.updateHealth
gce_instance compute.instanceGroups.detachHealthCheck
gce_instance compute.instanceNetworkConfig.updateName
gce_instance compute.regionInstanceGroups.attachHealthCheck
gce_instance compute.regionInstanceGroups.detachHealthCheck
gce_instance createFirewallSecurityPolicy
gce_instance deleteFirewallSecurityPolicy
gce_instance detachFirewallSecurityPolicy
gce_instance patchFirewallRuleInSecurityPolicy
gce_instance removeCloudLink
gce_instance removeFirewallRuleFromSecurityPolicy
gce_instance updateFirewallSecurityPolicy
gce_instance updateVpnTunnel
gce_instance_group compute.instanceGroups.attachHealthCheck
gce_instance_group compute.instanceGroups.attachNetworkInterfaces
gce_instance_group compute.instanceGroups.detachHealthCheck
gce_instance_group compute.regionInstanceGroups.attachHealthCheck
gce_instance_group compute.regionInstanceGroups.detachHealthCheck
gce_instance_template compute.zoneInstanceTemplates.insert
gce_network compute.networks.switchLegacyToCustomMode
gce_project compute.projects.moveProjectNetworking
gce_reserved_address compute.addresses.insertDnsForwarding
gce_reserved_address compute.addresses.insertNatAddress
gce_ssl_certificate RE_ENCRYPT_SSL_CERTIFICATE_FLOW
gce_ssl_certificate SslCertificateAddManagedCertificateChallenge
gce_ssl_certificate SslCertificateProvisionManagedCertificate
gce_ssl_certificate SslCertificateRemoveManagedCertificateChallenge
gce_subnetwork compute.subnetworks.createOrUpdateVirtualSubnetwork
vpn_tunnel updateVpnTunnel