11#!/usr/bin/env python
22
33from argparse import ArgumentParser
4+ from prettytable import PrettyTable
45import paramiko
56
67from hwinfo .pci import PCIDevice
78from hwinfo .pci .lspci import *
9+ from hwinfo .host .dmidecode import *
810
911def remote_command (host , username , password , cmd ):
1012 client = paramiko .SSHClient ()
1113 client .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
1214 client .connect (host , username = username , password = password , timeout = 10 )
1315 cmdstr = ' ' .join (cmd )
14- print "Executing '%s' on host '%s'" % (cmdstr , host )
16+ # print "Executing '%s' on host '%s'" % (cmdstr, host)
1517 _ , stdout , stderr = client .exec_command (cmdstr )
1618 output = stdout .readlines ()
1719 error = stderr .readlines ()
@@ -25,43 +27,136 @@ def local_command(cmd):
2527
2628class Host (object ):
2729
28- def __init__ (self , host , username , password ):
30+ def __init__ (self , host = 'localhost' , username = None , password = None ):
2931 self .host = host
3032 self .username = username
3133 self .password = password
3234
3335 def exec_command (self , cmd ):
34- assert not self .host
35- return local_command (cmd )
36+ if self .host == 'localhost' :
37+ return local_command (cmd )
38+ else :
39+ return remote_command (self .host , self .username , self .password , cmd )
3640
3741 def get_pci_devices (self ):
3842 data = self .exec_command (['lspci' , '-nnmm' ])
3943 parser = LspciNNMMParser (data )
4044 devices = parser .parse_items ()
4145 return [PCIDevice (device ) for device in devices ]
4246
43- class RemoteHost (Host ):
44-
45- def exec_command (self , cmd ):
46- return remote_command (self .host , self .username , self .password , cmd )
47-
47+ def get_info (self ):
48+ data = self .exec_command (['dmidecode' ])
49+ parser = DmidecodeParser (data )
50+ rec = parser .parse ()
51+ return rec
52+
53+ def pci_filter (devices , types ):
54+ res = []
55+ for device in devices :
56+ for t in types :
57+ if device .get_pci_class ().startswith (t ):
58+ res .append (device )
59+ break
60+ return res
61+
62+ def pci_filter_for_nics (devices ):
63+ nic_types = ['02' ]
64+ return pci_filter (devices , nic_types )
65+
66+ def pci_filter_for_storage (devices ):
67+ storage_types = ['00' , '01' ]
68+ return pci_filter (devices , storage_types )
69+
70+ def pci_filter_for_gpu (devices ):
71+ gpu_types = ['03' ]
72+ return pci_filter (devices , gpu_types )
73+
74+ def print_lines (lines ):
75+ max_len = 0
76+ output = []
77+ for line in lines :
78+ output .append (line )
79+ if len (line ) > max_len :
80+ max_len = len (line )
81+ print ""
82+ print "-" * max_len
83+ print '\n ' .join (output )
84+ print "-" * max_len
85+ print ""
86+
87+ def rec_to_table (rec ):
88+ table = PrettyTable (["Key" , "Value" ])
89+ table .align ['Key' ] = 'l'
90+ table .align ['Value' ] = 'l'
91+ for k , v in rec .iteritems ():
92+ table .add_row ([k , v ])
93+ return table
94+
95+ def tabulate_pci_recs (recs ):
96+ header = [
97+ 'vendor_name' ,
98+ 'vendor_id' ,
99+ 'device_name' ,
100+ 'device_id' ,
101+ 'subvendor_name' ,
102+ 'subvendor_id' ,
103+ 'subdevice_name' ,
104+ 'subdevice_id' ,
105+ ]
106+ table = PrettyTable (header )
107+ for rec in recs :
108+ vls = [rec [k ] for k in header ]
109+ table .add_row (vls )
110+ return table
48111
49112def main ():
50113 """Entry Point"""
51114
52115 parser = ArgumentParser (prog = "hwinfo" )
53- parser .add_argument ("cmd" )
54- parser .add_argument ("host" )
55- parser .add_argument ("username" )
56- parser .add_argument ("password" )
57116
58- args = parser .parse_args ()
59-
60- host = RemoteHost (args .host , args .username , args .password )
61-
62- if args .cmd == 'list' :
63- devices = host .get_pci_devices ()
64- for device in devices :
65- print device .get_info ()
117+ filter_choices = ['bios' , 'nic' , 'storage' , 'gpu' ]
118+ parser .add_argument ("-f" , "--filter" , choices = filter_choices )
119+ parser .add_argument ("-m" , "--machine" , default = 'localhost' )
120+ parser .add_argument ("-u" , "--username" )
121+ parser .add_argument ("-p" , "--password" )
66122
123+ args = parser .parse_args ()
67124
125+ host = Host (args .machine , args .username , args .password )
126+
127+ options = []
128+
129+ if args .filter :
130+ filter_args = args .filter .split (',' )
131+ for arg in filter_args :
132+ options .append (arg .strip ())
133+ else :
134+ options = filter_choices
135+
136+ if 'bios' in options :
137+ print "Bios Info:"
138+ print ""
139+ print rec_to_table (host .get_info ())
140+ print ""
141+
142+ if 'nic' in options :
143+ devices = pci_filter_for_nics (host .get_pci_devices ())
144+ print "Ethernet Controller Info:"
145+ print ""
146+ print tabulate_pci_recs ([dev .get_rec () for dev in devices ])
147+ print ""
148+
149+ if 'storage' in options :
150+ devices = pci_filter_for_storage (host .get_pci_devices ())
151+ print "Storage Controller Info:"
152+ print ""
153+ print tabulate_pci_recs ([dev .get_rec () for dev in devices ])
154+ print ""
155+
156+ if 'gpu' in options :
157+ devices = pci_filter_for_gpu (host .get_pci_devices ())
158+ if devices :
159+ print "GPU Info:"
160+ print ""
161+ print tabulate_pci_recs ([dev .get_rec () for dev in devices ])
162+ print ""
0 commit comments