Reputation: 154
I want to get data on the stop time of an instance in gcp. i.e. since when an instance is stopped. I want to implement it in a script to use it further and move the server to snapshot. Can anyone help me with how to get this data from gcp
either using gcloud
or a python script calling google apis?
Upvotes: 4
Views: 2439
Reputation: 5684
Using JQ
gcloud compute instances describe <VM-NAME> --project=<PROJECT_ID> --zone=<ZONE> --format=json | jq .lastStopTimestamp
"2022-05-05T04:04:11.410-07:00"
Upvotes: 0
Reputation: 51
To get the list of all compute instances with their last start and stop date and time.
gcloud compute instances list --format='table(name,status,lastStartTimestamp,lastStopTimestamp.list())'
Upvotes: 4
Reputation: 1225
From the command bellow you can get the JSON data about stopped instances, changing the timestamp
value accordingly:
gcloud logging read 'resource.type="gce_instance" AND logName:activity_log AND timestamp>="2019-08-27T00:00:00Z" AND jsonPayload.event_subtype:stop' --format json --project $project_id
Another way to get this kind of audit log information is using logName:cloudaudit.googleapis.com%2Factivity
instead. For example, now considering a time range:
gcloud logging read 'resource.type="gce_instance" AND logName:"cloudaudit.googleapis.com%2Factivity" AND timestamp>="2019-08-01T00:00:00Z" AND timestamp<="2019-08-30T00:00:00Z" AND protoPayload.methodName:stop' --format json --project $project_id
Upvotes: 0