samba.chart.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # -*- coding: utf-8 -*-
  2. # Description: samba netdata python.d module
  3. # Author: Christopher Cox <chris_cox@endlessnow.com>
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. #
  6. # The netdata user needs to be able to be able to sudo the smbstatus program
  7. # without password:
  8. # netdata ALL=(ALL) NOPASSWD: /usr/bin/smbstatus -P
  9. #
  10. # This makes calls to smbstatus -P
  11. #
  12. # This just looks at a couple of values out of syscall, and some from smb2.
  13. #
  14. # The Lesser Ops chart is merely a display of current counter values. They
  15. # didn't seem to change much to me. However, if you notice something changing
  16. # a lot there, bring one or more out into its own chart and make it incremental
  17. # (like find and notify... good examples).
  18. import re
  19. import os
  20. from bases.FrameworkServices.ExecutableService import ExecutableService
  21. from bases.collection import find_binary
  22. disabled_by_default = True
  23. update_every = 5
  24. ORDER = [
  25. 'syscall_rw',
  26. 'smb2_rw',
  27. 'smb2_create_close',
  28. 'smb2_info',
  29. 'smb2_find',
  30. 'smb2_notify',
  31. 'smb2_sm_count'
  32. ]
  33. CHARTS = {
  34. 'syscall_rw': {
  35. 'options': [None, 'R/Ws', 'KiB/s', 'syscall', 'syscall.rw', 'area'],
  36. 'lines': [
  37. ['syscall_sendfile_bytes', 'sendfile', 'incremental', 1, 1024],
  38. ['syscall_recvfile_bytes', 'recvfile', 'incremental', -1, 1024]
  39. ]
  40. },
  41. 'smb2_rw': {
  42. 'options': [None, 'R/Ws', 'KiB/s', 'smb2', 'smb2.rw', 'area'],
  43. 'lines': [
  44. ['smb2_read_outbytes', 'readout', 'incremental', 1, 1024],
  45. ['smb2_write_inbytes', 'writein', 'incremental', -1, 1024],
  46. ['smb2_read_inbytes', 'readin', 'incremental', 1, 1024],
  47. ['smb2_write_outbytes', 'writeout', 'incremental', -1, 1024]
  48. ]
  49. },
  50. 'smb2_create_close': {
  51. 'options': [None, 'Create/Close', 'operations/s', 'smb2', 'smb2.create_close', 'line'],
  52. 'lines': [
  53. ['smb2_create_count', 'create', 'incremental', 1, 1],
  54. ['smb2_close_count', 'close', 'incremental', -1, 1]
  55. ]
  56. },
  57. 'smb2_info': {
  58. 'options': [None, 'Info', 'operations/s', 'smb2', 'smb2.get_set_info', 'line'],
  59. 'lines': [
  60. ['smb2_getinfo_count', 'getinfo', 'incremental', 1, 1],
  61. ['smb2_setinfo_count', 'setinfo', 'incremental', -1, 1]
  62. ]
  63. },
  64. 'smb2_find': {
  65. 'options': [None, 'Find', 'operations/s', 'smb2', 'smb2.find', 'line'],
  66. 'lines': [
  67. ['smb2_find_count', 'find', 'incremental', 1, 1]
  68. ]
  69. },
  70. 'smb2_notify': {
  71. 'options': [None, 'Notify', 'operations/s', 'smb2', 'smb2.notify', 'line'],
  72. 'lines': [
  73. ['smb2_notify_count', 'notify', 'incremental', 1, 1]
  74. ]
  75. },
  76. 'smb2_sm_count': {
  77. 'options': [None, 'Lesser Ops', 'count', 'smb2', 'smb2.sm_counters', 'stacked'],
  78. 'lines': [
  79. ['smb2_tcon_count', 'tcon', 'absolute', 1, 1],
  80. ['smb2_negprot_count', 'negprot', 'absolute', 1, 1],
  81. ['smb2_tdis_count', 'tdis', 'absolute', 1, 1],
  82. ['smb2_cancel_count', 'cancel', 'absolute', 1, 1],
  83. ['smb2_logoff_count', 'logoff', 'absolute', 1, 1],
  84. ['smb2_flush_count', 'flush', 'absolute', 1, 1],
  85. ['smb2_lock_count', 'lock', 'absolute', 1, 1],
  86. ['smb2_keepalive_count', 'keepalive', 'absolute', 1, 1],
  87. ['smb2_break_count', 'break', 'absolute', 1, 1],
  88. ['smb2_sessetup_count', 'sessetup', 'absolute', 1, 1]
  89. ]
  90. }
  91. }
  92. SUDO = 'sudo'
  93. SMBSTATUS = 'smbstatus'
  94. class Service(ExecutableService):
  95. def __init__(self, configuration=None, name=None):
  96. ExecutableService.__init__(self, configuration=configuration, name=name)
  97. self.order = ORDER
  98. self.definitions = CHARTS
  99. self.rgx_smb2 = re.compile(r'(smb2_[^:]+|syscall_.*file_bytes):\s+(\d+)')
  100. def check(self):
  101. smbstatus_binary = find_binary(SMBSTATUS)
  102. if not smbstatus_binary:
  103. self.error("can't locate '{0}' binary".format(SMBSTATUS))
  104. return False
  105. if os.getuid() == 0:
  106. self.command = ' '.join([smbstatus_binary, '-P'])
  107. return ExecutableService.check(self)
  108. sudo_binary = find_binary(SUDO)
  109. if not sudo_binary:
  110. self.error("can't locate '{0}' binary".format(SUDO))
  111. return False
  112. command = [sudo_binary, '-n', '-l', smbstatus_binary, '-P']
  113. smbstatus = '{0} -P'.format(smbstatus_binary)
  114. allowed = self._get_raw_data(command=command)
  115. if not (allowed and allowed[0].strip() == smbstatus):
  116. self.error("not allowed to run sudo for command '{0}'".format(smbstatus))
  117. return False
  118. self.command = ' '.join([sudo_binary, '-n', smbstatus_binary, '-P'])
  119. return ExecutableService.check(self)
  120. def _get_data(self):
  121. """
  122. Format data received from shell command
  123. :return: dict
  124. """
  125. raw_data = self._get_raw_data()
  126. if not raw_data:
  127. return None
  128. parsed = self.rgx_smb2.findall(' '.join(raw_data))
  129. return dict(parsed) or None