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.

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