44797e
From 0e2514743b71f4e0d177b072036884c1d9b72621 Mon Sep 17 00:00:00 2001
44797e
From: Jakub Filak <jfilak@redhat.com>
44797e
Date: Tue, 16 Sep 2014 15:35:55 +0200
44797e
Subject: [ABRT PATCH 59/66] plugins: add abrt-action-generate-machine-id
44797e
44797e
Enabled by default on RHEL7.
44797e
44797e
Resolves: rhbz#1140044
44797e
44797e
Signed-off-by: Jakub Filak <jfilak@redhat.com>
44797e
---
44797e
 src/daemon/abrt_event.conf                  |  3 ++
44797e
 src/plugins/Makefile.am                     |  2 +
44797e
 src/plugins/abrt-action-generate-machine-id | 57 +++++++++++++++++++++++++++++
44797e
 3 files changed, 62 insertions(+)
44797e
 create mode 100644 src/plugins/abrt-action-generate-machine-id
44797e
44797e
diff --git a/src/daemon/abrt_event.conf b/src/daemon/abrt_event.conf
44797e
index 380b312..deda7c7 100644
44797e
--- a/src/daemon/abrt_event.conf
44797e
+++ b/src/daemon/abrt_event.conf
44797e
@@ -92,6 +92,9 @@ EVENT=post-create
44797e
         rm sosreport.log
44797e
         exit 1
44797e
 
44797e
+# Example: if you want to include *machineid* in dump directories:
44797e
+EVENT=post-create
44797e
+    /usr/libexec/abrt-action-generate-machine-id -o $DUMP_DIR/machineid
44797e
 
44797e
 # Example: if you want to upload data immediately at the moment of a crash:
44797e
 #EVENT=post-create
44797e
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
44797e
index 727dae0..326bb6e 100644
44797e
--- a/src/plugins/Makefile.am
44797e
+++ b/src/plugins/Makefile.am
44797e
@@ -35,6 +35,7 @@ libexec_PROGRAMS = \
44797e
     abrt-action-install-debuginfo-to-abrt-cache
44797e
 
44797e
 libexec_SCRIPTS = \
44797e
+    abrt-action-generate-machine-id \
44797e
     abrt-action-ureport \
44797e
     abrt-gdb-exploitable
44797e
 
44797e
@@ -91,6 +92,7 @@ EXTRA_DIST = \
44797e
     analyze_VMcore.xml.in \
44797e
     abrt-action-analyze-core.in \
44797e
     abrt-action-analyze-vmcore \
44797e
+    abrt-action-generate-machine-id \
44797e
     abrt-action-check-oops-for-hw-error \
44797e
     abrt-action-save-kernel-data \
44797e
     abrt-action-ureport \
44797e
diff --git a/src/plugins/abrt-action-generate-machine-id b/src/plugins/abrt-action-generate-machine-id
44797e
new file mode 100644
44797e
index 0000000..0aea787
44797e
--- /dev/null
44797e
+++ b/src/plugins/abrt-action-generate-machine-id
44797e
@@ -0,0 +1,57 @@
44797e
+#!/usr/bin/python
44797e
+from argparse import ArgumentParser
44797e
+
44797e
+import dmidecode
44797e
+import hashlib
44797e
+
44797e
+
44797e
+# Generate a machine_id based off dmidecode fields
44797e
+def generate_machine_id():
44797e
+    dmixml = dmidecode.dmidecodeXML()
44797e
+
44797e
+    # Fetch all DMI data into a libxml2.xmlDoc object
44797e
+    dmixml.SetResultType(dmidecode.DMIXML_DOC)
44797e
+    xmldoc = dmixml.QuerySection('all')
44797e
+
44797e
+    # Do some XPath queries on the XML document
44797e
+    dmixp = xmldoc.xpathNewContext()
44797e
+
44797e
+    # What to look for - XPath expressions
44797e
+    keys = ['/dmidecode/SystemInfo/Manufacturer',
44797e
+            '/dmidecode/SystemInfo/ProductName',
44797e
+            '/dmidecode/SystemInfo/SerialNumber',
44797e
+            '/dmidecode/SystemInfo/SystemUUID']
44797e
+
44797e
+    # Create a sha256 of ^ for machine_id
44797e
+    machine_id = hashlib.sha256()
44797e
+
44797e
+    # Run xpath expressions
44797e
+    for k in keys:
44797e
+        data = dmixp.xpathEval(k)
44797e
+        for d in data:
44797e
+            # Update the hash as we find the fields we are looking for
44797e
+            machine_id.update(d.get_content())
44797e
+
44797e
+    del dmixp
44797e
+    del xmldoc
44797e
+    # Create sha256 digest
44797e
+    return machine_id.hexdigest()
44797e
+
44797e
+
44797e
+if __name__ == "__main__":
44797e
+    CMDARGS = ArgumentParser(description = "Generate a machine_id based off dmidecode fields")
44797e
+    CMDARGS.add_argument('-o', '--output', type=str, help='Output file')
44797e
+
44797e
+    OPTIONS = CMDARGS.parse_args()
44797e
+    ARGS = vars(OPTIONS)
44797e
+
44797e
+    machineid =  generate_machine_id()
44797e
+
44797e
+    if ARGS['output']:
44797e
+        try:
44797e
+            with open(ARGS['output'], 'w') as outfile:
44797e
+                outfile.write(machineid)
44797e
+        except IOError as ex:
44797e
+            print ex
44797e
+    else:
44797e
+        print machineid
44797e
-- 
44797e
1.8.3.1
44797e