-
Notifications
You must be signed in to change notification settings - Fork 18
/
module_solaris.py
329 lines (305 loc) · 14.5 KB
/
module_solaris.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import paramiko
class GetSolarisData:
def __init__(self, ip, ssh_port, timeout, usr, pwd, use_key_file, key_file,
get_serial_info, get_hardware_info, get_os_details, add_hdd_as_parts,
get_cpu_info, get_memory_info, ignore_domain, upload_ipv6, debug):
self.machine_name = ip
self.port = int(ssh_port)
self.timeout = timeout
self.username = usr
self.password = pwd
self.ssh = paramiko.SSHClient()
self.use_key_file = use_key_file
self.key_file = key_file
self.get_serial_info = get_serial_info
self.get_hardware_info = get_hardware_info
self.get_os_details = get_os_details
self.add_hdd_as_parts = add_hdd_as_parts
self.get_cpu_info = get_cpu_info
self.get_memory_info = get_memory_info
self.ignore_domain = ignore_domain
self.upload_ipv6 = upload_ipv6
self.debug = debug
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.conn = None
self.sysdata = {}
self.alldata = []
self.hdd_parts = []
def main(self):
self.connect()
self.get_sys()
self.get_CPU()
self.get_RAM()
self.alldata.append(self.sysdata)
self.get_IP()
if self.add_hdd_as_parts:
self.alldata.append({'hdd_parts': self.hdd_parts})
self.get_hdd()
return self.alldata
def connect(self):
try:
if not self.use_key_file:
self.ssh.connect(str(self.machine_name), port=self.port, username=self.username, password=self.password,
timeout=self.timeout)
else:
self.ssh.connect(str(self.machine_name), port=self.port, username=self.username,
key_filename=self.key_file, timeout=self.timeout)
except paramiko.AuthenticationException:
print str(self.machine_name) + ': authentication failed'
return None
except Exception as err:
print str(self.machine_name) + ': ' + str(err)
return None
def get_CPU(self):
if self.get_cpu_info:
stdin, stdout, stderr = self.ssh.exec_command("/usr/bin/kstat cpu_info", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
cpupower = None
if not data_err:
cpu_ids = []
core_ids = []
for rec in data_out:
if 'clock_MHz' in rec:
cpupower = rec.split()[1].strip()
if 'chip_id' in rec:
cpu_ids.append(rec.split()[1].strip())
if 'core_id' in rec:
core_ids.append(rec.split()[1].strip())
cpucount = len(set(cpu_ids))
cpucores = len(set(core_ids))
if cpupower:
self.sysdata.update({'cpupower': cpupower})
self.sysdata.update({'cpucount': cpucount})
self.sysdata.update({'cpucore': cpucores})
else:
print data_err
def get_RAM(self):
if self.get_memory_info:
stdin, stdout, stderr = self.ssh.exec_command("/usr/sbin/prtconf", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
for rec in data_out:
if 'Memory ' in rec:
memory = (rec.split(':')[1]).split()[0]
self.sysdata.update({'memory': memory})
else:
print 'Error: ', data_err
def get_name(self):
stdin, stdout, stderr = self.ssh.exec_command("/usr/bin/hostname", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
return data_out[0].strip()
else:
print 'Error: ', data_err
def get_macs(self):
macs = {}
stdin, stdout, stderr = self.ssh.exec_command("/usr/sbin/dladm show-phys -m", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
for rec in data_out[1:]:
nic, slot, address, in_use, client = rec.split()
# dladm returns MACs in wrong format
if address:
raw = address.split(':')
address = ':'.join([x if len(x) == 2 else ('0' + x) for x in raw])
macs.update({nic: address})
return macs
else:
stdin, stdout, stderr = self.ssh.exec_command("/usr/sbin/arp -a", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
for rec in data_out[1:]:
rec= ' '.join(rec.split())
nic = rec.split()[0]
mac = rec.split()[-1]
flags = rec.split()[3]
#check for the L flag in output (meaning this is for a Local IP)
if 'L' in flags:
macs.update({nic: mac})
return macs
else:
print 'Error: ', data_err
def get_IP(self):
nic = None
macs = self.get_macs()
stdin, stdout, stderr = self.ssh.exec_command("/usr/sbin/ifconfig -a", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
name = self.get_name()
for num in range(len(data_out)):
row = data_out[num]
stripped_row = row.strip()
if 'flags=' in row:
nic = stripped_row.split(':')[0]
if nic and stripped_row.startswith('inet'):
ip = stripped_row.split()[1]
if ip not in ('', ' ') and not nic.startswith('lo'):
mac = macs.get(nic)
nicdata = {}
macdata = {}
if not mac:
mac = ''
if '/' in ip: # ipv6
ip = ip.split('/')[0]
nicdata.update({'ipaddress': ip})
nicdata.update({'macaddress': mac})
nicdata.update({'device': name})
nicdata.update({'tag': nic})
self.alldata.append(nicdata)
if mac != '':
macdata.update({'macaddress': mac})
macdata.update({'port_name': nic})
macdata.update({'device': name})
self.alldata.append(macdata)
else:
print 'Error: ', data_err
def get_sys(self):
stdin, stdout, stderr = self.ssh.exec_command("uname -X", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
for rec in data_out:
if 'System ' in rec:
if self.get_os_details:
os = rec.split('=')[1].strip()
self.sysdata.update({'os': os})
if 'KernelID ' in rec:
if self.get_os_details:
version = rec.split('=')[1].strip()
self.sysdata.update({'osverno': version})
if 'Release ' in rec:
if self.get_os_details:
version = rec.split('=')[1].strip()
self.sysdata.update({'osver': version if version else 'D42_NULL'})
if 'Node ' in rec:
name = rec.split('=')[1].strip()
self.sysdata.update({'name': name})
else:
print 'Error: ', data_err
stdin, stdout, stderr = self.ssh.exec_command("uname -p", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
if data_out[0].strip(" \n") == "sparc":
stdin, stdout, stderr = self.ssh.exec_command("/usr/sbin/prtconf", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
for rec in data_out:
if 'System Configuration:' in rec:
manufacturer = rec.split(':')[1].strip()
manufacturer = manufacturer.rsplit(' ', 1)[0].strip()
self.sysdata.update({'manufacturer': manufacturer})
else:
print 'Error: ', data_err
stdin, stdout, stderr = self.ssh.exec_command("/usr/sbin/sneep", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
serial = data_out[0].strip()
self.sysdata.update({'serial_no': serial})
else:
print 'Error: ', data_err
stdin, stdout, stderr = self.ssh.exec_command("/usr/sbin/prtconf -b", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
for rec in data_out:
if 'banner-name:' in rec:
if self.get_hardware_info:
hardware = rec.split(':')[1].strip()
self.sysdata.update({'hardware': hardware})
else:
print 'Error: ', data_err
#SPARC does not have a UUID (at least for global zones)
self.sysdata.update({'uuid': '00000000-0000-0000-0000-000000000000'})
else:
stdin, stdout, stderr = self.ssh.exec_command("/usr/sbin/smbios -t SMB_TYPE_SYSTEM", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
for rec in data_out:
if 'Manufacturer:' in rec:
manufacturer = rec.split(':')[1].strip()
for mftr in ['VMware, Inc.', 'Bochs', 'KVM', 'QEMU', 'Microsoft Corporation', 'Xen', 'innotek', 'innotek GmbH']:
if mftr.lower() == manufacturer.lower():
self.sysdata.update({'manufacturer': 'virtual'})
break
if manufacturer != 'virtual':
self.sysdata.update({'manufacturer': manufacturer})
if 'Product:' in rec:
if self.get_hardware_info:
hardware = rec.split(':')[1].strip()
self.sysdata.update({'hardware': hardware})
if 'Serial ' in rec:
if self.get_serial_info:
serial = rec.split(':')[1].strip()
self.sysdata.update({'serial_no': serial})
if 'UUID' in rec:
uuid = rec.split(':')[1].strip()
self.sysdata.update({'uuid': uuid})
else:
print 'Error: ', data_err
def get_hdd(self):
stdin, stdout, stderr = self.ssh.exec_command("iostat -En", timeout=30)
data_out = stdout.readlines()
data_err = stderr.readlines()
device_name = self.get_name()
if not data_err:
hdds = []
hdd = []
for rec in data_out:
if 'Soft Errors:' in rec:
if len(hdd) != 0:
hdds.append(hdd)
hdd = []
hdd.append(rec.strip())
else:
hdd.append(rec.strip())
else:
hdd.append(rec.strip())
hdds.append(hdd)
if hdds:
for hdd in hdds:
hdd_part = {}
for rec in hdd:
hdd_part.update({'type': 'hdd'})
hdd_part.update({'assignment': 'device'})
hdd_part.update({'device': device_name})
if 'Product:' in rec:
try:
hdd_model = rec.split('Product:')[1].split(':')[0].strip('Revision').strip()
hdd_part.update({'name': hdd_model})
except Exception as e:
if self.debug:
print '\t[!] Exception in get_hdd(). Message was: %s' % str(e)
if 'Serial No:' in rec:
try:
hdd_serial = rec.split('Serial No:')[1].split(':')[0].strip()
hdd_part.update({'serial_no': hdd_serial})
except Exception as e:
if self.debug:
print '\t[!] Exception in get_hdd(). Message was: %s' % str(e)
if 'Size:' in rec:
try:
hdd_size_raw = rec.split('Size:')[1].split(':')[0].split('<')[0].strip()
if hdd_size_raw.endswith('TB'):
hdd_size = float(hdd_size_raw.rstrip('TB').strip()) * 1024
else:
hdd_size = hdd_size_raw.rstrip('GB').strip()
hdd_part.update({'hddsize': hdd_size})
except Exception as e:
if self.debug:
print '\t[!] Exception in get_hdd(). Message was: %s' % str(e)
if hdd_part.get('serial_no') not in ('', None):
self.hdd_parts.append(hdd_part)
self.sysdata.update({'hddcount': len(self.hdd_parts)})
else:
print 'Error: %s' % data_err