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.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015-2016 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: # pragma: no cover
  11. psutil = None
  12. import urllib2
  13. from openerp import api, models
  14. from openerp.tools.config import config
  15. SEND_TIMEOUT = 60
  16. class DeadMansSwitchClient(models.AbstractModel):
  17. _name = 'dead.mans.switch.client'
  18. _register = True
  19. @api.model
  20. def _get_data(self):
  21. ram = 0
  22. cpu = 0
  23. if psutil:
  24. process = psutil.Process(os.getpid())
  25. # psutil changed its api through versions
  26. processes = [process]
  27. if config.get('workers') and process.parent: # pragma: no cover
  28. if hasattr(process.parent, '__call__'):
  29. process = process.parent()
  30. else:
  31. process = process.parent
  32. if hasattr(process, 'children'):
  33. processes += process.children(True)
  34. elif hasattr(process, 'get_children'):
  35. processes += process.get_children(True)
  36. for process in processes:
  37. if hasattr(process, 'memory_percent'):
  38. ram += process.memory_percent()
  39. else: # pragma: no cover
  40. ram = None
  41. if hasattr(process, 'cpu_percent'):
  42. cpu += process.cpu_percent()
  43. else: # pragma: no cover
  44. cpu = None
  45. user_count = 0
  46. if 'bus.presence' in self.env.registry:
  47. user_count = self.env['bus.presence'].search_count([
  48. ('status', '=', 'online'),
  49. ])
  50. return {
  51. 'database_uuid': self.env['ir.config_parameter'].get_param(
  52. 'database.uuid'),
  53. 'cpu': cpu,
  54. 'ram': ram,
  55. 'user_count': user_count,
  56. }
  57. @api.model
  58. def alive(self):
  59. url = self.env['ir.config_parameter'].get_param(
  60. 'dead_mans_switch_client.url')
  61. logger = logging.getLogger(__name__)
  62. if not url:
  63. logger.error('No server configured!')
  64. return
  65. timeout = self.env['ir.config_parameter'].get_param(
  66. 'dead_mans_switch_client.send_timeout', SEND_TIMEOUT)
  67. data = self._get_data()
  68. logger.debug('sending %s', data)
  69. urllib2.urlopen(
  70. urllib2.Request(
  71. url,
  72. json.dumps({
  73. 'jsonrpc': '2.0',
  74. 'method': 'call',
  75. 'params': data,
  76. }),
  77. {
  78. 'Content-Type': 'application/json',
  79. }),
  80. timeout=timeout)
  81. @api.model
  82. def _install_default_url(self):
  83. """Set up a default URL."""
  84. conf = self.env["ir.config_parameter"]
  85. name = "dead_mans_switch_client.url"
  86. param = conf.get_param(name)
  87. if not param:
  88. url = "{}/dead_mans_switch/alive".format(
  89. conf.get_param(
  90. "report.url",
  91. conf.get_param(
  92. "web.base.url",
  93. "http://localhost")))
  94. conf.set_param(name, url)