SimpleService.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # -*- coding: utf-8 -*-
  2. # Description:
  3. # Author: Pawel Krupa (paulfantom)
  4. # Author: Ilya Mashchenko (ilyam8)
  5. # SPDX-License-Identifier: GPL-3.0-or-later
  6. from time import sleep, time
  7. from third_party.monotonic import monotonic
  8. from bases.charts import Charts, ChartError, create_runtime_chart
  9. from bases.collection import safe_print
  10. from bases.loggers import PythonDLimitedLogger
  11. RUNTIME_CHART_UPDATE = 'BEGIN netdata.runtime_{job_name} {since_last}\n' \
  12. 'SET run_time = {elapsed}\n' \
  13. 'END\n'
  14. PENALTY_EVERY = 5
  15. MAX_PENALTY = 10 * 60 # 10 minutes
  16. class RuntimeCounters:
  17. def __init__(self, configuration):
  18. """
  19. :param configuration: <dict>
  20. """
  21. self.update_every = int(configuration.pop('update_every'))
  22. self.do_penalty = configuration.pop('penalty')
  23. self.start_mono = 0
  24. self.start_real = 0
  25. self.retries = 0
  26. self.penalty = 0
  27. self.elapsed = 0
  28. self.prev_update = 0
  29. self.runs = 1
  30. def calc_next(self):
  31. self.start_mono = monotonic()
  32. return self.start_mono - (self.start_mono % self.update_every) + self.update_every + self.penalty
  33. def sleep_until_next(self):
  34. next_time = self.calc_next()
  35. while self.start_mono < next_time:
  36. sleep(next_time - self.start_mono)
  37. self.start_mono = monotonic()
  38. self.start_real = time()
  39. def handle_retries(self):
  40. self.retries += 1
  41. if self.do_penalty and self.retries % PENALTY_EVERY == 0:
  42. self.penalty = round(min(self.retries * self.update_every / 2, MAX_PENALTY))
  43. def clean_module_name(name):
  44. if name.startswith('pythond_'):
  45. return name[8:]
  46. return name
  47. class SimpleService(PythonDLimitedLogger, object):
  48. """
  49. Prototype of Service class.
  50. Implemented basic functionality to run jobs by `python.d.plugin`
  51. """
  52. def __init__(self, configuration, name=''):
  53. """
  54. :param configuration: <dict>
  55. :param name: <str>
  56. """
  57. PythonDLimitedLogger.__init__(self)
  58. self.configuration = configuration
  59. self.order = list()
  60. self.definitions = dict()
  61. self.module_name = clean_module_name(self.__module__)
  62. self.job_name = configuration.pop('job_name')
  63. self.override_name = configuration.pop('override_name')
  64. self.fake_name = None
  65. self._runtime_counters = RuntimeCounters(configuration=configuration)
  66. self.charts = Charts(job_name=self.actual_name,
  67. priority=configuration.pop('priority'),
  68. cleanup=configuration.pop('chart_cleanup'),
  69. get_update_every=self.get_update_every,
  70. module_name=self.module_name)
  71. def __repr__(self):
  72. return '<{cls_bases}: {name}>'.format(cls_bases=', '.join(c.__name__ for c in self.__class__.__bases__),
  73. name=self.name)
  74. @property
  75. def name(self):
  76. if self.job_name and self.job_name != self.module_name:
  77. return '_'.join([self.module_name, self.override_name or self.job_name])
  78. return self.module_name
  79. def actual_name(self):
  80. return self.fake_name or self.name
  81. @property
  82. def runs_counter(self):
  83. return self._runtime_counters.runs
  84. @property
  85. def update_every(self):
  86. return self._runtime_counters.update_every
  87. @update_every.setter
  88. def update_every(self, value):
  89. """
  90. :param value: <int>
  91. :return:
  92. """
  93. self._runtime_counters.update_every = value
  94. def get_update_every(self):
  95. return self.update_every
  96. def check(self):
  97. """
  98. check() prototype
  99. :return: boolean
  100. """
  101. self.debug("job doesn't implement check() method. Using default which simply invokes get_data().")
  102. data = self.get_data()
  103. if data and isinstance(data, dict):
  104. return True
  105. self.debug('returned value is wrong: {0}'.format(data))
  106. return False
  107. @create_runtime_chart
  108. def create(self):
  109. for chart_name in self.order:
  110. chart_config = self.definitions.get(chart_name)
  111. if not chart_config:
  112. self.debug("create() => [NOT ADDED] chart '{chart_name}' not in definitions. "
  113. "Skipping it.".format(chart_name=chart_name))
  114. continue
  115. # create chart
  116. chart_params = [chart_name] + chart_config['options']
  117. try:
  118. self.charts.add_chart(params=chart_params)
  119. except ChartError as error:
  120. self.error("create() => [NOT ADDED] (chart '{chart}': {error})".format(chart=chart_name,
  121. error=error))
  122. continue
  123. # add dimensions to chart
  124. for dimension in chart_config['lines']:
  125. try:
  126. self.charts[chart_name].add_dimension(dimension)
  127. except ChartError as error:
  128. self.error("create() => [NOT ADDED] (dimension '{dimension}': {error})".format(dimension=dimension,
  129. error=error))
  130. continue
  131. # add variables to chart
  132. if 'variables' in chart_config:
  133. for variable in chart_config['variables']:
  134. try:
  135. self.charts[chart_name].add_variable(variable)
  136. except ChartError as error:
  137. self.error("create() => [NOT ADDED] (variable '{var}': {error})".format(var=variable,
  138. error=error))
  139. continue
  140. del self.order
  141. del self.definitions
  142. # True if job has at least 1 chart else False
  143. return bool(self.charts)
  144. def run(self):
  145. """
  146. Runs job in thread. Handles retries.
  147. Exits when job failed or timed out.
  148. :return: None
  149. """
  150. job = self._runtime_counters
  151. self.debug('started, update frequency: {freq}'.format(freq=job.update_every))
  152. while True:
  153. job.sleep_until_next()
  154. since = 0
  155. if job.prev_update:
  156. since = int((job.start_real - job.prev_update) * 1e6)
  157. try:
  158. updated = self.update(interval=since)
  159. except Exception as error:
  160. self.error('update() unhandled exception: {error}'.format(error=error))
  161. updated = False
  162. job.runs += 1
  163. if not updated:
  164. job.handle_retries()
  165. else:
  166. job.elapsed = int((monotonic() - job.start_mono) * 1e3)
  167. job.prev_update = job.start_real
  168. job.retries, job.penalty = 0, 0
  169. safe_print(RUNTIME_CHART_UPDATE.format(job_name=self.name,
  170. since_last=since,
  171. elapsed=job.elapsed))
  172. self.debug('update => [{status}] (elapsed time: {elapsed}, failed retries in a row: {retries})'.format(
  173. status='OK' if updated else 'FAILED',
  174. elapsed=job.elapsed if updated else '-',
  175. retries=job.retries))
  176. def update(self, interval):
  177. """
  178. :return:
  179. """
  180. data = self.get_data()
  181. if not data:
  182. self.debug('get_data() returned no data')
  183. return False
  184. elif not isinstance(data, dict):
  185. self.debug('get_data() returned incorrect type data')
  186. return False
  187. updated = False
  188. for chart in self.charts:
  189. if chart.flags.obsoleted:
  190. if chart.can_be_updated(data):
  191. chart.refresh()
  192. else:
  193. continue
  194. elif self.charts.cleanup and chart.penalty >= self.charts.cleanup:
  195. chart.obsolete()
  196. self.info("chart '{0}' was suppressed due to non updating".format(chart.name))
  197. continue
  198. ok = chart.update(data, interval)
  199. if ok:
  200. updated = True
  201. if not updated:
  202. self.debug('none of the charts has been updated')
  203. return updated
  204. def get_data(self):
  205. return self._get_data()
  206. def _get_data(self):
  207. raise NotImplementedError