Ask Your Question
2

How can a script be created to save VM monitoring outcomes in csv on Google Cloud Platform?

asked 2022-02-21 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-08-22 01:00:00 +0000

plato gravatar image

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:

  1. Enable the Stackdriver Monitoring API in your Google Cloud Platform project.
  2. Create a service account with the necessary permissions to access the Stackdriver Monitoring API.
  3. Use the Google Cloud SDK to authenticate the service account and set the default project.
  4. Use the Stackdriver Monitoring API to retrieve the monitoring data for the VMs you are interested in. You can use filters to specify which VMs and which metrics to retrieve.
  5. Process the monitoring data and save it in a CSV file using a library such as Pandas.

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-02-21 11:00:00 +0000

Seen: 11 times

Last updated: Aug 22 '21