class SyslogFormat:
    def __init__(self):
        self.System = Sys()

    def Message(self, system_ip, module, msg_type, msg_level, message):
        epoch = time.time()

        date = self.Date()
        timestamp = self.System.FormatTime(epoch)
        msg_type = self.ReturnType(msg_type)
        msg_level = self.ReturnLevel(msg_level)

        #add time offset??
        #20140624|19:08:15|EVENT|DNSProxy:Informational|192.168.83.1|src.mac={}; src.ip={}; domain={}; category={}; filter={}; action={}
        message = f'{date}|{timestamp}|{msg_type}|{module}:{msg_level}|{system_ip}|{message}'

        return message

    def ReturnType(self, msg_type):
        # T3 = System Daemons | T14 = Event/Log Alert
        if (msg_type == 3):
            msg_type = 'SYSTEM'
        elif (msg_type == 14):
            msg_type = 'EVENT'

        return msg_type

    def Date(self):
        d = self.System.Date()
        date = f'{d[0]}{d[1]}{d[2]}'

        return date

    def ReturnLevel(self, level):
        log_levels = {
            0: 'Emergency',  # system is unusable
            1: 'Alert',  #action must be taken immediately
            2: 'Critical',  # critical conditions
            3: 'Error',  # error conditions
            4: 'Warning',  # warning conditions
            5: 'Notice',  # normal but significant condition
            6: 'Informational',  # informational messages
            7: 'Debug',  # debug-level messages
        }

        return log_levels[level]
Exemple #2
0
class LogHandler:
    def __init__(self, process, module=None):
        self.process = process
        self.System = Sys()

        self.log_queue = deque()

        if (module):
            self.module = module

    async def Settings(self, module):
        print('[+] Starting: Log Settings Update Thread.')
        self.module = module
        while True:
            with open(f'{HOME_DIR}/data/config.json', 'r') as settings:
                setting = json.load(settings)

            self.process.logging_level = setting['settings']['logging'][
                'level']

            await asyncio.sleep(SETTINGS_TIMER)

    ## this is the message input for threadsafe/sequential modules
    def Message(self, message):
        timestamp = time.time()
        timestamp = self.System.FormatTime(timestamp)
        d = self.System.Date()
        with open(
                f'{HOME_DIR}/dnx_system/log/{self.module}/{d[0]}{d[1]}{d[2]}-{self.module}.log',
                'a+') as Log:
            Log.write(f'{timestamp}: {message}\n')

        ## make sure this works. should be fine, but front end might do something weird to chmod???
        user_id = os.geteuid()
        if (user_id == 0):
            file_path = f'{HOME_DIR}/dnx_system/log/{self.module}/{d[0]}{d[1]}{d[2]}-{self.module}.log'
            shutil.chown(file_path, user=USER, group=GROUP)
            os.chmod(file_path, 0o660)

            ## REPLACED THESE WITH CODE ABOVE. REMOVE AFTER TESTING AND VALIDATING CHANGES WORK
#            run(f'chown dnx:dnx {file_path}', shell=True)
#            run(f'chmod 660 {file_path}', shell=True)

    def AddtoQueue(self, message, log_queue_lock):
        timestamp = time.time()
        with log_queue_lock:
            self.log_queue.append((timestamp, message))

    ## This is the message handler for ensure thread safety in multi threaded or asynchronous tasks
    async def QueueHandler(self, log_queue_lock):
        while True:
            d = self.System.Date()
            if (not self.log_queue):
                # waiting 1 second before checking queue again for idle perf
                await asyncio.sleep(SHORT_POLL)
                continue

            with open(
                    f'{HOME_DIR}/dnx_system/log/{self.module}/{d[0]}{d[1]}{d[2]}-{self.module}.log',
                    'a+') as Log:
                with log_queue_lock:
                    while self.log_queue:
                        full_message = self.log_queue.popleft()
                        timestamp = full_message[0]
                        message = full_message[1]

                        Log.write(f'{timestamp}: {message}\n')

            user_id = os.geteuid()
            if (user_id == 0):
                file_path = f'{HOME_DIR}/dnx_system/log/{self.module}/{d[0]}{d[1]}{d[2]}-{self.module}.log'
                shutil.chown(file_path, user=USER, group=GROUP)
                os.chmod(file_path, 0o660)