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.

101 lines
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 ABF OSIELL <http://osiell.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import os
  5. from openerp import api, fields, models
  6. from openerp.tools import config
  7. class NscaServer(models.Model):
  8. _name = "nsca.server"
  9. _description = u"NSCA Server"
  10. name = fields.Char(u"Hostname", required=True)
  11. port = fields.Integer(u"Port", default=5667, required=True)
  12. password = fields.Char(u"Password")
  13. encryption_method = fields.Selection(
  14. selection='_selection_encryption_method',
  15. string=u"Encryption method", default='1', required=True)
  16. config_dir_path = fields.Char(
  17. u"Configuration directory",
  18. compute='_compute_config_dir_path')
  19. config_file_path = fields.Char(
  20. u"Configuration file",
  21. compute='_compute_config_file_path')
  22. node_hostname = fields.Char(
  23. u"Hostname of this node", required=True,
  24. help=u"This is the hostname of the current Odoo node declared in the "
  25. u"monitoring server.")
  26. check_ids = fields.One2many(
  27. 'nsca.check', 'server_id', string=u"Checks")
  28. def _selection_encryption_method(self):
  29. return [
  30. ('0', u"0 - None (Do NOT use this option)"),
  31. ('1', u"1 - Simple XOR"),
  32. ('2', u"2 - DES"),
  33. ('3', u"3 - 3DES (Triple DES)"),
  34. ('4', u"4 - CAST-128"),
  35. ('5', u"5 - CAST-256"),
  36. ('6', u"6 - xTEA"),
  37. ('7', u"7 - 3WAY"),
  38. ('8', u"8 - BLOWFISH"),
  39. ('9', u"9 - TWOFISH"),
  40. ('10', u"10 - LOKI97"),
  41. ('11', u"11 - RC2"),
  42. ('12', u"12 - ARCFOUR"),
  43. ('14', u"14 - RIJNDAEL-128"),
  44. ('15', u"15 - RIJNDAEL-192"),
  45. ('16', u"16 - RIJNDAEL-256"),
  46. ('19', u"19 - WAKE"),
  47. ('20', u"20 - SERPENT"),
  48. ('22', u"22 - ENIGMA (Unix crypt)"),
  49. ('23', u"23 - GOST"),
  50. ('24', u"24 - SAFER64"),
  51. ('25', u"25 - SAFER128"),
  52. ('26', u"26 - SAFER+"),
  53. ]
  54. @api.multi
  55. def _compute_config_dir_path(self):
  56. for server in self:
  57. data_dir_path = config.get('data_dir')
  58. dir_path = os.path.join(
  59. data_dir_path, 'nsca_client', self.env.cr.dbname)
  60. server.config_dir_path = dir_path
  61. @api.multi
  62. def _compute_config_file_path(self):
  63. for server in self:
  64. file_name = 'send_nsca_%s.cfg' % server.id
  65. full_path = os.path.join(server.config_dir_path, file_name)
  66. server.config_file_path = full_path
  67. @api.multi
  68. def write_config_file(self):
  69. for server in self:
  70. try:
  71. os.makedirs(server.config_dir_path)
  72. except OSError as exception:
  73. if exception.errno != os.errno.EEXIST:
  74. raise
  75. with open(server.config_file_path, 'w') as config_file:
  76. if server.password:
  77. config_file.write('password=%s\n' % server.password)
  78. config_file.write(
  79. 'encryption_method=%s\n' % server.encryption_method)
  80. return True
  81. @api.multi
  82. def write(self, vals):
  83. res = super(NscaServer, self).write(vals)
  84. self.write_config_file()
  85. return res
  86. @api.model
  87. def create(self, vals):
  88. res = super(NscaServer, self).create(vals)
  89. res.write_config_file()
  90. return res