replace UsefulFunc class with standalone funs
lgz committed
Oct 14, 2017 at 14:57 UTC
0783e2556434834f1ed81f8265c4ecf9caec5292
6 files changed
+31
-60
python.d/bind_rndc.chart.py
+6
-5
@@ -2,11 +2,12 @@
2
# Description: bind rndc netdata python.d module
3
# Author: l2isbad
4
5
+import os
6
+
7
from collections import defaultdict
6
-from os.path import getsize
7
-from os import access, R_OK
8
from subprocess import Popen
9
10
+from bases.collection import find_binary
11
from bases.FrameworkServices.SimpleService import SimpleService
12
13
priority = 60000
@@ -95,7 +96,7 @@ class Service(SimpleService):
96
self.order = ORDER
97
self.definitions = CHARTS
98
self.named_stats_path = self.configuration.get('named_stats_path', '/var/log/bind/named.stats')
98
- self.rndc = self.functions.find_binary('rndc')
99
+ self.rndc = find_binary('rndc')
100
self.data = dict(nms_requests=0, nms_responses=0, nms_failure=0, nms_auth=0,
101
nms_non_auth=0, nms_nxrrset=0, nms_success=0, nms_nxdomain=0,
102
nms_recursion=0, nms_duplicate=0, nms_rejected_queries=0,
@@ -106,7 +107,7 @@ class Service(SimpleService):
107
self.error('Can\'t locate "rndc" binary or binary is not executable by netdata')
108
return False
109
109
- if not self.functions.is_file_readable(self.named_stats_path):
110
+ if not (os.path.isfile(self.named_stats_path) and os.access(self.named_stats_path, os.R_OK)):
111
self.error('Cannot access file %s' % self.named_stats_path)
112
return False
113
@@ -125,7 +126,7 @@ class Service(SimpleService):
126
"""
127
result = dict()
128
try:
128
- current_size = getsize(self.named_stats_path)
129
+ current_size = os.path.getsize(self.named_stats_path)
130
run_rndc = Popen([self.rndc, 'stats'], shell=False)
131
run_rndc.wait()
132
python.d/freeradius.chart.py
+3
-2
@@ -5,6 +5,7 @@
5
from re import findall
6
from subprocess import Popen, PIPE
7
8
+from bases.collection import find_binary
9
from bases.FrameworkServices.SimpleService import SimpleService
10
11
# default module values (can be overridden per job in `config`)
@@ -78,8 +79,8 @@ class Service(SimpleService):
79
self.proxy_acct = self.configuration.get('proxy_acct', False)
80
chart_choice = [True, bool(self.acct), bool(self.proxy_auth), bool(self.proxy_acct)]
81
self.order = [chart for chart, choice in zip(ORDER, chart_choice) if choice]
81
- self.echo = self.functions.find_binary('echo')
82
- self.radclient = self.functions.find_binary('radclient')
82
+ self.echo = find_binary('echo')
83
+ self.radclient = find_binary('radclient')
84
self.sub_echo = [self.echo, RADIUS_MSG]
85
self.sub_radclient = [self.radclient, '-r', '1', '-t', '1',
86
':'.join([self.host, self.port]), 'status', self.secret]
python.d/python_modules/bases/FrameworkServices/ExecutableService.py
+6
-3
@@ -3,9 +3,12 @@
3
# Author: Pawel Krupa (paulfantom)
4
# Author: Ilya Mashchenko (l2isbad)
5
6
+import os
7
+
8
from subprocess import Popen, PIPE
9
10
from bases.FrameworkServices.SimpleService import SimpleService
11
+from bases.collection import find_binary
12
13
14
class ExecutableService(SimpleService):
@@ -57,14 +60,14 @@ class ExecutableService(SimpleService):
60
61
# Find absolute path ('echo' => '/bin/echo')
62
if '/' not in command:
60
- command = self.functions.find_binary(command)
63
+ command = find_binary(command)
64
if not command:
65
self.error('Can\'t locate "{command}" binary'.format(command=self.command))
66
return False
67
# Check if binary exist and executable
68
else:
66
- if not (self.functions.is_file_executable(command)):
67
- self.error('"{binary}" is not a file or not executable'.format(binary=command))
69
+ if not os.access(command, os.X_OK):
70
+ self.error('"{binary}" is not executable'.format(binary=command))
71
return False
72
73
self.command = [command] + opts if opts else [command]
python.d/python_modules/bases/FrameworkServices/LogService.py
+4
-3
@@ -3,6 +3,7 @@
3
# Author: Pawel Krupa (paulfantom)
4
5
from glob import glob
6
+import os
7
8
from bases.FrameworkServices.SimpleService import SimpleService
9
@@ -24,7 +25,7 @@ class LogService(SimpleService):
25
try:
26
if self.__re_find['current'] == self.__re_find['run']:
27
self._find_recent_log_file()
27
- size = self.functions.get_file_size(self.log_path)
28
+ size = os.path.getsize(self.log_path)
29
if size == self._last_position:
30
self.__re_find['current'] += 1
31
return list() # return empty list if nothing has changed
@@ -65,13 +66,13 @@ class LogService(SimpleService):
66
self.error('No path to log specified')
67
return None
68
68
- if self._find_recent_log_file() and self.functions.is_file_readable(self.log_path):
69
+ if self._find_recent_log_file() and os.access(self.log_path, os.R_OK) and os.path.isfile(self.log_path):
70
return True
71
self.error('Cannot access {0}'.format(self.log_path))
72
return False
73
74
def create(self):
75
# set cursor at last byte of log file
75
- self._last_position = self.functions.get_file_size(self.log_path)
76
+ self._last_position = os.path.getsize(self.log_path)
77
status = SimpleService.create(self)
78
return status
python.d/python_modules/bases/FrameworkServices/SimpleService.py
-1
@@ -70,7 +70,6 @@ class SimpleService(Thread, PythonDLimitedLogger, OldVersionCompatibility, objec
70
self.charts = Charts(job_name=self.actual_name,
71
priority=configuration.pop('priority', 60000),
72
update_every=self.update_every)
73
- self.functions = UsefulFuncs()
73
74
def __repr__(self):
75
return '<{cls_bases}: {name}>'.format(cls_bases=', '.join(c.__name__ for c in self.__class__.__bases__),
python.d/python_modules/bases/collection.py
+12
-46
@@ -60,6 +60,18 @@ def safe_print(msg):
60
print(msg)
61
62
63
+def find_binary(binary):
64
+ """
65
+ :param binary: <str>
66
+ :return:
67
+ """
68
+ for directory in PATH:
69
+ binary_name = '/'.join([directory, binary])
70
+ if os.path.isfile(binary_name) and os.access(binary_name, os.X_OK):
71
+ return binary_name
72
+ return None
73
+
74
+
75
class OldVersionCompatibility:
76
77
def __init__(self):
@@ -119,49 +131,3 @@ class OldVersionCompatibility:
131
def commit(self):
132
print(self._data_stream)
133
self._data_stream = str()
122
-
123
-
124
-class UsefulFuncs:
125
- @staticmethod
126
- def find_binary(binary):
127
- """
128
- :param binary: <str>
129
- :return:
130
- """
131
- for directory in PATH:
132
- binary_name = '/'.join([directory, binary])
133
- if os.path.isfile(binary_name) and os.access(binary_name, os.X_OK):
134
- return binary_name
135
- return None
136
-
137
- @staticmethod
138
- def is_file_readable(f):
139
- """
140
- :param f: <str>
141
- :return:
142
- """
143
- return os.path.isfile(f) and os.access(f, os.R_OK)
144
-
145
- @staticmethod
146
- def is_file_executable(f):
147
- """
148
- :param f: <str>
149
- :return:
150
- """
151
- return os.path.isfile(f) and os.access(f, os.X_OK)
152
-
153
- @staticmethod
154
- def get_file_size(f):
155
- """
156
- :param f: <str>
157
- :return:
158
- """
159
- return os.path.getsize(f)
160
-
161
- @staticmethod
162
- def is_directory(d):
163
- """
164
- :param d: <str>
165
- :return:
166
- """
167
- return os.path.isdir(d)