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.

68 lines
2.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import json
  5. import logging
  6. import os
  7. try:
  8. import psutil
  9. except ImportError:
  10. psutil = None
  11. import urllib2
  12. from openerp import api, models
  13. class DeadMansSwitchClient(models.AbstractModel):
  14. _name = 'dead.mans.switch.client'
  15. _register = True
  16. @api.model
  17. def _get_data(self):
  18. ram = 0
  19. cpu = 0
  20. if psutil:
  21. process = psutil.Process(os.getpid())
  22. # psutil changed its api through versions
  23. if process.parent:
  24. if hasattr(process.parent, '__call__'):
  25. process = process.parent()
  26. else:
  27. process = process.parent
  28. if hasattr(process, 'memory_percent'):
  29. ram = process.memory_percent()
  30. if hasattr(process, 'cpu_percent'):
  31. cpu = process.cpu_percent()
  32. user_count = 0
  33. if 'im_chat.presence' in self.env.registry:
  34. user_count = len(self.env['im_chat.presence'].search([
  35. ('status', '!=', 'offline'),
  36. ]))
  37. return {
  38. 'database_uuid': self.env['ir.config_parameter'].get_param(
  39. 'database.uuid'),
  40. 'cpu': cpu,
  41. 'ram': ram,
  42. 'user_count': user_count,
  43. }
  44. @api.model
  45. def alive(self):
  46. url = self.env['ir.config_parameter'].get_param(
  47. 'dead_mans_switch_client.url')
  48. logger = logging.getLogger(__name__)
  49. if not url:
  50. logger.error('No server configured!')
  51. return
  52. data = self._get_data()
  53. logger.debug('sending %s', data)
  54. urllib2.urlopen(
  55. urllib2.Request(
  56. url,
  57. json.dumps({
  58. 'jsonrpc': '2.0',
  59. 'method': 'call',
  60. 'params': data,
  61. }),
  62. {
  63. 'Content-Type': 'application/json',
  64. }))