From 0e2514743b71f4e0d177b072036884c1d9b72621 Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Tue, 16 Sep 2014 15:35:55 +0200
Subject: [ABRT PATCH 59/66] plugins: add abrt-action-generate-machine-id

Enabled by default on RHEL7.

Resolves: rhbz#1140044

Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 src/daemon/abrt_event.conf                  |  3 ++
 src/plugins/Makefile.am                     |  2 +
 src/plugins/abrt-action-generate-machine-id | 57 +++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+)
 create mode 100644 src/plugins/abrt-action-generate-machine-id

diff --git a/src/daemon/abrt_event.conf b/src/daemon/abrt_event.conf
index 380b312..deda7c7 100644
--- a/src/daemon/abrt_event.conf
+++ b/src/daemon/abrt_event.conf
@@ -92,6 +92,9 @@ EVENT=post-create
         rm sosreport.log
         exit 1
 
+# Example: if you want to include *machineid* in dump directories:
+EVENT=post-create
+    /usr/libexec/abrt-action-generate-machine-id -o $DUMP_DIR/machineid
 
 # Example: if you want to upload data immediately at the moment of a crash:
 #EVENT=post-create
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index 727dae0..326bb6e 100644
--- a/src/plugins/Makefile.am
+++ b/src/plugins/Makefile.am
@@ -35,6 +35,7 @@ libexec_PROGRAMS = \
     abrt-action-install-debuginfo-to-abrt-cache
 
 libexec_SCRIPTS = \
+    abrt-action-generate-machine-id \
     abrt-action-ureport \
     abrt-gdb-exploitable
 
@@ -91,6 +92,7 @@ EXTRA_DIST = \
     analyze_VMcore.xml.in \
     abrt-action-analyze-core.in \
     abrt-action-analyze-vmcore \
+    abrt-action-generate-machine-id \
     abrt-action-check-oops-for-hw-error \
     abrt-action-save-kernel-data \
     abrt-action-ureport \
diff --git a/src/plugins/abrt-action-generate-machine-id b/src/plugins/abrt-action-generate-machine-id
new file mode 100644
index 0000000..0aea787
--- /dev/null
+++ b/src/plugins/abrt-action-generate-machine-id
@@ -0,0 +1,57 @@
+#!/usr/bin/python
+from argparse import ArgumentParser
+
+import dmidecode
+import hashlib
+
+
+# Generate a machine_id based off dmidecode fields
+def generate_machine_id():
+    dmixml = dmidecode.dmidecodeXML()
+
+    # Fetch all DMI data into a libxml2.xmlDoc object
+    dmixml.SetResultType(dmidecode.DMIXML_DOC)
+    xmldoc = dmixml.QuerySection('all')
+
+    # Do some XPath queries on 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']
+
+    # Create a sha256 of ^ for machine_id
+    machine_id = hashlib.sha256()
+
+    # Run xpath expressions
+    for k in keys:
+        data = dmixp.xpathEval(k)
+        for d in data:
+            # Update the hash as we find the fields we are looking for
+            machine_id.update(d.get_content())
+
+    del dmixp
+    del xmldoc
+    # Create sha256 digest
+    return machine_id.hexdigest()
+
+
+if __name__ == "__main__":
+    CMDARGS = ArgumentParser(description = "Generate a machine_id based off dmidecode fields")
+    CMDARGS.add_argument('-o', '--output', type=str, help='Output file')
+
+    OPTIONS = CMDARGS.parse_args()
+    ARGS = vars(OPTIONS)
+
+    machineid =  generate_machine_id()
+
+    if ARGS['output']:
+        try:
+            with open(ARGS['output'], 'w') as outfile:
+                outfile.write(machineid)
+        except IOError as ex:
+            print ex
+    else:
+        print machineid
-- 
1.8.3.1

