简体   繁体   English

如何从 gcp projet 导入日志?

[英]How import logs from gcp projet?

I read some documentation on inte.net official and non official and i'm currently unable to import the logs from bigquery like "bigquery_resource" (for getting all my insert, update, merge... processing on my gcp project ) from a gcp project where i'm owner with python on my local.我阅读了一些关于 inte.net 官方和非官方的文档,我目前无法从 gcp 中导入像“bigquery_resource”这样的 bigquery 日志(用于获取我的所有插入、更新、合并...处理我的 gcp 项目)我在本地拥有 python 的所有者的项目。

Mandatory prerequisite:强制性先决条件:

  • Only use the scripts to read and catch the logs with a filter without creating CF, data in bucket, manual action from user on the gcp project etc...仅使用脚本通过过滤器读取和捕获日志而不创建 CF、存储桶中的数据、用户对 gcp 项目的手动操作等...
  • Using a service account in the process在此过程中使用服务帐户
  • Import the bigquery logs from the gcp on a local when i execute my script python当我执行我的脚本 python 时,从本地的 gcp 导入 bigquery 日志

Here the code below where i try to get the logs:下面是我尝试获取日志的代码:

import google.protobuf
from google.cloud.bigquery_logging_v1 import AuditData
import google.cloud.logging
from datetime import datetime, timedelta, timezone
import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="C:\\mypath\\credentials.json"

project_id = os.environ["GOOGLE_CLOUD_PROJECT"] = "project1"



yesterday = datetime.now(timezone.utc) - timedelta(days=2)
time_format = "%Y-%m-%dT%H:%M:%S.%f%z"

filter_str = (
    f'logName="projects/{project_id}/logs/cloudaudit.googleapis.com%2Factivity"'
    f' AND resource.type="bigquery_resource"'
    f' AND timestamp>="{yesterday.strftime(time_format)}"'
)


client = google.cloud.logging.Client(project="project1")


for entry in client.list_entries(filter_=filter_str):
    decoded_entry = entry.to_api_repr()
    #print(decoded_entry)
    print(entry) #the same output as print(decoded_entry)



open("C:\\mypath\\logs.txt", "w").close()
with open("C:\\mypath\\logs.txt", "w") as f:
    for entry in client.list_entries(filter_=filter_str):

        f.write(entry)
   

Unfortunately, it doesn't work(and my code is messy), i get a ProtobufEntry with the var entry like below and i don't know how get my data from my gcp project in a proper way.不幸的是,它不起作用(而且我的代码很乱),我得到了一个带有 var 条目的 ProtobufEntry,如下所示,我不知道如何以正确的方式从我的 gcp 项目中获取我的数据。

我的输出

All the help is welcome !欢迎所有帮助! (please don't answer me with a deprecated answer from openaichatgpt ) (请不要用 openaichatgpt 的弃用答案来回答我)

One way to achieve this as follows:实现此目的的一种方法如下:

Create a dedicated logging sink for BigQuery logs:为 BigQuery 日志创建专用的日志接收器:

gcloud logging sinks create my-example-sink bigquery.googleapis.com/projects/my-project-id/datasets/auditlog_dataset \
    --log-filter='protoPayload.metadata."@type"="type.googleapis.com/google.cloud.audit.BigQueryAuditMetadata"'

The above command will create logging sink in a dataset named auditlog_dataset that only includes BigQueryAuditMetadata messages.上述命令将在名为auditlog_dataset的数据集中创建日志接收器,该数据集仅包含BigQueryAuditMetadata消息。 Refer BigQueryAuditMetadata for all the events which are captured as part of GCP AuditData.请参阅BigQueryAuditMetadata ,了解作为 GCP AuditData 的一部分捕获的所有事件。

Create a service account and give access to above created dataset.创建一个服务帐户并授予对上面创建的数据集的访问权限。

For creating service account refer here and for granting access to dataset refer here .如需创建服务帐户, 请参阅此处,如需授予对数据集的访问权限, 请参阅此处

Use this service account to authenticate from your local environment and query the above created dataset using BigQuery Python client to get filtered BigQuery data.使用此服务帐户从您的本地环境进行身份验证,并使用BigQuery Python 客户端查询上面创建的数据集以获取过滤后的 BigQuery 数据。

from google.cloud import bigquery

client = bigquery.Client()

# Select rows from log dataset
QUERY = (
    'SELECT name FROM `MYPROJECTID.MYDATASETID.cloudaudit_googleapis_com_activity`'
    'LIMIT 100')
query_job = client.query(QUERY)  # API request
rows = query_job.result()  # Waits for query to finish

for row in rows:
    print(row.name)

Also, you can query the audit tables from the console directly.此外,您可以直接从控制台查询审计表

Reference BigQuery audit logging .参考BigQuery 审核日志记录

Another option is to use Python Script to query log events.另一种选择是使用Python 脚本来查询日志事件。 And one more option is to use Cloud Pub/Sub to route logs to external (out of gcp) clients.另一种选择是使用Cloud Pub/Sub将日志路由到外部(gcp 之外)客户端。

I mostly prefer to keep the filtered logs in dedicated Log Analytics bucket and query as per needs and create custom log based metrics using Cloud Monitoring.我更喜欢将过滤后的日志保存在专用的Log Analytics存储桶中,并根据需要进行查询,并使用 Cloud Monitoring 创建基于自定义日志的指标 Moving logs out of GCP may incur network egress charges, refer the documentation, if you are querying large volume of data.如果您要查询大量数据,将日志移出 GCP 可能会产生网络出口费用,请参阅文档。

Here how i export my logs without creating bucket, sink, pubsub, cloud function, table in bigquery etc..在这里,我如何在不创建存储桶、接收器、pubsub、云 function、bigquery 中的表等的情况下导出我的日志。

=> Only 1 Service account with rights on my project and 1 script.py on my local and added an option in the python script for scan only bigquery ressource during the last hour. => 只有1 个服务帐户有权访问我的项目和1 个 script.py在我的本地,并在 python 脚本中添加了一个选项,用于在最后一个小时内仅扫描 bigquery 资源。

I add the path of gcloud because i have some problem with path in my envvar in my local with the popen lib, maybe you won't need to do it.我添加了 gcloud 的路径,因为我的本地 envvar 中的路径与 popen lib 有一些问题,也许你不需要这样做。

from subprocess import Popen, PIPE
import json

from google.cloud.bigquery_logging_v1 import AuditData
import google.cloud.logging
from datetime import datetime, timedelta, timezone
import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="C:\\Users\\USERAAAA\\Documents\\Python Scripts\\credentials.json"

gcloud_path = "C:\\Program Files (x86)\\Google\\Cloud SDK\\google-cloud-sdk\\bin\\gcloud.cmd"
process = Popen([gcloud_path, "logging", "read", "resource.type=bigquery_resource AND logName=projects/PROJECTGCP1/logs/cloudaudit.googleapis.com%2Fdata_access", "--freshness=1h"], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
output_str = stdout.decode()

# data string into a a file
with open("C:\\Users\\USERAAAA\\Documents\\Python_Scripts\\testes.txt", "w") as f:
    f.write(output_str)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM