projectz

Tech, Gadgets, Photography, Social Media and Poor Spelling

Using lshw and xmlstarlet to extract system data

I’ve had the need to create a system tool which provides hardware information on the device ubuntu is being run on, so i can port this to a mysql database.

The extraction of the data is done using a combination of two commands, lshw and xmlstarlet

As an example if i wanted to extract the display information for the PC ubuntu was running on, i would run the following:

sudo lshw -xml -c display

this command runs lsht, outputs using xml format and outputs only the display class.. due to some of the information lshw needs it is recommended to run as sudo.

this command provides the following output (I have two video cards)

<?xml version=”1.0” standalone=”yes” ?>
<!— generated by lshw-B.02.14 —>
<!— GCC 4.4.3 —>
<!— Linux 2.6.32-32-generic-pae #62-Ubuntu SMP Wed Apr 20 22:10:33 UTC 2011 i686 —>
<!— GNU libc 2 (glibc 2.11.1) —>
  <node id=”display” claimed=”true” class=”display” handle=”PCI:0000:01:00.0”>
   <description>VGA compatible controller</description>
   <product>G98 [GeForce 8400 GS]</product>
   <vendor>nVidia Corporation</vendor>
   <physid>0</physid>
   <businfo>pci@0000:01:00.0</businfo>
   <version>a1</version>
   <width units=”bits”>64</width>
   <clock units=”Hz”>33000000</clock>
   <configuration>
    <setting id=”driver” value=”nvidia” />
    <setting id=”latency” value=”0” />
   </configuration>
   <capabilities>
    <capability id=”pm” >Power Management</capability>
    <capability id=”msi” >Message Signalled Interrupts</capability>
    <capability id=”pciexpress” >PCI Express</capability>
    <capability id=”bus_master” >bus mastering</capability>
    <capability id=”cap_list” >PCI capabilities listing</capability>
    <capability id=”rom” >extension ROM</capability>
   </capabilities>
   <resources>
    <resource type=”irq&quot; value=&quot;48” />
    <resource type=”memory&quot; value=&quot;fa000000-faffffff” />
    <resource type=”memory&quot; value=&quot;c0000000-cfffffff(prefetchable)” />
    <resource type=”memory&quot; value=&quot;f8000000-f9ffffff” />
    <resource type=”ioport&quot; value=&quot;dc80(size=128)” />
    <resource type=”memory&quot; value=&quot;fbe00000-fbe1ffff(prefetchable)” />
   </resources>
  </node>
  <node id=”display” claimed=”true” class=”display” handle=”PCI:0000:02:00.0”>
   <description>VGA compatible controller</description>
   <product>G84 [Quadro FX 570]</product>
   <vendor>nVidia Corporation</vendor>
   <physid>0</physid>
   <businfo>pci@0000:02:00.0</businfo>
   <version>a1</version>
   <width units=”bits”>64</width>
   <clock units=”Hz”>33000000</clock>
   <configuration>
    <setting id=”driver” value=”nvidia” />
    <setting id=”latency” value=”0” />
   </configuration>
   <capabilities>
    <capability id=”pm” >Power Management</capability>
    <capability id=”msi” >Message Signalled Interrupts</capability>
    <capability id=”pciexpress” >PCI Express</capability>
    <capability id=”bus_master” >bus mastering</capability>
    <capability id=”cap_list” >PCI capabilities listing</capability>
    <capability id=”rom” >extension ROM</capability>
   </capabilities>
   <resources>
    <resource type=”irq&quot; value=&quot;52” />
    <resource type=”memory&quot; value=&quot;f5000000-f5ffffff” />
    <resource type=”memory&quot; value=&quot;d0000000-dfffffff(prefetchable)” />
    <resource type=”memory&quot; value=&quot;f6000000-f7ffffff” />
    <resource type=”ioport&quot; value=&quot;cc80(size=128)” />
    <resource type=”memory&quot; value=&quot;f4f00000-f4f1ffff(prefetchable)” />
   </resources>
  </node>

While useful, i was looking to pull out some human readable output, so i piped the output of the above command to a text file.

sudo lshw -xml -c display > display.txt

The output while XML, isn’t actually true XML, its missing a root area, so we need to run the following command on the text file.

sed -e ‘/?>/a<lshw>’ -e ‘$a</lshw>’ lshw_display.txt

Which will add a root section

We can now use the XML tool xmlstarlet to parse and extract data from the display.txt XML file.

xmlstarlet sel -t -m “//lshw/node[@id=’display’]”  -v “description” -n display.txt

outputs

VGA compatible controller
VGA compatible controller

and other examples include:

xmlstarlet sel -t -m “//lshw/node[@id=’display’]”  -v “product” -n display.txt
G98 [GeForce 8400 GS]
G84 [Quadro FX 570]

xmlstarlet sel -t -m “//lshw/node[@id=’display’]”  -v “product” -n display.txt
G98 [GeForce 8400 GS]
G84 [Quadro FX 570]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on April 28, 2011 by in regular and tagged , , , , , .
%d bloggers like this: