forked from dougsland/ovirt-restapi-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetHosts_alldata.py
162 lines (144 loc) · 6.61 KB
/
getHosts_alldata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/python
#
# Copyright (C) 2011
#
# Author: Douglas Schilling Landgraf <[email protected]>
# Contributor: Amador Pahim <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import urllib2
import base64
import sys
from xml.etree import ElementTree
"""
EXAMPLE XML output
========================
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<hosts>
<host id="beaea688-cd59-11e0-a86f-5254006cd06b" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b">
<name>192.168.1.54</name>
<actions>
<link rel="install" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/install"/>
<link rel="activate" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/activate"/>
<link rel="fence" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/fence"/>
<link rel="deactivate" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/deactivate"/>
<link rel="approve" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/approve"/>
<link rel="iscsilogin" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/iscsilogin"/>
<link rel="iscsidiscover" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/iscsidiscover"/>
<link rel="commitnetconfig" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/commitnetconfig"/>
</actions>
<link rel="storage" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/storage"/>
<link rel="nics" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/nics"/>
<link rel="tags" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/tags"/>
<link rel="permissions" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/permissions"/>
<link rel="statistics" href="/api/hosts/beaea688-cd59-11e0-a86f-5254006cd06b/statistics"/>
<address>192.168.1.54</address>
<status>
<state>non_responsive</state>
</status>
<cluster id="99408929-82cf-4dc7-a532-9d998063fa95" href="/api/clusters/99408929-82cf-4dc7-a532-9d998063fa95"/>
<port>54321</port>
<storage_manager>false</storage_manager>
<power_management>
<enabled>false</enabled>
<options/>
</power_management>
<ksm>
<enabled>true</enabled>
</ksm>
<transparent_hugepages>
<enabled>true</enabled>
</transparent_hugepages>
<iscsi>
<initiator>iqn.1994-05.com.redhat:3ece8aa1d7f1</initiator>
</iscsi>
<cpu>
<topology cores="2"/>
<name>Intel(R) Pentium(R) D CPU 3.00GHz</name>
<speed>3000</speed>
</cpu>
<summary>
<active>0</active>
<migrating>0</migrating>
<total>0</total>
</summary>
</host>
</hosts>
"""
# Example
ADDR = "192.168.123.176"
API_PORT = "8443"
USER = "[email protected]"
PASSWD = "T0pSecreT!"
# Setting URL
URL = "https://" + ADDR + ":" + API_PORT + "/api/hosts"
request = urllib2.Request(URL)
print "Connecting to: %s\n" % (URL)
base64string = base64.encodestring('%s:%s' % (USER, PASSWD)).strip()
request.add_header("Authorization", "Basic %s" % base64string)
try:
xmldata = urllib2.urlopen(request).read()
except urllib2.URLError, e:
print "Error: cannot connect to REST API: %s" % (e)
print "Try to login using the same user/pass by the Admin Portal and check the error!"
sys.exit(2)
#print xmldata
tree = ElementTree.XML(xmldata)
list = tree.findall("host")
#print len(list)
for item in list:
print "id: %s" % (item.attrib["id"])
print "href: %s" % (item.attrib["href"])
print "name: %s" % (item.find("name").text)
for subitem in item.findall("actions/link"):
print "actions: link rel: %s" % (subitem).attrib["rel"]
print "actions: link href: %s" % (subitem).attrib["href"]
for subitem in item.findall("link"):
print "link rel: %s" % (subitem).attrib["rel"]
print "link href: %s" % (subitem).attrib["href"]
print "address: %s" % (item.find("address").text)
print "status: %s" % (item.find("status/state").text)
print "cluster id: %s" % (item.find("cluster").attrib["id"])
print "cluster href: %s" % (item.find("cluster").attrib["href"])
print "port: %s" % (item.find("port").text)
print "storage_manager: %s" % (item.find("storage_manager").text)
print "power_management/enabled: %s" % (item.find("power_management/enabled").text)
"""
EXAMPLE XML output with power management enabled
================================================
<power_management type="bladecenter">
<enabled>true</enabled>
<address>192.168.0.111</address>
<username>root</username>
<options>
<option value="123" name="port"/>
<option value="2" name="b"/>
<option value="1" name="a"/>
<option value="True" name="secure"/>
<option value="2" name="slot"/>
</options>
</power_management>
"""
if item.find("power_management/enabled").text == "true":
print "power_management type: %s" % (item.find("power_management").attrib["type"])
print "power_management/address: %s" % (item.find("power_management/address").text)
print "power_management/username: %s" % (item.find("power_management/username").text)
for subitem in item.findall("power_management/options/option"):
print "power management option: %s value: %s" % ((subitem).attrib["value"],(subitem).attrib["name"])
print "ksm: %s" % (item.find("ksm/enabled").text)
print "transparent_hugepages: %s" % (item.find("transparent_hugepages/enabled").text)
print "iscsi -> initiator: %s" % (item.find("iscsi/initiator").text)
print "cpu -> name: %s" % (item.find("cpu/name").text)
print "cpu -> speed: %s" % (item.find("cpu/speed").text)
print "cpu -> topology -> cores: %s" % (item.find("cpu/topology").attrib["cores"])
print "summary -> active: %s" % (item.find("summary/active").text)
print "summary -> migrating: %s" % (item.find("summary/migrating").text)
print "summary -> total: %s" % (item.find("summary/total").text)
print "\n"