summaryrefslogtreecommitdiffstats
path: root/src/tools/analyzer/source_files.py
blob: 0cadf99f035f48f3e13a07b4e168eaf6d0dc23a8 (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
import glob
import logging

from sssd.source_reader import Reader

logger = logging.getLogger()


class Files(Reader):
    """
    A class used to represent a Log Files Reader

    Args:
        path -- the path where SSSD logs are to
           be read (default /var/log/sssd/)
    """

    def __init__(self, path):
        super().__init__()
        self.log_files = []
        self.path = self.resolve_path(path)
        self.domains = self.get_domain_logfiles()

    def __iter__(self):
        """
        Yields:
            str: The next line in the log file
        """
        for files in self.log_files:
            try:
                with open(files) as file:
                    for line in file:
                        yield line
            except FileNotFoundError as err:
                logger.warning("Could not find domain log file, skipping")
                logger.warning(err)
                continue

    def resolve_path(self, path):
        if path.endswith("/"):
            return path
        else:
            return path + "/"

    def get_domain_logfiles(self, child=False):
        """ Retrieve list of SSSD log files, exclude rotated (.gz) files """
        domain_files = []
        exclude_list = ["ifp", "nss", "pam", "sudo", "autofs",
                        "ssh", "pac", "kcm", ".gz"]
        if child:
            file_list = glob.glob(self.path + "*.log")
        else:
            file_list = glob.glob(self.path + "sssd_*")
        for file in file_list:
            if not any(s in file for s in exclude_list):
                domain_files.append(file)

        return domain_files

    def set_component(self, component, child):
        """
        Switch the reader to interact with a certain SSSD component
        NSS, PAM, BE
        """
        self.log_files = []
        if component == self.Component.NSS:
            self.log_files.append(self.path + "sssd_nss.log")
        elif component == self.Component.PAM:
            self.log_files.append(self.path + "sssd_pam.log")
        elif component == self.Component.BE:
            domains = self.get_domain_logfiles(child)
            if not domains:
                raise IOError
            # error: No domains found?
            for dom in domains:
                self.log_files.append(dom)