简体   繁体   中英

Get CPU, memory and HDD info of a host with the OpenStack Python API

I have a working Python program using the OpenStack API to create instances, list the instances, etc. The authentication is working well ;).

I would like to get the CPU, memory and HDD information of a specific host. According to the python-novaclient documentation, the method get(host) is what I need.

A Python example:

from novaclient.client import Client
cl = Client(VERSION, USERNAME, PASSWORD, PROJECT_ID, AUTH_URL)
hosts_list = cl.hosts.list()
for h in hosts_list:
  print h # this works and there are elements in hosts_list
cl.hosts.get(hosts_list[0]) # this does not work

I get the following error:

Compute host < Host: my-host.example.com > could not be found. (HTTP 404) (Request-ID: req-338f5bdd-b9ec-49cf-8f5c-59eb825de2c7)

EDIT : my-host.example.com is normal, I changed it for privacy reasons.

Am I doing things right? I find the documentation pretty empty. I'm looking for a more detailed documentation but I feel like this is the only one.

Any help will be greatly appreciated.


UPDATE :

The nova host-list command (on the controller) gives me this output (edited for privacy reason):

+-----------------------+-------------+----------+
| host_name             | service     | zone     |
+-----------------------+-------------+----------+
| my-host-A.example.com | consoleauth | internal |
| my-host-A.example.com | scheduler   | internal |
| my-host-A.example.com | conductor   | internal |
| my-host-A.example.com | cert        | internal |
| my-host-B.example.com | compute     | nova     |
| my-host-B.example.com | cert        | internal |
| my-host-C.example.com | compute     | nova     |
| my-host-C.example.com | cert        | internal |
| my-host-B.example.com | network     | internal |
| my-host-C.example.com | network     | internal |
+-----------------------+-------------+----------+

And when I execute nova host-describe my-host-A.example.com , I get:

ERROR: Compute host my-host-A.example.com could not be found. (HTTP 404) (Request-ID: req-5563c44b-b784-420a-bd73-68c546240076)

But when I execute the same command for host-B and host-C, I get:

+---------------------------+------------+-----+-----------+---------+
| HOST                      | PROJECT    | cpu | memory_mb | disk_gb |
+---------------------------+------------+-----+-----------+---------+
| my-host-{B,C}.example.com | (total)    | 4   | 7987      | 206     |
| my-host-{B,C}.example.com | (used_now) | 0   | 512       | 0       |
| my-host-{B,C}.example.com | (used_max) | 0   | 0         | 0       |
+---------------------------+------------+-----+-----------+---------+

I conclude that only the hosts with a compute service should work, which seems normal. So I changed my Python example like this:

for h in hosts_list:
  try:
    hostname = str(h.host_name)
    print "Try to get system info from: " + hostname
    print cl.hosts.get(hostname)
  except Exception as e:
    logger.error(e)

Indeed, I get the same 404 error when I try to get info from host-A. But I also get an error for the remaining hosts:

Try to get system info from: my-host-{B,C}.example.com
2015-03-11 15:24:22 my-host-{B,C}.example.com urllib3.connectionpool[24249] DEBUG Setting read timeout to None
2015-03-11 15:24:22 my-host-{B,C}.example.com urllib3.connectionpool[24249] DEBUG "GET /v2/951c7a1decb44b4e8fcab59e49f2932f/os-hosts/my-host-{B,C}.example.com HTTP/1.1" 200 413
2015-03-11 15:24:22 my-host-{B,C}.example.com mylogger[24249] ERROR host_name

The error host_name is not really understandable.

I managed to solve my problem. I'd like to share my step-by-step solution because it could help others beginners like me.

Check network requests with tcpdump

Using tcpdump I managed to see the request my program was sending and also get the full response.

tcpdump -A -i lo -n "src host my-IP and dst host my-IP and port 8774"

The port 8774 is the one used by the Compute/Nova API service. With the given output I realised that my request and also the response were perfect:

{"host": 
  [
    {"resource": {"project": "(total)", "memory_mb": 32166, "host": "my-host-{B,C}.example.com", "cpu": 8, "disk_gb": 531}},
    {"resource": {"project": "(used_now)", "memory_mb": 512, "host": "my-host-{B,C}.example.com", "cpu": 0, "disk_gb": 0}},
    {"resource": {"project": "(used_max)", "memory_mb": 0, "host": "my-host-{B,C}.example.com", "cpu": 0, "disk_gb": 0}}
  ]
}

So I guessed the problem would be on the library my program is using, probably during the parsing from json to Python variable.

Check what the library is doing

I run my Python program on the controller. It makes sense that the Compute/Nova service is installed and running on this node. Thanks to it, I didn't need to install python-novaclient . My code was using the library from the OpenStack installation.

To debug my code, I decided to put some print calls in the library but I found it would be easier for me to set a virtualenv and install the right package:

virtualenv env
source env/bin/activate
pip install python-novaclient

At this time I put some print calls in the lib installed in the Python environment. When I restarted the program, it executed without any problem.

The globally installed Nova Python library is probably out of date. It tried to find the key " host_name " in the json response but the key is " host ". This is the reason why I had some trouble.

How to use the given variable

As you can see in the tcpdump response, there are a lot of data. The library parse the content with the response_key which is " host ". The given variable, let's call it foo , looks like this:

[<Host: my-host-{B,C}.example.com>,
 <Host: my-host-{B,C}.example.com>,
 <Host: my-host-{B,C}.example.com>]

With this foo[0].project , I get " (total) ", with foo[1].memory_mb , I get 512 , and so on.

I hope this will help others because there are no tutorials or good documentations on the Internet about this subject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM