Example #1
0
def main(args):
    if show_help(args):
        return

    # show the command list
    if args['list_commands']:
        print_commands()
        # no need to process further
        return

    # show the filter list
    if args['list_filters']:
        print_filters()
        # no need to process further
        return

    # create a Log instance and parse the log file
    log_file = Log(
        logfile=args['log'],
    )

    # apply the time frame filter
    if args['start'] or args['delta']:
        start = args['start'] or ''
        delta = args['delta'] or ''
        filter_func = filters.filter_time_frame(start, delta)

        log_file = log_file.filter(filter_func)

    # apply all other filters given
    if args['filters']:
        for filter_data in args['filters']:
            arg = filter_data[1] or ''
            filter_func = getattr(filters, 'filter_{0}'.format(filter_data[0]))
            log_file = log_file.filter(
                filter_func(arg),
                reverse=args['negate_filter'],
            )

    # run all commands
    for command in args['commands']:
        string = 'command: {0}'.format(command)
        print(string)
        print('=' * len(string))

        cmd = getattr(log_file, 'cmd_{0}'.format(command))
        result = cmd()
        print(result)

    return log_file  # return the log_file object so that tests can inspect it
    def test_filter_time_frame_start_and_delta(self):
        """Test that if empty strings are passed to filter_time_frame all log
        lines are accepted.
        """
        filter_func = filters.filter_time_frame('29/Jun/2012:15', '30m')

        results = []
        for accept_date in ('09/Dec/2013:10:53:42.33',
                            '19/Jan/2014:12:39:16.63',
                            '29/Jun/2012:15:27:23.66'):
            self.accept_date = accept_date
            raw_line = self._build_test_string()
            log_line = HaproxyLogLine(raw_line)

            results.append(filter_func(log_line))

        self.assertEqual(results, [False, False, True, ])
    def test_filter_time_frame_start_and_delta(self):
        """Test that if empty strings are passed to filter_time_frame all log
        lines are accepted.
        """
        filter_func = filters.filter_time_frame('29/Jun/2012:15', '30m')

        results = []
        for accept_date in ('09/Dec/2013:10:53:42.33',
                            '19/Jan/2014:12:39:16.63',
                            '29/Jun/2012:15:27:23.66'):
            self.accept_date = accept_date
            raw_line = self._build_test_string()
            log_line = Line(raw_line)

            results.append(filter_func(log_line))

        self.assertEqual(results, [False, False, True, ])
Example #4
0
    def test_filter_time_frame_no_limit(self):
        """Test that if empty strings are passed to filter_time_frame all log
        lines are accepted.
        """
        filter_func = filters.filter_time_frame('', '')

        results = []
        for accept_date in (
                '09/Dec/2013:10:53:42.33',
                '19/Jan/2014:12:39:16.63',
                '29/Jun/2012:15:27:23.66',
        ):
            self.accept_date = accept_date
            raw_line = self._build_test_string()
            log_line = Line(raw_line)

            results.append(filter_func(log_line))

        self.assertEqual(results, [True, True, True])