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.

93 lines
3.2 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_file_path = fields.Char(
  17. u"Configuration file",
  18. compute='_compute_config_file_path')
  19. node_hostname = fields.Char(
  20. u"Hostname of this node", required=True,
  21. help=u"This is the hostname of the current Odoo node declared in the "
  22. u"monitoring server.")
  23. check_ids = fields.One2many(
  24. 'nsca.check', 'server_id', string=u"Checks")
  25. def _selection_encryption_method(self):
  26. return [
  27. ('0', u"0 - None (Do NOT use this option)"),
  28. ('1', u"1 - Simple XOR"),
  29. ('2', u"2 - DES"),
  30. ('3', u"3 - 3DES (Triple DES)"),
  31. ('4', u"4 - CAST-128"),
  32. ('5', u"5 - CAST-256"),
  33. ('6', u"6 - xTEA"),
  34. ('7', u"7 - 3WAY"),
  35. ('8', u"8 - BLOWFISH"),
  36. ('9', u"9 - TWOFISH"),
  37. ('10', u"10 - LOKI97"),
  38. ('11', u"11 - RC2"),
  39. ('12', u"12 - ARCFOUR"),
  40. ('14', u"14 - RIJNDAEL-128"),
  41. ('15', u"15 - RIJNDAEL-192"),
  42. ('16', u"16 - RIJNDAEL-256"),
  43. ('19', u"19 - WAKE"),
  44. ('20', u"20 - SERPENT"),
  45. ('22', u"22 - ENIGMA (Unix crypt)"),
  46. ('23', u"23 - GOST"),
  47. ('24', u"24 - SAFER64"),
  48. ('25', u"25 - SAFER128"),
  49. ('26', u"26 - SAFER+"),
  50. ]
  51. @api.multi
  52. def _compute_config_file_path(self):
  53. for server in self:
  54. data_dir_path = config.get('data_dir')
  55. file_name = 'send_nsca_%s.cfg' % server.id
  56. full_path = os.path.join(data_dir_path, 'nsca_client', file_name)
  57. server.config_file_path = full_path
  58. @api.multi
  59. def write_config_file(self):
  60. for server in self:
  61. data_dir_path = config.get('data_dir')
  62. config_dir_path = os.path.join(data_dir_path, 'nsca_client')
  63. try:
  64. os.makedirs(config_dir_path)
  65. except OSError as exception:
  66. if exception.errno != os.errno.EEXIST:
  67. raise
  68. with open(server.config_file_path, 'w') as config_file:
  69. if server.password:
  70. config_file.write('password=%s\n' % server.password)
  71. config_file.write(
  72. 'encryption_method=%s\n' % server.encryption_method)
  73. return True
  74. @api.multi
  75. def write(self, vals):
  76. res = super(NscaServer, self).write(vals)
  77. self.write_config_file()
  78. return res
  79. @api.model
  80. def create(self, vals):
  81. res = super(NscaServer, self).create(vals)
  82. res.write_config_file()
  83. return res