要获取有关实际资源使用情况的值,您可以使用云监控API https://cloud.google.com/monitoring/docs/apis和消费者配额 https://cloud.google.com/monitoring/api/resources#tag_consumer_quota资源。它不会是百分比,而是实际值。
例如:
您想知道有多少个虚拟机us-central1
有一个metric https://cloud.google.com/monitoring/api/resources#tag_consumer_quota为了那个原因:serviceruntime.googleapis.com/quota/allocation/usage
。它提供了实际使用情况。
然后你可以创建 MQL https://cloud.google.com/monitoring/mql/query-editor查询以过滤您需要的所有内容:
fetch consumer_quota
| metric 'serviceruntime.googleapis.com/quota/allocation/usage'
| filter
(resource.service == 'compute.googleapis.com')
&& (metric.quota_metric == 'compute.googleapis.com/instances')
| group_by 1d, [value_usage_aggregate: aggregate(value.usage)]
| every 1d
现在——当你使用curl通过API运行它 https://cloud.google.com/monitoring/mql/qn-from-api#ql-timeseries-query:
curl -d @query.json -H "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" -X POST \
https://monitoring.googleapis.com/v3/projects/${PROJECT_ID}/timeSeries:query
您将得到回复:
{
"timeSeriesDescriptor": {
"labelDescriptors": [
{
"key": "resource.project_id"
},
{
"key": "resource.service"
},
{
"key": "resource.location"
},
{
"key": "metric.quota_metric"
}
],
"pointDescriptors": [
{
"key": "value_usage_aggregate",
"valueType": "INT64",
"metricKind": "GAUGE",
"unit": "1"
}
]
},
"timeSeriesData": [
{
"labelValues": [
{
"stringValue": "my-test-project"
},
{
"stringValue": "compute.googleapis.com"
},
{
"stringValue": "us-central1"
},
{
"stringValue": "compute.googleapis.com/instances"
}
],
"pointData": [
{
"values": [
{
"int64Value": "11"
}
],
"timeInterval": {
"startTime": "2022-03-30T08:38:44.064174Z",
"endTime": "2022-03-30T08:38:44.064174Z"
}
}
]
}
]
}
你感兴趣的线 "int64Value": "11"
其中包含数字 11,在我的例子中,它是当前虚拟机的数量(它是运行和停止的总和)。
现在您需要做的“所有”就是将其合并到您的 python 脚本中。