phpfpm.chart.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 Requests Duration Among All Idle Processes', 'milliseconds', 'request duration',
  63. 'phpfpm.request_duration',
  64. 'line'],
  65. 'lines': [
  66. ['minReqDur', 'min', 'absolute', 1, 1000],
  67. ['maxReqDur', 'max', 'absolute', 1, 1000],
  68. ['avgReqDur', 'avg', 'absolute', 1, 1000]
  69. ]
  70. },
  71. 'request_cpu': {
  72. 'options': [None, 'PHP-FPM Last Request CPU Usage Among All Idle Processes', 'percentage', 'request CPU',
  73. 'phpfpm.request_cpu', 'line'],
  74. 'lines': [
  75. ['minReqCpu', 'min'],
  76. ['maxReqCpu', 'max'],
  77. ['avgReqCpu', 'avg']
  78. ]
  79. },
  80. 'request_mem': {
  81. 'options': [None, 'PHP-FPM Last Request Memory Usage Among All Idle Processes', 'KB', 'request memory',
  82. 'phpfpm.request_mem', 'line'],
  83. 'lines': [
  84. ['minReqMem', 'min', 'absolute', 1, 1024],
  85. ['maxReqMem', 'max', 'absolute', 1, 1024],
  86. ['avgReqMem', 'avg', 'absolute', 1, 1024]
  87. ]
  88. }
  89. }
  90. class Service(UrlService):
  91. def __init__(self, configuration=None, name=None):
  92. UrlService.__init__(self, configuration=configuration, name=name)
  93. self.order = ORDER
  94. self.definitions = CHARTS
  95. self.url = self.configuration.get('url', 'http://localhost/status?full&json')
  96. self.json = '&json' in self.url or '?json' in self.url
  97. self.json_full = self.url.endswith(('?full&json', '?json&full'))
  98. self.if_all_processes_running = dict(
  99. [(c_name + p_name, 0) for c_name, func in CALC for metric, p_name in PER_PROCESS_INFO]
  100. )
  101. def _get_data(self):
  102. """
  103. Format data received from http request
  104. :return: dict
  105. """
  106. raw = self._get_raw_data()
  107. if not raw:
  108. return None
  109. raw_json = parse_raw_data_(is_json=self.json, raw_data=raw)
  110. # Per Pool info: active connections, requests and performance charts
  111. to_netdata = fetch_data_(raw_data=raw_json, metrics_list=POOL_INFO)
  112. # Per Process Info: duration, cpu and memory charts (min, max, avg)
  113. if self.json_full:
  114. p_info = dict()
  115. to_netdata.update(self.if_all_processes_running) # If all processes are in running state
  116. # Metrics are always 0 if the process is not in Idle state because calculation is done
  117. # when the request processing has terminated
  118. for process in [p for p in raw_json['processes'] if p['state'] == 'Idle']:
  119. p_info.update(fetch_data_(raw_data=process, metrics_list=PER_PROCESS_INFO, pid=str(process['pid'])))
  120. if p_info:
  121. for new_name in PER_PROCESS_INFO:
  122. for name, func in CALC:
  123. to_netdata[name + new_name[1]] = func([p_info[k] for k in p_info if new_name[1] in k])
  124. return to_netdata or None
  125. def fetch_data_(raw_data, metrics_list, pid=''):
  126. """
  127. :param raw_data: dict
  128. :param metrics_list: list
  129. :param pid: str
  130. :return: dict
  131. """
  132. result = dict()
  133. for metric, new_name in metrics_list:
  134. if metric in raw_data:
  135. result[new_name + pid] = float(raw_data[metric])
  136. return result
  137. def parse_raw_data_(is_json, raw_data):
  138. """
  139. :param is_json: bool
  140. :param regex: compiled regular expr
  141. :param raw_data: dict
  142. :return: dict
  143. """
  144. if is_json:
  145. try:
  146. return json.loads(raw_data)
  147. except ValueError:
  148. return dict()
  149. else:
  150. raw_data = ' '.join(raw_data.split())
  151. return dict(REGEX.findall(raw_data))