Reputation: 9
I am looking for some API's in GCP that can fetch the usage of resource in terms of usage hours, storage usage etc
Upvotes: 0
Views: 1724
Reputation: 189
Yes, you can use the Cloud monitoring API to fetch the resource usage data. Take a look into it: https://cloud.google.com/monitoring/api/ref_v3/rpc
You can list all the available resources and their respective metrics and you can make a ListTimeSeriesRequest with metric type as a filter to get the usage data. Here is a code sample in Python to fetch 'cpu_utilization' for VM instances:
from google.cloud import monitoring_v3
client = monitoring_v3.MetricServiceClient()
project_name = f"projects/{project_id}"
now = time.time()
seconds = int(now)
nanos = int((now - seconds) * 10 ** 9)
interval = monitoring_v3.TimeInterval(
{
"end_time": {"seconds": seconds, "nanos": nanos},
"start_time": {"seconds": (seconds - 1200), "nanos": nanos},
}
)
results = client.list_time_series(
request={
"name": project_name,
"filter": 'metric.type = "compute.googleapis.com/instance/cpu/utilization"',
"interval": interval,
"view": monitoring_v3.ListTimeSeriesRequest.TimeSeriesView.FULL,
}
)
for result in results:
print(result)
The monitoring API is available in other programming languages too, checkout the documentation to learn more.
Upvotes: 0
Reputation: 2605
If you are interested in obtaining the information about the use of Compute Engine resources so that correlate actual consumption with billing, you can use on the resource usage reports.
Once you set it up, usage reports will be delivered into a preconfigured Cloud Storage Bucket. There are two types of reports that are generated on a regular basis:
The supported metrics cover resources in question:
The generated reports contain quantity of consumed resources measured in units (count, seconds, hours, etc) specific for a resource type.
You can manage the usage reports feature with Cloud Console, gcloud
and API.
Please see Compute Engine > Doc > Viewing usage reports for more details.
Upvotes: 0