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.

76 lines
2.6 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.osv import orm
  13. from openerp.tools.config import config
  14. class DeadMansSwitchClient(orm.AbstractModel):
  15. _name = 'dead.mans.switch.client'
  16. _register = True
  17. def _get_data(self, cr, uid, context=None):
  18. ram = 0
  19. cpu = 0
  20. if psutil:
  21. process = psutil.Process(os.getpid())
  22. # psutil changed its api through versions
  23. processes = [process]
  24. if config.get('workers') and process.parent:
  25. if hasattr(process.parent, '__call__'):
  26. process = process.parent()
  27. else:
  28. process = process.parent
  29. if hasattr(process, 'children'):
  30. processes += process.children(True)
  31. elif hasattr(process, 'get_children'):
  32. processes += process.get_children(True)
  33. for process in processes:
  34. if hasattr(process, 'memory_percent'):
  35. ram += process.memory_percent()
  36. elif hasattr(process, 'get_memory_percent'):
  37. ram += process.get_memory_percent()
  38. else:
  39. ram = None
  40. if hasattr(process, 'cpu_percent'):
  41. cpu += process.cpu_percent()
  42. elif hasattr(process, 'get_cpu_percent'):
  43. cpu += process.get_cpu_percent()
  44. else:
  45. cpu = None
  46. return {
  47. 'database_uuid': self.pool['ir.config_parameter'].get_param(
  48. cr, uid, 'database.uuid', context=context),
  49. 'cpu': cpu,
  50. 'ram': ram,
  51. 'user_count': None,
  52. }
  53. def alive(self, cr, uid, context=None):
  54. url = self.pool['ir.config_parameter'].get_param(
  55. cr, uid, 'dead_mans_switch_client.url')
  56. logger = logging.getLogger(__name__)
  57. if not url:
  58. logger.error('No server configured!')
  59. return
  60. data = self._get_data(cr, uid, context=context)
  61. logger.debug('sending %s', data)
  62. urllib2.urlopen(
  63. urllib2.Request(
  64. url,
  65. json.dumps({
  66. 'jsonrpc': '2.0',
  67. 'method': 'call',
  68. 'params': data,
  69. }),
  70. {
  71. 'Content-Type': 'application/json',
  72. }))