]> arthur.barton.de Git - netdata.git/blobdiff - python.d/python_modules/base.py
Merge pull request #1093 from romain-dartigues/rdartigues
[netdata.git] / python.d / python_modules / base.py
index 47d92a430bee723ae676f81e9d5579c0a9d624ac..70c586f8e411a276b829406cddc75cbcf8a4b7cf 100644 (file)
@@ -289,7 +289,7 @@ class SimpleService(threading.Thread):
         try:
             value = str(int(value))
         except TypeError:
-            self.error("cannot set non-numeric value:", value)
+            self.error("cannot set non-numeric value:", str(value))
             return False
         self._line("SET", id, "=", str(value))
         self.__chart_set = True
@@ -412,7 +412,7 @@ class UrlService(SimpleService):
 
     def __add_openers(self):
         # TODO add error handling
-        opener = urllib2.build_opener()
+        self.opener = urllib2.build_opener()
 
         # Proxy handling
         # TODO currently self.proxies isn't parsed from configuration file
@@ -439,9 +439,10 @@ class UrlService(SimpleService):
         if self.user is not None and self.password is not None:
             passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
             passman.add_password(None, self.url, self.user, self.password)
-            opener.add_handler(urllib2.HTTPBasicAuthHandler(passman))
+            self.opener.add_handler(urllib2.HTTPBasicAuthHandler(passman))
+            self.debug("Enabling HTTP basic auth")
 
-        urllib2.install_opener(opener)
+        #urllib2.install_opener(opener)
 
     def _get_raw_data(self):
         """
@@ -450,7 +451,8 @@ class UrlService(SimpleService):
         """
         raw = None
         try:
-            f = urllib2.urlopen(self.url, timeout=self.update_every * 2)
+            f = self.opener.open(self.url, timeout=self.update_every * 2)
+            # f = urllib2.urlopen(self.url, timeout=self.update_every * 2)
         except Exception as e:
             self.error(str(e))
             return None
@@ -488,7 +490,8 @@ class UrlService(SimpleService):
 
         self.__add_openers()
 
-        if self._get_data() is None or len(self._get_data()) == 0:
+        test = self._get_data()
+        if test is None or len(test) == 0:
             return False
         else:
             return True
@@ -543,8 +546,13 @@ class SocketService(SimpleService):
                         self._disconnect()
             else:
                 # connect to unix socket
-                self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
-                self._sock.connect(self.unix_socket)
+                try:
+                    self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
+                    self._sock.connect(self.unix_socket)
+                except socket.error:
+                    self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+                    self._sock.connect(self.unix_socket)
+
         except Exception as e:
             self.error(str(e),
                        "Cannot create socket with following configuration: host:", str(self.host),
@@ -591,7 +599,7 @@ class SocketService(SimpleService):
         data = ""
         while True:
             try:
-                ready_to_read, _, in_error = select.select([self._sock], [], [], 15)
+                ready_to_read, _, in_error = select.select([self._sock], [], [], 5)
             except Exception as e:
                 self.debug("SELECT", str(e))
                 self._disconnect()
@@ -600,7 +608,7 @@ class SocketService(SimpleService):
                 buf = self._sock.recv(4096)
                 if len(buf) == 0 or buf is None:  # handle server disconnect
                     break
-                data += buf.decode()
+                data += buf.decode(errors='ignore')
                 if self._check_raw_data(data):
                     break
             else:
@@ -650,6 +658,7 @@ class SocketService(SimpleService):
             self.unix_socket = str(self.configuration['socket'])
         except (KeyError, TypeError):
             self.debug("No unix socket specified. Trying TCP/IP socket.")
+            self.unix_socket = None
             try:
                 self.host = str(self.configuration['host'])
             except (KeyError, TypeError):
@@ -664,6 +673,10 @@ class SocketService(SimpleService):
             self.debug("No request specified. Using: '" + str(self.request) + "'")
         self.request = self.request.encode()
 
+    def check(self):
+        self._parse_config()
+        return SimpleService.check(self)
+
 
 class LogService(SimpleService):
     def __init__(self, configuration=None, name=None):
@@ -767,20 +780,20 @@ class ExecutableService(SimpleService):
             self.command = str(self.configuration['command'])
         except (KeyError, TypeError):
             self.error("No command specified. Using: '" + self.command + "'")
-        self.command = self.command.split(' ')
+        command = self.command.split(' ')
 
-        for arg in self.command[1:]:
+        for arg in command[1:]:
             if any(st in arg for st in self.bad_substrings):
                 self.error("Bad command argument:" + " ".join(self.command[1:]))
                 return False
         # test command and search for it in /usr/sbin or /sbin when failed
-        base = self.command[0].split('/')[-1]
+        base = command[0].split('/')[-1]
         if self._get_raw_data() is None:
             for prefix in ['/sbin/', '/usr/sbin/']:
-                self.command[0] = prefix + base
-                if os.path.isfile(self.command[0]):
+                command[0] = prefix + base
+                if os.path.isfile(command[0]):
                     break
-
+        self.command = command
         if self._get_data() is None or len(self._get_data()) == 0:
             self.error("Command", self.command, "returned no data")
             return False