<commit_before># (C) Datadog, Inc. 2010-present
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import socket
from contextlib import closing

from datadog_checks.base import AgentCheck, ConfigurationError
from datadog_checks.base.errors import CheckException
from datadog_checks.base.utils.time import get_precise_time


class TCPCheck(AgentCheck):

    SOURCE_TYPE_NAME = 'system'
    SERVICE_CHECK_NAME = 'tcp.can_connect'
    CONFIGURATION_ERROR_MSG = "`{}` is an invalid `{}`; a {} must be specified."
    DEFAULT_IP_CACHE_DURATION = None

    def __init__(self, name, init_config, instances):
        super(TCPCheck, self).__init__(name, init_config, instances)
        instance = self.instances[0]

        self.instance_name = self.normalize_tag(instance['name'])
        self.timeout = float(instance.get('timeout', 10))
        self.collect_response_time = instance.get('collect_response_time', False)
        self.host = instance.get('host', None)
        self.socket_type = None
        self._addr = None
        self.ip_cache_last_ts = 0
        self.ip_cache_duration = self.DEFAULT_IP_CACHE_DURATION

        ip_cache_duration = instance.get('ip_cache_duration', None)
        if ip_cache_duration is not None:
            try:
                self.ip_cache_duration = int(ip_cache_duration)
            except Exception:
                raise ConfigurationError(
                    self.CONFIGURATION_ERROR_MSG.format(ip_cache_duration, 'ip_cache_duration', 'number')
                )

        port = instance.get('port', None)
        try:
            self.port = int(port)
        except Exception:
            raise ConfigurationError(self.CONFIGURATION_ERROR_MSG.format(port, 'port', 'number'))
        try:
            split_url = self.host.split(":")
        except Exception:  # Would be raised if url is not a string
            raise ConfigurationError(self.CONFIGURATION_ERROR_MSG.format(self.host, 'url', 'string'))

        custom_tags = instance.get('tags', [])
        self.tags = [
            'url:{}:{}'.format(self.host, self.port),
            'instance:{}'.format(instance.get('name')),
        ] + custom_tags

        self.service_check_tags = custom_tags + [
            'target_host:{}'.format(self.host),
            'port:{}'.format(self.port),
            'instance:{}'.format(self.instance_name),
        ]

        # IPv6 address format: 2001:db8:85a3:8d3:1319:8a2e:370:7348
        if len(split_url) == 8:  # It may then be a IP V6 address, we check that
            for block in split_url:
                if len(block) != 4:
                    raise ConfigurationError(
                        self.CONFIGURATION_ERROR_MSG.format(self.host, 'IPv6 address', 'valid address')
                    )
            # It's a correct IP V6 address
            self._addr = self.host
            self.socket_type = socket.AF_INET6
        else:
            self.socket_type = socket.AF_INET
            # IP will be resolved at check time

    @property
    def addr(self):
        if self._addr is None:
            try:
                self.resolve_ip()
            except Exception as e:
                self.log.debug(str(e))
                msg = "URL: {} could not be resolved".format(self.host)
                raise CheckException(msg)
        return self._addr

    def resolve_ip(self):
        self._addr = socket.gethostbyname(self.host)
        self.log.debug("%s resolved to %s", self.host, self._addr)

    def should_resolve_ip(self):
        if self.ip_cache_duration is None:
            return False
        return get_precise_time() - self.ip_cache_last_ts > self.ip_cache_duration

    def connect(self):
        with closing(socket.socket(self.socket_type)) as sock:
            sock.settimeout(self.timeout)
            start = get_precise_time()
            sock.connect((self.addr, self.port))
            response_time = get_precise_time() - start
            return response_time

    def check(self, _):
        start = get_precise_time()  # Avoid initialisation warning

        if self.should_resolve_ip():
            self.resolve_ip()
            self.ip_cache_last_ts = start
<commit_msg>Elevate log level for failed connections (#9965)<commit_after># (C) Datadog, Inc. 2010-present
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import socket
from contextlib import closing

from datadog_checks.base import AgentCheck, ConfigurationError
from datadog_checks.base.errors import CheckException
from datadog_checks.base.utils.time import get_precise_time


class TCPCheck(AgentCheck):

    SOURCE_TYPE_NAME = 'system'
    SERVICE_CHECK_NAME = 'tcp.can_connect'
    CONFIGURATION_ERROR_MSG = "`{}` is an invalid `{}`; a {} must be specified."
    DEFAULT_IP_CACHE_DURATION = None

    def __init__(self, name, init_config, instances):
        super(TCPCheck, self).__init__(name, init_config, instances)
        instance = self.instances[0]

        self.instance_name = self.normalize_tag(instance['name'])
        self.timeout = float(instance.get('timeout', 10))
        self.collect_response_time = instance.get('collect_response_time', False)
        self.host = instance.get('host', None)
        self.socket_type = None
        self._addr = None
        self.ip_cache_last_ts = 0
        self.ip_cache_duration = self.DEFAULT_IP_CACHE_DURATION

        ip_cache_duration = instance.get('ip_cache_duration', None)
        if ip_cache_duration is not None:
            try:
                self.ip_cache_duration = int(ip_cache_duration)
            except Exception:
                raise ConfigurationError(
                    self.CONFIGURATION_ERROR_MSG.format(ip_cache_duration, 'ip_cache_duration', 'number')
                )

        port = instance.get('port', None)
        try:
            self.port = int(port)
        except Exception:
            raise ConfigurationError(self.CONFIGURATION_ERROR_MSG.format(port, 'port', 'number'))
        try:
            split_url = self.host.split(":")
        except Exception:  # Would be raised if url is not a string
            raise ConfigurationError(self.CONFIGURATION_ERROR_MSG.format(self.host, 'url', 'string'))

        custom_tags = instance.get('tags', [])
        self.tags = [
            'url:{}:{}'.format(self.host, self.port),
            'instance:{}'.format(instance.get('name')),
        ] + custom_tags

        self.service_check_tags = custom_tags + [
            'target_host:{}'.format(self.host),
            'port:{}'.format(self.port),
            'instance:{}'.format(self.instance_name),
        ]

        # IPv6 address format: 2001:db8:85a3:8d3:1319:8a2e:370:7348
        if len(split_url) == 8:  # It may then be a IP V6 address, we check that
            for block in split_url:
                if len(block) != 4:
                    raise ConfigurationError(
                        self.CONFIGURATION_ERROR_MSG.format(self.host, 'IPv6 address', 'valid address')
                    )
            # It's a correct IP V6 address
            self._addr = self.host
            self.socket_type = socket.AF_INET6
        else:
            self.socket_type = socket.AF_INET
            # IP will be resolved at check time

    @property
    def addr(self):
        if self._addr is None:
            try:
                self.resolve_ip()
            except Exception as e:
                self.log.error(str(e))
                msg = "URL: {} could not be resolved".format(self.host)
                raise CheckException(msg)
        return self._addr

    def resolve_ip(self):
        self._addr = socket.gethostbyname(self.host)
        self.log.debug("%s resolved to %s", self.host, self._addr)

    def should_resolve_ip(self):
        if self.ip_cache_duration is None:
            return False
        return get_precise_time() - self.ip_cache_last_ts > self.ip_cache_duration

    def connect(self):
        with closing(socket.socket(self.socket_type)) as sock:
            sock.settimeout(self.timeout)
            start = get_precise_time()
            sock.connect((self.addr, self.port))
            response_time = get_precise_time() - start
            return response_time

    def check(self, _):
        start = get_precise_time()  # Avoid initialisation warning

        if self.should_resolve_ip():
            self.resolve_ip()
            self.ip_cache_last_ts = start

{'fraction_non_alphanumeric': 0.09948888624747415, 'fraction_numerical': 0.009509093070248425, 'mean_word_length': 2.265036864571207, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2643698', 'n_tokens_mistral': 2503, 'n_tokens_neox': 2402, 'n_words': 538}