3

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?

bhito
  • 2,083
  • 7
  • 13
shrey tyagi
  • 164
  • 1
  • 2
  • 14

2 Answers2

2

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())'

0

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
manasouza
  • 1,189
  • 1
  • 14
  • 26
  • This one also worked out for me but using `jsonPayload.event_subtype="compute.instances.stop"` instead of `jsonPayload.event_subtype="stop"` – bhito Sep 02 '19 at 09:03
  • @bhito I mentioned `jsonPayload.event_subtype:stop` which executes the "contains" approach instead of equals. As you mentioned, `="compute.instances.stop"` should work also. – manasouza Sep 03 '19 at 00:13
  • I am not getting data for instances which are stopped 3-4 months ago, even though I put timestamp for 2018. Is there any time restriction? – shrey tyagi Sep 03 '19 at 07:25
  • @shreytyagi just updated my answer. Please check if this attends you – manasouza Sep 03 '19 at 12:12