summaryrefslogtreecommitdiffstats
path: root/collectors/python.d.plugin/portcheck/portcheck.chart.py
blob: 818ac765da4ff54613f1c935a339863c51ce356c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
# Description: simple port check netdata python.d module
# Original Author: ccremer (github.com/ccremer)
# SPDX-License-Identifier: GPL-3.0-or-later

import socket

try:
    from time import monotonic as time
except ImportError:
    from time import time

from bases.FrameworkServices.SimpleService import SimpleService

PORT_LATENCY = 'connect'

PORT_SUCCESS = 'success'
PORT_TIMEOUT = 'timeout'
PORT_FAILED = 'no_connection'

ORDER = ['latency', 'status']

CHARTS = {
    'latency': {
        'options': [None, 'TCP connect latency', 'milliseconds', 'latency', 'portcheck.latency', 'line'],
        'lines': [
            [PORT_LATENCY, 'connect', 'absolute', 100, 1000]
        ]
    },
    'status': {
        'options': [None, 'Portcheck status', 'boolean', 'status', 'portcheck.status', 'line'],
        'lines': [
            [PORT_SUCCESS, 'success', 'absolute'],
            [PORT_TIMEOUT, 'timeout', 'absolute'],
            [PORT_FAILED, 'no connection', 'absolute']
        ]
    }
}


# Not deriving from SocketService, too much is different
class Service(SimpleService):
    def __init__(self, configuration=None, name=None):
        SimpleService.__init__(self, configuration=configuration, name=name)
        self.order = ORDER
        self.definitions = CHARTS
        self.host = self.configuration.get('host')
        self.port = self.configuration.get('port')
        self.timeout = self.configuration.get('timeout', 1)

    def check(self):
        """
        Parse configuration, check if configuration is available, and dynamically create chart lines data
        :return: boolean
        """
        if self.host is None or self.port is None:
            self.error('Host or port missing')
            return False
        if not isinstance(self.port, int):
            self.error('"port" is not an integer. Specify a numerical value, not service name.')
            return False

        self.debug('Enabled portcheck: {host}:{port}, update every {update}s, timeout: {timeout}s'.format(
            host=self.host, port=self.port, update=self.update_every, timeout=self.timeout
        ))
        # We will accept any (valid-ish) configuration, even if initial connection fails (a service might be down from
        # the beginning)
        return True

    def _get_data(self):
        """
        Get data from socket
        :return: dict
        """
        data = dict()
        data[PORT_SUCCESS] = 0
        data[PORT_TIMEOUT] = 0
        data[PORT_FAILED] = 0

        success = False
        try:
            for socket_config in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
                # use first working socket
                sock = self._create_socket(socket_config)
                if sock is not None:
                    self._connect2socket(data, socket_config, sock)
                    self._disconnect(sock)
                    success = True
                    break
        except socket.gaierror as error:
            self.debug('Failed to connect to "{host}:{port}", error: {error}'.format(
                host=self.host, port=self.port, error=error
            ))

        # We could not connect
        if not success:
            data[PORT_FAILED] = 1

        return data

    def _create_socket(self, socket_config):
        af, sock_type, proto, _, sa = socket_config
        try:
            self.debug('Creating socket to "{address}", port {port}'.format(address=sa[0], port=sa[1]))
            sock = socket.socket(af, sock_type, proto)
            sock.settimeout(self.timeout)
            return sock
        except socket.error as error:
            self.debug('Failed to create socket "{address}", port {port}, error: {error}'.format(
                address=sa[0], port=sa[1], error=error
            ))
            return None

    def _connect2socket(self, data, socket_config, sock):
        """
        Connect to a socket, passing the result of getaddrinfo()
        :return: dict
        """

        _, _, _, _, sa = socket_config
        port = str(sa[1])
        try:
            self.debug('Connecting socket to "{address}", port {port}'.format(address=sa[0], port=port))
            start = time()
            sock.connect(sa)
            diff = time() - start
            self.debug('Connected to "{address}", port {port}, latency {latency}'.format(
                address=sa[0], port=port, latency=diff
            ))
            # we will set it at least 0.1 ms. 0.0 would mean failed connection (handy for 3rd-party-APIs)
            data[PORT_LATENCY] = max(round(diff * 10000), 0)
            data[PORT_SUCCESS] = 1

        except socket.timeout as error:
            self.debug('Socket timed out on "{address}", port {port}, error: {error}'.format(
                address=sa[0], port=port, error=error
            ))
            data[PORT_TIMEOUT] = 1

        except socket.error as error:
            self.debug('Failed to connect to "{address}", port {port}, error: {error}'.format(
                address=sa[0], port=port, error=error
            ))
            data[PORT_FAILED] = 1

    def _disconnect(self, sock):
        """
        Close socket connection
        :return:
        """
        if sock is not None:
            try:
                self.debug('Closing socket')
                sock.shutdown(2)  # 0 - read, 1 - write, 2 - all
                sock.close()
            except socket.error:
                pass