首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过GCP Python API获取GCP监控时出错

通过GCP Python API获取GCP监控时出错
EN

Stack Overflow用户
提问于 2021-09-07 06:05:01
回答 1查看 69关注 0票数 0

我正在尝试获取GCP SQL实例最近5分钟的CPU利用率,但得到以下错误。请帮我解决。我使用的服务帐户拥有对监控的完全读取权限。

我的脚本:

代码语言:javascript
运行
复制
from google.cloud import monitoring_v3
from google.cloud.monitoring_v3 import query

credential_file = "/home/user/my-first-project.json"

GCP_SCOPES = [
    'https://www.googleapis.com/auth/sqlservice.admin',
    "https://www.googleapis.com/auth/logging.read",
    "https://www.googleapis.com/auth/cloud-platform",
    "https://www.googleapis.com/auth/monitoring",
    "https://www.googleapis.com/auth/monitoring.read"
]

gcp_credential = service_account.Credentials.from_service_account_file(credential_file,scopes=GCP_SCOPES)

client = monitoring_v3.MetricServiceClient(credentials=gcp_credential)

project_name = f"projects/my-first-project"

cpu_query = query.Query(client,
                        project=project_name,
                        metric_type='cloudsql.googleapis.com/database/cpu/utilization',
                        minutes=5)

next(cpu_query.iter())

错误:

代码语言:javascript
运行
复制
  File "$PATH\grpc_helpers.py", line 75, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.PermissionDenied: 403 Permission monitoring.timeSeries.list denied (or the resource may not exist).
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-09 10:11:42

删除前缀"projects/“起作用了。因此,基本上使用项目id而不是project_name是可行的。

代码语言:javascript
运行
复制
cpu_query = query.Query(
    client,
    project="my-first-project",                    
    metric_type='cloudsql.googleapis.com/database/cpu/utilization',
    minutes=5
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69083095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档