# Buggy code
def check(self, instance):
    service_check_msg = None
    offset_threshold = instance.get("offset_threshold", DEFAULT_OFFSET_THRESHOLD)
    custom_tags = instance.get("tags", [])
    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)
        )

    req_args = NTPUtil().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, tags=custom_tags)

        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,
        tags=custom_tags,
    )


# Traceback (gaierror)
2018-05-17 11:30:31 UTC | INFO | dd.collector | config(config.py:1249) | initialized checks.d checks: ['kube_dns', 'network', 'kubernetes', 'ntp', 'docker_daemon', 'http_check', 'system_core', 'redisdb', 'disk', 'kube_proxy']
2018-05-17 11:30:31 UTC | INFO | dd.collector | config(config.py:1250) | initialization failed checks.d checks: []
2018-05-17 11:30:31 UTC | INFO | dd.collector | collector(agent.py:166) | Check reload was successful. Running 11 checks.
2018-05-17 11:30:35 UTC | ERROR | dd.collector | checks.ntp(__init__.py:829) | Check 'ntp' instance #0 failed
Traceback (most recent call last):
File "/opt/datadog-agent/agent/checks/__init__.py", line 812, in run
self.check(copy.deepcopy(instance))
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/ntp/ntp.py", line 33, in check
ntp_stats = ntplib.NTPClient().request(**req_args)
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/ntplib.py", line 292, in request
addrinfo = socket.getaddrinfo(host, port)[0]
gaierror: [Errno -8] Servname not supported for ai_socktype

# Fixed code
def check(self, instance):
    service_check_msg = None
    offset_threshold = instance.get("offset_threshold", DEFAULT_OFFSET_THRESHOLD)
    custom_tags = instance.get("tags", [])
    try:
        offset_threshold = int(offset_threshold)
    except (TypeError, ValueError):
        msg = "Must specify an integer value for offset_threshold. Configured value is {}".format(
            offset_threshold
        )
        raise Exception(msg)

    req_args = {
        "host": instance.get("host", DEFAULT_HOST),
        "port": self._get_service_port(instance),
        "version": int(instance.get("version", DEFAULT_VERSION)),
        "timeout": float(instance.get("timeout", DEFAULT_TIMEOUT)),
    }

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

    try:
        ntp_stats = ntplib.NTPClient().request(**req_args)
    except ntplib.NTPException:
        self.log.debug("Could not connect to NTP Server {}".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, tags=custom_tags)

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

    self.service_check(
        "ntp.in_sync",
        status,
        timestamp=ntp_ts,
        message=service_check_msg,
        tags=custom_tags,
    )