uwsgi.chart.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # -*- coding: utf-8 -*-
  2. # Description: uwsgi netdata python.d module
  3. # Author: Robbert Segeren (robbert-ef)
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. import json
  6. from copy import deepcopy
  7. from bases.FrameworkServices.SocketService import SocketService
  8. ORDER = [
  9. 'requests',
  10. 'tx',
  11. 'avg_rt',
  12. 'memory_rss',
  13. 'memory_vsz',
  14. 'exceptions',
  15. 'harakiri',
  16. 'respawn',
  17. ]
  18. DYNAMIC_CHARTS = [
  19. 'requests',
  20. 'tx',
  21. 'avg_rt',
  22. 'memory_rss',
  23. 'memory_vsz',
  24. ]
  25. # NOTE: lines are created dynamically in `check()` method
  26. CHARTS = {
  27. 'requests': {
  28. 'options': [None, 'Requests', 'requests/s', 'requests', 'uwsgi.requests', 'stacked'],
  29. 'lines': [
  30. ['requests', 'requests', 'incremental']
  31. ]
  32. },
  33. 'tx': {
  34. 'options': [None, 'Transmitted data', 'KiB/s', 'requests', 'uwsgi.tx', 'stacked'],
  35. 'lines': [
  36. ['tx', 'tx', 'incremental']
  37. ]
  38. },
  39. 'avg_rt': {
  40. 'options': [None, 'Average request time', 'milliseconds', 'requests', 'uwsgi.avg_rt', 'line'],
  41. 'lines': [
  42. ['avg_rt', 'avg_rt', 'absolute']
  43. ]
  44. },
  45. 'memory_rss': {
  46. 'options': [None, 'RSS (Resident Set Size)', 'MiB', 'memory', 'uwsgi.memory_rss', 'stacked'],
  47. 'lines': [
  48. ['memory_rss', 'memory_rss', 'absolute', 1, 1 << 20]
  49. ]
  50. },
  51. 'memory_vsz': {
  52. 'options': [None, 'VSZ (Virtual Memory Size)', 'MiB', 'memory', 'uwsgi.memory_vsz', 'stacked'],
  53. 'lines': [
  54. ['memory_vsz', 'memory_vsz', 'absolute', 1, 1 << 20]
  55. ]
  56. },
  57. 'exceptions': {
  58. 'options': [None, 'Exceptions', 'exceptions', 'exceptions', 'uwsgi.exceptions', 'line'],
  59. 'lines': [
  60. ['exceptions', 'exceptions', 'incremental']
  61. ]
  62. },
  63. 'harakiri': {
  64. 'options': [None, 'Harakiris', 'harakiris', 'harakiris', 'uwsgi.harakiris', 'line'],
  65. 'lines': [
  66. ['harakiri_count', 'harakiris', 'incremental']
  67. ]
  68. },
  69. 'respawn': {
  70. 'options': [None, 'Respawns', 'respawns', 'respawns', 'uwsgi.respawns', 'line'],
  71. 'lines': [
  72. ['respawn_count', 'respawns', 'incremental']
  73. ]
  74. },
  75. }
  76. class Service(SocketService):
  77. def __init__(self, configuration=None, name=None):
  78. super(Service, self).__init__(configuration=configuration, name=name)
  79. self.order = ORDER
  80. self.definitions = deepcopy(CHARTS)
  81. self.url = self.configuration.get('host', 'localhost')
  82. self.port = self.configuration.get('port', 1717)
  83. # Clear dynamic dimensions, these are added during `_get_data()` to allow adding workers at run-time
  84. for chart in DYNAMIC_CHARTS:
  85. self.definitions[chart]['lines'] = []
  86. self.last_result = {}
  87. self.workers = []
  88. def read_data(self):
  89. """
  90. Read data from socket and parse as JSON.
  91. :return: (dict) stats
  92. """
  93. raw_data = self._get_raw_data()
  94. if not raw_data:
  95. return None
  96. try:
  97. return json.loads(raw_data)
  98. except ValueError as err:
  99. self.error(err)
  100. return None
  101. def check(self):
  102. """
  103. Parse configuration and check if we can read data.
  104. :return: boolean
  105. """
  106. self._parse_config()
  107. return bool(self.read_data())
  108. def add_worker_dimensions(self, key):
  109. """
  110. Helper to add dimensions for a worker.
  111. :param key: (int or str) worker identifier
  112. :return:
  113. """
  114. for chart in DYNAMIC_CHARTS:
  115. for line in CHARTS[chart]['lines']:
  116. dimension_id = '{}_{}'.format(line[0], key)
  117. dimension_name = str(key)
  118. dimension = [dimension_id, dimension_name] + line[2:]
  119. self.charts[chart].add_dimension(dimension)
  120. @staticmethod
  121. def _check_raw_data(data):
  122. # The server will close the connection when it's done sending
  123. # data, so just keep looping until that happens.
  124. return False
  125. def _get_data(self):
  126. """
  127. Read data from socket
  128. :return: dict
  129. """
  130. stats = self.read_data()
  131. if not stats:
  132. return None
  133. result = {
  134. 'exceptions': 0,
  135. 'harakiri_count': 0,
  136. 'respawn_count': 0,
  137. }
  138. for worker in stats['workers']:
  139. key = worker['pid']
  140. # Add dimensions for new workers
  141. if key not in self.workers:
  142. self.add_worker_dimensions(key)
  143. self.workers.append(key)
  144. result['requests_{}'.format(key)] = worker['requests']
  145. result['tx_{}'.format(key)] = worker['tx']
  146. result['avg_rt_{}'.format(key)] = worker['avg_rt']
  147. # avg_rt is not reset by uwsgi, so reset here
  148. if self.last_result.get('requests_{}'.format(key)) == worker['requests']:
  149. result['avg_rt_{}'.format(key)] = 0
  150. result['memory_rss_{}'.format(key)] = worker['rss']
  151. result['memory_vsz_{}'.format(key)] = worker['vsz']
  152. result['exceptions'] += worker['exceptions']
  153. result['harakiri_count'] += worker['harakiri_count']
  154. result['respawn_count'] += worker['respawn_count']
  155. self.last_result = result
  156. return result