process_collector.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from __future__ import unicode_literals
  2. import os
  3. from .metrics_core import CounterMetricFamily, GaugeMetricFamily
  4. from .registry import REGISTRY
  5. try:
  6. import resource
  7. _PAGESIZE = resource.getpagesize()
  8. except ImportError:
  9. # Not Unix
  10. _PAGESIZE = 4096
  11. class ProcessCollector(object):
  12. """Collector for Standard Exports such as cpu and memory."""
  13. def __init__(self, namespace='', pid=lambda: 'self', proc='/proc', registry=REGISTRY):
  14. self._namespace = namespace
  15. self._pid = pid
  16. self._proc = proc
  17. if namespace:
  18. self._prefix = namespace + '_process_'
  19. else:
  20. self._prefix = 'process_'
  21. self._ticks = 100.0
  22. try:
  23. self._ticks = os.sysconf('SC_CLK_TCK')
  24. except (ValueError, TypeError, AttributeError, OSError):
  25. pass
  26. self._pagesize = _PAGESIZE
  27. # This is used to test if we can access /proc.
  28. self._btime = 0
  29. try:
  30. self._btime = self._boot_time()
  31. except IOError:
  32. pass
  33. if registry:
  34. registry.register(self)
  35. def _boot_time(self):
  36. with open(os.path.join(self._proc, 'stat'), 'rb') as stat:
  37. for line in stat:
  38. if line.startswith(b'btime '):
  39. return float(line.split()[1])
  40. def collect(self):
  41. if not self._btime:
  42. return []
  43. pid = os.path.join(self._proc, str(self._pid()).strip())
  44. result = []
  45. try:
  46. with open(os.path.join(pid, 'stat'), 'rb') as stat:
  47. parts = (stat.read().split(b')')[-1].split())
  48. vmem = GaugeMetricFamily(self._prefix + 'virtual_memory_bytes',
  49. 'Virtual memory size in bytes.', value=float(parts[20]))
  50. rss = GaugeMetricFamily(self._prefix + 'resident_memory_bytes', 'Resident memory size in bytes.',
  51. value=float(parts[21]) * self._pagesize)
  52. start_time_secs = float(parts[19]) / self._ticks
  53. start_time = GaugeMetricFamily(self._prefix + 'start_time_seconds',
  54. 'Start time of the process since unix epoch in seconds.',
  55. value=start_time_secs + self._btime)
  56. utime = float(parts[11]) / self._ticks
  57. stime = float(parts[12]) / self._ticks
  58. cpu = CounterMetricFamily(self._prefix + 'cpu_seconds_total',
  59. 'Total user and system CPU time spent in seconds.',
  60. value=utime + stime)
  61. result.extend([vmem, rss, start_time, cpu])
  62. except IOError:
  63. pass
  64. try:
  65. with open(os.path.join(pid, 'limits'), 'rb') as limits:
  66. for line in limits:
  67. if line.startswith(b'Max open file'):
  68. max_fds = GaugeMetricFamily(self._prefix + 'max_fds',
  69. 'Maximum number of open file descriptors.',
  70. value=float(line.split()[3]))
  71. break
  72. open_fds = GaugeMetricFamily(self._prefix + 'open_fds',
  73. 'Number of open file descriptors.',
  74. len(os.listdir(os.path.join(pid, 'fd'))))
  75. result.extend([open_fds, max_fds])
  76. except (IOError, OSError):
  77. pass
  78. return result
  79. PROCESS_COLLECTOR = ProcessCollector()
  80. """Default ProcessCollector in default Registry REGISTRY."""