summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2010-03-02 18:48:35 +0100
committerDavid Sommerseth <davids@redhat.com>2010-03-02 18:48:35 +0100
commit60a85298dffa1497c2d24334c92989bc57201406 (patch)
treea51c2e9ca0c5db9e0f1af716c9ffb56e13b7236c
parentd86d22c89cda5f8d2736dadfc398e8f6a29e1b82 (diff)
downloadpython-dmidecode-60a85298dffa1497c2d24334c92989bc57201406.tar.gz
python-dmidecode-60a85298dffa1497c2d24334c92989bc57201406.tar.xz
python-dmidecode-60a85298dffa1497c2d24334c92989bc57201406.zip
Updated examples/dmidump.py to be more up-to-date against a lot of new features
Rewrote bigger parts to be more intelligent, checking for warnings on the way. Added more warnings if script is run as a non-root user, and made it work as non-root user if a dmidata.dump file exists (created when run as root). Added examples with the XML API as well.
-rwxr-xr-xexamples/dmidump.py203
1 files changed, 164 insertions, 39 deletions
diff --git a/examples/dmidump.py b/examples/dmidump.py
index ed9908f..977504f 100755
--- a/examples/dmidump.py
+++ b/examples/dmidump.py
@@ -1,54 +1,179 @@
#!/usr/bin/env python
+#
+# Examples which makes use of the different python-dmidecode features
+# This script should be run as root, or else expect permission warnings
+#
+# Copyright 2008-2009 Nima Talebi <nima@autonomy.net.au>
+# Copyright 2010 David Sommerseth <davids@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# For the avoidance of doubt the "preferred form" of this code is one which
+# is in an open unpatent encumbered format. Where cryptographic key signing
+# forms part of the process of creating an executable the information
+# including keys needed to generate an equivalently functional executable
+# are deemed to be part of the source code.
+#
+
import dmidecode
-import sys
+import sys, os
from pprint import pprint
-#. Test all functions using /dev/mem...
-print "*** bios ***\n"; dmidecode.bios()
-print "*** system ***\n"; dmidecode.system()
-print "*** system ***\n"; dmidecode.system()
-print "*** baseboard ***\n"; dmidecode.baseboard()
-print "*** chassis ***\n"; dmidecode.chassis()
-print "*** processor ***\n"; dmidecode.processor()
-print "*** memory ***\n"; dmidecode.memory()
-print "*** cache ***\n"; dmidecode.cache()
-print "*** connector ***\n"; dmidecode.connector()
-print "*** slot ***\n"; dmidecode.slot()
+def print_warnings():
+ "Simple function, dumping out warnings with a prefix if warnings are found and clearing warning buffer"
+ warn = dmidecode.get_warnings()
+ if warn:
+ print "### WARNING: %s" % warn
+ dmidecode.clear_warnings()
+
+
+# Check if running as root .... provide a warning if not
+root_user = (os.getuid() == 0 and True or False)
+if not root_user:
+ print "####"
+ print "#### NOT RUNNING AS ROOT"
+ print "####"
+ print "#### The first run must always be done as root for this example to work."
+ print "#### When not run as root, quite some permission errors might appear"
+ print "####"
+ print "#### If this script is first run as root, it should be possible to run this script"
+ print "#### as an unprivileged user afterwards, with less warnings."
+ print "####"
+ print
+ print
+
+
+#. Test all functions using /dev/mem... Using the legacy API
+if root_user:
+ print "*** bios ***\n"; dmidecode.bios()
+ print_warnings()
+ print "*** system ***\n"; dmidecode.system()
+ print_warnings()
+ print "*** system ***\n"; dmidecode.system()
+ print_warnings()
+ print "*** baseboard ***\n"; dmidecode.baseboard()
+ print_warnings()
+ print "*** chassis ***\n"; dmidecode.chassis()
+ print_warnings()
+ print "*** processor ***\n"; dmidecode.processor()
+ print_warnings()
+ print "*** memory ***\n"; dmidecode.memory()
+ print_warnings()
+ print "*** cache ***\n"; dmidecode.cache()
+ print_warnings()
+ print "*** connector ***\n"; dmidecode.connector()
+ print_warnings()
+ print "*** slot ***\n"; dmidecode.slot()
+ print_warnings()
+
#. Now test get/set of memory device file...
+print "*** get_dev()"
print dmidecode.get_dev()
-print dmidecode.set_dev("private/mem-XXX");
+print_warnings()
+print "*** set_dev('dmidata.dump')"
+print dmidecode.set_dev("dmidata.dump");
+print_warnings()
+print "*** get_dev()"
print dmidecode.get_dev()
+print_warnings()
#. Test taking a dump...
-print dmidecode.dump()
-
-#. Test reading the dump...
-print "*** bios ***\n"; pprint(dmidecode.bios())
-print "*** system ***\n"; pprint(dmidecode.system())
-print "*** system ***\n"; pprint(dmidecode.system())
-print "*** baseboard ***\n"; pprint(dmidecode.baseboard())
-print "*** chassis ***\n"; pprint(dmidecode.chassis())
-print "*** processor ***\n"; pprint(dmidecode.processor())
-print "*** memory ***\n"; pprint(dmidecode.memory())
-print "*** cache ***\n"; pprint(dmidecode.cache())
-print "*** connector ***\n"; pprint(dmidecode.connector())
-print "*** slot ***\n"; pprint(dmidecode.slot())
-
-sys.exit(0)
-print "*** bios ***\n"; pprint(dmidecode.bios())
-print "*** system ***\n"; pprint(dmidecode.system())
-print "*** baseboard ***\n"; pprint(dmidecode.baseboard())
-print "*** chassis ***\n"; pprint(dmidecode.chassis())
-print "*** processor ***\n"; pprint(dmidecode.processor())
-print "*** memory ***\n"; pprint(dmidecode.memory())
-print "*** cache ***\n"; pprint(dmidecode.cache())
-print "*** connector ***\n"; pprint(dmidecode.connector())
-print "*** slot ***\n"; pprint(dmidecode.slot())
+if root_user:
+ print "*** Dumping DMI data to dump file"
+ print dmidecode.dump()
+ print_warnings()
+#. Test reading the dump... Using the preferred API
+print "*** bios ***\n"; pprint(dmidecode.QuerySection('bios'))
+print_warnings()
+print "*** system ***\n"; pprint(dmidecode.QuerySection('system'))
+print_warnings()
+print "*** system ***\n"; pprint(dmidecode.QuerySection('system'))
+print_warnings()
+print "*** baseboard ***\n"; pprint(dmidecode.QuerySection('baseboard'))
+print_warnings()
+print "*** chassis ***\n"; pprint(dmidecode.QuerySection('chassis'))
+print_warnings()
+print "*** processor ***\n"; pprint(dmidecode.QuerySection('processor'))
+print_warnings()
+print "*** memory ***\n"; pprint(dmidecode.QuerySection('memory'))
+print_warnings()
+print "*** cache ***\n"; pprint(dmidecode.QuerySection('cache'))
+print_warnings()
+print "*** connector ***\n"; pprint(dmidecode.QuerySection('connector'))
+print_warnings()
+print "*** slot ***\n"; pprint(dmidecode.QuerySection('slot'))
+print_warnings()
+
+print "*** Extracting memory information"
for v in dmidecode.memory().values():
if type(v) == dict and v['dmi_type'] == 17:
pprint(v['data']['Size']),
-pprint(dmidecode.type('3'))
-pprint(dmidecode.type('bios'))
+print "*** Querying for DMI type 3 and 7"
+pprint(dmidecode.type(3)) # <-- Legacy API
+pprint(dmidecode.QueryTypeId(7)) # <-- preferred API
+print_warnings()
+
+print "*** Querying for the BIOS section"
+pprint(dmidecode.QuerySection('bios'))
+print_warnings()
+
+#
+# Test XML stuff
+#
+print
+print
+print
+print "---------------------------------------"
+print "*** *** *** Testing XML API *** *** ***"
+print "---------------------------------------"
+print
+print
+dmixml = dmidecode.dmidecodeXML()
+
+# Fetch all DMI data into a libxml2.xmlDoc object
+print "*** Getting all DMI data into a XML document variable"
+dmixml.SetResultType(dmidecode.DMIXML_DOC) # Valid values: dmidecode.DMIXML_DOC, dmidecode.DMIXML_NODE
+xmldoc = dmixml.QuerySection('all')
+
+# Dump the XML to dmidump.xml - formated in UTF-8 decoding
+print "*** Dumping XML document to dmidump.xml"
+xmldoc.saveFormatFileEnc('dmidump.xml','UTF-8',1)
+
+# Do some XPath queries on the XML document
+print "*** Doing some XPath queries against the XML document"
+dmixp = xmldoc.xpathNewContext()
+
+# What to look for - XPath expressions
+keys = ['/dmidecode/SystemInfo/Manufacturer',
+ '/dmidecode/SystemInfo/ProductName',
+ '/dmidecode/SystemInfo/SerialNumber',
+ '/dmidecode/SystemInfo/SystemUUID']
+
+# Extract data and print it
+for k in keys:
+ data = dmixp.xpathEval(k)
+ for d in data:
+ print "%s: %s" % (k, d.get_content())
+
+del dmixp
+del xmldoc
+
+# Query for only a particular DMI TypeID - 0x04 - Processor
+print "*** Quering for Type ID 0x04 - Processor - dumping XML document to stdout"
+dmixml.QueryTypeId(0x04).saveFormatFileEnc('-','UTF-8',1)
+print_warnings()