To save VM monitoring outcomes in CSV on Google Cloud Platform, you can create a script that uses the Stackdriver Monitoring API to retrieve the monitoring data and then saves it in a CSV file. Here are the steps to create this script:
Here is some sample code to get you started:
from google.cloud import monitoring_v3
import pandas as pd
# Authenticate with the service account and set the default project
client = monitoring_v3.MetricServiceClient()
client.project_path = 'projects/[PROJECT_ID]'
# Define the filter to retrieve monitoring data for VMs with a certain label
filter_str = 'resource.labels.[LABEL_KEY]=[LABEL_VALUE]'
# Define the time range for the monitoring data
interval = monitoring_v3.TimeInterval()
interval.end_time.seconds = int(time.time())
interval.start_time.seconds = interval.end_time.seconds - 3600 # last hour
# Define the metric to retrieve (CPU utilization)
metric_type = 'compute.googleapis.com/instance/cpu/utilization'
# Retrieve the monitoring data using the filter and time range
results = client.list_time_series(
client.project_path, filter_str, interval, metric_type)
# Process the monitoring data using Pandas
df = pd.DataFrame(columns=['instance_name', 'cpu_utilization'])
for result in results:
instance_name = result.resource.labels['instance_name']
cpu_utilization = result.points[0].value.double_value
df = df.append({'instance_name': instance_name, 'cpu_utilization': cpu_utilization}, ignore_index=True)
# Save the monitoring data to a CSV file
df.to_csv('vm_monitoring.csv', index=False)
Note that this is just an example script to get you started. You will need to adapt it to your specific needs and add error handling and logging as necessary.
Asked: 2022-02-21 11:00:00 +0000
Seen: 11 times
Last updated: Aug 22 '21