socsvn commit: r238961 - soc2012/tzabal/server-side/akcrs-release/9.0.0/usr.sbin/crashreportd

tzabal at FreeBSD.org tzabal at FreeBSD.org
Wed Jul 4 15:21:19 UTC 2012


Author: tzabal
Date: Wed Jul  4 15:21:15 2012
New Revision: 238961
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=238961

Log:
  Updated version of crashreportd.

Modified:
  soc2012/tzabal/server-side/akcrs-release/9.0.0/usr.sbin/crashreportd/crashreportd.py

Modified: soc2012/tzabal/server-side/akcrs-release/9.0.0/usr.sbin/crashreportd/crashreportd.py
==============================================================================
--- soc2012/tzabal/server-side/akcrs-release/9.0.0/usr.sbin/crashreportd/crashreportd.py	Wed Jul  4 14:25:14 2012	(r238960)
+++ soc2012/tzabal/server-side/akcrs-release/9.0.0/usr.sbin/crashreportd/crashreportd.py	Wed Jul  4 15:21:15 2012	(r238961)
@@ -1,47 +1,142 @@
-#!/usr/bin/python
+#!/usr/local/bin/python
 
 import os
+import re
 import time
 import tarfile
 
-# Check if the given filename matches the pattern of a valid crash report
-# Return 1 in failure and 0 in success
-def check_filename(filename):
-    matchObj = re.match('^crashreport\.[A-Za-z0-9]{6}\.tar\.gz$', filename)
+
+# Check if the filename matches the pattern of a valid crash report name
+# Return False in failure and True in success
+def check_report_name(filename):
+    match_obj = re.match('^crashreport\.[A-Za-z0-9]{6}\.tar\.gz$', filename)
+
+    if not match_obj:
+        print "debug1"
+        return False
     
-    if not matchObj:
-        return 1
+    return True
+
+
+# Check if the filename matches the pattern of a valid crash data name
+# Return False in failure and True in success
+def check_data_name(filename):
+    match_obj = re.match('^crashreport\.[A-Za-z0-9]{6}\.xml$', filename)
+
+    if not match_obj:
+        print "debug2"
+        return False
     
-    return 0
+    return True
 
 
-def extract_file(filename):
+# Check if the report is a tar.gz file and if it has only one file inside
+# with a valid name.
+# Return False in failure and True in success
+def check_report_contents(filename):
+    report = crashreports_dir + "/" + filename
+    
+    if not tarfile.is_tarfile(report):
+        print "debug3"
+        return False
+    
+    try:
+        tarfile_obj = tarfile.open(report, 'r:gz')
+        contents_list = tarfile_obj.getnames()
+    except ReadError:
+        print "debug4"
+        return False
+    except CompressionError:
+        print "debug5"
+        return False
+    
+    if not len(contents_list) == 1:
+        print "debug6"
+        return False
+    
+    if not check_data_name(contents_list[0]):
+        print "debug7"
+        return False
+    
+    return True
+
+
+def extract_report(filename):
+    if not os.path.isdir(extraction_dir):
+        print "debug11"
+        return False
+    
     try:
         report = crashreports_dir + "/" + filename
-        tarfileObj = tarfile.open(report)
+        tarfile_obj = tarfile.open(report, 'r:gz')
+        tarfile_obj.extractall(extraction_dir);
     except ReadError:
-        return 1
-        
-    return 0
+        print "debug12"
+        return False
+    except CompressionError:
+        print "debug13"
+        return False
+    
+    dirlist = os.listdir(extraction_dir)
+    if not len(dirlist) == 1:
+        print "debug14"
+        return False
+    
+    data_name = dirlist[0]
+    #print "data_name: ", data_name
+
+    return True
 
 
+# Container function that call all the check related functions
+# Return False in failure and True in success
 def check_report(filename):
-    check_filename(filename)
+    if not check_report_name(filename):
+        print "debug8"
+        return False
+    
+    if not check_report_contents(filename):
+        print "debug9"
+        return False
     
-    return 0;
+    if not extract_report(filename):
+        print "debug10"
+        return False
+    
+    return True
+
+
+# Create the Process ID file that contains the PID of crashreportd.
+# It used from the rc.d script to stop the program normally.
+def create_pid_file():
+    pid = os.getpid()
 
-# Obtain the Process ID
-pid = os.getpid()
+    pid_file = '/home/tzabal/Desktop/crashreportd.pid'
+    #pid_file = '/var/run/crashreportd.pid'
+    try:
+        file_obj = open(pid_file, 'w')
+        file_obj.write(str(pid))
+        file_obj.close()
+    except IOError:
+        return False
+    finally:
+        file_obj.close()
+    
+    return True
 
-# Create the pid file and write the Process ID inside
-#pid_file = '/var/run/crashreportd.pid'
-pid_file = '/home/tzabal/crashreportd.pid'
-f = open(pid_file, 'w')
-f.write(str(pid))
-f.close()
 
 # The infinite loop of the daemon
-crashreports_dir = '/home/tzabal/crashreports'
-dirlist = os.listdir(crashreports_dir)
-for filename in dirlist:
-    check_report(filename)
+interval = 10
+crashreports_dir = '/home/tzabal/Desktop/crashreports'
+#crashreports_dir = '/var/spool/crashreports'
+extraction_dir = '/tmp/crashreports'
+counter = 1
+create_pid_file()
+while True:
+    print "== Pass:", counter, "=="
+    dirlist = os.listdir(crashreports_dir)
+    for filename in dirlist:
+        print "Report:", filename
+        check_report(filename)
+    counter += 1
+    time.sleep(interval)
\ No newline at end of file


More information about the svn-soc-all mailing list