puppet.chart.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. # Description: puppet netdata python.d module
  3. # Author: Andrey Galkin <andrey@futoin.org> (andvgal)
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. #
  6. # This module should work both with OpenSource and PE versions
  7. # of PuppetServer and PuppetDB.
  8. #
  9. # NOTE: PuppetDB may be configured to require proper TLS
  10. # client certificate for security reasons. Use tls_key_file
  11. # and tls_cert_file options then.
  12. #
  13. import socket
  14. from json import loads
  15. from bases.FrameworkServices.UrlService import UrlService
  16. update_every = 5
  17. MiB = 1 << 20
  18. CPU_SCALE = 1000
  19. ORDER = [
  20. 'jvm_heap',
  21. 'jvm_nonheap',
  22. 'cpu',
  23. 'fd_open',
  24. ]
  25. CHARTS = {
  26. 'jvm_heap': {
  27. 'options': [None, 'JVM Heap', 'MiB', 'resources', 'puppet.jvm', 'area'],
  28. 'lines': [
  29. ['jvm_heap_committed', 'committed', 'absolute', 1, MiB],
  30. ['jvm_heap_used', 'used', 'absolute', 1, MiB],
  31. ],
  32. 'variables': [
  33. ['jvm_heap_max'],
  34. ['jvm_heap_init'],
  35. ],
  36. },
  37. 'jvm_nonheap': {
  38. 'options': [None, 'JVM Non-Heap', 'MiB', 'resources', 'puppet.jvm', 'area'],
  39. 'lines': [
  40. ['jvm_nonheap_committed', 'committed', 'absolute', 1, MiB],
  41. ['jvm_nonheap_used', 'used', 'absolute', 1, MiB],
  42. ],
  43. 'variables': [
  44. ['jvm_nonheap_max'],
  45. ['jvm_nonheap_init'],
  46. ],
  47. },
  48. 'cpu': {
  49. 'options': [None, 'CPU usage', 'percentage', 'resources', 'puppet.cpu', 'stacked'],
  50. 'lines': [
  51. ['cpu_time', 'execution', 'absolute', 1, CPU_SCALE],
  52. ['gc_time', 'GC', 'absolute', 1, CPU_SCALE],
  53. ]
  54. },
  55. 'fd_open': {
  56. 'options': [None, 'File Descriptors', 'descriptors', 'resources', 'puppet.fdopen', 'line'],
  57. 'lines': [
  58. ['fd_used', 'used', 'absolute'],
  59. ],
  60. 'variables': [
  61. ['fd_max'],
  62. ],
  63. },
  64. }
  65. class Service(UrlService):
  66. def __init__(self, configuration=None, name=None):
  67. UrlService.__init__(self, configuration=configuration, name=name)
  68. self.order = ORDER
  69. self.definitions = CHARTS
  70. self.url = 'https://{0}:8140'.format(socket.getfqdn())
  71. def _get_data(self):
  72. # NOTE: there are several ways to retrieve data
  73. # 1. Only PE versions:
  74. # https://puppet.com/docs/pe/2018.1/api_status/status_api_metrics_endpoints.html
  75. # 2. Individual Metrics API (JMX):
  76. # https://puppet.com/docs/pe/2018.1/api_status/metrics_api.html
  77. # 3. Extended status at debug level:
  78. # https://puppet.com/docs/pe/2018.1/api_status/status_api_json_endpoints.html
  79. #
  80. # For sake of simplicity and efficiency the status one is used..
  81. raw_data = self._get_raw_data(self.url + '/status/v1/services?level=debug')
  82. if raw_data is None:
  83. return None
  84. raw_data = loads(raw_data)
  85. data = {}
  86. try:
  87. try:
  88. jvm_metrics = raw_data['status-service']['status']['experimental']['jvm-metrics']
  89. except KeyError:
  90. jvm_metrics = raw_data['status-service']['status']['jvm-metrics']
  91. heap_mem = jvm_metrics['heap-memory']
  92. non_heap_mem = jvm_metrics['non-heap-memory']
  93. for k in ['max', 'committed', 'used', 'init']:
  94. data['jvm_heap_' + k] = heap_mem[k]
  95. data['jvm_nonheap_' + k] = non_heap_mem[k]
  96. fd_open = jvm_metrics['file-descriptors']
  97. data['fd_max'] = fd_open['max']
  98. data['fd_used'] = fd_open['used']
  99. data['cpu_time'] = int(jvm_metrics['cpu-usage'] * CPU_SCALE)
  100. data['gc_time'] = int(jvm_metrics['gc-cpu-usage'] * CPU_SCALE)
  101. except KeyError:
  102. pass
  103. return data or None