SimpleService: _add_new_dimension method added
Ilya committed
Jun 12, 2017 at 00:13 UTC
0f2ee5c04727ebf31022e0efbbc9598f7eef8611
1 file changed
+42
-10
python.d/python_modules/base.py
+42
-10
@@ -21,22 +21,19 @@ import time
21
import os
22
import socket
23
import threading
24
-import msg
24
import ssl
25
from subprocess import Popen, PIPE
26
from sys import exc_info
27
from glob import glob
29
-
28
+import re
29
try:
30
from urlparse import urlparse
31
except ImportError:
32
from urllib.parse import urlparse
34
-
33
try:
34
import urllib.request as urllib2
35
except ImportError:
36
import urllib2
39
-
37
try:
38
import MySQLdb
39
PYMYSQL = True
@@ -46,6 +43,7 @@ except ImportError:
43
PYMYSQL = True
44
except ImportError:
45
PYMYSQL = False
46
+import msg
47
48
try:
49
PATH = os.getenv('PATH').split(':')
@@ -175,14 +173,15 @@ class SimpleService(threading.Thread):
173
# it is important to do this in a loop
174
# sleep() is interruptable
175
while now < next:
178
- self.debug("sleeping for", str(next - now), "secs to reach frequency of", str(step), "secs, now:", str(now), " next:", str(next), " penalty:", str(penalty))
176
+ self.debug("sleeping for", str(next - now), "secs to reach frequency of",
177
+ str(step), "secs, now:", str(now), " next:", str(next), " penalty:", str(penalty))
178
time.sleep(next - now)
179
now = float(time.time())
180
181
# do the job
182
try:
183
status = self._run_once()
185
- except Exception as e:
184
+ except Exception:
185
status = False
186
187
if status:
@@ -202,10 +201,12 @@ class SimpleService(threading.Thread):
201
penalty = 600
202
203
self.retries_left = self.retries
205
- self.alert("failed to collect data for " + str(self.retries) + " times - increasing penalty to " + str(penalty) + " sec and trying again")
204
+ self.alert("failed to collect data for " + str(self.retries) +
205
+ " times - increasing penalty to " + str(penalty) + " sec and trying again")
206
207
else:
208
- self.error("failed to collect data - " + str(self.retries_left) + " retries left - penalty: " + str(penalty) + " sec")
208
+ self.error("failed to collect data - " + str(self.retries_left)
209
+ + " retries left - penalty: " + str(penalty) + " sec")
210
211
# --- CHART ---
212
@@ -460,11 +461,42 @@ class SimpleService(threading.Thread):
461
return next(('/'.join([p, binary]) for p in PATH
462
if os.path.isfile('/'.join([p, binary]))
463
and os.access('/'.join([p, binary]), os.X_OK)))
463
- else:
464
- return None
464
+ return None
465
except StopIteration:
466
return None
467
468
+ def _add_new_dimension(self, dimension_id, chart_name, dimension=None, algorithm='incremental',
469
+ multiplier=1, divisor=1, priority=65000):
470
+ """
471
+ :param dimension_id:
472
+ :param chart_name:
473
+ :param dimension:
474
+ :param algorithm:
475
+ :param multiplier:
476
+ :param divisor:
477
+ :param priority:
478
+ :return:
479
+ """
480
+ if not all([dimension_id not in self._dimensions,
481
+ chart_name in self.order,
482
+ chart_name in self.definitions]):
483
+ return
484
+ self._dimensions.append(dimension_id)
485
+ dimension_list = list(map(str, [dimension_id,
486
+ dimension if dimension else dimension_id,
487
+ algorithm,
488
+ multiplier,
489
+ divisor]))
490
+ self.definitions[chart_name]['lines'].append(dimension_list)
491
+ add_to_name = self.override_name or self.name
492
+ job_name = ('_'.join([self.__module__, re.sub('\s+', '_', add_to_name)])
493
+ if add_to_name != 'None' else self.__module__)
494
+ chart = 'CHART {0}.{1} '.format(job_name, chart_name)
495
+ options = '"" "{0}" {1} "{2}" {3} {4} '.format(*self.definitions[chart_name]['options'][1:6])
496
+ other = '{0} {1}\n'.format(priority, self.update_every)
497
+ new_dimension = "DIMENSION {0}\n".format(' '.join(dimension_list))
498
+ print(chart + options + other + new_dimension)
499
+
500
501
class UrlService(SimpleService):
502
def __init__(self, configuration=None, name=None):