Skip to content

Instantly share code, notes, and snippets.

@gowatana
Created November 6, 2018 14:33
Show Gist options
  • Save gowatana/e6e7323bdab824d484a0555ee0912fac to your computer and use it in GitHub Desktop.
Save gowatana/e6e7323bdab824d484a0555ee0912fac to your computer and use it in GitHub Desktop.
Nutanix CE の Prism から起動中 VM の AHV ホストを取得する。
import sys
import json
import requests
import datetime
args = sys.argv
conf_file = args[1]
with open(conf_file, "r") as file:
conf = file.read()
conf = json.loads(conf)
prism_addr = conf["prism_address"]
prism_user = conf["user_name"]
prism_pass = conf["password"]
insecure_warn = requests.packages.urllib3.exceptions.InsecureRequestWarning
def main():
base_url = 'https://' + prism_addr + ':9440/PrismGateway/services/rest/v2.0/'
requests.packages.urllib3.disable_warnings(insecure_warn)
s = requests.Session()
s.auth = (prism_user, prism_pass)
s.headers.update({'Content-Type': 'application/json; charset=utf-8'})
hosts = {}
data = s.get(base_url + 'hosts', verify=False).json()
for entity in data['entities']:
hv_uuid = entity['uuid']
hosts[hv_uuid] = entity['name']
ts = datetime.datetime.now()
ts_str = ts.isoformat()
data = s.get(base_url + 'vms', verify=False).json()
for entity in data['entities']:
if(entity['power_state'] == 'on'):
vm_name = entity['name']
hv_uuid = entity['host_uuid']
hv_name = hosts[hv_uuid]
to_csv_list = (ts_str, hv_name, vm_name)
print(','.join(to_csv_list))
if __name__ == '__main__':
main()
@gowatana
Copy link
Author

gowatana commented Nov 6, 2018

下記の投稿むけ。

Nutanix API v2 で VM - AHV の CSV を取得してみる。(Python)
http://blog.ntnx.jp/entry/2018/11/06/234722

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment