1 # -*- coding: utf-8 -*-
2 # Description: netdata python modules framework
3 # Author: Pawel Krupa (paulfantom)
6 # ALL CODE NEEDS TO BE COMPATIBLE WITH Python > 2.7 and Python > 3.1
7 # Follow PEP8 as much as it is possible
8 # "check" and "create" CANNOT be blocking.
9 # "update" CAN be blocking
10 # "update" function needs to be fast, so follow:
11 # https://wiki.python.org/moin/PythonSpeed/PerformanceTips
13 # - use local variables wherever it is possible
14 # - avoid dots in expressions that are executed many times
15 # - use "join()" instead of "+"
16 # - use "import" only at the beginning
18 # using ".encode()" in one thread can block other threads as well (only in python2)
26 import urllib.request as urllib2
30 from subprocess import Popen, PIPE
36 PATH = os.getenv('PATH').split(':')
37 except AttributeError:
38 PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'.split(':')
41 # class BaseService(threading.Thread):
42 class SimpleService(threading.Thread):
44 Prototype of Service class.
45 Implemented basic functionality to run jobs by `python.d.plugin`
47 def __init__(self, configuration=None, name=None):
49 This needs to be initialized in child classes
50 :param configuration: dict
53 threading.Thread.__init__(self)
54 self._data_stream = ""
58 self.priority = 140000
61 self.override_name = None
65 self.__chart_set = False
66 self.__first_run = True
69 if configuration is None:
70 self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
73 self._extract_base_config(configuration)
75 self.create_timetable()
77 # --- BASIC SERVICE CONFIGURATION ---
79 def _extract_base_config(self, config):
81 Get basic parameters to run service
83 config = {'update_every':1,
90 self.override_name = pop('name')
93 self.update_every = int(pop('update_every'))
94 self.priority = int(pop('priority'))
95 self.retries = int(pop('retries'))
96 self.retries_left = self.retries
97 self.configuration = config
99 def create_timetable(self, freq=None):
101 Create service timetable.
104 timetable = {'last': 1466370091.3767564,
110 freq = self.update_every
112 self.timetable = {'last': now,
113 'next': now - (now % freq) + freq,
116 # --- THREAD CONFIGURATION ---
120 Executes self.update(interval) and draws run time chart.
121 Return value presents exit status of update()
124 t_start = float(time.time())
125 chart_name = self.chart_name
127 since_last = int((t_start - self.timetable['last']) * 1000000)
131 if not self.update(since_last):
132 self.error("update function failed.")
135 # draw performance graph
136 run_time = int((time.time() - t_start) * 1000)
137 print("BEGIN netdata.plugin_pythond_%s %s\nSET run_time = %s\nEND\n" %
138 (self.chart_name, str(since_last), str(run_time)))
140 self.debug(chart_name, "updated in", str(run_time), "ms")
141 self.timetable['last'] = t_start
142 self.__first_run = False
147 Runs job in thread. Handles retries.
148 Exits when job failed or timed out.
151 step = float(self.timetable['freq'])
153 self.timetable['last'] = float(time.time() - step)
154 self.debug("starting data collection - update frequency:", str(step), " retries allowed:", str(self.retries))
155 while True: # run forever, unless something is wrong
156 now = float(time.time())
157 next = self.timetable['next'] = now - (now % step) + step + penalty
159 # it is important to do this in a loop
160 # sleep() is interruptable
162 self.debug("sleeping for", str(next - now), "secs to reach frequency of", str(step), "secs, now:", str(now), " next:", str(next), " penalty:", str(penalty))
163 time.sleep(next - now)
164 now = float(time.time())
168 status = self._run_once()
169 except Exception as e:
174 self.retries_left = self.retries
178 self.retries_left -= 1
179 if self.retries_left <= 0:
181 penalty = float(self.retries * step) / 2
188 self.retries_left = self.retries
189 self.alert("failed to collect data for " + str(self.retries) + " times - increasing penalty to " + str(penalty) + " sec and trying again")
192 self.error("failed to collect data - " + str(self.retries_left) + " retries left - penalty: " + str(penalty) + " sec")
199 Escape and convert passed arguments.
200 :param args: anything
204 append = params.append
209 if type(p) is not str:
216 def _line(self, instruction, *params):
218 Converts *params to string and joins them with one space between every one.
219 Result is appended to self._data_stream
220 :param params: str/int/float
222 tmp = list(map((lambda x: "''" if x is None or len(x) == 0 else x), params))
223 self._data_stream += "%s %s\n" % (instruction, str(" ".join(tmp)))
225 def chart(self, type_id, name="", title="", units="", family="",
226 category="", chart_type="line", priority="", update_every=""):
235 :param chart_type: str
236 :param priority: int/str
237 :param update_every: int/str
239 self._charts.append(type_id)
241 p = self._format(type_id, name, title, units, family, category, chart_type, priority, update_every)
242 self._line("CHART", *p)
244 def dimension(self, id, name=None, algorithm="absolute", multiplier=1, divisor=1, hidden=False):
246 Defines a new dimension for the chart
249 :param algorithm: str
250 :param multiplier: int/str
251 :param divisor: int/str
252 :param hidden: boolean
258 self.error("malformed dimension: multiplier is not a number:", multiplier)
263 self.error("malformed dimension: divisor is not a number:", divisor)
267 if algorithm not in ("absolute", "incremental", "percentage-of-absolute-row", "percentage-of-incremental-row"):
268 algorithm = "absolute"
270 self._dimensions.append(str(id))
272 p = self._format(id, name, algorithm, multiplier, divisor, "hidden")
274 p = self._format(id, name, algorithm, multiplier, divisor)
276 self._line("DIMENSION", *p)
278 def begin(self, type_id, microseconds=0):
282 :param microseconds: int
285 if type_id not in self._charts:
286 self.error("wrong chart type_id:", type_id)
291 self.error("malformed begin statement: microseconds are not a number:", microseconds)
294 self._line("BEGIN", type_id, str(microseconds))
297 def set(self, id, value):
299 Set value to dimension
301 :param value: int/float
304 if id not in self._dimensions:
305 self.error("wrong dimension id:", id, "Available dimensions are:", *self._dimensions)
308 value = str(int(value))
310 self.error("cannot set non-numeric value:", str(value))
312 self._line("SET", id, "=", str(value))
313 self.__chart_set = True
319 self.__chart_set = False
321 pos = self._data_stream.rfind("BEGIN")
322 self._data_stream = self._data_stream[:pos]
326 Upload new data to netdata.
329 print(self._data_stream)
330 except Exception as e:
331 msg.fatal('cannot send data to netdata:', str(e))
332 self._data_stream = ""
334 # --- ERROR HANDLING ---
336 def error(self, *params):
338 Show error message on stderr
340 msg.error(self.chart_name, *params)
342 def alert(self, *params):
344 Show error message on stderr
346 msg.alert(self.chart_name, *params)
348 def debug(self, *params):
350 Show debug message on stderr
352 msg.debug(self.chart_name, *params)
354 def info(self, *params):
356 Show information message on stderr
358 msg.info(self.chart_name, *params)
360 # --- MAIN METHODS ---
374 self.debug("Module", str(self.__module__), "doesn't implement check() function. Using default.")
375 data = self._get_data()
378 self.debug("failed to receive data during check().")
382 self.debug("empty data during check().")
385 self.debug("successfully received data during check(): '" + str(data) + "'")
393 data = self._get_data()
395 self.debug("failed to receive data during create().")
399 for name in self.order:
400 options = self.definitions[name]['options'] + [self.priority + idx, self.update_every]
401 self.chart(self.chart_name + "." + name, *options)
402 # check if server has this datapoint
403 for line in self.definitions[name]['lines']:
405 self.dimension(*line)
411 def update(self, interval):
417 data = self._get_data()
419 self.debug("failed to receive data during update().")
423 for chart in self.order:
424 if self.begin(self.chart_name + "." + chart, interval):
426 for dim in self.definitions[chart]['lines']:
428 self.set(dim[0], data[dim[0]])
435 self.error("no charts to update")
439 def find_binary(self, binary):
441 if isinstance(binary, str):
442 binary = os.path.basename(binary)
443 return next(('/'.join([p, binary]) for p in PATH
444 if os.path.isfile('/'.join([p, binary]))
445 and os.access('/'.join([p, binary]), os.X_OK)))
448 except StopIteration:
452 class UrlService(SimpleService):
453 # TODO add support for https connections
454 def __init__(self, configuration=None, name=None):
459 SimpleService.__init__(self, configuration=configuration, name=name)
461 def __add_openers(self):
462 # TODO add error handling
463 self.opener = urllib2.build_opener()
466 # TODO currently self.proxies isn't parsed from configuration file
467 # if len(self.proxies) > 0:
468 # for proxy in self.proxies:
471 # if "user" in proxy and "pass" in proxy:
472 # if url.lower().startswith('https://'):
473 # url = 'https://' + proxy['user'] + ':' + proxy['pass'] + '@' + url[8:]
475 # url = 'http://' + proxy['user'] + ':' + proxy['pass'] + '@' + url[7:]
476 # # FIXME move proxy auth to sth like this:
477 # # passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
478 # # passman.add_password(None, url, proxy['user'], proxy['password'])
479 # # opener.add_handler(urllib2.HTTPBasicAuthHandler(passman))
481 # if url.lower().startswith('https://'):
482 # opener.add_handler(urllib2.ProxyHandler({'https': url}))
484 # opener.add_handler(urllib2.ProxyHandler({'https': url}))
487 if self.user is not None and self.password is not None:
488 passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
489 passman.add_password(None, self.url, self.user, self.password)
490 self.opener.add_handler(urllib2.HTTPBasicAuthHandler(passman))
491 self.debug("Enabling HTTP basic auth")
493 #urllib2.install_opener(opener)
495 def _get_raw_data(self):
497 Get raw data from http request
502 f = self.opener.open(self.url, timeout=self.update_every * 2)
503 # f = urllib2.urlopen(self.url, timeout=self.update_every * 2)
504 except Exception as e:
509 raw = f.read().decode('utf-8', 'ignore')
510 except Exception as e:
518 Format configuration data and try to connect to server
521 if self.name is None or self.name == str(None):
523 self.chart_name += "_" + self.name
525 self.name = str(self.name)
527 self.url = str(self.configuration['url'])
528 except (KeyError, TypeError):
531 self.user = str(self.configuration['user'])
532 except (KeyError, TypeError):
535 self.password = str(self.configuration['pass'])
536 except (KeyError, TypeError):
541 test = self._get_data()
542 if test is None or len(test) == 0:
548 class SocketService(SimpleService):
549 def __init__(self, configuration=None, name=None):
551 self._keep_alive = False
552 self.host = "localhost"
554 self.unix_socket = None
556 self.__socket_config = None
557 self.__empty_request = "".encode()
558 SimpleService.__init__(self, configuration=configuration, name=name)
560 def _socketerror(self, message=None):
561 if self.unix_socket is not None:
562 self.error("unix socket '" + self.unix_socket + "':", message)
564 if self.__socket_config is not None:
565 af, socktype, proto, canonname, sa = self.__socket_config
566 self.error("socket to '" + str(sa[0]) + "' port " + str(sa[1]) + ":", message)
568 self.error("unknown socket:", message)
570 def _connect2socket(self, res=None):
572 Connect to a socket, passing the result of getaddrinfo()
576 res = self.__socket_config
578 self.error("Cannot create socket to 'None':")
581 af, socktype, proto, canonname, sa = res
583 self.debug("creating socket to '" + str(sa[0]) + "', port " + str(sa[1]))
584 self._sock = socket.socket(af, socktype, proto)
585 except socket.error as e:
586 self.error("Failed to create socket to '" + str(sa[0]) + "', port " + str(sa[1]) + ":", str(e))
588 self.__socket_config = None
592 self.debug("connecting socket to '" + str(sa[0]) + "', port " + str(sa[1]))
593 self._sock.connect(sa)
594 except socket.error as e:
595 self.error("Failed to connect to '" + str(sa[0]) + "', port " + str(sa[1]) + ":", str(e))
597 self.__socket_config = None
600 self.debug("connected to '" + str(sa[0]) + "', port " + str(sa[1]))
601 self.__socket_config = res
604 def _connect2unixsocket(self):
606 Connect to a unix socket, given its filename
609 if self.unix_socket is None:
610 self.error("cannot connect to unix socket 'None'")
614 self.debug("attempting DGRAM unix socket '" + str(self.unix_socket) + "'")
615 self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
616 self._sock.connect(self.unix_socket)
617 self.debug("connected DGRAM unix socket '" + str(self.unix_socket) + "'")
619 except socket.error as e:
620 self.debug("Failed to connect DGRAM unix socket '" + str(self.unix_socket) + "':", str(e))
623 self.debug("attempting STREAM unix socket '" + str(self.unix_socket) + "'")
624 self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
625 self._sock.connect(self.unix_socket)
626 self.debug("connected STREAM unix socket '" + str(self.unix_socket) + "'")
628 except socket.error as e:
629 self.debug("Failed to connect STREAM unix socket '" + str(self.unix_socket) + "':", str(e))
630 self.error("Failed to connect to unix socket '" + str(self.unix_socket) + "':", str(e))
636 Recreate socket and connect to it since sockets cannot be reused after closing
637 Available configurations are IPv6, IPv4 or UNIX socket
641 if self.unix_socket is not None:
642 self._connect2unixsocket()
645 if self.__socket_config is not None:
646 self._connect2socket()
648 for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
649 if self._connect2socket(res): break
651 except Exception as e:
653 self.__socket_config = None
655 if self._sock is not None:
656 self._sock.setblocking(0)
657 self._sock.settimeout(5)
658 self.debug("set socket timeout to: " + str(self._sock.gettimeout()))
660 def _disconnect(self):
662 Close socket connection
665 if self._sock is not None:
667 self.debug("closing socket")
668 self._sock.shutdown(2) # 0 - read, 1 - write, 2 - all
679 # Send request if it is needed
680 if self.request != self.__empty_request:
682 self.debug("sending request:", str(self.request))
683 self._sock.send(self.request)
684 except Exception as e:
685 self._socketerror("error sending request:" + str(e))
692 Receive data from socket
697 self.debug("receiving response")
699 buf = self._sock.recv(4096)
700 except Exception as e:
701 self._socketerror("failed to receive response:" + str(e))
705 if buf is None or len(buf) == 0: # handle server disconnect
707 self._socketerror("unexpectedly disconnected")
709 self.debug("server closed the connection")
713 self.debug("received data:", str(buf))
714 data += buf.decode('utf-8', 'ignore')
715 if self._check_raw_data(data):
718 self.debug("final response:", str(data))
721 def _get_raw_data(self):
723 Get raw data with low-level "socket" module.
726 if self._sock is None:
728 if self._sock is None:
731 # Send request if it is needed
735 data = self._receive()
737 if not self._keep_alive:
742 def _check_raw_data(self, data):
744 Check if all data has been gathered from socket
750 def _parse_config(self):
752 Parse configuration data
755 if self.name is None or self.name == str(None):
758 self.name = str(self.name)
761 self.unix_socket = str(self.configuration['socket'])
762 except (KeyError, TypeError):
763 self.debug("No unix socket specified. Trying TCP/IP socket.")
764 self.unix_socket = None
766 self.host = str(self.configuration['host'])
767 except (KeyError, TypeError):
768 self.debug("No host specified. Using: '" + self.host + "'")
770 self.port = int(self.configuration['port'])
771 except (KeyError, TypeError):
772 self.debug("No port specified. Using: '" + str(self.port) + "'")
775 self.request = str(self.configuration['request'])
776 except (KeyError, TypeError):
777 self.debug("No request specified. Using: '" + str(self.request) + "'")
779 self.request = self.request.encode()
783 return SimpleService.check(self)
786 class LogService(SimpleService):
787 def __init__(self, configuration=None, name=None):
789 self._last_position = 0
790 # self._log_reader = None
791 SimpleService.__init__(self, configuration=configuration, name=name)
792 self.retries = 100000 # basically always retry
794 def _get_raw_data(self):
796 Get log lines since last poll
801 if os.path.getsize(self.log_path) < self._last_position:
802 self._last_position = 0 # read from beginning if file has shrunk
803 elif os.path.getsize(self.log_path) == self._last_position:
804 self.debug("Log file hasn't changed. No new data.")
805 return [] # return empty list if nothing has changed
806 with open(self.log_path, "r") as fp:
807 fp.seek(self._last_position)
808 for i, line in enumerate(fp):
810 self._last_position = fp.tell()
811 except Exception as e:
817 self.error("No data collected.")
822 Parse basic configuration and check if log file exists
825 if self.name is not None or self.name != str(None):
828 self.name = str(self.name)
830 self.log_path = str(self.configuration['path'])
831 except (KeyError, TypeError):
832 self.info("No path to log specified. Using: '" + self.log_path + "'")
834 if os.access(self.log_path, os.R_OK):
837 self.error("Cannot access file: '" + self.log_path + "'")
841 # set cursor at last byte of log file
842 self._last_position = os.path.getsize(self.log_path)
843 status = SimpleService.create(self)
844 # self._last_position = 0
848 class ExecutableService(SimpleService):
849 bad_substrings = ('&', '|', ';', '>', '<')
851 def __init__(self, configuration=None, name=None):
853 SimpleService.__init__(self, configuration=configuration, name=name)
855 def _get_raw_data(self):
857 Get raw data from executed command
861 p = Popen(self.command, stdout=PIPE, stderr=PIPE)
862 except Exception as e:
863 self.error("Executing command", self.command, "resulted in error:", str(e))
866 for line in p.stdout.readlines():
867 data.append(str(line.decode()))
870 self.error("No data collected.")
877 Parse basic configuration, check if command is whitelisted and is returning values
880 if self.name is not None or self.name != str(None):
883 self.name = str(self.name)
885 self.command = str(self.configuration['command'])
886 except (KeyError, TypeError):
887 self.info("No command specified. Using: '" + self.command + "'")
888 # Splitting self.command on every space so subprocess.Popen reads it properly
889 self.command = self.command.split(' ')
891 for arg in self.command[1:]:
892 if any(st in arg for st in self.bad_substrings):
893 self.error("Bad command argument:" + " ".join(self.command[1:]))
896 # test command and search for it in /usr/sbin or /sbin when failed
897 base = self.command[0].split('/')[-1]
898 if self._get_raw_data() is None:
899 for prefix in ['/sbin/', '/usr/sbin/']:
900 self.command[0] = prefix + base
901 if os.path.isfile(self.command[0]):
904 if self._get_data() is None or len(self._get_data()) == 0:
905 self.error("Command", self.command, "returned no data")