4

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.

David Guyon
  • 2,759
  • 1
  • 28
  • 40
  • Check auth url, its accessible or not. – Nilesh Mar 11 '15 at 13:08
  • If you talk about `OS_AUTH_URL`, it works well (used for the authentication). Then I don't have access to the URL used for the Nova service but I saw on the logs that it is using the correct URL and the correct port (8774). And I know the URL works because I can get the list of host as shown in the snippet of code. – David Guyon Mar 11 '15 at 13:12
  • Why its showing `my-host.example.com` ? – Nilesh Mar 11 '15 at 13:13
  • I changed to not disclose the machine I'm working on. I don't know if I'm allowed to publicly give this information. I can tell that the given machine is the correct one, and the list given by `cl.hosts.list()` contains all of my machines. – David Guyon Mar 11 '15 at 13:15
  • R u able to run `nova list` on console ? – Nilesh Mar 11 '15 at 13:32
  • I think I answered to your question on the edit I just made. The `nova list` command gives the running instances. In my case I use the `nova host-list` command. – David Guyon Mar 11 '15 at 13:45

1 Answers1

0

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.

David Guyon
  • 2,759
  • 1
  • 28
  • 40