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

两盒软妹~` 提交于 2019-12-13 07:29:42

问题


I am writing a script to create a VM on Openstack. I may get error if floating IPs get exhausted in pool. How can I check if there are floating IPs available in that pool or not? Is there a way where openstack can automatically choose the pool from all available pools?


回答1:


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. The CLI is easier for straight scripting. Here is how you query for floating IPs. Caveat: It is becoming very commonplace to use '-f json' output and then the 'jq' command for field querying. You can also use '-f csv' or '-f value' and parse using grep or sed. But, although you may not have done it before, I recommend trying json and 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:

[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. Piping in jq is like piping in the bash shell. 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).

[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. 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



回答2:


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:

   $ 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.



来源:https://stackoverflow.com/questions/41609785/how-to-check-number-of-floating-ips-available-in-a-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!