Beispiel #1
0
 def setup(self):
     self.lm = LogMonitor(
         self.LOG_FILE,
         self.CACHED_PATH,
         self.WARNING_PATTERN,
         self.CRITICAL_PATTERN,
         self.OK_PATTERN,
         self.ROTATION_PATTERN,
     )
     self.lm_no_ok_pattern = LogMonitor(
         self.LOG_FILE,
         self.CACHED_PATH,
         self.WARNING_PATTERN,
         self.CRITICAL_PATTERN,
         None,
         self.ROTATION_PATTERN,
     )
     self.lm_bdcoe = LogMonitor(
         log_filename=None,
         cached_path=self.CACHED_PATH,
         warning_pattern=None,
         critical_pattern=
         "error|err|Err|Error|ERROR|FAIL|fail|Fail|Failure|FAILURE|failure",
         ok_pattern=None,
         rotation_pattern="cssng_ss_radiobasestation[0-9]{8}\.log",
         log_prefix="%s/cssng_ss_radiobasestation*" % self.CURR_PATH)
Beispiel #2
0
 def setup(self):
     self.lm = LogMonitor(
         self.LOG_FILE, self.CACHED_PATH,
         self.WARNING_PATTERN, self.CRITICAL_PATTERN,
         self.OK_PATTERN, self.ROTATION_PATTERN,
     )
     self.lm_no_ok_pattern = LogMonitor(
         self.LOG_FILE, self.CACHED_PATH,
         self.WARNING_PATTERN, self.CRITICAL_PATTERN,
         None, self.ROTATION_PATTERN,
     )
Beispiel #3
0
    def _test_get_file_type(self, compression_type):
        log_fh = self._setup_log()
        self._inject_error(log_fh)
        self._inject_ok(log_fh)
        log_fh.close()

        rotated_log_filename = self._rotate_log(compress_type=compression_type)
        assert LogMonitor.get_file_type(rotated_log_filename) == compression_type
Beispiel #4
0
    def test_curr_log_filename(self):
        # no log_filename, with rotation_pattern
        file_tmpl = "test_monitor-%s.log"
        dt = datetime(2014, 12, 1)
        delta = timedelta(days=1)

        file_lst = [
            file_tmpl % (dt + (delta * x)).strftime("%Y%m%d")
            for x in xrange(4)
        ]

        for f in file_lst:
            fh = open(f, "w+")
            fh.close()
            time.sleep(1)

        lm = LogMonitor(None, self.CACHED_PATH, self.WARNING_PATTERN,
                        self.CRITICAL_PATTERN, self.OK_PATTERN,
                        self.ROTATION_PATTERN,
                        "%s/%s" % (self.CURR_PATH, self.ROTATION_PATTERN))
        print lm.curr_log_filename
        assert lm.curr_log_filename == "%s/%s" % (self.CURR_PATH, file_lst[-1])
Beispiel #5
0
class TestLogMonitoring(object):
    """
    cd into this directory and run

    To Test everything:
    nosetests -s test_log_monitoring.py

    To run a single test:
    nosetests -s test_log_monitoring:TestLogMonitoring.<method_name>
    Example:
    nosetests -s test_log_monitoring:TestLogMonitoring.test_log__empty_log_no_cached
    """

    CURR_PATH = os.path.dirname(os.path.realpath('__file__'))
    LOG_FILE = "%s/test_monitor.log" % CURR_PATH
    CACHED_PATH = CURR_PATH
    WARNING_PATTERN = "^WARN.*$"
    CRITICAL_PATTERN = "^FATAL.*$"
    OK_PATTERN = "^SUCCESS.*$"
    ROTATION_PATTERN = "test_monitor*"

    def _setup_log(self):
        if os.path.isfile("test_monitor.log"):
            fh = open("test_monitor.log", "a")
        else:
            fh = open("test_monitor.log", "w")
        return fh

    def _inject_innocuous_line(self, fh):
        fh.write("foo bar baz..\n")

    def _inject_error(self, fh):
        fh.write("FATAL - %s" % "this is a fatal error message.\n")

    def _inject_warn(self, fh):
        fh.write("WARN - %s" % "this is a warning message.\n")

    def _inject_ok(self, fh):
        fh.write("SUCCESS - %s" % "yay\n")

    def setup(self):
        self.lm = LogMonitor(
            self.LOG_FILE,
            self.CACHED_PATH,
            self.WARNING_PATTERN,
            self.CRITICAL_PATTERN,
            self.OK_PATTERN,
            self.ROTATION_PATTERN,
        )
        self.lm_no_ok_pattern = LogMonitor(
            self.LOG_FILE,
            self.CACHED_PATH,
            self.WARNING_PATTERN,
            self.CRITICAL_PATTERN,
            None,
            self.ROTATION_PATTERN,
        )
        self.lm_bdcoe = LogMonitor(
            log_filename=None,
            cached_path=self.CACHED_PATH,
            warning_pattern=None,
            critical_pattern=
            "error|err|Err|Error|ERROR|FAIL|fail|Fail|Failure|FAILURE|failure",
            ok_pattern=None,
            rotation_pattern="cssng_ss_radiobasestation[0-9]{8}\.log",
            log_prefix="%s/cssng_ss_radiobasestation*" % self.CURR_PATH)

    def teardown(self):
        log_file_lst = self._gen_log_file_lst('all')
        for f in log_file_lst:
            if os.path.isfile(f):
                os.remove(f)

        for f in glob.glob("*.log"):
            os.remove(f)

        if os.path.isfile(self.lm_no_ok_pattern.cached_filename):
            os.remove(self.lm_no_ok_pattern.cached_filename)

        if os.path.isfile(self.lm_no_ok_pattern.cached_filename):
            os.remove(self.lm.cached_filename)

        if os.path.isfile(self.lm_bdcoe.cached_filename):
            os.remove(self.lm_bdcoe.cached_filename)

    def test_log__empty_log_no_cached(self):
        log_fh = self._setup_log()
        log_fh.close()
        status_code = self.lm._run_impl()

        # no cached file should be created
        assert os.path.isfile(
            self.lm.cached_filename
        ) == True, "cached file shouldn't have been created."
        assert status_code == 0, "Encountered an empty log file. Status code should be 0."

    def test_log__error_no_cached(self):
        log_fh = self._setup_log()
        self._inject_error(log_fh)
        log_fh.close()
        status_code = self.lm._run_impl()

        # a cached file should be created
        assert os.path.isfile(self.lm.cached_filename
                              ) == True, "cached file should've been created."
        assert status_code == 2, "Encountered an error in the log file. Status code should be 2."

    def test_log__warn_no_cached(self):
        log_fh = self._setup_log()
        self._inject_warn(log_fh)
        log_fh.close()
        status_code = self.lm._run_impl()

        # a cached file should be created
        assert os.path.isfile(self.lm.cached_filename
                              ) == True, "cached file should've been created."
        assert status_code == 1, "Encountered a warning in the log file. Status code should be 1."

    def test_log__error_with_cached(self):
        log_fh = self._setup_log()
        self._inject_error(log_fh)
        log_fh.close()

        status_code = self.lm._run_impl()
        assert os.path.isfile(self.lm.cached_filename
                              ) == True, "cached file should've been created."
        assert status_code == 2, "Encountered an error in the log file. Status code should be 2."

        # run monitoring again.
        status_code = self.lm._run_impl()
        assert os.path.isfile(self.lm.cached_filename
                              ) == True, "cached file should've been created."
        assert status_code == 2, "Status code should remain as 2."

    def test_log__warning_with_cached(self):
        log_fh = self._setup_log()
        self._inject_warn(log_fh)
        log_fh.close()

        status_code = self.lm._run_impl()
        assert os.path.isfile(self.lm.cached_filename
                              ) == True, "cached file should've been created."
        assert status_code == 1, "Encountered a warning in the log file. Status code should be 1."

        # run monitoring again.
        status_code = self.lm._run_impl()
        assert os.path.isfile(self.lm.cached_filename
                              ) == True, "cached file should've been created."
        assert status_code == 1, "Status code should remain as 1."

    def test_log__error_with_cached_with_ok(self):
        # intermitten checks
        log_fh = self._setup_log()
        self._inject_error(log_fh)
        log_fh.close()

        status_code = self.lm._run_impl()
        assert os.path.isfile(self.lm.cached_filename
                              ) == True, "cached file should've been created."
        assert status_code == 2, "Encountered an error in the log file. Status code should be 2."

        log_fh = self._setup_log()
        self._inject_ok(log_fh)
        log_fh.close()
        status_code = self.lm._run_impl()

        assert os.path.isfile(self.lm.cached_filename
                              ) == True, "cached file should've been created."
        assert status_code == 0, "The OK statement should've cleared the error status code."

    def test_log__error_with_cached_with_ok2(self):
        # check all in one go.
        log_fh = self._setup_log()
        self._inject_error(log_fh)
        self._inject_error(log_fh)
        self._inject_ok(log_fh)
        log_fh.close()

        status_code = self.lm._run_impl()
        assert os.path.isfile(self.lm.cached_filename
                              ) == True, "cached file should've been created."
        assert status_code == 0, "The OK statement should've cleared the error status code."

    def test_log_without_ok_pattern(self):
        log_fh = self._setup_log()
        self._inject_error(log_fh)
        log_fh.close()
        status_code = self.lm_no_ok_pattern._run_impl()

        assert status_code == 2, "Encounterd an error in the log file. Status code should be 2."
        assert os.path.isfile(self.lm_no_ok_pattern.cached_filename
                              ) == True, "cached file should've been created."

        log_fh = self._setup_log()
        self._inject_innocuous_line(log_fh)
        self._inject_innocuous_line(log_fh)
        log_fh.close()
        status_code = self.lm_no_ok_pattern._run_impl()

        assert status_code == 0, "OK_pattern is omitted. Shouldn't fire off old errors."
        assert os.path.isfile(self.lm_no_ok_pattern.cached_filename
                              ) == True, "cached file should've been created."

        log_fh = self._setup_log()
        self._inject_error(log_fh)
        log_fh.close()
        status_code = self.lm_no_ok_pattern._run_impl()
        assert status_code == 2, "Encounterd an error in the log file again. Status code should be 2."
        assert os.path.isfile(self.lm_no_ok_pattern.cached_filename
                              ) == True, "cached file should've been created."

    def test_missing_log_file(self):
        cached_fh = open(self.lm.cached_filename, "w+")
        json_dict = {
            "offset": 0,
            "checksum": "DEADBEEF",
        }
        cached_fh.write(json.dumps(json_dict))  #dummy json
        cached_fh.close()

        try:
            self.lm._run_impl()
        except LogMissingException:
            assert True, "should be throw a LogMissingException."
        except Exception, e:
            assert False, "should be throw a LogMissingException, but got: %s" % e.__class__