From 3778706351eb2c457000854a21aff22a98baa4cb Mon Sep 17 00:00:00 2001 From: paulfantom Date: Fri, 8 Jul 2016 11:32:22 +0200 Subject: [PATCH] add option to connect to unix socket with `SocketService` --- python.d/hddtemp.chart.py | 6 +++--- python.d/python_modules/base.py | 33 ++++++++++++++++++++++----------- python.d/squid.chart.py | 6 +++--- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/python.d/hddtemp.chart.py b/python.d/hddtemp.chart.py index fac2ec6a..d0b53fc5 100644 --- a/python.d/hddtemp.chart.py +++ b/python.d/hddtemp.chart.py @@ -2,7 +2,7 @@ # Description: hddtemp netdata python.d module # Author: Pawel Krupa (paulfantom) -from base import NetSocketService +from base import SocketService # default module values (can be overridden per job in `config`) #update_every = 2 @@ -29,9 +29,9 @@ CHARTS = { } -class Service(NetSocketService): +class Service(SocketService): def __init__(self, configuration=None, name=None): - NetSocketService.__init__(self, configuration=configuration, name=name) + SocketService.__init__(self, configuration=configuration, name=name) self.request = "" self.host = "127.0.0.1" self.port = 7634 diff --git a/python.d/python_modules/base.py b/python.d/python_modules/base.py index eebc78ee..2e43a553 100644 --- a/python.d/python_modules/base.py +++ b/python.d/python_modules/base.py @@ -432,11 +432,12 @@ class UrlService(SimpleService): return False -class NetSocketService(SimpleService): +class SocketService(SimpleService): def __init__(self, configuration=None, name=None): self.host = "localhost" self.port = None self.sock = None + self.unix_socket = "" self.request = "" SimpleService.__init__(self, configuration=configuration, name=name) @@ -447,10 +448,16 @@ class NetSocketService(SimpleService): """ if self.sock is None: try: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.settimeout(self.update_every) - sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - sock.connect((self.host, self.port)) + if len(self.unix_socket) == 0: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.settimeout(self.update_every) + sock.connect((self.host, self.port)) + else: + sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) + sock.settimeout(self.update_every) + sock.connect(self.unix_socket) + except Exception as e: self.error(e) self.sock = None @@ -492,13 +499,17 @@ class NetSocketService(SimpleService): else: self.name = str(self.name) try: - self.host = str(self.configuration['host']) - except (KeyError, TypeError): - self.error("No host specified. Using: '" + self.host + "'") - try: - self.port = int(self.configuration['port']) + self.unix_socket = int(self.configuration['unix_socket']) except (KeyError, TypeError): - self.error("No port specified. Using: '" + str(self.port) + "'") + self.error("No unix socket specified. Trying TCP/IP socket.") + try: + self.host = str(self.configuration['host']) + except (KeyError, TypeError): + self.error("No host specified. Using: '" + self.host + "'") + try: + self.port = int(self.configuration['port']) + except (KeyError, TypeError): + self.error("No port specified. Using: '" + str(self.port) + "'") try: self.request = str(self.configuration['request']) except (KeyError, TypeError): diff --git a/python.d/squid.chart.py b/python.d/squid.chart.py index f68ce7e5..71327c6e 100644 --- a/python.d/squid.chart.py +++ b/python.d/squid.chart.py @@ -2,7 +2,7 @@ # Description: squid netdata python.d module # Author: Pawel Krupa (paulfantom) -from base import NetSocketService +from base import SocketService # default module values (can be overridden per job in `config`) # update_every = 2 @@ -42,9 +42,9 @@ CHARTS = { } -class Service(NetSocketService): +class Service(SocketService): def __init__(self, configuration=None, name=None): - NetSocketService.__init__(self, configuration=configuration, name=name) + SocketService.__init__(self, configuration=configuration, name=name) self.request = "" self.host = "localhost" self.port = 3128 -- 2.39.2