|
4 | 4 | from prettytable import PrettyTable |
5 | 5 | import paramiko |
6 | 6 | import subprocess |
| 7 | +import os |
7 | 8 |
|
8 | 9 | from hwinfo.pci import PCIDevice |
9 | 10 | from hwinfo.pci.lspci import * |
@@ -47,19 +48,51 @@ def exec_command(self, cmd): |
47 | 48 | else: |
48 | 49 | return remote_command(self.host, self.username, self.password, cmd) |
49 | 50 |
|
| 51 | + def get_lspci_data(self): |
| 52 | + return self.exec_command(['lspci', '-nnmm']) |
| 53 | + |
| 54 | + def get_dmidecode_data(self): |
| 55 | + return self.exec_command(['dmidecode']) |
| 56 | + |
50 | 57 | def get_pci_devices(self): |
51 | | - data = self.exec_command(['lspci', '-nnmm']) |
| 58 | + data = self.get_lspci_data() |
52 | 59 | parser = LspciNNMMParser(data) |
53 | 60 | devices = parser.parse_items() |
54 | 61 | return [PCIDevice(device) for device in devices] |
55 | 62 |
|
56 | 63 | def get_info(self): |
57 | | - data = self.exec_command(['dmidecode']) |
| 64 | + data = self.get_dmidecode_data() |
58 | 65 | parser = dmidecode.DmidecodeParser(data) |
59 | | - print "test : '%s'" % parser |
60 | 66 | rec = parser.parse() |
61 | 67 | return rec |
62 | 68 |
|
| 69 | +def search_for_file(dirname, filename): |
| 70 | + for root, _, files in os.walk(dirname): |
| 71 | + if filename in files: |
| 72 | + return os.path.join(root, filename) |
| 73 | + raise Exception("Could not find '%s' in directory '%s'" % (filename, dirname)) |
| 74 | + |
| 75 | +def read_from_file(filename): |
| 76 | + fh = open(filename, 'r') |
| 77 | + data = fh.read() |
| 78 | + fh.close() |
| 79 | + return data |
| 80 | + |
| 81 | +class HostFromLogs(Host): |
| 82 | + |
| 83 | + def __init__(self, dirname): |
| 84 | + self.dirname = dirname |
| 85 | + |
| 86 | + def _load_from_file(self, filename): |
| 87 | + filename = search_for_file(self.dirname, filename) |
| 88 | + return read_from_file(filename) |
| 89 | + |
| 90 | + def get_lspci_data(self): |
| 91 | + return self._load_from_file('lspci-nnm.out') |
| 92 | + |
| 93 | + def get_dmidecode_data(self): |
| 94 | + return self._load_from_file('dmidecode.out') |
| 95 | + |
63 | 96 | def pci_filter(devices, types): |
64 | 97 | res = [] |
65 | 98 | for device in devices: |
@@ -112,14 +145,18 @@ def main(): |
112 | 145 | parser = ArgumentParser(prog="hwinfo") |
113 | 146 |
|
114 | 147 | filter_choices = ['bios', 'nic', 'storage', 'gpu'] |
115 | | - parser.add_argument("-f", "--filter", choices=filter_choices) |
116 | | - parser.add_argument("-m", "--machine", default='localhost') |
117 | | - parser.add_argument("-u", "--username") |
118 | | - parser.add_argument("-p", "--password") |
| 148 | + parser.add_argument("-f", "--filter", choices=filter_choices, help="Query a specific class.") |
| 149 | + parser.add_argument("-m", "--machine", default='localhost', help="Remote host address.") |
| 150 | + parser.add_argument("-u", "--username", help="Username for remote host.") |
| 151 | + parser.add_argument("-p", "--password", help="Password for remote host.") |
| 152 | + parser.add_argument("-l", "--logs", help="Path to the directory with the logfiles.") |
119 | 153 |
|
120 | 154 | args = parser.parse_args() |
121 | 155 |
|
122 | | - host = Host(args.machine, args.username, args.password) |
| 156 | + if args.logs: |
| 157 | + host = HostFromLogs(args.logs) |
| 158 | + else: |
| 159 | + host = Host(args.machine, args.username, args.password) |
123 | 160 |
|
124 | 161 | options = [] |
125 | 162 |
|
|
0 commit comments