]> arthur.barton.de Git - netdata.git/blob - python.d/python_modules/base.py
Merge pull request #1822 from l2isbad/mongodb_plugin
[netdata.git] / python.d / python_modules / base.py
1 # -*- coding: utf-8 -*-
2 # Description: netdata python modules framework
3 # Author: Pawel Krupa (paulfantom)
4
5 # Remember:
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
12 # basically:
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
17 #
18 # using ".encode()" in one thread can block other threads as well (only in python2)
19
20 import time
21 # import sys
22 import os
23 import socket
24 import select
25 try:
26     import urllib.request as urllib2
27 except ImportError:
28     import urllib2
29
30 from subprocess import Popen, PIPE
31
32 import threading
33 import msg
34 import ssl
35
36 try:
37     PATH = os.getenv('PATH').split(':')
38 except AttributeError:
39     PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'.split(':')
40
41
42 # class BaseService(threading.Thread):
43 class SimpleService(threading.Thread):
44     """
45     Prototype of Service class.
46     Implemented basic functionality to run jobs by `python.d.plugin`
47     """
48     def __init__(self, configuration=None, name=None):
49         """
50         This needs to be initialized in child classes
51         :param configuration: dict
52         :param name: str
53         """
54         threading.Thread.__init__(self)
55         self._data_stream = ""
56         self.daemon = True
57         self.retries = 0
58         self.retries_left = 0
59         self.priority = 140000
60         self.update_every = 1
61         self.name = name
62         self.override_name = None
63         self.chart_name = ""
64         self._dimensions = []
65         self._charts = []
66         self.__chart_set = False
67         self.__first_run = True
68         self.order = []
69         self.definitions = {}
70         if configuration is None:
71             self.error("BaseService: no configuration parameters supplied. Cannot create Service.")
72             raise RuntimeError
73         else:
74             self._extract_base_config(configuration)
75             self.timetable = {}
76             self.create_timetable()
77
78     # --- BASIC SERVICE CONFIGURATION ---
79
80     def _extract_base_config(self, config):
81         """
82         Get basic parameters to run service
83         Minimum config:
84             config = {'update_every':1,
85                       'priority':100000,
86                       'retries':0}
87         :param config: dict
88         """
89         pop = config.pop
90         try:
91             self.override_name = pop('name')
92         except KeyError:
93             pass
94         self.update_every = int(pop('update_every'))
95         self.priority = int(pop('priority'))
96         self.retries = int(pop('retries'))
97         self.retries_left = self.retries
98         self.configuration = config
99
100     def create_timetable(self, freq=None):
101         """
102         Create service timetable.
103         `freq` is optional
104         Example:
105             timetable = {'last': 1466370091.3767564,
106                          'next': 1466370092,
107                          'freq': 1}
108         :param freq: int
109         """
110         if freq is None:
111             freq = self.update_every
112         now = time.time()
113         self.timetable = {'last': now,
114                           'next': now - (now % freq) + freq,
115                           'freq': freq}
116
117     # --- THREAD CONFIGURATION ---
118
119     def _run_once(self):
120         """
121         Executes self.update(interval) and draws run time chart.
122         Return value presents exit status of update()
123         :return: boolean
124         """
125         t_start = float(time.time())
126         chart_name = self.chart_name
127
128         since_last = int((t_start - self.timetable['last']) * 1000000)
129         if self.__first_run:
130             since_last = 0
131
132         if not self.update(since_last):
133             self.error("update function failed.")
134             return False
135
136         # draw performance graph
137         run_time = int((time.time() - t_start) * 1000)
138         print("BEGIN netdata.plugin_pythond_%s %s\nSET run_time = %s\nEND\n" %
139               (self.chart_name, str(since_last), str(run_time)))
140
141         self.debug(chart_name, "updated in", str(run_time), "ms")
142         self.timetable['last'] = t_start
143         self.__first_run = False
144         return True
145
146     def run(self):
147         """
148         Runs job in thread. Handles retries.
149         Exits when job failed or timed out.
150         :return: None
151         """
152         step = float(self.timetable['freq'])
153         penalty = 0
154         self.timetable['last'] = float(time.time() - step)
155         self.debug("starting data collection - update frequency:", str(step), " retries allowed:", str(self.retries))
156         while True:  # run forever, unless something is wrong
157             now = float(time.time())
158             next = self.timetable['next'] = now - (now % step) + step + penalty
159
160             # it is important to do this in a loop
161             # sleep() is interruptable
162             while now < next:
163                 self.debug("sleeping for", str(next - now), "secs to reach frequency of", str(step), "secs, now:", str(now), " next:", str(next), " penalty:", str(penalty))
164                 time.sleep(next - now)
165                 now = float(time.time())
166
167             # do the job
168             try:
169                 status = self._run_once()
170             except Exception as e:
171                 status = False
172
173             if status:
174                 # it is good
175                 self.retries_left = self.retries
176                 penalty = 0
177             else:
178                 # it failed
179                 self.retries_left -= 1
180                 if self.retries_left <= 0:
181                     if penalty == 0:
182                         penalty = float(self.retries * step) / 2
183                     else:
184                         penalty *= 1.5
185
186                     if penalty > 600:
187                         penalty = 600
188
189                     self.retries_left = self.retries
190                     self.alert("failed to collect data for " + str(self.retries) + " times - increasing penalty to " + str(penalty) + " sec and trying again")
191
192                 else:
193                     self.error("failed to collect data - " + str(self.retries_left) + " retries left - penalty: " + str(penalty) + " sec")
194
195     # --- CHART ---
196
197     @staticmethod
198     def _format(*args):
199         """
200         Escape and convert passed arguments.
201         :param args: anything
202         :return: list
203         """
204         params = []
205         append = params.append
206         for p in args:
207             if p is None:
208                 append(p)
209                 continue
210             if type(p) is not str:
211                 p = str(p)
212             if ' ' in p:
213                 p = "'" + p + "'"
214             append(p)
215         return params
216
217     def _line(self, instruction, *params):
218         """
219         Converts *params to string and joins them with one space between every one.
220         Result is appended to self._data_stream
221         :param params: str/int/float
222         """
223         tmp = list(map((lambda x: "''" if x is None or len(x) == 0 else x), params))
224         self._data_stream += "%s %s\n" % (instruction, str(" ".join(tmp)))
225
226     def chart(self, type_id, name="", title="", units="", family="",
227               category="", chart_type="line", priority="", update_every=""):
228         """
229         Defines a new chart.
230         :param type_id: str
231         :param name: str
232         :param title: str
233         :param units: str
234         :param family: str
235         :param category: str
236         :param chart_type: str
237         :param priority: int/str
238         :param update_every: int/str
239         """
240         self._charts.append(type_id)
241
242         p = self._format(type_id, name, title, units, family, category, chart_type, priority, update_every)
243         self._line("CHART", *p)
244
245     def dimension(self, id, name=None, algorithm="absolute", multiplier=1, divisor=1, hidden=False):
246         """
247         Defines a new dimension for the chart
248         :param id: str
249         :param name: str
250         :param algorithm: str
251         :param multiplier: int/str
252         :param divisor: int/str
253         :param hidden: boolean
254         :return:
255         """
256         try:
257             int(multiplier)
258         except TypeError:
259             self.error("malformed dimension: multiplier is not a number:", multiplier)
260             multiplier = 1
261         try:
262             int(divisor)
263         except TypeError:
264             self.error("malformed dimension: divisor is not a number:", divisor)
265             divisor = 1
266         if name is None:
267             name = id
268         if algorithm not in ("absolute", "incremental", "percentage-of-absolute-row", "percentage-of-incremental-row"):
269             algorithm = "absolute"
270
271         self._dimensions.append(str(id))
272         if hidden:
273             p = self._format(id, name, algorithm, multiplier, divisor, "hidden")
274         else:
275             p = self._format(id, name, algorithm, multiplier, divisor)
276
277         self._line("DIMENSION", *p)
278
279     def begin(self, type_id, microseconds=0):
280         """
281         Begin data set
282         :param type_id: str
283         :param microseconds: int
284         :return: boolean
285         """
286         if type_id not in self._charts:
287             self.error("wrong chart type_id:", type_id)
288             return False
289         try:
290             int(microseconds)
291         except TypeError:
292             self.error("malformed begin statement: microseconds are not a number:", microseconds)
293             microseconds = ""
294
295         self._line("BEGIN", type_id, str(microseconds))
296         return True
297
298     def set(self, id, value):
299         """
300         Set value to dimension
301         :param id: str
302         :param value: int/float
303         :return: boolean
304         """
305         if id not in self._dimensions:
306             self.error("wrong dimension id:", id, "Available dimensions are:", *self._dimensions)
307             return False
308         try:
309             value = str(int(value))
310         except TypeError:
311             self.error("cannot set non-numeric value:", str(value))
312             return False
313         self._line("SET", id, "=", str(value))
314         self.__chart_set = True
315         return True
316
317     def end(self):
318         if self.__chart_set:
319             self._line("END")
320             self.__chart_set = False
321         else:
322             pos = self._data_stream.rfind("BEGIN")
323             self._data_stream = self._data_stream[:pos]
324
325     def commit(self):
326         """
327         Upload new data to netdata.
328         """
329         try:
330             print(self._data_stream)
331         except Exception as e:
332             msg.fatal('cannot send data to netdata:', str(e))
333         self._data_stream = ""
334
335     # --- ERROR HANDLING ---
336
337     def error(self, *params):
338         """
339         Show error message on stderr
340         """
341         msg.error(self.chart_name, *params)
342
343     def alert(self, *params):
344         """
345         Show error message on stderr
346         """
347         msg.alert(self.chart_name, *params)
348
349     def debug(self, *params):
350         """
351         Show debug message on stderr
352         """
353         msg.debug(self.chart_name, *params)
354
355     def info(self, *params):
356         """
357         Show information message on stderr
358         """
359         msg.info(self.chart_name, *params)
360
361     # --- MAIN METHODS ---
362
363     def _get_data(self):
364         """
365         Get some data
366         :return: dict
367         """
368         return {}
369
370     def check(self):
371         """
372         check() prototype
373         :return: boolean
374         """
375         self.debug("Module", str(self.__module__), "doesn't implement check() function. Using default.")
376         data = self._get_data()
377
378         if data is None:
379             self.debug("failed to receive data during check().")
380             return False
381
382         if len(data) == 0:
383             self.debug("empty data during check().")
384             return False
385
386         self.debug("successfully received data during check(): '" + str(data) + "'")
387         return True
388
389     def create(self):
390         """
391         Create charts
392         :return: boolean
393         """
394         data = self._get_data()
395         if data is None:
396             self.debug("failed to receive data during create().")
397             return False
398
399         idx = 0
400         for name in self.order:
401             options = self.definitions[name]['options'] + [self.priority + idx, self.update_every]
402             self.chart(self.chart_name + "." + name, *options)
403             # check if server has this datapoint
404             for line in self.definitions[name]['lines']:
405                 if line[0] in data:
406                     self.dimension(*line)
407             idx += 1
408
409         self.commit()
410         return True
411
412     def update(self, interval):
413         """
414         Update charts
415         :param interval: int
416         :return: boolean
417         """
418         data = self._get_data()
419         if data is None:
420             self.debug("failed to receive data during update().")
421             return False
422
423         updated = False
424         for chart in self.order:
425             if self.begin(self.chart_name + "." + chart, interval):
426                 updated = True
427                 for dim in self.definitions[chart]['lines']:
428                     try:
429                         self.set(dim[0], data[dim[0]])
430                     except KeyError:
431                         pass
432                 self.end()
433
434         self.commit()
435         if not updated:
436             self.error("no charts to update")
437
438         return updated
439
440     def find_binary(self, binary):
441         try:
442             if isinstance(binary, str):
443                 binary = os.path.basename(binary)
444                 return next(('/'.join([p, binary]) for p in PATH
445                             if os.path.isfile('/'.join([p, binary]))
446                             and os.access('/'.join([p, binary]), os.X_OK)))
447             else:
448                 return None
449         except StopIteration:
450             return None
451
452
453 class UrlService(SimpleService):
454     # TODO add support for https connections
455     def __init__(self, configuration=None, name=None):
456         self.url = ""
457         self.user = None
458         self.password = None
459         self.proxies = {}
460         SimpleService.__init__(self, configuration=configuration, name=name)
461
462     def __add_openers(self):
463         # TODO add error handling
464         if self.ss_cert:
465             try:
466                 ctx = ssl.create_default_context()
467                 ctx.check_hostname = False
468                 ctx.verify_mode = ssl.CERT_NONE
469                 self.opener = urllib2.build_opener(urllib2.HTTPSHandler(context=ctx))
470             except Exception as error:
471                 self.error(str(error))
472                 self.opener = urllib2.build_opener()
473         else:
474             self.opener = urllib2.build_opener()
475
476         # Proxy handling
477         # TODO currently self.proxies isn't parsed from configuration file
478         # if len(self.proxies) > 0:
479         #     for proxy in self.proxies:
480         #         url = proxy['url']
481         #         # TODO test this:
482         #         if "user" in proxy and "pass" in proxy:
483         #             if url.lower().startswith('https://'):
484         #                 url = 'https://' + proxy['user'] + ':' + proxy['pass'] + '@' + url[8:]
485         #             else:
486         #                 url = 'http://' + proxy['user'] + ':' + proxy['pass'] + '@' + url[7:]
487         #         # FIXME move proxy auth to sth like this:
488         #         #     passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
489         #         #     passman.add_password(None, url, proxy['user'], proxy['password'])
490         #         #     opener.add_handler(urllib2.HTTPBasicAuthHandler(passman))
491         #
492         #         if url.lower().startswith('https://'):
493         #             opener.add_handler(urllib2.ProxyHandler({'https': url}))
494         #         else:
495         #             opener.add_handler(urllib2.ProxyHandler({'https': url}))
496
497         # HTTP Basic Auth
498         if self.user is not None and self.password is not None:
499             passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
500             passman.add_password(None, self.url, self.user, self.password)
501             self.opener.add_handler(urllib2.HTTPBasicAuthHandler(passman))
502             self.debug("Enabling HTTP basic auth")
503
504         #urllib2.install_opener(opener)
505
506     def _get_raw_data(self):
507         """
508         Get raw data from http request
509         :return: str
510         """
511         raw = None
512         try:
513             f = self.opener.open(self.url, timeout=self.update_every * 2)
514             # f = urllib2.urlopen(self.url, timeout=self.update_every * 2)
515         except Exception as e:
516             self.error(str(e))
517             return None
518
519         try:
520             raw = f.read().decode('utf-8', 'ignore')
521         except Exception as e:
522             self.error(str(e))
523         finally:
524             f.close()
525         return raw
526
527     def check(self):
528         """
529         Format configuration data and try to connect to server
530         :return: boolean
531         """
532         if self.name is None or self.name == str(None):
533             self.name = 'local'
534             self.chart_name += "_" + self.name
535         else:
536             self.name = str(self.name)
537         try:
538             self.url = str(self.configuration['url'])
539         except (KeyError, TypeError):
540             pass
541         try:
542             self.user = str(self.configuration['user'])
543         except (KeyError, TypeError):
544             pass
545         try:
546             self.password = str(self.configuration['pass'])
547         except (KeyError, TypeError):
548             pass
549         self.ss_cert = self.configuration.get('ss_cert')
550         self.__add_openers()
551
552         test = self._get_data()
553         if test is None or len(test) == 0:
554             return False
555         else:
556             return True
557
558
559 class SocketService(SimpleService):
560     def __init__(self, configuration=None, name=None):
561         self._sock = None
562         self._keep_alive = False
563         self.host = "localhost"
564         self.port = None
565         self.unix_socket = None
566         self.request = ""
567         self.__socket_config = None
568         self.__empty_request = "".encode()
569         SimpleService.__init__(self, configuration=configuration, name=name)
570
571     def _socketerror(self, message=None):
572         if self.unix_socket is not None:
573             self.error("unix socket '" + self.unix_socket + "':", message)
574         else:
575             if self.__socket_config is not None:
576                 af, socktype, proto, canonname, sa = self.__socket_config
577                 self.error("socket to '" + str(sa[0]) + "' port " + str(sa[1]) + ":", message)
578             else:
579                 self.error("unknown socket:", message)
580
581     def _connect2socket(self, res=None):
582         """
583         Connect to a socket, passing the result of getaddrinfo()
584         :return: boolean
585         """
586         if res is None:
587             res = self.__socket_config
588             if res is None:
589                 self.error("Cannot create socket to 'None':")
590                 return False
591
592         af, socktype, proto, canonname, sa = res
593         try:
594             self.debug("creating socket to '" + str(sa[0]) + "', port " + str(sa[1]))
595             self._sock = socket.socket(af, socktype, proto)
596         except socket.error as e:
597             self.error("Failed to create socket to '" + str(sa[0]) + "', port " + str(sa[1]) + ":", str(e))
598             self._sock = None
599             self.__socket_config = None
600             return False
601
602         try:
603             self.debug("connecting socket to '" + str(sa[0]) + "', port " + str(sa[1]))
604             self._sock.connect(sa)
605         except socket.error as e:
606             self.error("Failed to connect to '" + str(sa[0]) + "', port " + str(sa[1]) + ":", str(e))
607             self._disconnect()
608             self.__socket_config = None
609             return False
610
611         self.debug("connected to '" + str(sa[0]) + "', port " + str(sa[1]))
612         self.__socket_config = res
613         return True
614
615     def _connect2unixsocket(self):
616         """
617         Connect to a unix socket, given its filename
618         :return: boolean
619         """
620         if self.unix_socket is None:
621             self.error("cannot connect to unix socket 'None'")
622             return False
623
624         try:
625             self.debug("attempting DGRAM unix socket '" + str(self.unix_socket) + "'")
626             self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
627             self._sock.connect(self.unix_socket)
628             self.debug("connected DGRAM unix socket '" + str(self.unix_socket) + "'")
629             return True
630         except socket.error as e:
631             self.debug("Failed to connect DGRAM unix socket '" + str(self.unix_socket) + "':", str(e))
632
633         try:
634             self.debug("attempting STREAM unix socket '" + str(self.unix_socket) + "'")
635             self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
636             self._sock.connect(self.unix_socket)
637             self.debug("connected STREAM unix socket '" + str(self.unix_socket) + "'")
638             return True
639         except socket.error as e:
640             self.debug("Failed to connect STREAM unix socket '" + str(self.unix_socket) + "':", str(e))
641             self.error("Failed to connect to unix socket '" + str(self.unix_socket) + "':", str(e))
642             self._sock = None
643             return False
644
645     def _connect(self):
646         """
647         Recreate socket and connect to it since sockets cannot be reused after closing
648         Available configurations are IPv6, IPv4 or UNIX socket
649         :return:
650         """
651         try:
652             if self.unix_socket is not None:
653                 self._connect2unixsocket()
654
655             else:
656                 if self.__socket_config is not None:
657                     self._connect2socket()
658                 else:
659                     for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
660                         if self._connect2socket(res): break
661
662         except Exception as e:
663             self._sock = None
664             self.__socket_config = None
665
666         if self._sock is not None:
667             self._sock.setblocking(0)
668             self._sock.settimeout(5)
669             self.debug("set socket timeout to: " + str(self._sock.gettimeout()))
670
671     def _disconnect(self):
672         """
673         Close socket connection
674         :return:
675         """
676         if self._sock is not None:
677             try:
678                 self.debug("closing socket")
679                 self._sock.shutdown(2)  # 0 - read, 1 - write, 2 - all
680                 self._sock.close()
681             except Exception:
682                 pass
683             self._sock = None
684
685     def _send(self):
686         """
687         Send request.
688         :return: boolean
689         """
690         # Send request if it is needed
691         if self.request != self.__empty_request:
692             try:
693                 self.debug("sending request:", str(self.request))
694                 self._sock.send(self.request)
695             except Exception as e:
696                 self._socketerror("error sending request:" + str(e))
697                 self._disconnect()
698                 return False
699         return True
700
701     def _receive(self):
702         """
703         Receive data from socket
704         :return: str
705         """
706         data = ""
707         while True:
708             self.debug("receiving response")
709             try:
710                 buf = self._sock.recv(4096)
711             except Exception as e:
712                 self._socketerror("failed to receive response:" + str(e))
713                 self._disconnect()
714                 break
715
716             if buf is None or len(buf) == 0:  # handle server disconnect
717                 if data == "":
718                     self._socketerror("unexpectedly disconnected")
719                 else:
720                     self.debug("server closed the connection")
721                 self._disconnect()
722                 break
723
724             self.debug("received data:", str(buf))
725             data += buf.decode('utf-8', 'ignore')
726             if self._check_raw_data(data):
727                 break
728
729         self.debug("final response:", str(data))
730         return data
731
732     def _get_raw_data(self):
733         """
734         Get raw data with low-level "socket" module.
735         :return: str
736         """
737         if self._sock is None:
738             self._connect()
739             if self._sock is None:
740                 return None
741
742         # Send request if it is needed
743         if not self._send():
744             return None
745
746         data = self._receive()
747
748         if not self._keep_alive:
749             self._disconnect()
750
751         return data
752
753     def _check_raw_data(self, data):
754         """
755         Check if all data has been gathered from socket
756         :param data: str
757         :return: boolean
758         """
759         return True
760
761     def _parse_config(self):
762         """
763         Parse configuration data
764         :return: boolean
765         """
766         if self.name is None or self.name == str(None):
767             self.name = ""
768         else:
769             self.name = str(self.name)
770
771         try:
772             self.unix_socket = str(self.configuration['socket'])
773         except (KeyError, TypeError):
774             self.debug("No unix socket specified. Trying TCP/IP socket.")
775             self.unix_socket = None
776             try:
777                 self.host = str(self.configuration['host'])
778             except (KeyError, TypeError):
779                 self.debug("No host specified. Using: '" + self.host + "'")
780             try:
781                 self.port = int(self.configuration['port'])
782             except (KeyError, TypeError):
783                 self.debug("No port specified. Using: '" + str(self.port) + "'")
784
785         try:
786             self.request = str(self.configuration['request'])
787         except (KeyError, TypeError):
788             self.debug("No request specified. Using: '" + str(self.request) + "'")
789
790         self.request = self.request.encode()
791
792     def check(self):
793         self._parse_config()
794         return SimpleService.check(self)
795
796
797 class LogService(SimpleService):
798     def __init__(self, configuration=None, name=None):
799         self.log_path = ""
800         self._last_position = 0
801         # self._log_reader = None
802         SimpleService.__init__(self, configuration=configuration, name=name)
803         self.retries = 100000  # basically always retry
804
805     def _get_raw_data(self):
806         """
807         Get log lines since last poll
808         :return: list
809         """
810         lines = []
811         try:
812             if os.path.getsize(self.log_path) < self._last_position:
813                 self._last_position = 0  # read from beginning if file has shrunk
814             elif os.path.getsize(self.log_path) == self._last_position:
815                 self.debug("Log file hasn't changed. No new data.")
816                 return []  # return empty list if nothing has changed
817             with open(self.log_path, "r") as fp:
818                 fp.seek(self._last_position)
819                 for i, line in enumerate(fp):
820                     lines.append(line)
821                 self._last_position = fp.tell()
822         except Exception as e:
823             self.error(str(e))
824
825         if len(lines) != 0:
826             return lines
827         else:
828             self.error("No data collected.")
829             return None
830
831     def check(self):
832         """
833         Parse basic configuration and check if log file exists
834         :return: boolean
835         """
836         if self.name is not None or self.name != str(None):
837             self.name = ""
838         else:
839             self.name = str(self.name)
840         try:
841             self.log_path = str(self.configuration['path'])
842         except (KeyError, TypeError):
843             self.info("No path to log specified. Using: '" + self.log_path + "'")
844
845         if os.access(self.log_path, os.R_OK):
846             return True
847         else:
848             self.error("Cannot access file: '" + self.log_path + "'")
849             return False
850
851     def create(self):
852         # set cursor at last byte of log file
853         self._last_position = os.path.getsize(self.log_path)
854         status = SimpleService.create(self)
855         # self._last_position = 0
856         return status
857
858
859 class ExecutableService(SimpleService):
860     bad_substrings = ('&', '|', ';', '>', '<')
861
862     def __init__(self, configuration=None, name=None):
863         self.command = ""
864         SimpleService.__init__(self, configuration=configuration, name=name)
865
866     def _get_raw_data(self):
867         """
868         Get raw data from executed command
869         :return: str
870         """
871         try:
872             p = Popen(self.command, stdout=PIPE, stderr=PIPE)
873         except Exception as e:
874             self.error("Executing command", self.command, "resulted in error:", str(e))
875             return None
876         data = []
877         for line in p.stdout.readlines():
878             data.append(str(line.decode()))
879
880         if len(data) == 0:
881             self.error("No data collected.")
882             return None
883
884         return data
885
886     def check(self):
887         """
888         Parse basic configuration, check if command is whitelisted and is returning values
889         :return: boolean
890         """
891         if self.name is not None or self.name != str(None):
892             self.name = ""
893         else:
894             self.name = str(self.name)
895         try:
896             self.command = str(self.configuration['command'])
897         except (KeyError, TypeError):
898             self.info("No command specified. Using: '" + self.command + "'")
899         # Splitting self.command on every space so subprocess.Popen reads it properly
900         self.command = self.command.split(' ')
901
902         for arg in self.command[1:]:
903             if any(st in arg for st in self.bad_substrings):
904                 self.error("Bad command argument:" + " ".join(self.command[1:]))
905                 return False
906
907         # test command and search for it in /usr/sbin or /sbin when failed
908         base = self.command[0].split('/')[-1]
909         if self._get_raw_data() is None:
910             for prefix in ['/sbin/', '/usr/sbin/']:
911                 self.command[0] = prefix + base
912                 if os.path.isfile(self.command[0]):
913                     break
914
915         if self._get_data() is None or len(self._get_data()) == 0:
916             self.error("Command", self.command, "returned no data")
917             return False
918
919         return True