[英]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 ;). 我有一个有效的Python程序,使用OpenStack API来创建实例,列出实例等。身份验证运行良好;)。
I would like to get the CPU, memory and HDD information of a specific host. 我想获得特定主机的CPU,内存和HDD信息。 According to the python-novaclient documentation, the method
get(host)
is what I need. 根据python-novaclient文档,方法
get(host)
是我需要的。
A Python example: 一个Python示例:
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.
无法找到计算主机<Host:my-host.example.com>。 (HTTP 404) (Request-ID: req-338f5bdd-b9ec-49cf-8f5c-59eb825de2c7)
(HTTP 404)(请求ID:req-338f5bdd-b9ec-49cf-8f5c-59eb825de2c7)
EDIT : my-host.example.com is normal, I changed it for privacy reasons. 编辑 : my-host.example.com是正常的,我出于隐私原因更改了它。
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): nova host-list
命令(在控制器上)给我这个输出(为了隐私原因编辑):
+-----------------------+-------------+----------+
| 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: 当我执行
nova host-describe my-host-A.example.com
,我得到:
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-B和host-C执行相同的命令时,我得到:
+---------------------------+------------+-----+-----------+---------+
| 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:
所以我改变了我的Python示例:
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. 实际上,当我尝试从主机-A获取信息时,我得到了相同的404错误。 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. 错误host_name不是真的可以理解。
I managed to solve my problem. 我设法解决了我的问题。 I'd like to share my step-by-step solution because it could help others beginners like me.
我想分享我的逐步解决方案,因为它可以帮助像我这样的其他初学者。
Using tcpdump
I managed to see the request my program was sending and also get the full response. 使用
tcpdump
我设法看到我的程序发送的请求,并获得完整的响应。
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. 端口8774是Compute / Nova API服务使用的端口。 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. 所以我猜测问题将出现在我的程序正在使用的库上,可能是在从json到Python变量的解析过程中。
I run my Python program on the controller. 我在控制器上运行我的Python程序。 It makes sense that the Compute/Nova service is installed and running on this node.
在此节点上安装并运行Compute / Nova服务是有道理的。 Thanks to it, I didn't need to install
python-novaclient
. 多亏了它,我不需要安装
python-novaclient
。 My code was using the library from the OpenStack installation. 我的代码使用的是OpenStack安装中的库。
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: 为了调试我的代码,我决定在库中放置一些
print
调用,但我发现设置virtualenv并安装正确的包更容易:
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. 这时我在Python环境中安装的lib中放了一些
print
调用。 When I restarted the program, it executed without any problem. 当我重新启动程序时,它执行没有任何问题。
The globally installed Nova Python library is probably out of date. 全球安装的Nova Python库可能已经过时了。 It tried to find the key " host_name " in the json response but the key is " host ".
它试图在json响应中找到键“ host_name ”,但键是“ host ”。 This is the reason why I had some trouble.
这就是我遇到麻烦的原因。
As you can see in the tcpdump response, there are a lot of data. 正如您在tcpdump响应中看到的,有很多数据。 The library parse the content with the response_key which is " host ".
该库使用response_key解析内容,该响应是“ host ”。 The given variable, let's call it foo , looks like this:
给定变量,我们称之为foo ,如下所示:
[<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. 有了这个
foo[0].project
,我得到“ (总) ”,用foo[1].memory_mb
,我得到512 ,依此类推。
I hope this will help others because there are no tutorials or good documentations on the Internet about this subject. 我希望这会对其他人有所帮助,因为互联网上没有关于这个主题的教程或文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.