prototype of `ExecutableService`
paulfantom committed
Jul 5, 2016 at 01:48 UTC
740ed7d5a8be38f2c311e540dba11fea163388e0
1 file changed
+50
-4
python.d/python_modules/base.py
+50
-4
@@ -7,13 +7,12 @@ import sys
7
import os
8
import socket
9
try:
10
- # from urllib.request import urlopen, Request, HTTPPasswordMgrWithDefaultRealm
10
import urllib.request as urllib2
11
except ImportError:
13
- # from urllib2 import urlopen, Request, HTTPPasswordMgrWithDefaultRealm
12
import urllib2
13
16
-# from subprocess import STDOUT, PIPE, Popen
14
+from subprocess import Popen, PIPE
15
+
16
import threading
17
import msg
18
@@ -529,7 +528,7 @@ class LogService(SimpleService):
528
lines.append(line)
529
self._last_position = fp.tell()
530
except Exception as e:
532
- msg.error(self.__module__, str(e))
531
+ self.error(self.__module__, str(e))
532
533
if len(lines) != 0:
534
return lines
@@ -560,3 +559,50 @@ class LogService(SimpleService):
559
self._last_position = 0
560
return status
561
562
+
563
+class ExecutableService(SimpleService):
564
+ command_whitelist = ['exim']
565
+
566
+ def __init__(self, configuration=None, name=None):
567
+ self.command = ""
568
+ SimpleService.__init__(self, configuration=configuration, name=name)
569
+
570
+ def _get_raw_data(self):
571
+ """
572
+ Get raw data from executed command
573
+ :return: str
574
+ """
575
+ try:
576
+ p = Popen(self.command, stdout=PIPE, stderr=PIPE)
577
+ except Exception as e:
578
+ self.error(self.__module__, str(e))
579
+ return None
580
+ data = []
581
+ for line in p.stdout.readlines():
582
+ data.append(line)
583
+
584
+ return data
585
+
586
+ def check(self):
587
+ """
588
+ Parse basic configuration, check if command is whitelisted and is returning values
589
+ :return: boolean
590
+ """
591
+ if self.name is not None or self.name != str(None):
592
+ self.name = ""
593
+ else:
594
+ self.name = str(self.name)
595
+ # try:
596
+ # self.command = str(self.configuration['path'])
597
+ # except (KeyError, TypeError):
598
+ # self.error("No command specified. Using: '" + self.command + "'")
599
+ self.command = self.command.split(' ')
600
+ for i in self.command:
601
+ if i.startswith('-') or i in self.command_whitelist:
602
+ pass
603
+ else:
604
+ self.error("Wrong command. Probably not on whitelist.")
605
+ return False
606
+ if len(self._get_data()) == 0:
607
+ return False
608
+ return True