55import paramiko
66import subprocess
77import os
8+ import sys
89
910from hwinfo .pci import PCIDevice
1011from hwinfo .pci .lspci import *
@@ -76,18 +77,40 @@ def get_cpu_info(self):
7677 parser = cpuinfo .CPUInfoParser (data )
7778 return parser .parse_items ()
7879
80+ class FileNotFound (Exception ):
81+ pass
82+
7983def search_for_file (dirname , filename ):
8084 for root , _ , files in os .walk (dirname ):
8185 if filename in files :
8286 return os .path .join (root , filename )
83- raise Exception ("Could not find '%s' in directory '%s'" % (filename , dirname ))
87+ raise FileNotFound ("Could not find '%s' in directory '%s'" % (filename , dirname ))
8488
8589def read_from_file (filename ):
8690 fh = open (filename , 'r' )
8791 data = fh .read ()
8892 fh .close ()
8993 return data
9094
95+ def parse_data (parser , data ):
96+ p = parser (data )
97+ return p .parse_items ()
98+
99+ def combine_recs (rec_list , key ):
100+ """Use a common key to combine a list of recs"""
101+ final_recs = {}
102+ for rec in rec_list :
103+ rec_key = rec [key ]
104+ if rec_key in final_recs :
105+ for k , v in rec .iteritems ():
106+ if k in final_recs [rec_key ] and final_recs [rec_key ][k ] != v :
107+ raise Exception ("Mis-match for key '%s'" % k )
108+ final_recs [rec_key ][k ] = v
109+ else :
110+ final_recs [rec_key ] = rec
111+ return final_recs .values ()
112+
113+
91114class HostFromLogs (Host ):
92115
93116 def __init__ (self , dirname ):
@@ -106,6 +129,20 @@ def get_dmidecode_data(self):
106129 def get_cpuinfo_data (self ):
107130 return self ._load_from_file ('cpuinfo' )
108131
132+ def get_pci_devices (self ):
133+ try :
134+ devs = super (HostFromLogs , self ).get_pci_devices ()
135+ return devs
136+ except FileNotFound :
137+ # Fall back to looking for the file lspci-vv.out
138+ print "***lspci-nnm.out found. Falling back to looking for lspci-vv.out and lspci-n.out.***"
139+ lspci_vv_recs = parse_data (LspciVVParser , self ._load_from_file ('lspci-vv.out' ))
140+ lspci_n_recs = parse_data (LspciNParser , self ._load_from_file ('lspci-n.out' ))
141+ all_recs = lspci_vv_recs + lspci_n_recs
142+ recs = combine_recs (all_recs , 'pci_device_bus_id' )
143+ return [PCIDevice (rec ) for rec in recs ]
144+
145+
109146def pci_filter (devices , types ):
110147 res = []
111148 for device in devices :
@@ -167,6 +204,12 @@ def tabulate_cpu_recs(recs):
167204 ]
168205 return tabulate_recs (recs , header )
169206
207+ def print_unit (title , content ):
208+ print "%s" % title
209+ print ""
210+ print content
211+ print ""
212+
170213def main ():
171214 """Entry Point"""
172215
@@ -184,6 +227,10 @@ def main():
184227 if args .logs :
185228 host = HostFromLogs (args .logs )
186229 else :
230+ if args .machine and not args .username or not args .password :
231+ print "Error: you must specify a username and password to query a remote machine."
232+ sys .exit (1 )
233+
187234 host = Host (args .machine , args .username , args .password )
188235
189236 options = []
@@ -196,35 +243,20 @@ def main():
196243 options = filter_choices
197244
198245 if 'bios' in options :
199- print "Bios Info:"
200- print ""
201- print rec_to_table (host .get_info ())
202- print ""
246+ print_unit ("Bios Info:" , rec_to_table (host .get_info ()))
203247
204248 if 'cpu' in options :
205- print "CPU Info:"
206- print ""
207- print tabulate_cpu_recs (host .get_cpu_info ())
208- print ""
249+ print_unit ("CPU Info:" , tabulate_cpu_recs (host .get_cpu_info ()))
209250
210251 if 'nic' in options :
211252 devices = pci_filter_for_nics (host .get_pci_devices ())
212- print "Ethernet Controller Info:"
213- print ""
214- print tabulate_pci_recs ([dev .get_rec () for dev in devices ])
215- print ""
253+ print_unit ("Ethernet Controller Info:" , tabulate_pci_recs ([dev .get_rec () for dev in devices ]))
216254
217255 if 'storage' in options :
218256 devices = pci_filter_for_storage (host .get_pci_devices ())
219- print "Storage Controller Info:"
220- print ""
221- print tabulate_pci_recs ([dev .get_rec () for dev in devices ])
222- print ""
257+ print_unit ("Storage Controller Info:" , tabulate_pci_recs ([dev .get_rec () for dev in devices ]))
223258
224259 if 'gpu' in options :
225260 devices = pci_filter_for_gpu (host .get_pci_devices ())
226261 if devices :
227- print "GPU Info:"
228- print ""
229- print tabulate_pci_recs ([dev .get_rec () for dev in devices ])
230- print ""
262+ print_unit ("GPU Info:" , tabulate_pci_recs ([dev .get_rec () for dev in devices ]))
0 commit comments