示例#1
0
def get_ntp_info():
    req_args = get_ntp_args()
    ntp_offset = ntplib.NTPClient().request(**req_args).offset
    if abs(ntp_offset) > NTP_OFFSET_THRESHOLD:
        ntp_styles = ['red', 'bold']
    else:
        ntp_styles = []
    return ntp_offset, ntp_styles
示例#2
0
def get_ntp_info():
    req_args = get_ntp_args()
    ntp_offset = ntplib.NTPClient().request(**req_args).offset
    if abs(ntp_offset) > NTP_OFFSET_THRESHOLD:
        ntp_styles = ['red', 'bold']
    else:
        ntp_styles = []
    return ntp_offset, ntp_styles
示例#3
0
def get_ntp_info():
    set_user_ntp_settings()
    req_args = get_ntp_args()
    ntp_offset = ntplib.NTPClient().request(**req_args).offset
    if abs(ntp_offset) > NTP_OFFSET_THRESHOLD:
        ntp_styles = ["red", "bold"]
    else:
        ntp_styles = []
    return ntp_offset, ntp_styles
示例#4
0
    def test_ntp_global_settings(self):
        config = {'instances': [{
            "host": "foo.com",
            "port": "bar",
            "version": 42,
            "timeout": 13.37}],
            'init_config': {}}

        agentConfig = {
            'version': '0.1',
            'api_key': 'toto'
        }

        # default min collection interval for that check was 20sec
        check = load_check('ntp', config, agentConfig)
        check.run()

        ntp_args = get_ntp_args()

        self.assertEqual(ntp_args["host"], "foo.com")
        self.assertEqual(ntp_args["port"], "bar")
        self.assertEqual(ntp_args["version"], 42)
        self.assertEqual(ntp_args["timeout"], 13.37)

        config = {'instances': [{}], 'init_config': {}}

        agentConfig = {
            'version': '0.1',
            'api_key': 'toto'
        }

        # default min collection interval for that check was 20sec
        check = load_check('ntp', config, agentConfig)
        try:
            check.run()
        except Exception:
            pass

        ntp_args = get_ntp_args()

        self.assertTrue(ntp_args["host"].endswith("datadog.pool.ntp.org"))
        self.assertEqual(ntp_args["port"], "ntp")
        self.assertEqual(ntp_args["version"], 3)
        self.assertEqual(ntp_args["timeout"], 1.0)
示例#5
0
    def test_ntp_global_settings(self):
        config = {'instances': [{
            "host": "foo.com",
            "port": "bar",
            "version": 42,
            "timeout": 13.37}],
            'init_config': {}}

        agentConfig = {
            'version': '0.1',
            'api_key': 'toto'
        }

        # default min collection interval for that check was 20sec
        check = load_check('ntp', config, agentConfig)
        check.run()

        ntp_args = get_ntp_args()

        self.assertEqual(ntp_args["host"], "foo.com")
        self.assertEqual(ntp_args["port"], "bar")
        self.assertEqual(ntp_args["version"], 42)
        self.assertEqual(ntp_args["timeout"], 13.37)

        config = {'instances': [{}], 'init_config': {}}

        agentConfig = {
            'version': '0.1',
            'api_key': 'toto'
        }

        # default min collection interval for that check was 20sec
        check = load_check('ntp', config, agentConfig)
        try:
            check.run()
        except Exception:
            pass

        ntp_args = get_ntp_args()

        self.assertTrue(ntp_args["host"].endswith("datadog.pool.ntp.org"))
        self.assertEqual(ntp_args["port"], "ntp")
        self.assertEqual(ntp_args["version"], 3)
        self.assertEqual(ntp_args["timeout"], 1.0)
示例#6
0
文件: ntp.py 项目: itorres/sd-agent
    def check(self, instance):
        service_check_msg = None
        offset_threshold = instance.get('offset_threshold',
                                        DEFAULT_OFFSET_THRESHOLD)
        try:
            offset_threshold = int(offset_threshold)
        except (TypeError, ValueError):
            raise Exception(
                'Must specify an integer value for offset_threshold. Configured value is %s'
                % repr(offset_threshold))

        set_user_ntp_settings(dict(instance))

        req_args = get_ntp_args()

        self.log.debug("Using ntp host: {0}".format(req_args['host']))

        try:
            ntp_stats = ntplib.NTPClient().request(**req_args)
        except ntplib.NTPException:
            self.log.debug("Could not connect to NTP Server {0}".format(
                req_args['host']))
            status = AgentCheck.UNKNOWN
            ntp_ts = None
        else:
            ntp_offset = ntp_stats.offset

            # Use the ntp server's timestamp for the time of the result in
            # case the agent host's clock is messed up.
            ntp_ts = ntp_stats.recv_time
            self.gauge('ntp.offset', ntp_offset, timestamp=ntp_ts)

            if abs(ntp_offset) > offset_threshold:
                status = AgentCheck.CRITICAL
                service_check_msg = "Offset {0} secs higher than offset threshold ({1} secs)".format(
                    ntp_offset, offset_threshold)
            else:
                status = AgentCheck.OK

        self.service_check('ntp.in_sync',
                           status,
                           timestamp=ntp_ts,
                           message=service_check_msg)
示例#7
0
    def check(self, instance):

        offset_threshold = instance.get('offset_threshold',
                                        DEFAULT_OFFSET_THRESHOLD)
        try:
            offset_threshold = int(offset_threshold)
        except (TypeError, ValueError):
            raise Exception(
                'Must specify an integer value for offset_threshold. Configured value is %s'
                % repr(offset_threshold))

        set_user_ntp_settings(dict(instance))

        req_args = get_ntp_args()

        self.log.debug("Using ntp host: {0}".format(req_args['host']))

        try:
            ntp_stats = ntplib.NTPClient().request(**req_args)
        except ntplib.NTPException:
            self.log.debug("Could not connect to NTP Server {0}".format(
                req_args['host']))
            ntp_offset = self.get_server_time()
            self.log.info("ntplib ntpexcption error:{}".format(ntp_offset))
            if ntp_offset:
                self.update_check(ntp_offset, time.time(), offset_threshold)

        except socket.gaierror as e:
            ntp_offset = self.get_server_time()

            self.log.info("socker error:{}".format(ntp_offset))
            if ntp_offset:
                self.update_check(ntp_offset, time.time(), offset_threshold)

        else:
            ntp_offset = ntp_stats.offset

            # Use the ntp server's timestamp for the time of the result in
            # case the agent host's clock is messed up.
            ntp_ts = ntp_stats.recv_time

            self.update_check(ntp_offset, ntp_ts, offset_threshold)
示例#8
0
    def check(self, instance):
        service_check_msg = None
        offset_threshold = instance.get('offset_threshold', DEFAULT_OFFSET_THRESHOLD)
        try:
            offset_threshold = int(offset_threshold)
        except (TypeError, ValueError):
            raise Exception('Must specify an integer value for offset_threshold. Configured value is %s' % repr(offset_threshold))

        set_user_ntp_settings(dict(instance))

        req_args = get_ntp_args()

        self.log.debug("Using ntp host: {0}".format(req_args['host']))

        try:
            ntp_stats = ntplib.NTPClient().request(**req_args)
        except ntplib.NTPException:
            self.log.debug("Could not connect to NTP Server {0}".format(
                req_args['host']))
            status = AgentCheck.UNKNOWN
            ntp_ts = None
        else:
            ntp_offset = ntp_stats.offset

            # Use the ntp server's timestamp for the time of the result in
            # case the agent host's clock is messed up.
            ntp_ts = ntp_stats.recv_time
            self.gauge('ntp.offset', ntp_offset, timestamp=ntp_ts)

            if abs(ntp_offset) > offset_threshold:
                status = AgentCheck.CRITICAL
                service_check_msg = "Offset {0} secs higher than offset threshold ({1} secs)".format(ntp_offset, offset_threshold)
            else:
                status = AgentCheck.OK

        self.service_check('ntp.in_sync', status, timestamp=ntp_ts, message=service_check_msg)