You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.9 KiB

  1. # (Copyright) 2015 ABF OSIELL <http://osiell.com>
  2. # (Copyright) 2018 Creu Blanca
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import psutil
  5. import os
  6. from odoo import api, fields, models
  7. from odoo.tools import config
  8. class NscaServer(models.Model):
  9. _name = "nsca.server"
  10. _description = u"NSCA Server"
  11. name = fields.Char(u"Hostname", required=True)
  12. port = fields.Integer(u"Port", default=5667, required=True)
  13. password = fields.Char(u"Password")
  14. encryption_method = fields.Selection(
  15. selection='_selection_encryption_method',
  16. string=u"Encryption method", default='1', required=True)
  17. config_dir_path = fields.Char(
  18. u"Configuration directory",
  19. compute='_compute_config_dir_path')
  20. config_file_path = fields.Char(
  21. u"Configuration file",
  22. compute='_compute_config_file_path')
  23. node_hostname = fields.Char(
  24. u"Hostname of this node", required=True,
  25. help=u"This is the hostname of the current Odoo node declared in the "
  26. u"monitoring server.")
  27. check_ids = fields.One2many(
  28. 'nsca.check', 'server_id', string=u"Checks")
  29. def _selection_encryption_method(self):
  30. return [
  31. ('0', u"0 - None (Do NOT use this option)"),
  32. ('1', u"1 - Simple XOR"),
  33. ('2', u"2 - DES"),
  34. ('3', u"3 - 3DES (Triple DES)"),
  35. ('4', u"4 - CAST-128"),
  36. ('5', u"5 - CAST-256"),
  37. ('6', u"6 - xTEA"),
  38. ('7', u"7 - 3WAY"),
  39. ('8', u"8 - BLOWFISH"),
  40. ('9', u"9 - TWOFISH"),
  41. ('10', u"10 - LOKI97"),
  42. ('11', u"11 - RC2"),
  43. ('12', u"12 - ARCFOUR"),
  44. ('14', u"14 - RIJNDAEL-128"),
  45. ('15', u"15 - RIJNDAEL-192"),
  46. ('16', u"16 - RIJNDAEL-256"),
  47. ('19', u"19 - WAKE"),
  48. ('20', u"20 - SERPENT"),
  49. ('22', u"22 - ENIGMA (Unix crypt)"),
  50. ('23', u"23 - GOST"),
  51. ('24', u"24 - SAFER64"),
  52. ('25', u"25 - SAFER128"),
  53. ('26', u"26 - SAFER+"),
  54. ]
  55. @api.multi
  56. def _compute_config_dir_path(self):
  57. for server in self:
  58. data_dir_path = config.get('data_dir')
  59. dir_path = os.path.join(
  60. data_dir_path, 'nsca_client', self.env.cr.dbname)
  61. server.config_dir_path = dir_path
  62. @api.multi
  63. def _compute_config_file_path(self):
  64. for server in self:
  65. file_name = 'send_nsca_%s.cfg' % server.id
  66. full_path = os.path.join(server.config_dir_path, file_name)
  67. server.config_file_path = full_path
  68. @api.multi
  69. def write_config_file(self):
  70. for server in self:
  71. try:
  72. os.makedirs(server.config_dir_path)
  73. except OSError as exception:
  74. if exception.errno != os.errno.EEXIST:
  75. raise
  76. with open(server.config_file_path, 'w') as config_file:
  77. if server.password:
  78. config_file.write('password=%s\n' % server.password)
  79. config_file.write(
  80. 'encryption_method=%s\n' % server.encryption_method)
  81. return True
  82. @api.multi
  83. def write(self, vals):
  84. res = super(NscaServer, self).write(vals)
  85. self.write_config_file()
  86. return res
  87. @api.model
  88. def create(self, vals):
  89. res = super(NscaServer, self).create(vals)
  90. res.write_config_file()
  91. return res
  92. @api.model
  93. def current_status(self):
  94. ram = 0
  95. cpu = 0
  96. if psutil:
  97. process = psutil.Process(os.getpid())
  98. # psutil changed its api through versions
  99. processes = [process]
  100. if config.get(
  101. 'workers') and process.parent: # pragma: no cover
  102. if hasattr(process.parent, '__call__'):
  103. process = process.parent()
  104. else:
  105. process = process.parent
  106. if hasattr(process, 'children'):
  107. processes += process.children(True)
  108. elif hasattr(process, 'get_children'):
  109. processes += process.get_children(True)
  110. for process in processes:
  111. if hasattr(process, 'memory_percent'):
  112. ram += process.memory_percent()
  113. if hasattr(process, 'cpu_percent'):
  114. cpu += process.cpu_percent(interval=1)
  115. user_count = 0
  116. if 'bus.presence' in self.env.registry:
  117. user_count = self.env['bus.presence'].search_count([
  118. ('status', '=', 'online'),
  119. ])
  120. performance = {
  121. 'cpu': {
  122. 'value': cpu,
  123. },
  124. 'ram': {
  125. 'value': ram,
  126. },
  127. 'user_count': {
  128. 'value': user_count,
  129. },
  130. }
  131. return 0, u"OK", performance