summaryrefslogtreecommitdiffstats
path: root/utils/log_picker/logmining.py
blob: 2bbf1c3cc74e91b10e0c6161b5ddf1fa67434283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import os
import stat
import shlex
import time
import subprocess


class LogMinerError(Exception):
    pass


class LogMinerBaseClass(object):
    """Base class for LogMiner classes. 
    LogMiner object represents one file/command/function 
    to get useful information (log)."""
        
    _name = "name"
    _description = "Description"
    _filename = "filename"
    _prefer_separate_file = True
    
    def __init__(self, logfile=None, *args, **kwargs):
        """@logfile open file object. This open file object will be used for 
        output generated during getlog() call."""
        self.logfile = logfile
        self._used = False
    
    @classmethod
    def get_filename(cls):
        """Suggested log filename."""
        return cls._filename
        
    @classmethod
    def get_description(cls):
        """Log description."""
        return cls._description
    
    def set_logfile(self, logfile):
        self.logfile = logfile
    
    def _write_separator(self):
        self.logfile.write('\n\n')
       
    def _write_files(self, files):
        if not isinstance(files, list):
            files = [files]

        if self._used:
            self._write_separator()
        self._used = True
        
        for filename in files:
            self.logfile.write('%s:\n' % filename)
            try:
                with open(filename, 'r') as f: 
                    self.logfile.writelines(f)
                    self.logfile.write('\n')
            except (IOError) as e:
                self.logfile.write("Exception while opening: %s\n" % e)
                continue
    
    def _run_command(self, command):
        if self._used:
            self._write_separator()
        self._used = True
        
        if isinstance(command, basestring):
            command = shlex.split(command)
        
        proc = subprocess.Popen(command, stdout=subprocess.PIPE, 
                                stderr=subprocess.PIPE)
        (out, err) = proc.communicate()
        self.logfile.write('STDOUT:\n%s\n' % out)
        self.logfile.write('STDERR:\n%s\n' % err)
        self.logfile.write('RETURN CODE: %s\n' % proc.returncode)
    
    def getlog(self):
        """Create log and write it to a file object 
        recieved in the constructor."""
        self._action()
        self._write_separator()
    
    def _action(self):
        raise NotImplementedError()



class AnacondaLogMiner(LogMinerBaseClass):
    """Class represents way to get Anaconda dump."""
    
    _name = "anaconda_log"
    _description = "Log dumped from Anaconda."
    _filename = "anaconda-dump"
    _prefer_separate_file = True

    def _action(self):
        # Actual state of /tmp
        old_state = set(os.listdir('/tmp'))
        
        # Tell Anaconda to dump itself
        try:
            anaconda_pid = open('/var/run/anaconda.pid').read().strip()
        except (IOError):
            raise LogMinerError("Anaconda pid file doesn't exists")
        
        proc = subprocess.Popen(shlex.split("kill -s USR2 %s" % anaconda_pid), 
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        proc.communicate()
        if proc.returncode:
            raise LogMinerError('Error while sending signal to Anaconda')
        
        time.sleep(5)
        
        # Check if new traceback file exists
        new_state = set(os.listdir('/tmp'))
        tbpfiles = list(new_state - old_state)
        
        if not len(tbpfiles):
            raise LogMinerError('Error: No anaconda traceback file exist')
            
        for file in tbpfiles:
            if file.startswith('anaconda-tb-'):
                tbpfile_name = file
                break
        else:
            raise LogMinerError('Error: No anaconda traceback file exist')
        
        # Copy anaconda traceback log
        self._write_files('/tmp/%s' % tbpfile_name)



class FileSystemLogMiner(LogMinerBaseClass):
    """Class represents way to get image of filesystem structure."""

    _name = "filesystem"
    _description = "Image of disc structure."
    _filename = "filesystem"
    _prefer_separate_file = True

    FSTREE_FORMAT = "%1s %6s%1s %s" # Format example: "d 1023.9K somedir"
    DADPOINT = 1                    # Number of Digits After the Decimal POINT

    def _action(self):
        self._get_tree_structure()

    def _size_conversion(self, size):
        """Converts bytes into KB, MB or GB"""
        if size >= 1073741824:  # Gigabytes
            size = round(size / 1073741824.0, self.DADPOINT)
            unit = "G"
        elif size >= 1048576:   # Megabytes
            size = round(size / 1048576.0, self.DADPOINT)
            unit = "M"
        elif size >= 1024:      # Kilobytes
            size = round(size / 1024.0, self.DADPOINT)
            unit = "K"
        else:
            size = size
            unit = ""
        return size, unit
    
    
    def _get_tree_structure(self, human_readable=True):
        """Creates filesystem structure image."""
        white_list = ['/sys']
        
        logfile = self.logfile
        
        for path, dirs, files in os.walk('/'):
            line = "\n%s:" % (path)
            logfile.write('%s\n' % line)
            
            # List dirs
            dirs.sort()
            for directory in dirs:
                fullpath = os.path.join(path, directory)
                size = os.path.getsize(fullpath)
                unit = ""
                if human_readable:
                    size, unit = self._size_conversion(size)
                line = self.FSTREE_FORMAT % ("d", size, unit, directory)
                logfile.write('%s\n' % line)
            
            # Skip mounted directories
            original_dirs = dirs[:]
            for directory in original_dirs:
                dirpath = os.path.join(path, directory)
                if os.path.ismount(dirpath) and not dirpath in white_list:
                    dirs.remove(directory)
            
            # List files
            files.sort()
            for filename in files:               
                fullpath = os.path.join(path, filename)
                if os.path.islink(fullpath):
                    line = self.FSTREE_FORMAT % ("l", "0", "", filename)
                    line += " -> %s" % os.path.realpath(fullpath)
                    if not os.path.isfile(fullpath):
                        # Broken symlink
                        line += " (Broken)"
                else:
                    stat_res = os.stat(fullpath)[stat.ST_MODE]
                    if stat.S_ISREG(stat_res):
                        filetype = "-"
                    elif stat.S_ISCHR(stat_res):
                        filetype = "c"
                    elif stat.S_ISBLK(stat_res):
                        filetype = "b"
                    elif stat.S_ISFIFO(stat_res):
                        filetype = "p"
                    elif stat.S_ISSOCK(stat_res):
                        filetype = "s"
                    else:
                        filetype = "-"
                    
                    size = os.path.getsize(fullpath)
                    unit = ""
                    if human_readable:
                        size, unit = self._size_conversion(size)
                    line = self.FSTREE_FORMAT % (filetype, size, unit, filename)
                logfile.write('%s\n' % line)



class DmSetupLsLogMiner(LogMinerBaseClass):
    """Class represents way to get 'dmsetup ls --tree' output."""

    _name = "dmsetup ls"
    _description = "Output from \"dmsetup ls --tree\"."
    _filename = "dmsetup-ls"
    _prefer_separate_file = True

    def _action(self):
        self._run_command("dmsetup ls --tree")


class DmSetupInfoLogMiner(LogMinerBaseClass):
    """Class represents way to get 'dmsetup info' output."""

    _name = "dmsetup info"
    _description = "Output from \"dmsetup info -c\"."
    _filename = "dmsetup-info"
    _prefer_separate_file = True

    def _action(self):
        self._run_command("dmsetup info -c")


ALL_MINERS = [AnacondaLogMiner(),
              FileSystemLogMiner(),
              DmSetupLsLogMiner(),
              DmSetupInfoLogMiner(),
             ]