1

How can I know how long has an instance been running (machine) on Google Cloud Platform?

If I run

gcloud compute instances describe <instance-name> --zone <zone>

I can know if it's running, but i want to know how long it has been running.

TasosZG
  • 1,274
  • 1
  • 6
  • 13
Vito Lipari
  • 795
  • 8
  • 35

5 Answers5

1

If you SSH into the instance you can run the command uptime -p and it will show you how long the instance has been running.

TasosZG
  • 1,274
  • 1
  • 6
  • 13
1

You can execute this command:

 gcloud compute --project <project_id> ssh --zone <zone> <instance-name> -- command 'uptime'
hkanjih
  • 1,271
  • 1
  • 11
  • 29
1

gcloud compute instances describe lists lastStartTimestamp, but another way to get it is:

gcloud compute instances list --format="table(name,lastStartTimestamp)"
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
0

Similar to uptime you can use "createdTimstamp" as something to measure against. Check out the compute engine's REST API: cloud.google.com/compute/docs/reference/rest/v1/instances/get. In python:

import googleapiclient.discovery
import google.auth
credentials, project_id = google.auth.default()
compute = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
zone = "us-central1-a"

result = compute.instances().list(project=project_id, zone=zone).execute()
for instance in result['items']:
     print(instance['name'], instance['creationTimestamp'])

Example output:

my-awesome-instance 2020-10-27T03:35:45.814-07:00

The timestamp is in ISO 8601 format and can be easily parsed: How to convert a timezone aware string to datetime in Python without dateutil?

corticalhazard
  • 189
  • 2
  • 6
0

Probably the best way is a combination of a few gcloud commands.

FIRST

gcloud compute intances describe <instance-name> --format="get(status)"

will return the current status: GCloud Status Info

PROVISIONING: resources are allocated for the VM. The VM is not running yet.

STAGING: resources are acquired, and the VM is preparing for first boot.

RUNNING: the VM is booting up or running.

STOPPING: the VM is being stopped. You requested a stop, or a failure occurred. This is a temporary status after which the VM enters the TERMINATED status.

REPAIRING: the VM is being repaired. Repairing occurs when the VM encounters an internal error or the underlying machine is unavailable due to maintenance. During this time, the VM is unusable. If repair succeeds, the VM returns to one of the above states.

TERMINATED: the VM is stopped. You stopped the VM, or the VM encountered a failure. You can restart or delete the VM.

SUSPENDING: The VM is in the process of being suspended. You suspended the VM.

SUSPENDED: The VM is in a suspended state. You can resume the VM or delete it.

SECOND

IF status == 'RUNNING' THEN...

gcloud compute instances describe total-server --format="get(lastStartTimestamp)"

Which will return a UTC Timestamp in ISO 8601 Format as discussed above.

Example: 2022-05-18T02:18:26.086-07:00

From here you just need to get UTC NOW in whatever programming language you're using and subtract lastStartTimestamp to get your 'Running Time'

OPTIONAL THIRD

If status == 'TERMINATED' Then you'll do the same as above but you'll use

gcloud compute instances describe total-server --format="get(lastStopTimestamp)"
  

From there you'll again do the same as above and do a UTC NOW - lastStopTimestamp to get the 'Time since last start' or 'Stopped Time' or whatever you want to call it.

  • I always find tagging the end of my gcloud calls that are retrieving information with --format=json helpful to see all the metadata in a readable format that that call retrieves and then I can use --format="get()" to get specific content out. – Aeryn Connerley May 26 '22 at 17:45