简体   繁体   English

如何检查池中可用的浮动IP数量?

[英]How to check number of floating IPs available in a pool?

I am writing a script to create a VM on Openstack. 我正在编写脚本以在Openstack上创建VM。 I may get error if floating IPs get exhausted in pool. 如果浮动IP在池中耗尽,我可能会收到错误消息。 How can I check if there are floating IPs available in that pool or not? 我如何检查该池中是否有可用的浮动IP? Is there a way where openstack can automatically choose the pool from all available pools? Openstack是否可以从所有可用池中自动选择池?

You can see the documentation of scripting API you are using , but from the command line to list all floating IP addresses that are allocated to the current project, run: 您可以查看正在使用的脚本API的文档,但是可以从命令行列出分配给当前项目的所有浮动IP地址,然后运行:

   $ openstack floating ip list
   +--------------------------------------+---------------------+------------------+------+
   | ID                                   | Floating IP Address |  Fixed IP Address | Port |
    +--------------------------------------+---------------------+------------------+------+
   | 760963b2-779c-4a49-a50d-f073c1ca5b9e | 172.24.4.228        | None             | None |
   | 89532684-13e1-4af3-bd79-f434c9920cc3 | 172.24.4.235        | None             | None |
   | ea3ebc6d-a146-47cd-aaa8-35f06e1e8c3d | 172.24.4.229        | None             | None |
   +--------------------------------------+---------------------+------------------+------+

you can then do some command line editing to extract the ip colmn and have an ip count. 然后,您可以进行一些命令行编辑以提取ip colmn并获得ip计数。

You have a choice of working from the API (using curl, for example) or using the openstack CLI, which is what you were using when you submitted this question. 您可以选择使用API​​(例如使用curl)或使用openstack CLI(提交问题时使用的方法)进行选择。 The CLI is easier for straight scripting. CLI更易于直接编写脚本。 Here is how you query for floating IPs. 这是查询浮动IP的方法。 Caveat: It is becoming very commonplace to use '-f json' output and then the 'jq' command for field querying. 警告:使用“ -f json”输出然后使用“ jq”命令进行字段查询已变得非常普遍。 You can also use '-f csv' or '-f value' and parse using grep or sed. 您也可以使用-f csv或-f值,并使用grep或sed进行解析。 But, although you may not have done it before, I recommend trying json and jq. 但是,尽管您可能以前没有做过,但是我建议尝试json和jq。 It is worth your time for exactly the problem you are solving. 确切地解决您的问题值得您花费时间。

(Be aware the "None" column is DISPLAY ONLY text. The actual stored field value is 'null'.) (请注意,“无”列是“仅显示”文本。实际存储的字段值为“空”。)

Given: 鉴于:

[user@system ~]$ openstack floating ip list
+--------------------------------------+---------------------+------------------+--------------------------------------+
| ID                                   | Floating IP Address | Fixed IP Address | Port                                 |
+--------------------------------------+---------------------+------------------+--------------------------------------+
| 2492aa71-cadf-4011-9c4f-87f856cd2551 | 172.25.250.29       | 192.168.3.10     | 1e0b868b-8b3c-4e8d-8f11-d6ed15d0e750 |
| 74c9233e-1420-4681-aaa7-357843f48962 | 172.25.250.36       | None             | None                                 |
| f235dfae-a01c-4290-864d-89b83f9a8de9 | 172.25.250.37       | None             | None                                 |
+--------------------------------------+---------------------+------------------+--------------------------------------+

Which looks like this in json: 在json中看起来像这样:

[stack@director ~]$ openstack floating ip list -f json
[
  {
    "Fixed IP Address": "192.168.3.10", 
    "ID": "2492aa71-cadf-4011-9c4f-87f856cd2551", 
    "Floating IP Address": "172.25.250.29", 
    "Port": "1e0b868b-8b3c-4e8d-8f11-d6ed15d0e750"
  }, 
  {
    "Fixed IP Address": null, 
    "ID": "74c9233e-1420-4681-aaa7-357843f48962", 
    "Floating IP Address": "172.25.250.36", 
    "Port": null
  }, 
  {
    "Fixed IP Address": null, 
    "ID": "f235dfae-a01c-4290-864d-89b83f9a8de9", 
    "Floating IP Address": "172.25.250.37", 
    "Port": null
  }
]

Using 'jq' to parse this output, allow me to paraphrase in English first. 使用“ jq”解析此输出,请允许我首先用英语释义。 Piping in jq is like piping in the bash shell. jq中的管道就像bash外壳中的管道。 So "take the whole array" | 因此,“采用整个阵列” "select this-field equals this-value" | “选择此字段等于此值” "return this other field". “返回另一个字段”。 Could return multiple fields if wanted. 如果需要,可以返回多个字段。

[user@system ~]$ openstack floating ip list -f json | jq  '.[] | select(.["Fixed IP Address"] == null ) | .["Floating IP Address"] '
"172.25.250.36"
"172.25.250.37"

If you don't want the results in quotes, ask for raw output (-r for short). 如果您不希望结果用引号引起来,请请求原始输出(简称-r)。

[user@system ~]$ openstack floating ip list -f json | jq  --raw-output '.[] | select(.["Fixed IP Address"] == null ) | .["Floating IP Address"]'
172.25.250.36
172.25.250.37

These are your available floating IPs. 这些是您可用的浮动IP。 If you pull them into an array, you can query the array to see how many you have. 如果将它们放入数组,则可以查询该数组以查看有多少。

[user@system ~]$ floats=( $( openstack floating ip list -f json | jq  --raw-output '.[] | select(.["Fixed IP Address"] == null ) | .["Floating IP Address"]' ) )
[user@system ~]$ echo ${#floats[@]}
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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