How can I get VM instance running time in openstac

2019-08-09 02:13发布

I need a small billing report for the usage of the VMs inside openstack after it is stopped, so far I already find the way to get flavor information (vCPU, disk, memory) from instance name.

And I want to know the VM's startup time to calculate now.

Are there any good ways to fetch it from openstack python API ?

It will be nice if you can paste the code as well.

3条回答
The star\"
2楼-- · 2019-08-09 02:58

Why not just use metadata :

Custom server metadata can also be supplied at launch time.

At creation you can save a date time, then when it starts up you can calculate a difference.

查看更多
倾城 Initia
3楼-- · 2019-08-09 03:07

I just wanted to retrieve server's uptime. I mean real uptime for the time the server has been UP, not since its creation.

  • I created a new machine, the machine was running and I was getting some update value; this was nicely incremented
  • Then I stopped the machine and issued the request again: The response correctly reports "state": "stopped", but the uptime attr. is still being incremented. ==> Again, in this extension it is not uptime, it is time from creation

Request to the os-simple-tenant-usage extension (after obtaining an auth. token): GET http://rdo:8774/v2/4e1900cf21924a098709c23480e157c0/os-simple-tenant-usage/4e1900cf21924a098709c23480e157c0 (with the correct tenant ID)

Response (notice the machine is stopped and uptime is a non-zero value):

{
    "tenant_usage": {
        "total_memory_mb_usage": 0.000007111111111111112,
        "total_vcpus_usage": 1.388888888888889e-8,
        "start": "2014-02-25T14:20:19.660179",
        "tenant_id": "4e1900cf21924a098709c23480e157c0",
        "stop": "2014-02-25T14:20:19.660184",
        "server_usages": [
            {
                "instance_id": "ca4465a8-38ca-40de-b138-82efcc88c7cf",
                "uptime": 1199,
                "started_at": "2014-02-25T14:00:20.000000",
                "ended_at": null,
                "memory_mb": 512,
                "tenant_id": "4e1900cf21924a098709c23480e157c0",
                "state": "stopped",
                "hours": 1.388888888888889e-8,
                "vcpus": 1,
                "flavor": "m1.tiny",
                "local_gb": 1,
                "name": "m1"
            }
        ],
        "total_hours": 1.388888888888889e-8,
        "total_local_gb_usage": 1.388888888888889e-8
    }
}

So despite its name uptime it is just time since the server creation.

查看更多
对你真心纯属浪费
4楼-- · 2019-08-09 03:12

(I got the answer from china-openstack community, and shared here)

In the novaclient usage module, all the instance (active or terminated) can be fetched by list API, the detail information is fetched via get API, it is not clear what information are exposed via this python document.

Fortunately the openstack api : os-simple-tenant-usage tells the data structure, the uptime is what I want.

 "tenant_usage": {
    "server_usages": [
        {
            ... (skipped)
            "uptime": 3600,
            "vcpus": 1
        }
    ],

openstack dashboard (at least Folsom version) use this API as well.

查看更多
登录 后发表回答