Package spacewalk_abrt :: Module abrt
[hide private]
[frames] | no frames]

Source Code for Module spacewalk_abrt.abrt

  1  #!/usr/bin/python 
  2  # 
  3  # Copyright (c) 2013--2018 Red Hat, Inc. 
  4  # 
  5  # This software is licensed to you under the GNU General Public License, 
  6  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  7  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  8  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  9  # along with this software; if not, see 
 10  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 11  # 
 12  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 13  # granted to use or replicate Red Hat trademarks that are incorporated 
 14  # in this software or its documentation. 
 15  # 
 16   
 17  import base64 
 18  import os 
 19  import sys 
 20  import errno 
 21  from rhn.i18n import bstr 
 22   
 23  import gettext 
 24  t = gettext.translation('spacewalk-abrt', fallback=True) 
 25  _ = t.ugettext 
 26   
 27  from up2date_client import config 
 28  from up2date_client import up2dateAuth 
 29  from up2date_client import rhnserver 
 30  from up2date_client import up2dateLog 
 31   
 32  encodestring = base64.encodestring 
 33  if hasattr(base64, 'encodebytes'): 
 34      encodestring = base64.encodebytes 
 35   
36 -def _readline(filepath):
37 firstline = None 38 try: 39 f = open(filepath, 'r') 40 firstline = f.readline().strip() 41 f.close() 42 except IOError: 43 e = sys.exc_info()[1] 44 if e.errno == errno.ENOENT: 45 pass 46 else: 47 raise 48 return firstline
49 50
51 -def _get_abrt_dir():
52 abrt_dir = '/var/tmp/abrt' 53 for directory in ['/var/tmp/abrt', '/var/spool/abrt']: 54 if os.path.exists(directory) and os.path.isdir(directory): 55 abrt_dir = directory 56 57 cf = config.ConfigFile('/etc/abrt/abrt.conf') 58 return cf['DumpLocation'] or abrt_dir
59 60
61 -def report(problem_dir):
62 problem_dir = os.path.normpath(os.path.abspath(problem_dir)) 63 basename = os.path.basename(problem_dir) 64 log = up2dateLog.initLog() 65 if not (os.path.exists(problem_dir) and os.path.isdir(problem_dir)): 66 log.log_me("The specified path [%s] is not a valid directory." % problem_dir) 67 return -1 68 69 crash_items = ['analyzer', 'cmdline', 'reason'] 70 if os.path.exists(os.path.join(problem_dir, 'vmcore')): 71 crash_items = ['analyzer', 'vmcore-dmesg.txt'] 72 73 for item in crash_items: 74 item_path = os.path.join(problem_dir, item) 75 if not os.path.exists(item_path): 76 log.log_me("Crash directory [%s] is incomplete or invalid" % problem_dir) 77 return -1 78 79 server = rhnserver.RhnServer() 80 if not server.capabilities.hasCapability('abrt'): 81 return -1 82 83 systemid = up2dateAuth.getSystemId() 84 85 # Package information 86 pkg_data = {} 87 for item in ['package', 'pkg_name', 'pkg_epoch', 'pkg_version', 'pkg_release', 'pkg_arch']: 88 pkg_item_path = os.path.join(problem_dir, item) 89 if os.path.exists(pkg_item_path): 90 filecontent = _readline(pkg_item_path) 91 92 if filecontent: 93 pkg_data[item] = filecontent 94 95 # Crash information 96 crash_data = {'crash': basename, 'path': problem_dir} 97 # Crash count 98 crash_count = _readline(os.path.join(problem_dir, 'count')) 99 if crash_count: 100 crash_data['count'] = crash_count 101 102 # Create record about the crash 103 r = server.abrt.create_crash(systemid, crash_data, pkg_data) 104 105 if (r < 0): # Error creating new crash report 106 log.log_me("Error creating new crash report.") 107 return -1 108 109 # Upload every particular file in the problem directory to the server 110 for i in os.listdir(problem_dir): 111 path = os.path.join(problem_dir, i) 112 if not os.path.isfile(path): 113 continue 114 115 filesize = os.stat(path).st_size 116 117 crash_file_data = {'filename': os.path.basename(i), 118 'path': path, 119 'filesize': filesize, 120 'filecontent': encodestring(bstr("")), 121 'content-encoding': 'base64'} 122 if server.abrt.is_crashfile_upload_enabled(systemid) and filesize <= server.abrt.get_crashfile_uploadlimit(systemid): 123 f = open(path, 'r') 124 try: 125 crash_file_data['filecontent'] = encodestring(bstr(f.read())) 126 finally: 127 f.close() 128 129 server.abrt.upload_crash_file(systemid, basename, crash_file_data) 130 131 return 1
132 133
134 -def update_count(problem_dir):
135 problem_dir = os.path.normpath(os.path.abspath(problem_dir)) 136 basename = os.path.basename(problem_dir) 137 log = up2dateLog.initLog() 138 if not (os.path.exists(problem_dir) and os.path.isdir(problem_dir)): 139 log.log_me("The specified path [%s] is not a valid directory." % problem_dir) 140 return -1 141 142 server = rhnserver.RhnServer() 143 if not server.capabilities.hasCapability('abrt'): 144 return -1 145 146 systemid = up2dateAuth.getSystemId() 147 crash_count_path = os.path.join(problem_dir, 'count') 148 if not (os.path.exists(crash_count_path) and os.path.isfile(crash_count_path)): 149 log.log_me("The problem directory [%s] does not contain any crash count information." % problem_dir) 150 return 0 151 152 crash_count = _readline(crash_count_path) 153 server.abrt.update_crash_count(systemid, basename, crash_count) 154 155 return 1
156 157
158 -def sync():
159 abrt_dir = os.path.normpath(_get_abrt_dir()) 160 if not (os.path.exists(abrt_dir) and os.path.isdir(abrt_dir)): 161 log.log_me("The specified path [%s] is not a valid directory." % abrt_dir) 162 return -1 163 164 for i in os.listdir(abrt_dir): 165 problem_dir = os.path.join(abrt_dir, i) 166 if not os.path.isdir(problem_dir): 167 continue 168 169 report(problem_dir) 170 171 return 1
172