Skip to content

Commit e4818e7

Browse files
committed
Adding support for exporting data in JSON format.
Signed-off-by: Rob Dobson <[email protected]>
1 parent 9557323 commit e4818e7

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

hwinfo/tools/inspector.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#!/usr/bin/env python
22

33
from argparse import ArgumentParser
4-
from prettytable import PrettyTable
54
import paramiko
65
import subprocess
76
import os
87
import sys
98
import tarfile
109
import tempfile
1110
import shutil
11+
import json
12+
13+
from prettytable import PrettyTable
1214

1315
from hwinfo.pci import PCIDevice
1416
from hwinfo.pci.lspci import *
@@ -152,7 +154,6 @@ def get_info(self):
152154
data = self.get_dmidecode_data()
153155
parser = dmidecode.DmidecodeParser(data)
154156
rec = parser.parse()
155-
print rec
156157
#Count sockets
157158
if 'socket_designation' in rec:
158159
rec['socket_count'] = len(rec['socket_designation'].split(','))
@@ -371,6 +372,29 @@ def print_system_info(host, options):
371372
if devices:
372373
print_unit("GPU Info:", tabulate_pci_recs([dev.get_rec() for dev in devices]))
373374

375+
def export_system_info(host, options):
376+
rec = {}
377+
378+
if 'bios' in options:
379+
rec["bios"] = host.get_info()
380+
381+
if 'cpu' in options:
382+
rec["cpu"] = host.get_cpu_info()
383+
384+
if 'nic' in options:
385+
devices = pci_filter_for_nics(host.get_pci_devices())
386+
rec["nics"] = [dev.get_rec() for dev in devices]
387+
388+
if 'storage' in options:
389+
devices = pci_filter_for_storage(host.get_pci_devices())
390+
rec["storage_controllers"] = [dev.get_rec() for dev in devices]
391+
392+
if 'gpu' in options:
393+
devices = pci_filter_for_gpu(host.get_pci_devices())
394+
rec["gpus"] = [dev.get_rec() for dev in devices]
395+
396+
print json.dumps(rec, indent=4, separators=(',', ': '))
397+
374398
def main():
375399
"""Entry Point"""
376400

@@ -382,6 +406,7 @@ def main():
382406
parser.add_argument("-u", "--username", help="Username for remote host.")
383407
parser.add_argument("-p", "--password", help="Password for remote host.")
384408
parser.add_argument("-l", "--logs", help="Path to the directory with the logfiles.")
409+
parser.add_argument("-e", "--export", action="store_true", help="Export result in JSON format.")
385410

386411
args = parser.parse_args()
387412
validate_args(args)
@@ -400,4 +425,7 @@ def main():
400425
else:
401426
options = filter_choices
402427

403-
print_system_info(host, options)
428+
if args.export:
429+
export_system_info(host, options)
430+
else:
431+
print_system_info(host, options)

hwinfo/tools/tests/test_inspector.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,14 @@ def test_local_machine_filter_for_gpu(self, host_cls, print_system_info):
369369
inspector.main()
370370
print_system_info.assert_called_with(mhost, ['gpu'])
371371

372+
@patch('hwinfo.tools.inspector.export_system_info')
373+
@patch('hwinfo.tools.inspector.Host')
374+
def test_export(self, host_cls, export_system_info):
375+
sys.argv = ['hwinfo', '-e']
376+
mhost = host_cls.return_value = mock.MagicMock()
377+
inspector.main()
378+
export_system_info.assert_called_with(mhost, self.OPTIONS)
379+
372380
@patch('sys.exit')
373381
def test_validate_args_no_username(self, exit):
374382
args = mock.MagicMock()

0 commit comments

Comments
 (0)