phpfpm.chart.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # -*- coding: utf-8 -*-
  2. # Description: PHP-FPM netdata python.d module
  3. # Author: Pawel Krupa (paulfantom)
  4. # Author: Ilya Mashchenko (ilyam8)
  5. # SPDX-License-Identifier: GPL-3.0-or-later
  6. import json
  7. import re
  8. from bases.FrameworkServices.UrlService import UrlService
  9. REGEX = re.compile(r'([a-z][a-z ]+): ([\d.]+)')
  10. POOL_INFO = [
  11. ('active processes', 'active'),
  12. ('max active processes', 'maxActive'),
  13. ('idle processes', 'idle'),
  14. ('accepted conn', 'requests'),
  15. ('max children reached', 'reached'),
  16. ('slow requests', 'slow')
  17. ]
  18. PER_PROCESS_INFO = [
  19. ('request duration', 'ReqDur'),
  20. ('last request cpu', 'ReqCpu'),
  21. ('last request memory', 'ReqMem')
  22. ]
  23. def average(collection):
  24. return sum(collection, 0.0) / max(len(collection), 1)
  25. CALC = [
  26. ('min', min),
  27. ('max', max),
  28. ('avg', average)
  29. ]
  30. ORDER = [
  31. 'connections',
  32. 'requests',
  33. 'performance',
  34. 'request_duration',
  35. 'request_cpu',
  36. 'request_mem',
  37. ]
  38. CHARTS = {
  39. 'connections': {
  40. 'options': [None, 'PHP-FPM Active Connections', 'connections', 'active connections', 'phpfpm.connections',
  41. 'line'],
  42. 'lines': [
  43. ['active'],
  44. ['maxActive', 'max active'],
  45. ['idle']
  46. ]
  47. },
  48. 'requests': {
  49. 'options': [None, 'PHP-FPM Requests', 'requests/s', 'requests', 'phpfpm.requests', 'line'],
  50. 'lines': [
  51. ['requests', None, 'incremental']
  52. ]
  53. },
  54. 'performance': {
  55. 'options': [None, 'PHP-FPM Performance', 'status', 'performance', 'phpfpm.performance', 'line'],
  56. 'lines': [
  57. ['reached', 'max children reached'],
  58. ['slow', 'slow requests']
  59. ]
  60. },
  61. 'request_duration': {
  62. 'options': [None, 'PHP-FPM Request Duration', 'milliseconds', 'request duration', 'phpfpm.request_duration',
  63. 'line'],
  64. 'lines': [
  65. ['minReqDur', 'min', 'absolute', 1, 1000],
  66. ['maxReqDur', 'max', 'absolute', 1, 1000],
  67. ['avgReqDur', 'avg', 'absolute', 1, 1000]
  68. ]
  69. },
  70. 'request_cpu': {
  71. 'options': [None, 'PHP-FPM Request CPU', 'percentage', 'request CPU', 'phpfpm.request_cpu', 'line'],
  72. 'lines': [
  73. ['minReqCpu', 'min'],
  74. ['maxReqCpu', 'max'],
  75. ['avgReqCpu', 'avg']
  76. ]
  77. },
  78. 'request_mem': {
  79. 'options': [None, 'PHP-FPM Request Memory', 'KB', 'request memory', 'phpfpm.request_mem', 'line'],
  80. 'lines': [
  81. ['minReqMem', 'min', 'absolute', 1, 1024],
  82. ['maxReqMem', 'max', 'absolute', 1, 1024],
  83. ['avgReqMem', 'avg', 'absolute', 1, 1024]
  84. ]
  85. }
  86. }
  87. class Service(UrlService):
  88. def __init__(self, configuration=None, name=None):
  89. UrlService.__init__(self, configuration=configuration, name=name)
  90. self.order = ORDER
  91. self.definitions = CHARTS
  92. self.url = self.configuration.get('url', 'http://localhost/status?full&json')
  93. self.json = '&json' in self.url or '?json' in self.url
  94. self.json_full = self.url.endswith(('?full&json', '?json&full'))
  95. self.if_all_processes_running = dict(
  96. [(c_name + p_name, 0) for c_name, func in CALC for metric, p_name in PER_PROCESS_INFO]
  97. )
  98. def _get_data(self):
  99. """
  100. Format data received from http request
  101. :return: dict
  102. """
  103. raw = self._get_raw_data()
  104. if not raw:
  105. return None
  106. raw_json = parse_raw_data_(is_json=self.json, raw_data=raw)
  107. # Per Pool info: active connections, requests and performance charts
  108. to_netdata = fetch_data_(raw_data=raw_json, metrics_list=POOL_INFO)
  109. # Per Process Info: duration, cpu and memory charts (min, max, avg)
  110. if self.json_full:
  111. p_info = dict()
  112. to_netdata.update(self.if_all_processes_running) # If all processes are in running state
  113. # Metrics are always 0 if the process is not in Idle state because calculation is done
  114. # when the request processing has terminated
  115. for process in [p for p in raw_json['processes'] if p['state'] == 'Idle']:
  116. p_info.update(fetch_data_(raw_data=process, metrics_list=PER_PROCESS_INFO, pid=str(process['pid'])))
  117. if p_info:
  118. for new_name in PER_PROCESS_INFO:
  119. for name, func in CALC:
  120. to_netdata[name + new_name[1]] = func([p_info[k] for k in p_info if new_name[1] in k])
  121. return to_netdata or None
  122. def fetch_data_(raw_data, metrics_list, pid=''):
  123. """
  124. :param raw_data: dict
  125. :param metrics_list: list
  126. :param pid: str
  127. :return: dict
  128. """
  129. result = dict()
  130. for metric, new_name in metrics_list:
  131. if metric in raw_data:
  132. result[new_name + pid] = float(raw_data[metric])
  133. return result
  134. def parse_raw_data_(is_json, raw_data):
  135. """
  136. :param is_json: bool
  137. :param regex: compiled regular expr
  138. :param raw_data: dict
  139. :return: dict
  140. """
  141. if is_json:
  142. try:
  143. return json.loads(raw_data)
  144. except ValueError:
  145. return dict()
  146. else:
  147. raw_data = ' '.join(raw_data.split())
  148. return dict(REGEX.findall(raw_data))