process_collector.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. TICKS_PER_SEC = 100
  17. BYTES_PER_PAGE = 4096
  18. HAVE_PROC_STAT = os.path.exists("/proc/stat")
  19. HAVE_PROC_SELF_STAT = os.path.exists("/proc/self/stat")
  20. HAVE_PROC_SELF_LIMITS = os.path.exists("/proc/self/limits")
  21. HAVE_PROC_SELF_FD = os.path.exists("/proc/self/fd")
  22. # Field indexes from /proc/self/stat, taken from the proc(5) manpage
  23. STAT_FIELDS = {
  24. "utime": 14,
  25. "stime": 15,
  26. "starttime": 22,
  27. "vsize": 23,
  28. "rss": 24,
  29. }
  30. stats = {}
  31. # In order to report process_start_time_seconds we need to know the
  32. # machine's boot time, because the value in /proc/self/stat is relative to
  33. # this
  34. boot_time = None
  35. if HAVE_PROC_STAT:
  36. with open("/proc/stat") as _procstat:
  37. for line in _procstat:
  38. if line.startswith("btime "):
  39. boot_time = int(line.split()[1])
  40. def update_resource_metrics():
  41. if HAVE_PROC_SELF_STAT:
  42. global stats
  43. with open("/proc/self/stat") as s:
  44. line = s.read()
  45. # line is PID (command) more stats go here ...
  46. raw_stats = line.split(") ", 1)[1].split(" ")
  47. for (name, index) in STAT_FIELDS.iteritems():
  48. # subtract 3 from the index, because proc(5) is 1-based, and
  49. # we've lost the first two fields in PID and COMMAND above
  50. stats[name] = int(raw_stats[index - 3])
  51. def _count_fds():
  52. # Not every OS will have a /proc/self/fd directory
  53. if not HAVE_PROC_SELF_FD:
  54. return 0
  55. return len(os.listdir("/proc/self/fd"))
  56. def register_process_collector(process_metrics):
  57. process_metrics.register_collector(update_resource_metrics)
  58. if HAVE_PROC_SELF_STAT:
  59. process_metrics.register_callback(
  60. "cpu_user_seconds_total",
  61. lambda: float(stats["utime"]) / TICKS_PER_SEC
  62. )
  63. process_metrics.register_callback(
  64. "cpu_system_seconds_total",
  65. lambda: float(stats["stime"]) / TICKS_PER_SEC
  66. )
  67. process_metrics.register_callback(
  68. "cpu_seconds_total",
  69. lambda: (float(stats["utime"] + stats["stime"])) / TICKS_PER_SEC
  70. )
  71. process_metrics.register_callback(
  72. "virtual_memory_bytes",
  73. lambda: int(stats["vsize"])
  74. )
  75. process_metrics.register_callback(
  76. "resident_memory_bytes",
  77. lambda: int(stats["rss"]) * BYTES_PER_PAGE
  78. )
  79. process_metrics.register_callback(
  80. "start_time_seconds",
  81. lambda: boot_time + int(stats["starttime"]) / TICKS_PER_SEC
  82. )
  83. if HAVE_PROC_SELF_FD:
  84. process_metrics.register_callback(
  85. "open_fds",
  86. lambda: _count_fds()
  87. )
  88. if HAVE_PROC_SELF_LIMITS:
  89. def _get_max_fds():
  90. with open("/proc/self/limits") as limits:
  91. for line in limits:
  92. if not line.startswith("Max open files "):
  93. continue
  94. # Line is Max open files $SOFT $HARD
  95. return int(line.split()[3])
  96. return None
  97. process_metrics.register_callback(
  98. "max_fds",
  99. lambda: _get_max_fds()
  100. )