phpfpm.chart.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. # Description: PHP-FPM netdata python.d module
  3. # Author: Pawel Krupa (paulfantom)
  4. # Author: Ilya Mashchenko (l2isbad)
  5. # SPDX-License-Identifier: GPL-3.0-or-later
  6. import json
  7. import re
  8. from bases.FrameworkServices.UrlService import UrlService
  9. # default module values (can be overridden per job in `config`)
  10. # update_every = 2
  11. priority = 60000
  12. # default job configuration (overridden by python.d.plugin)
  13. # config = {'local': {
  14. # 'update_every': update_every,
  15. # 'retries': retries,
  16. # 'priority': priority,
  17. # 'url': 'http://localhost/status?full&json'
  18. # }}
  19. # charts order (can be overridden if you want less charts, or different order)
  20. POOL_INFO = [
  21. ('active processes', 'active'),
  22. ('max active processes', 'maxActive'),
  23. ('idle processes', 'idle'),
  24. ('accepted conn', 'requests'),
  25. ('max children reached', 'reached'),
  26. ('slow requests', 'slow')
  27. ]
  28. PER_PROCESS_INFO = [
  29. ('request duration', 'ReqDur'),
  30. ('last request cpu', 'ReqCpu'),
  31. ('last request memory', 'ReqMem')
  32. ]
  33. def average(collection):
  34. return sum(collection, 0.0) / max(len(collection), 1)
  35. CALC = [
  36. ('min', min),
  37. ('max', max),
  38. ('avg', average)
  39. ]
  40. ORDER = ['connections', 'requests', 'performance', 'request_duration', 'request_cpu', 'request_mem']
  41. CHARTS = {
  42. 'connections': {
  43. 'options': [None, 'PHP-FPM Active Connections', 'connections', 'active connections', 'phpfpm.connections',
  44. 'line'],
  45. 'lines': [
  46. ['active'],
  47. ['maxActive', 'max active'],
  48. ['idle']
  49. ]
  50. },
  51. 'requests': {
  52. 'options': [None, 'PHP-FPM Requests', 'requests/s', 'requests', 'phpfpm.requests', 'line'],
  53. 'lines': [
  54. ['requests', None, 'incremental']
  55. ]
  56. },
  57. 'performance': {
  58. 'options': [None, 'PHP-FPM Performance', 'status', 'performance', 'phpfpm.performance', 'line'],
  59. 'lines': [
  60. ['reached', 'max children reached'],
  61. ['slow', 'slow requests']
  62. ]
  63. },
  64. 'request_duration': {
  65. 'options': [None, 'PHP-FPM Request Duration', 'milliseconds', 'request duration', 'phpfpm.request_duration',
  66. 'line'],
  67. 'lines': [
  68. ['minReqDur', 'min', 'absolute', 1, 1000],
  69. ['maxReqDur', 'max', 'absolute', 1, 1000],
  70. ['avgReqDur', 'avg', 'absolute', 1, 1000]
  71. ]
  72. },
  73. 'request_cpu': {
  74. 'options': [None, 'PHP-FPM Request CPU', 'percent', 'request CPU', 'phpfpm.request_cpu', 'line'],
  75. 'lines': [
  76. ['minReqCpu', 'min'],
  77. ['maxReqCpu', 'max'],
  78. ['avgReqCpu', 'avg']
  79. ]
  80. },
  81. 'request_mem': {
  82. 'options': [None, 'PHP-FPM Request Memory', 'kilobytes', 'request memory', '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.url = self.configuration.get('url', 'http://localhost/status?full&json')
  94. self.order = ORDER
  95. self.definitions = CHARTS
  96. self.regex = re.compile(r'([a-z][a-z ]+): ([\d.]+)')
  97. self.json = '&json' in self.url or '?json' in self.url
  98. self.json_full = self.url.endswith(('?full&json', '?json&full'))
  99. self.if_all_processes_running = dict([(c_name + p_name, 0) for c_name, func in CALC
  100. for metric, p_name in PER_PROCESS_INFO])
  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, regex=self.regex, 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, regex, 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))