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)
27 from subprocess import Popen, PIPE
28 from sys import exc_info
31 import urllib.request as urllib2
40 import pymysql as MySQLdb
46 PATH = os.getenv('PATH').split(':')
47 except AttributeError:
48 PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'.split(':')
51 # class BaseService(threading.Thread):
52 class SimpleService(threading.Thread):
54 Prototype of Service class.
55 Implemented basic functionality to run jobs by `python.d.plugin`
57 def __init__(self, configuration=None, name=None):
59 This needs to be initialized in child classes
60 :param configuration: dict
63 threading.Thread.__init__(self)
64 self._data_stream = ""
68 self.priority = 140000
71 self.override_name = None
75 self.__chart_set = False
76 self.__first_run = True
79 self._data_from_check = dict()
80 if configuration is None:
81 self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
84 self._extract_base_config(configuration)
86 self.create_timetable()
88 # --- BASIC SERVICE CONFIGURATION ---
90 def _extract_base_config(self, config):
92 Get basic parameters to run service
94 config = {'update_every':1,
101 self.override_name = pop('name')
104 self.update_every = int(pop('update_every'))
105 self.priority = int(pop('priority'))
106 self.retries = int(pop('retries'))
107 self.retries_left = self.retries
108 self.configuration = config
110 def create_timetable(self, freq=None):
112 Create service timetable.
115 timetable = {'last': 1466370091.3767564,
121 freq = self.update_every
123 self.timetable = {'last': now,
124 'next': now - (now % freq) + freq,
127 # --- THREAD CONFIGURATION ---
131 Executes self.update(interval) and draws run time chart.
132 Return value presents exit status of update()
135 t_start = float(time.time())
136 chart_name = self.chart_name
138 since_last = int((t_start - self.timetable['last']) * 1000000)
142 if not self.update(since_last):
143 self.error("update function failed.")
146 # draw performance graph
147 run_time = int((time.time() - t_start) * 1000)
148 print("BEGIN netdata.plugin_pythond_%s %s\nSET run_time = %s\nEND\n" %
149 (self.chart_name, str(since_last), str(run_time)))
151 self.debug(chart_name, "updated in", str(run_time), "ms")
152 self.timetable['last'] = t_start
153 self.__first_run = False
158 Runs job in thread. Handles retries.
159 Exits when job failed or timed out.
162 step = float(self.timetable['freq'])
164 self.timetable['last'] = float(time.time() - step)
165 self.debug("starting data collection - update frequency:", str(step), " retries allowed:", str(self.retries))
166 while True: # run forever, unless something is wrong
167 now = float(time.time())
168 next = self.timetable['next'] = now - (now % step) + step + penalty
170 # it is important to do this in a loop
171 # sleep() is interruptable
173 self.debug("sleeping for", str(next - now), "secs to reach frequency of", str(step), "secs, now:", str(now), " next:", str(next), " penalty:", str(penalty))
174 time.sleep(next - now)
175 now = float(time.time())
179 status = self._run_once()
180 except Exception as e:
185 self.retries_left = self.retries
189 self.retries_left -= 1
190 if self.retries_left <= 0:
192 penalty = float(self.retries * step) / 2
199 self.retries_left = self.retries
200 self.alert("failed to collect data for " + str(self.retries) + " times - increasing penalty to " + str(penalty) + " sec and trying again")
203 self.error("failed to collect data - " + str(self.retries_left) + " retries left - penalty: " + str(penalty) + " sec")
210 Escape and convert passed arguments.
211 :param args: anything
215 append = params.append
220 if type(p) is not str:
227 def _line(self, instruction, *params):
229 Converts *params to string and joins them with one space between every one.
230 Result is appended to self._data_stream
231 :param params: str/int/float
233 tmp = list(map((lambda x: "''" if x is None or len(x) == 0 else x), params))
234 self._data_stream += "%s %s\n" % (instruction, str(" ".join(tmp)))
236 def chart(self, type_id, name="", title="", units="", family="",
237 category="", chart_type="line", priority="", update_every=""):
246 :param chart_type: str
247 :param priority: int/str
248 :param update_every: int/str
250 self._charts.append(type_id)
252 p = self._format(type_id, name, title, units, family, category, chart_type, priority, update_every)
253 self._line("CHART", *p)
255 def dimension(self, id, name=None, algorithm="absolute", multiplier=1, divisor=1, hidden=False):
257 Defines a new dimension for the chart
260 :param algorithm: str
261 :param multiplier: int/str
262 :param divisor: int/str
263 :param hidden: boolean
269 self.error("malformed dimension: multiplier is not a number:", multiplier)
274 self.error("malformed dimension: divisor is not a number:", divisor)
278 if algorithm not in ("absolute", "incremental", "percentage-of-absolute-row", "percentage-of-incremental-row"):
279 algorithm = "absolute"
281 self._dimensions.append(str(id))
283 p = self._format(id, name, algorithm, multiplier, divisor, "hidden")
285 p = self._format(id, name, algorithm, multiplier, divisor)
287 self._line("DIMENSION", *p)
289 def begin(self, type_id, microseconds=0):
293 :param microseconds: int
296 if type_id not in self._charts:
297 self.error("wrong chart type_id:", type_id)
302 self.error("malformed begin statement: microseconds are not a number:", microseconds)
305 self._line("BEGIN", type_id, str(microseconds))
308 def set(self, id, value):
310 Set value to dimension
312 :param value: int/float
315 if id not in self._dimensions:
316 self.error("wrong dimension id:", id, "Available dimensions are:", *self._dimensions)
319 value = str(int(value))
321 self.error("cannot set non-numeric value:", str(value))
323 self._line("SET", id, "=", str(value))
324 self.__chart_set = True
330 self.__chart_set = False
332 pos = self._data_stream.rfind("BEGIN")
333 self._data_stream = self._data_stream[:pos]
337 Upload new data to netdata.
340 print(self._data_stream)
341 except Exception as e:
342 msg.fatal('cannot send data to netdata:', str(e))
343 self._data_stream = ""
345 # --- ERROR HANDLING ---
347 def error(self, *params):
349 Show error message on stderr
351 msg.error(self.chart_name, *params)
353 def alert(self, *params):
355 Show error message on stderr
357 msg.alert(self.chart_name, *params)
359 def debug(self, *params):
361 Show debug message on stderr
363 msg.debug(self.chart_name, *params)
365 def info(self, *params):
367 Show information message on stderr
369 msg.info(self.chart_name, *params)
371 # --- MAIN METHODS ---
385 self.debug("Module", str(self.__module__), "doesn't implement check() function. Using default.")
386 data = self._get_data()
389 self.debug("failed to receive data during check().")
393 self.debug("empty data during check().")
396 self.debug("successfully received data during check(): '" + str(data) + "'")
404 data = self._data_from_check or self._get_data()
406 self.debug("failed to receive data during create().")
410 for name in self.order:
411 options = self.definitions[name]['options'] + [self.priority + idx, self.update_every]
412 self.chart(self.chart_name + "." + name, *options)
413 # check if server has this datapoint
414 for line in self.definitions[name]['lines']:
416 self.dimension(*line)
422 def update(self, interval):
428 data = self._get_data()
430 self.debug("failed to receive data during update().")
434 for chart in self.order:
435 if self.begin(self.chart_name + "." + chart, interval):
437 for dim in self.definitions[chart]['lines']:
439 self.set(dim[0], data[dim[0]])
446 self.error("no charts to update")
451 def find_binary(binary):
453 if isinstance(binary, str):
454 binary = os.path.basename(binary)
455 return next(('/'.join([p, binary]) for p in PATH
456 if os.path.isfile('/'.join([p, binary]))
457 and os.access('/'.join([p, binary]), os.X_OK)))
460 except StopIteration:
464 class UrlService(SimpleService):
465 # TODO add support for https connections
466 def __init__(self, configuration=None, name=None):
471 SimpleService.__init__(self, configuration=configuration, name=name)
473 def __add_openers(self):
474 # TODO add error handling
477 ctx = ssl.create_default_context()
478 ctx.check_hostname = False
479 ctx.verify_mode = ssl.CERT_NONE
480 self.opener = urllib2.build_opener(urllib2.HTTPSHandler(context=ctx))
481 except Exception as error:
482 self.error(str(error))
483 self.opener = urllib2.build_opener()
485 self.opener = urllib2.build_opener()
488 # TODO currently self.proxies isn't parsed from configuration file
489 # if len(self.proxies) > 0:
490 # for proxy in self.proxies:
493 # if "user" in proxy and "pass" in proxy:
494 # if url.lower().startswith('https://'):
495 # url = 'https://' + proxy['user'] + ':' + proxy['pass'] + '@' + url[8:]
497 # url = 'http://' + proxy['user'] + ':' + proxy['pass'] + '@' + url[7:]
498 # # FIXME move proxy auth to sth like this:
499 # # passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
500 # # passman.add_password(None, url, proxy['user'], proxy['password'])
501 # # opener.add_handler(urllib2.HTTPBasicAuthHandler(passman))
503 # if url.lower().startswith('https://'):
504 # opener.add_handler(urllib2.ProxyHandler({'https': url}))
506 # opener.add_handler(urllib2.ProxyHandler({'https': url}))
509 if self.user is not None and self.password is not None:
510 passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
511 passman.add_password(None, self.url, self.user, self.password)
512 self.opener.add_handler(urllib2.HTTPBasicAuthHandler(passman))
513 self.debug("Enabling HTTP basic auth")
515 #urllib2.install_opener(opener)
517 def _get_raw_data(self):
519 Get raw data from http request
524 f = self.opener.open(self.url, timeout=self.update_every * 2)
525 # f = urllib2.urlopen(self.url, timeout=self.update_every * 2)
526 except Exception as e:
531 raw = f.read().decode('utf-8', 'ignore')
532 except Exception as e:
540 Format configuration data and try to connect to server
543 if self.name is None or self.name == str(None):
545 self.chart_name += "_" + self.name
547 self.name = str(self.name)
549 self.url = str(self.configuration['url'])
550 except (KeyError, TypeError):
553 self.user = str(self.configuration['user'])
554 except (KeyError, TypeError):
557 self.password = str(self.configuration['pass'])
558 except (KeyError, TypeError):
560 self.ss_cert = self.configuration.get('ss_cert')
563 test = self._get_data()
564 if test is None or len(test) == 0:
570 class SocketService(SimpleService):
571 def __init__(self, configuration=None, name=None):
573 self._keep_alive = False
574 self.host = "localhost"
576 self.unix_socket = None
578 self.__socket_config = None
579 self.__empty_request = "".encode()
580 SimpleService.__init__(self, configuration=configuration, name=name)
582 def _socketerror(self, message=None):
583 if self.unix_socket is not None:
584 self.error("unix socket '" + self.unix_socket + "':", message)
586 if self.__socket_config is not None:
587 af, socktype, proto, canonname, sa = self.__socket_config
588 self.error("socket to '" + str(sa[0]) + "' port " + str(sa[1]) + ":", message)
590 self.error("unknown socket:", message)
592 def _connect2socket(self, res=None):
594 Connect to a socket, passing the result of getaddrinfo()
598 res = self.__socket_config
600 self.error("Cannot create socket to 'None':")
603 af, socktype, proto, canonname, sa = res
605 self.debug("creating socket to '" + str(sa[0]) + "', port " + str(sa[1]))
606 self._sock = socket.socket(af, socktype, proto)
607 except socket.error as e:
608 self.error("Failed to create socket to '" + str(sa[0]) + "', port " + str(sa[1]) + ":", str(e))
610 self.__socket_config = None
614 self.debug("connecting socket to '" + str(sa[0]) + "', port " + str(sa[1]))
615 self._sock.connect(sa)
616 except socket.error as e:
617 self.error("Failed to connect to '" + str(sa[0]) + "', port " + str(sa[1]) + ":", str(e))
619 self.__socket_config = None
622 self.debug("connected to '" + str(sa[0]) + "', port " + str(sa[1]))
623 self.__socket_config = res
626 def _connect2unixsocket(self):
628 Connect to a unix socket, given its filename
631 if self.unix_socket is None:
632 self.error("cannot connect to unix socket 'None'")
636 self.debug("attempting DGRAM unix socket '" + str(self.unix_socket) + "'")
637 self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
638 self._sock.connect(self.unix_socket)
639 self.debug("connected DGRAM unix socket '" + str(self.unix_socket) + "'")
641 except socket.error as e:
642 self.debug("Failed to connect DGRAM unix socket '" + str(self.unix_socket) + "':", str(e))
645 self.debug("attempting STREAM unix socket '" + str(self.unix_socket) + "'")
646 self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
647 self._sock.connect(self.unix_socket)
648 self.debug("connected STREAM unix socket '" + str(self.unix_socket) + "'")
650 except socket.error as e:
651 self.debug("Failed to connect STREAM unix socket '" + str(self.unix_socket) + "':", str(e))
652 self.error("Failed to connect to unix socket '" + str(self.unix_socket) + "':", str(e))
658 Recreate socket and connect to it since sockets cannot be reused after closing
659 Available configurations are IPv6, IPv4 or UNIX socket
663 if self.unix_socket is not None:
664 self._connect2unixsocket()
667 if self.__socket_config is not None:
668 self._connect2socket()
670 for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
671 if self._connect2socket(res): break
673 except Exception as e:
675 self.__socket_config = None
677 if self._sock is not None:
678 self._sock.setblocking(0)
679 self._sock.settimeout(5)
680 self.debug("set socket timeout to: " + str(self._sock.gettimeout()))
682 def _disconnect(self):
684 Close socket connection
687 if self._sock is not None:
689 self.debug("closing socket")
690 self._sock.shutdown(2) # 0 - read, 1 - write, 2 - all
701 # Send request if it is needed
702 if self.request != self.__empty_request:
704 self.debug("sending request:", str(self.request))
705 self._sock.send(self.request)
706 except Exception as e:
707 self._socketerror("error sending request:" + str(e))
714 Receive data from socket
719 self.debug("receiving response")
721 buf = self._sock.recv(4096)
722 except Exception as e:
723 self._socketerror("failed to receive response:" + str(e))
727 if buf is None or len(buf) == 0: # handle server disconnect
729 self._socketerror("unexpectedly disconnected")
731 self.debug("server closed the connection")
735 self.debug("received data:", str(buf))
736 data += buf.decode('utf-8', 'ignore')
737 if self._check_raw_data(data):
740 self.debug("final response:", str(data))
743 def _get_raw_data(self):
745 Get raw data with low-level "socket" module.
748 if self._sock is None:
750 if self._sock is None:
753 # Send request if it is needed
757 data = self._receive()
759 if not self._keep_alive:
764 def _check_raw_data(self, data):
766 Check if all data has been gathered from socket
772 def _parse_config(self):
774 Parse configuration data
777 if self.name is None or self.name == str(None):
780 self.name = str(self.name)
783 self.unix_socket = str(self.configuration['socket'])
784 except (KeyError, TypeError):
785 self.debug("No unix socket specified. Trying TCP/IP socket.")
786 self.unix_socket = None
788 self.host = str(self.configuration['host'])
789 except (KeyError, TypeError):
790 self.debug("No host specified. Using: '" + self.host + "'")
792 self.port = int(self.configuration['port'])
793 except (KeyError, TypeError):
794 self.debug("No port specified. Using: '" + str(self.port) + "'")
797 self.request = str(self.configuration['request'])
798 except (KeyError, TypeError):
799 self.debug("No request specified. Using: '" + str(self.request) + "'")
801 self.request = self.request.encode()
805 return SimpleService.check(self)
808 class LogService(SimpleService):
809 def __init__(self, configuration=None, name=None):
811 self._last_position = 0
812 # self._log_reader = None
813 SimpleService.__init__(self, configuration=configuration, name=name)
814 self.retries = 100000 # basically always retry
816 def _get_raw_data(self):
818 Get log lines since last poll
823 if os.path.getsize(self.log_path) < self._last_position:
824 self._last_position = 0 # read from beginning if file has shrunk
825 elif os.path.getsize(self.log_path) == self._last_position:
826 self.debug("Log file hasn't changed. No new data.")
827 return [] # return empty list if nothing has changed
828 with open(self.log_path, "r") as fp:
829 fp.seek(self._last_position)
830 for i, line in enumerate(fp):
832 self._last_position = fp.tell()
833 except Exception as e:
839 self.error("No data collected.")
844 Parse basic configuration and check if log file exists
847 if self.name is not None or self.name != str(None):
850 self.name = str(self.name)
852 self.log_path = str(self.configuration['path'])
853 except (KeyError, TypeError):
854 self.info("No path to log specified. Using: '" + self.log_path + "'")
856 if os.access(self.log_path, os.R_OK):
859 self.error("Cannot access file: '" + self.log_path + "'")
863 # set cursor at last byte of log file
864 self._last_position = os.path.getsize(self.log_path)
865 status = SimpleService.create(self)
866 # self._last_position = 0
870 class ExecutableService(SimpleService):
872 def __init__(self, configuration=None, name=None):
873 SimpleService.__init__(self, configuration=configuration, name=name)
876 def _get_raw_data(self):
878 Get raw data from executed command
882 p = Popen(self.command, stdout=PIPE, stderr=PIPE)
883 except Exception as error:
884 self.error("Executing command", self.command, "resulted in error:", str(error))
887 for line in p.stdout.readlines():
888 data.append(line.decode())
894 Parse basic configuration, check if command is whitelisted and is returning values
897 # Preference: 1. "command" from configuration file 2. "command" from plugin (if specified)
898 if 'command' in self.configuration:
899 self.command = self.configuration['command']
901 # "command" must be: 1.not None 2. type <str>
902 if not (self.command and isinstance(self.command, str)):
903 self.error('Command is not defined or command type is not <str>')
906 # Split "command" into: 1. command <str> 2. options <list>
907 command, opts = self.command.split()[0], self.command.split()[1:]
909 # Check for "bad" symbols in options. No pipes, redirects etc. TODO: what is missing?
910 bad_opts = set(''.join(opts)) & set(['&', '|', ';', '>', '<'])
912 self.error("Bad command argument(s): %s" % bad_opts)
915 # Find absolute path ('echo' => '/bin/echo')
916 if '/' not in command:
917 command = self.find_binary(command)
919 self.error('Can\'t locate "%s" binary in PATH(%s)' % (self.command, PATH))
921 # Check if binary exist and executable
923 if not (os.path.isfile(command) and os.access(command, os.X_OK)):
924 self.error('"%s" is not a file or not executable' % command)
927 self.command = [command] + opts if opts else [command]
930 data = self._get_data()
931 except Exception as error:
932 self.error('_get_data() failed. Command: %s. Error: %s' % (self.command, error))
935 if isinstance(data, dict) and data:
936 # We need this for create() method. No reason to execute get_data() again if result is not empty dict()
937 self._data_from_check = data
940 self.error("Command", str(self.command), "returned no data")
944 class MySQLService(SimpleService):
946 def __init__(self, configuration=None, name=None):
947 SimpleService.__init__(self, configuration=configuration, name=name)
948 self.__connection = None
949 self.conn_properties = dict()
950 self.queries = dict()
954 connection = MySQLdb.connect(connect_timeout=self.update_every, **self.conn_properties)
955 except (MySQLdb.MySQLError, TypeError) as error:
956 return None, str(error)
958 return connection, None
961 def get_connection_properties(conf):
963 if 'user' in conf and conf['user']:
964 properties['user'] = conf['user']
965 if 'pass' in conf and conf['pass']:
966 properties['passwd'] = conf['pass']
967 if 'socket' in conf and conf['socket']:
968 properties['unix_socket'] = conf['socket']
969 elif 'host' in conf and conf['host']:
970 properties['host'] = conf['host']
971 properties['port'] = int(conf['port']) if conf.get('port') else 3306
972 elif 'my.cnf' in conf and conf['my.cnf']:
973 properties['read_default_file'] = conf['my.cnf']
975 return properties or None
977 def is_valid_queries_dict(raw_queries, log_error):
979 :param raw_queries: dict:
980 :param log_error: function:
981 :return: dict or None
983 raw_queries is valid when: type <dict> and not empty after is_valid_query(for all queries)
985 def is_valid_query(query):
986 return all([isinstance(query, str),
987 query.startswith(('SELECT', 'select', 'SHOW', 'show'))])
989 if isinstance(raw_queries, dict) and raw_queries:
990 valid_queries = dict([(n, q) for n, q in raw_queries.items() if is_valid_query(q)])
991 bad_queries = set(raw_queries) - set(valid_queries)
994 log_error('Removed query(s): %s' % bad_queries)
997 log_error('Unsupported "queries" format. Must be not empty <dict>')
1001 self.error('MySQLdb or PyMySQL module is needed to use mysql.chart.py plugin')
1004 # Check if "self.queries" exist, not empty and all queries are in valid format
1005 self.queries = is_valid_queries_dict(self.queries, self.error)
1006 if not self.queries:
1009 # Get connection properties
1010 self.conn_properties = get_connection_properties(self.configuration)
1011 if not self.conn_properties:
1012 self.error('Connection properties are missing')
1015 # Create connection to the database
1016 self.__connection, error = self.__connect()
1018 self.error('Can\'t establish connection to MySQL: %s' % error)
1022 data = self._get_data()
1023 except Exception as error:
1024 self.error('_get_data() failed. Error: %s' % error)
1027 if isinstance(data, dict) and data:
1028 # We need this for create() method
1029 self._data_from_check = data
1032 self.error("_get_data() returned no data or type is not <dict>")
1035 def _get_raw_data(self, description=None):
1037 Get raw data from MySQL server
1038 :return: dict: fetchall() or (fetchall(), description)
1041 if not self.__connection:
1042 self.__connection, error = self.__connect()
1048 with self.__connection as cursor:
1049 for name, query in self.queries.items():
1051 cursor.execute(query)
1052 except (MySQLdb.ProgrammingError, MySQLdb.OperationalError) as error:
1053 if exc_info()[0] == MySQLdb.OperationalError and 'denied' not in str(error):
1055 self.error('Removed query: %s[%s]. Error: %s'
1056 % (name, query, error))
1057 self.queries.pop(name)
1060 raw_data[name] = (cursor.fetchall(), cursor.description) if description else cursor.fetchall()
1061 self.__connection.commit()
1062 except (MySQLdb.MySQLError, RuntimeError, TypeError, AttributeError):
1063 self.__connection.close()
1064 self.__connection = None
1067 return raw_data or None