1

I need to know the uptime of GCP VM instances (windows and Linux both) and based on the time I need to stop the VM. Somehow, I am not getting any simpler way to get the uptime of my all GCP VMs which are like 100 in numbers and will be increasing.

I went through below answer but even there it is not answered, I could not add comment so had to ask new question. Get vm uptime data from stackdriver-agent in gcp?

In the python code snippet at below link, there is no module available for instance uptime all we have is creating uptime check for service availability. https://github.com/GoogleCloudPlatform/python-docs-samples

How can I get uptime of all GCP VM instances ?

Slava Medvediev
  • 1,431
  • 19
  • 35
saurabh umathe
  • 315
  • 2
  • 17
  • 1
    Does "createdTimstamp" from compute engine's REST API work for you? https://cloud.google.com/compute/docs/reference/rest/v1/instances/get – Slava Medvediev Oct 05 '20 at 05:53
  • Not actually, it is a createdTime time stamp of VM which would be constant. I need to know the time since when the instance was last restarted.thanks. – saurabh umathe Oct 05 '20 at 06:29

2 Answers2

3

Assuming that you can adjust process of starting VMs, I think solution below is viable:

  1. When VM is started, add a custom tag with current timestamp (API reference)
  2. Use this tag's value to determine actual instance's uptime

I realize that it sounds overcomplicated, but I don't see any better OS-independent solution.

Update:
The feature you need is already requested in Google's issue tracker. You can check the progress and\or "start" it here: https://issuetracker.google.com/issues/136105125
Note: the issue referenced above is marked as blocked by another one, non-public

Slava Medvediev
  • 1,431
  • 19
  • 35
  • Thanks, I will wait for any simpler solution or can try this out, the issue I see here is, it needs a lot of efforts just to get uptime of VM. And it would also add a unnecessary n/w tag on each VM.. – saurabh umathe Oct 05 '20 at 07:18
  • 1
    I totally agree with you @ssumathe. Just shared the way that might do what you need. In the meanwhile, you can try to check status of this feature request: https://issuetracker.google.com/issues/136105125 – Slava Medvediev Oct 05 '20 at 07:21
  • Thanks for this issue tracker reference @Medvediev. – saurabh umathe Oct 05 '20 at 07:41
  • @ssumathe you're welcome. You can "star" the issue in top-left corner of tracker's web UI. this might affect issue's priority. – Slava Medvediev Oct 07 '20 at 07:05
1
  1. Go to GCP console
  2. Select monitoring
  3. Click Uptime checks.
  4. Click Create Uptime check.

for more info check the below document

https://cloud.google.com/monitoring/uptime-checks

Subhash
  • 762
  • 9
  • 25
  • Does it allow to see uptime in hours, days etc? – Slava Medvediev Oct 05 '20 at 05:54
  • 1
    @subhash, that would be a viable solution for few instances. But the OP has asked "_get the uptime of my all GCP VMs which are like 100 in numbers and will be increasing._". and this solution wouldn't scale – Tibebes. M Oct 05 '20 at 05:56
  • 1
    Also, based on google's docs (https://cloud.google.com/monitoring/alerts#limits) there's limit of 100 uptime checks per workspace, which might be not enough – Slava Medvediev Oct 05 '20 at 05:56
  • @MedvedievV. currently, you can Choose 1, 5, 10, or 15 minutes. For example, if you select 5 minutes, each geographic location attempts to reach your service once in every 5 minute period. Using the default 6 locations, and checking every 5 minutes, your service would see an average of 1.2 requests per minute. Checking every 1 minute, your service would see an average of 6 requests per minute. – Subhash Oct 05 '20 at 06:00
  • @MedvedievV. yes you can create a group of instance and choose that group in configuration – Subhash Oct 05 '20 at 06:01
  • @Subhash let me rephrase. Does it allow to see how many days every VM of, let's say, 200 instances, is up? – Slava Medvediev Oct 05 '20 at 06:10
  • I do not think that is a feasible solution here. I want uptime check of hundreds of GCP VM instances and i can not create uptime for all of those. I am more interested in any gcloud command which can give me uptime of that specific VM. FYI, In instance metadata there is no information about uptime. – saurabh umathe Oct 05 '20 at 06:22