def handle_frame(frame):
    """Handle a SysLog event.

    Looks at the event to decide if it should generate a Nagios
    checkresult.

    """

    logger = logging.getLogger(__name__)

    # Get the hostname and tag, lookup the properties for this pair:
    _hostname = clean_host_name(frame.header.hostname)
    if _hostname is None:
        logger.debug("bad or missing hostname, ignoring message")
        return
    _tag = clean_tag_name(frame.msg.tag)
    if _tag is None:
        logger.debug("bad or missing tag, ignoring message")
        return
    _app = lookup_app(_hostname, _tag)

    # Check if we need to notify Nagios
    if frame.pri.severity <= svc_state_threshold:
        if "last_event" in _app.keys():
            if (_app["last_event"] + svc_submission_interval) > time.time():
                # ignore multiple error events with svc_submission_interval
                # seconds after the last checkresult was sent to Nagios
                return
        logger.debug("Must tell Nagios")
        check_result = CheckResult(checkresult_dir)
        desc = make_desc(_hostname, _tag)
        output = "PID=%s, logged: %s" % (frame.msg.pid, frame.msg.content)
        if frame.pri.severity == syslog.LOG_WARNING:
            ret = 1
        else:
            # for LOG_ERR and worse
            ret = 2
        check_result.service_result(
            _hostname,
            desc,
            return_code=ret,
            output=output,
            check_type=1,
            check_options=0,
            scheduled_check=0,
            reschedule_check=0,
            latency=0.1,
            exited_ok=1)
        check_result.submit()
        _app["last_event"] = time.time()
class TestNagiosCheckResult(unittest.TestCase):

    def _findstr(self, file_name, string):
        """Returns true if string found in file"""
        fh = open(file_name)
        output = fh.read()
        fh.close()
        return string in output

    def setUp(self):
        self.tempdir = mkdtemp()
        self.check_result = CheckResult(self.tempdir)

    def test_header(self):
        """Test if the result header is correct"""
        filename = self.check_result.submit()

        self.assertTrue(self._findstr(
            filename,
            "### Active Check Result File ###\n"
        ))

        self.assertTrue(self._findstr(
            filename,
            "file_time=%s\n" % self.check_result.file_time
        ))

    def test_service(self):
        """Test if the service is created correctly"""
        self.check_result.service_result('testhost', 'test service')
        filename = self.check_result.submit()

        self.assertTrue(self._findstr(
            filename,
            '\nhost_name=testhost\n'
        ))
        self.assertTrue(self._findstr(
            filename,
            '\nservice_description=test service\n'
        ))

    def test_host(self):
        """Test if the host is created correctly"""
        self.check_result.host_result('testhost')
        filename = self.check_result.submit()

        self.assertTrue(self._findstr(
            filename,
            '\nhost_name=testhost\n'
        ))
class TestNagiosCheckResult(unittest.TestCase):

    def _findstr(self, file_name, string):
        """Returns true if string found in file"""
        fh = open(file_name)
        output = fh.read()
        fh.close()
        return string in output

    def setUp(self):
        self.tempdir = mkdtemp()
        self.check_result = CheckResult(self.tempdir)

    def test_header(self):
        """Test if the result header is correct"""
        filename = self.check_result.submit()

        self.assertTrue(self._findstr(
            filename,
            "### Active Check Result File ###\n"
        ))

        self.assertTrue(self._findstr(
            filename,
            "file_time=%s\n" % self.check_result.file_time
        ))

    def test_service(self):
        """Test if the service is created correctly"""
        self.check_result.service_result('testhost', 'test service')
        filename = self.check_result.submit()

        self.assertTrue(self._findstr(
            filename,
            '\nhost_name=testhost\n'
        ))
        self.assertTrue(self._findstr(
            filename,
            '\nservice_description=test service\n'
        ))

    def test_host(self):
        """Test if the host is created correctly"""
        self.check_result.host_result('testhost')
        filename = self.check_result.submit()

        self.assertTrue(self._findstr(
            filename,
            '\nhost_name=testhost\n'
        ))
Beispiel #4
0
def handle_frame(frame):
    """Handle a SysLog event.

    Looks at the event to decide if it should generate a Nagios
    checkresult.

    """

    logger = logging.getLogger(__name__)

    # Get the hostname and tag, lookup the properties for this pair:
    _hostname = clean_host_name(frame.header.hostname)
    if _hostname is None:
        logger.debug("bad or missing hostname, ignoring message")
        return
    _tag = clean_tag_name(frame.msg.tag)
    if _tag is None:
        logger.debug("bad or missing tag, ignoring message")
        return
    _app = lookup_app(_hostname, _tag)

    # Check if we need to notify Nagios
    if frame.pri.severity <= svc_state_threshold:
        if "last_event" in _app.keys():
            if (_app["last_event"] + svc_submission_interval) > time.time():
                # ignore multiple error events with svc_submission_interval
                # seconds after the last checkresult was sent to Nagios
                return
        logger.debug("Must tell Nagios")
        check_result = CheckResult(checkresult_dir)
        desc = make_desc(_hostname, _tag)
        output = "PID=%s, logged: %s" % (frame.msg.pid, frame.msg.content)
        if frame.pri.severity == syslog.LOG_WARNING:
            ret = 1
        else:
            # for LOG_ERR and worse
            ret = 2
        check_result.service_result(_hostname,
                                    desc,
                                    return_code=ret,
                                    output=output,
                                    check_type=1,
                                    check_options=0,
                                    scheduled_check=0,
                                    reschedule_check=0,
                                    latency=0.1,
                                    exited_ok=1)
        check_result.submit()
        _app["last_event"] = time.time()
 def setUp(self):
     self.tempdir = mkdtemp()
     self.check_result = CheckResult(self.tempdir)
Beispiel #6
0
 def setUp(self):
     self.tempdir = mkdtemp()
     self.check_result = CheckResult(self.tempdir)