-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathucs_serial.py
More file actions
73 lines (55 loc) · 2.46 KB
/
ucs_serial.py
File metadata and controls
73 lines (55 loc) · 2.46 KB
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
#!/usr/local/bin/python
#coding: utf-8
"""Get the Serial Numbers for Blades, Rack Servers, Fabric Interconnnects,
and IOMs"""
import csv
import argparse
import getpass
from ucsmsdk.ucshandle import UcsHandle
from ucsmsdk.ucsconstants import NamingId
from ucsmsdk.ucsexception import UcsException
CONST_HEADER_ROW = (('EQUIPMENT_TYPE', 'EQUIPMENT_ID', 'SERIAL_NUMBER'))
def get_equipment(handle, equipment_type):
"""function to wrap the handle query"""
equipment_out = []
try:
equipment_out = handle.query_classid(equipment_type)
except UcsException as err:
print err
return equipment_out
def main(args):
"""Main Function, open a csv file and write to it"""
#get the username if not set
if not args.id:
args.id = raw_input("Please Enter UCS Username: ")
#set secure=False to disable cert chain validation
handle = UcsHandle(args.ucs, args.id, getpass.getpass(), secure=True)
handle.login()
#list of tuples specifying equipment alias and their sdk Constants
# Note: Using the NamingId Classs Constants rather than their values
equipment_to_get = [('BLADE', NamingId.COMPUTE_BLADE),
('SERVER', NamingId.COMPUTE_RACK_UNIT),
('CHASSIS', NamingId.EQUIPMENT_CHASSIS),
('IOM', NamingId.EQUIPMENT_IOCARD),
('FABRIC_INTERCONNECT', NamingId.NETWORK_ELEMENT),
('FABRIC_EXPANSION', NamingId.EQUIPMENT_SWITCH_CARD)]
#build a dictionary of aliases and the list of equipment
equipment_output = {}
for equipment in equipment_to_get:
print "Getting Serials for {0}".format(equipment[0])
equipment_output[equipment[0]] = get_equipment(handle, equipment[1])
with open(args.out, 'w') as file_out:
writer = csv.writer(file_out)
writer.writerow(CONST_HEADER_ROW)
for key in equipment_output:
for equipment in equipment_output[key]:
writer.writerow((key, equipment.dn, equipment.serial))
print "Wrote output to {0}".format(args.out)
handle.logout()
if __name__ == '__main__':
PARSER = argparse.ArgumentParser(description="Dump Serial Numbers to CSV")
PARSER.add_argument('-u', '--ucs', help="UCS Manager IP", required=True)
PARSER.add_argument('-i', '--id', help="UCS User ID", required=False)
PARSER.add_argument('-o', '--out', help="Out CSV File Path", required=True)
ARGS = PARSER.parse_args()
main(ARGS)