forked from AnykeyNL/OCI-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_instances.py
290 lines (240 loc) · 11 KB
/
list_instances.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Oracle OCI - Instance report script
# Version: 1.8 22-November 2018
# Written by: [email protected]
# More info see: www.oc-blog.com
#
# This script will create a CSV report for all compute and DB instances (including ADW and ATP)
# in your OCI account, including predefined tags
#
# Instructions:
# - you need the OCI python API, this can be installed by running: pip install oci
# - you need the OCI CLI, this can be installed by running: pip install oci-cli
# - Make sure you have a user with an API key setup in the OCI Identity
# - Create a config file using the oci cli tool by running: oci setup config
# - In the script specify the config file to be used for running the report
# - You can specify any region in the config file, the script will query all enabled regions
import oci
import json
import shapes
import logging
# Script configuation ###################################################################################
configfile = "c:\\oci\\config" # Define config file to be used.
AllPredefinedTags = True # use only predefined tags from root compartment or include all compartment tags as well
NoValueString = "n/a" # what value should be used when no data is available
FieldSeperator = "," # what value should be used as field seperator
ReportFile = "C:\\oci\\report.csv"
EndLine = "\n"
# #######################################################################################################
#logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
def DisplayInstances(instances, compartmentName, instancetype, regionname):
for instance in instances:
privateips = ""
publicips = ""
instancetypename = ""
tagtxt = ""
OS = ""
LicenseIncluded = ""
#print (instance)
# Handle details for Compute Instances
if instancetype=="Compute":
OCPU, MEM, SSD = shapes.ComputeShape(instance.shape)
response = ComputeClient.list_vnic_attachments(compartment_id = instance.compartment_id, instance_id = instance.id)
vnics = response.data
try:
for vnic in vnics:
responsenic = NetworkClient.get_vnic(vnic_id=vnic.vnic_id)
nicinfo = responsenic.data
privateips = privateips + nicinfo.private_ip + " "
publicips = publicips + nicinfo.public_ip + " "
except:
privateips = NoValueString
publicips = NoValueString
instancetypename = "Compute"
version = NoValueString
namespaces = instance.defined_tags
shape = instance.shape
# Get OS Details
try:
response = ComputeClient.get_image(instance.source_details.image_id)
imagedetails = response.data
OS = imagedetails.display_name
except:
OS = NoValueString
prefix,AD = instance.availability_domain.split(":")
LicenseIncluded = "BYOL"
# Handle details for Database Instances
if instancetype=="DB":
OCPU, MEM, SSD = shapes.ComputeShape(instance.shape)
OCPU = instance.cpu_core_count # Overwrite Shape's CPU count, with DB enabled CPU count
response = databaseClient.list_db_nodes(compartment_id = instance.compartment_id, db_system_id = instance.id)
dbnodes = response.data
try:
for dbnode in dbnodes:
responsenic = NetworkClient.get_vnic(vnic_id=dbnode.vnic_id)
nicinfo = responsenic.data
privateips = privateips + nicinfo.private_ip + " "
publicips = publicips + nicinfo.public_ip + " "
except:
privateips = NoValueString
publicips = NoValueString
if instance.license_model == "LICENSE_INCLUDED":
LicenseIncluded = "YES"
else:
LicenseIncluded = "BYOL"
instancetypename= "DB " + instance.database_edition
version = instance.version
OS = "Oracle Linux 6.8"
shape = instance.shape
prefix,AD = instance.availability_domain.split(":")
# Handle details for Autonomous Database (ATP)
if instancetype == "ATP":
OCPU = instance.cpu_core_count
MEM = NoValueString
SSD = instance.data_storage_size_in_tbs
instancetypename = "ATP"
version = NoValueString
OS = NoValueString
shape = "ATP"
AD = regionname.upper()
privateips = NoValueString
publicips = NoValueString
if instance.license_model == "LICENSE_INCLUDED":
LicenseIncluded = "YES"
else:
LicenseIncluded = "BYOL"
# Handle details for Autonomous Database (ADW)
if instancetype == "ADW":
OCPU = instance.cpu_core_count
MEM = NoValueString
SSD = instance.data_storage_size_in_tbs
instancetypename = "ADW"
version = NoValueString
OS = NoValueString
shape = "ADW"
AD = regionname.upper()
privateips = NoValueString
publicips = NoValueString
if instance.license_model == "LICENSE_INCLUDED":
LicenseIncluded = "YES"
else:
LicenseIncluded = "BYOL"
try:
namespaces = instance.defined_tags
for customertag in customertags:
try:
tagtxt = tagtxt + FieldSeperator + namespaces[customertag[0]][customertag[1]]
except:
tagtxt = tagtxt + FieldSeperator + NoValueString
except:
tagtxt = "" # No Tags
#line = "{},{},{},{},{},{},{},{},{},{},{},{},{},{}{}".format(instance.display_name, instance.lifecycle_state, instancetypename, LicenseIncluded, version, OS, shape, OCPU, MEM, SSD, compartmentName, AD, privateips, publicips, tagtxt)
line = "{}{}".format( instance.display_name, FieldSeperator)
line = "{}{}{}".format(line, instance.lifecycle_state, FieldSeperator)
line = "{}{}{}".format(line, instancetypename, FieldSeperator)
line = "{}{}{}".format(line, LicenseIncluded, FieldSeperator)
line = "{}{}{}".format(line, version, FieldSeperator)
line = "{}{}{}".format(line, OS, FieldSeperator)
line = "{}{}{}".format(line, shape, FieldSeperator)
line = "{}{}{}".format(line, OCPU, FieldSeperator)
line = "{}{}{}".format(line, MEM, FieldSeperator)
line = "{}{}{}".format(line, SSD, FieldSeperator)
line = "{}{}{}".format(line, compartmentName, FieldSeperator)
line = "{}{}{}".format(line, AD, FieldSeperator)
line = "{}{}{}".format(line, privateips, FieldSeperator)
line = "{}{}".format(line, publicips)
line = "{}{}".format(line, tagtxt)
print (line)
report.write(line + EndLine)
#Do only once
report = open(ReportFile,'w')
customertags = []
header = "Name,State,Service,Licensed,Version,OS,Shape,OCPU,MEMORY,SSD TB,Compartment,AD,PrivateIP,PublicIP".replace(",", FieldSeperator)
config = oci.config.from_file(configfile)
identity = oci.identity.IdentityClient(config)
user = identity.get_user(config["user"]).data
RootCompartmentID = user.compartment_id
print ("Logged in as: {} @ {}".format(user.description, config["region"]))
print ("Querying Enabled Regions:")
response = identity.list_region_subscriptions(config["tenancy"])
regions = response.data
for region in regions:
if region.is_home_region:
home = "Home region"
else:
home = ""
print ("- {} ({}) {}".format(region.region_name, region.status, home))
# Get all the predefined tags, so the initial header line can be created.
response = identity.list_tag_namespaces(RootCompartmentID)
tags_namespaces = response.data
for namespace in tags_namespaces:
tagresponse = identity.list_tags(namespace.id)
tags = tagresponse.data
for tag in tags:
customertags.append([namespace.name,tag.name])
header = header + FieldSeperator + "{}.{}".format(namespace.name,tag.name)
if AllPredefinedTags:
response = identity.list_compartments(RootCompartmentID)
compartments = response.data
for compartment in compartments:
response = identity.list_tag_namespaces(compartment.id)
tags_namespaces = response.data
for namespace in tags_namespaces:
tagresponse = identity.list_tags(namespace.id)
tags = tagresponse.data
for tag in tags:
customertags.append([namespace.name,tag.name])
header = header + FieldSeperator + "{}.{}".format(namespace.name,tag.name)
print (header)
report.write(header+EndLine)
#Retrieve all instances for each config file (regions)
for region in regions:
config = oci.config.from_file(configfile)
config["region"] = region.region_name
identity = oci.identity.IdentityClient(config)
user = identity.get_user(config["user"]).data
RootCompartmentID = user.compartment_id
ComputeClient = oci.core.ComputeClient(config)
NetworkClient = oci.core.VirtualNetworkClient(config)
# Check instances for all the underlaying Compartments
response = oci.pagination.list_call_get_all_results(identity.list_compartments,RootCompartmentID,compartment_id_in_subtree=True)
compartments = response.data
# Insert (on top) the root compartment
RootCompartment = oci.identity.models.Compartment()
RootCompartment.id = RootCompartmentID
RootCompartment.name = "root"
RootCompartment.lifecycle_state = "ACTIVE"
compartments.insert(0, RootCompartment)
for compartment in compartments:
compartmentName = compartment.name
#print ("Checking : " + compartment.name)
if compartment.lifecycle_state == "ACTIVE":
print ("process Compartment:" + compartmentName)
compartmentID = compartment.id
try:
response = oci.pagination.list_call_get_all_results(ComputeClient.list_instances,compartment_id=compartmentID)
if len(response.data) > 0:
DisplayInstances(response.data, compartmentName, "Compute", region.region_name)
except:
print ("Error?")
databaseClient = oci.database.DatabaseClient(config)
try:
response = oci.pagination.list_call_get_all_results(databaseClient.list_db_systems,compartment_id=compartmentID)
if len(response.data) > 0:
DisplayInstances(response.data, compartmentName, "DB", region.region_name)
except:
print ("Error?")
try:
response = oci.pagination.list_call_get_all_results(databaseClient.list_autonomous_data_warehouses,compartment_id=compartmentID)
if len(response.data) > 0:
DisplayInstances(response.data, compartmentName, "ADW", region.region_name)
except:
print ("Error?")
try:
response = oci.pagination.list_call_get_all_results(databaseClient.list_autonomous_databases,compartment_id=compartmentID)
if len(response.data) > 0:
DisplayInstances(response.data, compartmentName, "ATP", region.region_name)
except:
print ("Error?")
print (" ")
print ("Done, report written to: {}".format(ReportFile))
report.close()