Skip to content

Commit 60a8529

Browse files
author
David Sommerseth
committed
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.
1 parent d86d22c commit 60a8529

1 file changed

Lines changed: 164 additions & 39 deletions

File tree

examples/dmidump.py

Lines changed: 164 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,179 @@
11
#!/usr/bin/env python
2+
#
3+
# Examples which makes use of the different python-dmidecode features
4+
# This script should be run as root, or else expect permission warnings
5+
#
6+
# Copyright 2008-2009 Nima Talebi <nima@autonomy.net.au>
7+
# Copyright 2010 David Sommerseth <davids@redhat.com>
8+
#
9+
# This program is free software; you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation; either version 2 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# This program is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with this program; if not, write to the Free Software
21+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22+
#
23+
# For the avoidance of doubt the "preferred form" of this code is one which
24+
# is in an open unpatent encumbered format. Where cryptographic key signing
25+
# forms part of the process of creating an executable the information
26+
# including keys needed to generate an equivalently functional executable
27+
# are deemed to be part of the source code.
28+
#
29+
230
import dmidecode
3-
import sys
31+
import sys, os
432
from pprint import pprint
533

6-
#. Test all functions using /dev/mem...
7-
print "*** bios ***\n"; dmidecode.bios()
8-
print "*** system ***\n"; dmidecode.system()
9-
print "*** system ***\n"; dmidecode.system()
10-
print "*** baseboard ***\n"; dmidecode.baseboard()
11-
print "*** chassis ***\n"; dmidecode.chassis()
12-
print "*** processor ***\n"; dmidecode.processor()
13-
print "*** memory ***\n"; dmidecode.memory()
14-
print "*** cache ***\n"; dmidecode.cache()
15-
print "*** connector ***\n"; dmidecode.connector()
16-
print "*** slot ***\n"; dmidecode.slot()
34+
def print_warnings():
35+
"Simple function, dumping out warnings with a prefix if warnings are found and clearing warning buffer"
36+
warn = dmidecode.get_warnings()
37+
if warn:
38+
print "### WARNING: %s" % warn
39+
dmidecode.clear_warnings()
40+
41+
42+
# Check if running as root .... provide a warning if not
43+
root_user = (os.getuid() == 0 and True or False)
44+
if not root_user:
45+
print "####"
46+
print "#### NOT RUNNING AS ROOT"
47+
print "####"
48+
print "#### The first run must always be done as root for this example to work."
49+
print "#### When not run as root, quite some permission errors might appear"
50+
print "####"
51+
print "#### If this script is first run as root, it should be possible to run this script"
52+
print "#### as an unprivileged user afterwards, with less warnings."
53+
print "####"
54+
print
55+
print
56+
57+
58+
#. Test all functions using /dev/mem... Using the legacy API
59+
if root_user:
60+
print "*** bios ***\n"; dmidecode.bios()
61+
print_warnings()
62+
print "*** system ***\n"; dmidecode.system()
63+
print_warnings()
64+
print "*** system ***\n"; dmidecode.system()
65+
print_warnings()
66+
print "*** baseboard ***\n"; dmidecode.baseboard()
67+
print_warnings()
68+
print "*** chassis ***\n"; dmidecode.chassis()
69+
print_warnings()
70+
print "*** processor ***\n"; dmidecode.processor()
71+
print_warnings()
72+
print "*** memory ***\n"; dmidecode.memory()
73+
print_warnings()
74+
print "*** cache ***\n"; dmidecode.cache()
75+
print_warnings()
76+
print "*** connector ***\n"; dmidecode.connector()
77+
print_warnings()
78+
print "*** slot ***\n"; dmidecode.slot()
79+
print_warnings()
80+
1781

1882
#. Now test get/set of memory device file...
83+
print "*** get_dev()"
1984
print dmidecode.get_dev()
20-
print dmidecode.set_dev("private/mem-XXX");
85+
print_warnings()
86+
print "*** set_dev('dmidata.dump')"
87+
print dmidecode.set_dev("dmidata.dump");
88+
print_warnings()
89+
print "*** get_dev()"
2190
print dmidecode.get_dev()
91+
print_warnings()
2292

2393
#. Test taking a dump...
24-
print dmidecode.dump()
25-
26-
#. Test reading the dump...
27-
print "*** bios ***\n"; pprint(dmidecode.bios())
28-
print "*** system ***\n"; pprint(dmidecode.system())
29-
print "*** system ***\n"; pprint(dmidecode.system())
30-
print "*** baseboard ***\n"; pprint(dmidecode.baseboard())
31-
print "*** chassis ***\n"; pprint(dmidecode.chassis())
32-
print "*** processor ***\n"; pprint(dmidecode.processor())
33-
print "*** memory ***\n"; pprint(dmidecode.memory())
34-
print "*** cache ***\n"; pprint(dmidecode.cache())
35-
print "*** connector ***\n"; pprint(dmidecode.connector())
36-
print "*** slot ***\n"; pprint(dmidecode.slot())
37-
38-
sys.exit(0)
39-
print "*** bios ***\n"; pprint(dmidecode.bios())
40-
print "*** system ***\n"; pprint(dmidecode.system())
41-
print "*** baseboard ***\n"; pprint(dmidecode.baseboard())
42-
print "*** chassis ***\n"; pprint(dmidecode.chassis())
43-
print "*** processor ***\n"; pprint(dmidecode.processor())
44-
print "*** memory ***\n"; pprint(dmidecode.memory())
45-
print "*** cache ***\n"; pprint(dmidecode.cache())
46-
print "*** connector ***\n"; pprint(dmidecode.connector())
47-
print "*** slot ***\n"; pprint(dmidecode.slot())
94+
if root_user:
95+
print "*** Dumping DMI data to dump file"
96+
print dmidecode.dump()
97+
print_warnings()
4898

99+
#. Test reading the dump... Using the preferred API
100+
print "*** bios ***\n"; pprint(dmidecode.QuerySection('bios'))
101+
print_warnings()
102+
print "*** system ***\n"; pprint(dmidecode.QuerySection('system'))
103+
print_warnings()
104+
print "*** system ***\n"; pprint(dmidecode.QuerySection('system'))
105+
print_warnings()
106+
print "*** baseboard ***\n"; pprint(dmidecode.QuerySection('baseboard'))
107+
print_warnings()
108+
print "*** chassis ***\n"; pprint(dmidecode.QuerySection('chassis'))
109+
print_warnings()
110+
print "*** processor ***\n"; pprint(dmidecode.QuerySection('processor'))
111+
print_warnings()
112+
print "*** memory ***\n"; pprint(dmidecode.QuerySection('memory'))
113+
print_warnings()
114+
print "*** cache ***\n"; pprint(dmidecode.QuerySection('cache'))
115+
print_warnings()
116+
print "*** connector ***\n"; pprint(dmidecode.QuerySection('connector'))
117+
print_warnings()
118+
print "*** slot ***\n"; pprint(dmidecode.QuerySection('slot'))
119+
print_warnings()
120+
121+
print "*** Extracting memory information"
49122
for v in dmidecode.memory().values():
50123
if type(v) == dict and v['dmi_type'] == 17:
51124
pprint(v['data']['Size']),
52125

53-
pprint(dmidecode.type('3'))
54-
pprint(dmidecode.type('bios'))
126+
print "*** Querying for DMI type 3 and 7"
127+
pprint(dmidecode.type(3)) # <-- Legacy API
128+
pprint(dmidecode.QueryTypeId(7)) # <-- preferred API
129+
print_warnings()
130+
131+
print "*** Querying for the BIOS section"
132+
pprint(dmidecode.QuerySection('bios'))
133+
print_warnings()
134+
135+
#
136+
# Test XML stuff
137+
#
138+
print
139+
print
140+
print
141+
print "---------------------------------------"
142+
print "*** *** *** Testing XML API *** *** ***"
143+
print "---------------------------------------"
144+
print
145+
print
146+
dmixml = dmidecode.dmidecodeXML()
147+
148+
# Fetch all DMI data into a libxml2.xmlDoc object
149+
print "*** Getting all DMI data into a XML document variable"
150+
dmixml.SetResultType(dmidecode.DMIXML_DOC) # Valid values: dmidecode.DMIXML_DOC, dmidecode.DMIXML_NODE
151+
xmldoc = dmixml.QuerySection('all')
152+
153+
# Dump the XML to dmidump.xml - formated in UTF-8 decoding
154+
print "*** Dumping XML document to dmidump.xml"
155+
xmldoc.saveFormatFileEnc('dmidump.xml','UTF-8',1)
156+
157+
# Do some XPath queries on the XML document
158+
print "*** Doing some XPath queries against the XML document"
159+
dmixp = xmldoc.xpathNewContext()
160+
161+
# What to look for - XPath expressions
162+
keys = ['/dmidecode/SystemInfo/Manufacturer',
163+
'/dmidecode/SystemInfo/ProductName',
164+
'/dmidecode/SystemInfo/SerialNumber',
165+
'/dmidecode/SystemInfo/SystemUUID']
166+
167+
# Extract data and print it
168+
for k in keys:
169+
data = dmixp.xpathEval(k)
170+
for d in data:
171+
print "%s: %s" % (k, d.get_content())
172+
173+
del dmixp
174+
del xmldoc
175+
176+
# Query for only a particular DMI TypeID - 0x04 - Processor
177+
print "*** Quering for Type ID 0x04 - Processor - dumping XML document to stdout"
178+
dmixml.QueryTypeId(0x04).saveFormatFileEnc('-','UTF-8',1)
179+
print_warnings()

0 commit comments

Comments
 (0)