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.

90 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Therp BV <http://therp.nl>
  3. # © 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. import json
  6. import logging
  7. import os
  8. try:
  9. import psutil
  10. except ImportError:
  11. psutil = None
  12. import urllib2
  13. from openerp import api, models
  14. SEND_TIMEOUT = 60
  15. class DeadMansSwitchClient(models.AbstractModel):
  16. _name = 'dead.mans.switch.client'
  17. _register = True
  18. @api.model
  19. def _get_data(self):
  20. ram = 0
  21. cpu = 0
  22. if psutil:
  23. process = psutil.Process(os.getpid())
  24. # psutil changed its api through versions
  25. if process.parent:
  26. if hasattr(process.parent, '__call__'):
  27. process = process.parent()
  28. else:
  29. process = process.parent
  30. if hasattr(process, 'memory_percent'):
  31. ram = process.memory_percent()
  32. if hasattr(process, 'cpu_percent'):
  33. cpu = process.cpu_percent()
  34. user_count = 0
  35. if 'im_chat.presence' in self.env.registry:
  36. user_count = len(self.env['im_chat.presence'].search([
  37. ('status', '!=', 'offline'),
  38. ]))
  39. return {
  40. 'database_uuid': self.env['ir.config_parameter'].get_param(
  41. 'database.uuid'),
  42. 'cpu': cpu,
  43. 'ram': ram,
  44. 'user_count': user_count,
  45. }
  46. @api.model
  47. def alive(self):
  48. url = self.env['ir.config_parameter'].get_param(
  49. 'dead_mans_switch_client.url')
  50. logger = logging.getLogger(__name__)
  51. if not url:
  52. logger.error('No server configured!')
  53. return
  54. timeout = self.env['ir.config_parameter'].get_param(
  55. 'dead_mans_switch_client.send_timeout', SEND_TIMEOUT)
  56. data = self._get_data()
  57. logger.debug('sending %s', data)
  58. urllib2.urlopen(
  59. urllib2.Request(
  60. url,
  61. json.dumps({
  62. 'jsonrpc': '2.0',
  63. 'method': 'call',
  64. 'params': data,
  65. }),
  66. {
  67. 'Content-Type': 'application/json',
  68. }),
  69. timeout)
  70. @api.model
  71. def _install_default_url(self):
  72. """Set up a default URL."""
  73. conf = self.env["ir.config_parameter"]
  74. name = "dead_mans_switch_client.url"
  75. param = conf.get_param(name)
  76. if not param:
  77. url = "{}/dead_mans_switch/alive".format(
  78. conf.get_param(
  79. "report.url",
  80. conf.get_param(
  81. "web.base.url",
  82. "http://localhost")))
  83. conf.set_param(name, url)