Refactored portcheck.
Chris committed
Feb 28, 2018 at 18:30 UTC
b1e25be55a965e0192f219fd4478564d811ca7a4
1 file changed
+13
-8
python.d/portcheck.chart.py
+13
-8
@@ -3,7 +3,12 @@
3
# Original Author: ccremer (github.com/ccremer)
4
5
import socket
6
-import time
6
+
7
+try:
8
+ from time import monotonic as time
9
+except ImportError:
10
+ from time import time
11
+
12
from bases.FrameworkServices.SimpleService import SimpleService
13
14
# default module values (can be overridden per job in `config`)
@@ -35,14 +40,14 @@ CHARTS = {
40
}
41
42
43
+# Not deriving from SocketService, too much is different
44
class Service(SimpleService):
45
def __init__(self, configuration=None, name=None):
46
SimpleService.__init__(self, configuration=configuration, name=name)
47
self.order = ORDER
48
self.definitions = CHARTS
43
- self.chart_name = ""
44
- self.host = self.configuration.get('host', None)
45
- self.port = self.configuration.get('port', None)
49
+ self.host = self.configuration.get('host')
50
+ self.port = self.configuration.get('port')
51
self.timeout = self.configuration.get('timeout', 1)
52
53
def check(self):
@@ -57,7 +62,7 @@ class Service(SimpleService):
62
self.error('"port" is not an integer. Specify a numerical value, not service name.')
63
return False
64
60
- self.info("Enabled portcheck: {host}:{port}, update every {update}s, timeout: {timeout}s".format(
65
+ self.debug("Enabled portcheck: {host}:{port}, update every {update}s, timeout: {timeout}s".format(
66
host=self.host, port=self.port, update=self.update_every, timeout=self.timeout
67
))
68
# We will accept any (valid-ish) configuration, even if initial connection fails (a service might be down from
@@ -118,9 +123,9 @@ class Service(SimpleService):
123
port = str(sa[1])
124
try:
125
self.debug('Connecting socket to "{address}", port {port}'.format(address=sa[0], port=port))
121
- start = time.time()
126
+ start = time()
127
sock.connect(sa)
123
- diff = time.time() - start
128
+ diff = time() - start
129
self.debug('Connected to "{address}", port {port}, latency {latency}'.format(
130
address=sa[0], port=port, latency=diff
131
))
@@ -150,5 +155,5 @@ class Service(SimpleService):
155
self.debug('Closing socket')
156
sock.shutdown(2) # 0 - read, 1 - write, 2 - all
157
sock.close()
153
- except Exception:
158
+ except socket.error:
159
pass