@cryptotaxi247 / netdata-1 / commits / 377870635

add option to connect to unix socket with `SocketService`

paulfantom committed Jul 8, 2016 at 11:32 UTC 3778706351eb2c457000854a21aff22a98baa4cb
3 files changed +28 -17
python.d/hddtemp.chart.py
+3 -3
@@ -2,7 +2,7 @@
2 # Description: hddtemp netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
4
5 -from base import NetSocketService
5 +from base import SocketService
6
7 # default module values (can be overridden per job in `config`)
8 #update_every = 2
@@ -29,9 +29,9 @@ CHARTS = {
29 }
30
31
32 -class Service(NetSocketService):
32 +class Service(SocketService):
33 def __init__(self, configuration=None, name=None):
34 - NetSocketService.__init__(self, configuration=configuration, name=name)
34 + SocketService.__init__(self, configuration=configuration, name=name)
35 self.request = ""
36 self.host = "127.0.0.1"
37 self.port = 7634
python.d/python_modules/base.py
+22 -11
@@ -432,11 +432,12 @@ class UrlService(SimpleService):
432 return False
433
434
435 -class NetSocketService(SimpleService):
435 +class SocketService(SimpleService):
436 def __init__(self, configuration=None, name=None):
437 self.host = "localhost"
438 self.port = None
439 self.sock = None
440 + self.unix_socket = ""
441 self.request = ""
442 SimpleService.__init__(self, configuration=configuration, name=name)
443
@@ -447,10 +448,16 @@ class NetSocketService(SimpleService):
448 """
449 if self.sock is None:
450 try:
450 - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
451 - sock.settimeout(self.update_every)
452 - sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
453 - sock.connect((self.host, self.port))
451 + if len(self.unix_socket) == 0:
452 + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
453 + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
454 + sock.settimeout(self.update_every)
455 + sock.connect((self.host, self.port))
456 + else:
457 + sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
458 + sock.settimeout(self.update_every)
459 + sock.connect(self.unix_socket)
460 +
461 except Exception as e:
462 self.error(e)
463 self.sock = None
@@ -492,13 +499,17 @@ class NetSocketService(SimpleService):
499 else:
500 self.name = str(self.name)
501 try:
495 - self.host = str(self.configuration['host'])
496 - except (KeyError, TypeError):
497 - self.error("No host specified. Using: '" + self.host + "'")
498 - try:
499 - self.port = int(self.configuration['port'])
502 + self.unix_socket = int(self.configuration['unix_socket'])
503 except (KeyError, TypeError):
501 - self.error("No port specified. Using: '" + str(self.port) + "'")
504 + self.error("No unix socket specified. Trying TCP/IP socket.")
505 + try:
506 + self.host = str(self.configuration['host'])
507 + except (KeyError, TypeError):
508 + self.error("No host specified. Using: '" + self.host + "'")
509 + try:
510 + self.port = int(self.configuration['port'])
511 + except (KeyError, TypeError):
512 + self.error("No port specified. Using: '" + str(self.port) + "'")
513 try:
514 self.request = str(self.configuration['request'])
515 except (KeyError, TypeError):
python.d/squid.chart.py
+3 -3
@@ -2,7 +2,7 @@
2 # Description: squid netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
4
5 -from base import NetSocketService
5 +from base import SocketService
6
7 # default module values (can be overridden per job in `config`)
8 # update_every = 2
@@ -42,9 +42,9 @@ CHARTS = {
42 }
43
44
45 -class Service(NetSocketService):
45 +class Service(SocketService):
46 def __init__(self, configuration=None, name=None):
47 - NetSocketService.__init__(self, configuration=configuration, name=name)
47 + SocketService.__init__(self, configuration=configuration, name=name)
48 self.request = ""
49 self.host = "localhost"
50 self.port = 3128