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.

85 lines
2.6 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. class DeadMansSwitchClient(models.AbstractModel):
  15. _name = 'dead.mans.switch.client'
  16. _register = True
  17. @api.model
  18. def _get_data(self):
  19. ram = 0
  20. cpu = 0
  21. if psutil:
  22. process = psutil.Process(os.getpid())
  23. # psutil changed its api through versions
  24. if process.parent:
  25. if hasattr(process.parent, '__call__'):
  26. process = process.parent()
  27. else:
  28. process = process.parent
  29. if hasattr(process, 'memory_percent'):
  30. ram = process.memory_percent()
  31. if hasattr(process, 'cpu_percent'):
  32. cpu = process.cpu_percent()
  33. user_count = 0
  34. if 'im_chat.presence' in self.env.registry:
  35. user_count = len(self.env['im_chat.presence'].search([
  36. ('status', '!=', 'offline'),
  37. ]))
  38. return {
  39. 'database_uuid': self.env['ir.config_parameter'].get_param(
  40. 'database.uuid'),
  41. 'cpu': cpu,
  42. 'ram': ram,
  43. 'user_count': user_count,
  44. }
  45. @api.model
  46. def alive(self):
  47. url = self.env['ir.config_parameter'].get_param(
  48. 'dead_mans_switch_client.url')
  49. logger = logging.getLogger(__name__)
  50. if not url:
  51. logger.error('No server configured!')
  52. return
  53. data = self._get_data()
  54. logger.debug('sending %s', data)
  55. urllib2.urlopen(
  56. urllib2.Request(
  57. url,
  58. json.dumps({
  59. 'jsonrpc': '2.0',
  60. 'method': 'call',
  61. 'params': data,
  62. }),
  63. {
  64. 'Content-Type': 'application/json',
  65. }))
  66. @api.model
  67. def _install_default_url(self):
  68. """Set up a default URL."""
  69. conf = self.env["ir.config_parameter"]
  70. name = "dead_mans_switch_client.url"
  71. param = conf.get_param(name)
  72. if not param:
  73. url = "{}/dead_mans_switch/alive".format(
  74. conf.get_param(
  75. "report.url",
  76. conf.get_param(
  77. "web.base.url",
  78. "http://localhost")))
  79. conf.set_param(name, url)