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.

108 lines
3.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 logging
  5. from odoo import _, api, fields, models
  6. from odoo.tools.safe_eval import safe_eval
  7. _logger = logging.getLogger(__name__)
  8. class NscaCheck(models.Model):
  9. _name = "nsca.check"
  10. _description = u"NSCA Check"
  11. _inherits = {'ir.cron': 'cron_id'}
  12. cron_id = fields.Many2one(
  13. 'ir.cron', string=u"Cron",
  14. required=True, ondelete='cascade', readonly=True)
  15. server_id = fields.Many2one(
  16. 'nsca.server', string=u"Server", required=True)
  17. service = fields.Char(u"Service", required=True)
  18. nsca_model = fields.Char(u"Model")
  19. nsca_function = fields.Char(u"Method")
  20. nsca_args = fields.Char(u"Arguments")
  21. allow_void_result = fields.Boolean(
  22. u"Allow void result", default=False,
  23. help=u"By default, a CRITICAL message is sent if the method does not "
  24. u"return.\nIf checked, no message will be sent in such a case.")
  25. @api.model
  26. def default_get(self, fields_list):
  27. """Set some default values on the fly, without overriding fields (which
  28. has the side effect to re-create the fields on the current model).
  29. """
  30. res = super(NscaCheck, self).default_get(fields_list)
  31. res['name'] = 'TEMP' # Required on 'ir.cron', replaced later
  32. res['interval_number'] = 10
  33. res['interval_type'] = 'minutes'
  34. return res
  35. @api.multi
  36. def _force_values(self):
  37. """Force some values:
  38. - Compute the name of the NSCA check to be readable
  39. among the others 'ir.cron' records.
  40. """
  41. model = self.env['ir.model'].search([('model', '=', self._name)])
  42. for check in self:
  43. vals = {
  44. 'name': u"%s - %s" % (_(u"NSCA Check"), check.service),
  45. 'model_id': model.id,
  46. 'state': 'code',
  47. 'code': 'model._cron_check(%s,)' % check.id,
  48. 'doall': False,
  49. 'numbercall': -1
  50. }
  51. super(NscaCheck, check).write(vals)
  52. @api.model
  53. def create(self, vals):
  54. if not vals.get('model_id', False):
  55. vals['model_id'] = self.env['ir.model'].search([
  56. ('model', '=', self._name)]).id
  57. if not vals.get('state', False):
  58. vals['state'] = 'code'
  59. check = super(NscaCheck, self).create(vals)
  60. check._force_values()
  61. return check
  62. @api.multi
  63. def write(self, vals):
  64. res = super(NscaCheck, self).write(vals)
  65. if 'service' in vals:
  66. self._force_values()
  67. return res
  68. @api.model
  69. def _cron_check(self, check_id):
  70. self.env['nsca.server']._check_send_nsca_command()
  71. check = self.browse(check_id)
  72. rc, message, performance = 3, "Unknown", {}
  73. try:
  74. NscaModel = self.env[check.nsca_model]
  75. results = {'model': NscaModel}
  76. safe_eval(
  77. 'result = model.%s(%s)' % (
  78. check.nsca_function, check.nsca_args or ''),
  79. results, mode="exec", nocopy=True)
  80. result = results['result']
  81. if not result:
  82. if check.allow_void_result:
  83. return False
  84. raise ValueError(
  85. "'%s' method does not return" % check.nsca_function)
  86. if len(result) == 2:
  87. rc, message = result
  88. else:
  89. rc, message, performance = result
  90. except Exception as exc:
  91. rc, message = 2, "%s" % exc
  92. _logger.warning("%s - %s", check.service, message)
  93. check._send_nsca(rc, message, performance)
  94. return True
  95. @api.multi
  96. def _send_nsca(self, rc, message, performance):
  97. for check in self:
  98. check.server_id._send_nsca(check.service, rc, message, performance)