Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 1 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | # Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 6 | # Use of this source code is governed by a BSD-style license that can be |
| 7 | # found in the LICENSE file. |
| 8 | |
| 9 | """System metrics.""" |
| 10 | |
| 11 | from __future__ import print_function |
| 12 | |
| 13 | import errno |
| 14 | import os |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 15 | import time |
| 16 | |
| 17 | import psutil |
| 18 | |
| 19 | from chromite.lib import cros_logging as logging |
| 20 | from infra_libs import ts_mon |
| 21 | |
Allen Li | 79317bb | 2016-12-16 18:25:07 -0800 | [diff] [blame] | 22 | logger = logging.getLogger(__name__) |
| 23 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 24 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 25 | _cpu_count_metric = ts_mon.GaugeMetric( |
| 26 | 'dev/cpu/count', |
| 27 | description='Number of CPU cores.') |
| 28 | _cpu_time_metric = ts_mon.FloatMetric( |
| 29 | 'dev/cpu/time', |
| 30 | description='percentage of time spent by the CPU ' |
| 31 | 'in different states.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 32 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 33 | _disk_free_metric = ts_mon.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 34 | 'dev/disk/free', |
| 35 | description='Available bytes on disk partition.', |
| 36 | units=ts_mon.MetricsDataUnits.BYTES) |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 37 | _disk_total_metric = ts_mon.GaugeMetric( |
| 38 | 'dev/disk/total', |
| 39 | description='Total bytes on disk partition.', |
| 40 | units=ts_mon.MetricsDataUnits.BYTES) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 41 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 42 | _inodes_free_metric = ts_mon.GaugeMetric( |
| 43 | 'dev/inodes/free', |
| 44 | description='Number of available inodes on ' |
| 45 | 'disk partition (unix only).') |
| 46 | _inodes_total_metric = ts_mon.GaugeMetric( |
| 47 | 'dev/inodes/total', |
| 48 | description='Number of possible inodes on ' |
| 49 | 'disk partition (unix only)') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 50 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 51 | _mem_free_metric = ts_mon.GaugeMetric( |
| 52 | 'dev/mem/free', |
| 53 | description='Amount of memory available to a ' |
| 54 | 'process (in Bytes). Buffers are considered ' |
| 55 | 'free memory.', |
| 56 | units=ts_mon.MetricsDataUnits.BYTES) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 57 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 58 | _mem_total_metric = ts_mon.GaugeMetric( |
| 59 | 'dev/mem/total', |
| 60 | description='Total physical memory in Bytes.', |
| 61 | units=ts_mon.MetricsDataUnits.BYTES) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 62 | |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame^] | 63 | _BOOT_TIME = psutil.boot_time() |
Allen Li | c987fc9 | 2017-03-02 14:54:51 -0800 | [diff] [blame] | 64 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 65 | _disk_read_metric = ts_mon.CounterMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame^] | 66 | 'dev/disk/read', start_time=_BOOT_TIME, |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 67 | description='Number of Bytes read on disk.', |
| 68 | units=ts_mon.MetricsDataUnits.BYTES) |
| 69 | _disk_write_metric = ts_mon.CounterMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame^] | 70 | 'dev/disk/write', start_time=_BOOT_TIME, |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 71 | description='Number of Bytes written on disk.', |
| 72 | units=ts_mon.MetricsDataUnits.BYTES) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 73 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 74 | _uptime_metric = ts_mon.GaugeMetric( |
| 75 | 'dev/uptime', |
| 76 | description='Machine uptime, in seconds.', |
| 77 | units=ts_mon.MetricsDataUnits.SECONDS) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 78 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 79 | _proc_count_metric = ts_mon.GaugeMetric( |
| 80 | 'dev/proc/count', |
| 81 | description='Number of processes currently running.') |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 82 | _autoserv_proc_count_metric = ts_mon.GaugeMetric( |
| 83 | 'dev/proc/autoserv_count', |
| 84 | description='Number of autoserv processes currently running.') |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 85 | _sysmon_proc_count_metric = ts_mon.GaugeMetric( |
| 86 | 'dev/proc/sysmon_count', |
| 87 | description='Number of sysmon processes currently running.') |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 88 | _load_average_metric = ts_mon.FloatMetric( |
| 89 | 'dev/proc/load_average', |
| 90 | description='Number of processes currently ' |
| 91 | 'in the system run queue.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 92 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 93 | # ts_mon pipeline uses backend clocks when assigning timestamps to metric |
| 94 | # points. By comparing point timestamp to the point value (i.e. time by |
| 95 | # machine's local clock), we can potentially detect some anomalies (clock |
| 96 | # drift, unusually high metrics pipeline delay, completely wrong clocks, etc). |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 97 | # |
| 98 | # It is important to gather this metric right before the flush. |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 99 | _unix_time_metric = ts_mon.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 100 | 'dev/unix_time', |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 101 | description='Number of milliseconds since epoch' |
| 102 | ' based on local machine clock.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 103 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 104 | _os_name_metric = ts_mon.StringMetric( |
| 105 | 'proc/os/name', |
| 106 | description='OS name on the machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 107 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 108 | _os_version_metric = ts_mon.StringMetric( |
| 109 | 'proc/os/version', |
| 110 | description='OS version on the machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 111 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 112 | _os_arch_metric = ts_mon.StringMetric( |
| 113 | 'proc/os/arch', |
| 114 | description='OS architecture on this machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 115 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 116 | _python_arch_metric = ts_mon.StringMetric( |
| 117 | 'proc/python/arch', |
| 118 | description='python userland ' |
| 119 | 'architecture on this machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 120 | |
| 121 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 122 | def collect_uptime(): |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame^] | 123 | _uptime_metric.set(int(time.time() - _BOOT_TIME)) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 124 | |
| 125 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 126 | def collect_cpu_info(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 127 | _cpu_count_metric.set(psutil.cpu_count()) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 128 | |
| 129 | times = psutil.cpu_times_percent() |
| 130 | for mode in ('user', 'system', 'idle'): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 131 | _cpu_time_metric.set(getattr(times, mode), {'mode': mode}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 132 | |
| 133 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 134 | def collect_disk_info(mountpoints=None): |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 135 | if mountpoints is None: |
| 136 | mountpoints = [disk.mountpoint for disk in psutil.disk_partitions()] |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 137 | for mountpoint in mountpoints: |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 138 | _collect_disk_info_single(mountpoint) |
| 139 | _collect_fs_inode_info(mountpoint) |
| 140 | _collect_disk_io_info() |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 141 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 142 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 143 | def _collect_disk_info_single(mountpoint): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 144 | fields = {'path': mountpoint} |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 145 | |
| 146 | try: |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 147 | usage = psutil.disk_usage(mountpoint) |
| 148 | except OSError as ex: |
| 149 | if ex.errno == errno.ENOENT: |
| 150 | # This happens on Windows when querying a removable drive that |
| 151 | # doesn't have any media inserted right now. |
| 152 | pass |
| 153 | else: |
| 154 | raise |
| 155 | else: |
| 156 | _disk_free_metric.set(usage.free, fields=fields) |
| 157 | _disk_total_metric.set(usage.total, fields=fields) |
| 158 | |
| 159 | # inode counts are only available on Unix. |
| 160 | if os.name == 'posix': |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 161 | _collect_fs_inode_info(mountpoint) |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 162 | |
| 163 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 164 | def _collect_fs_inode_info(mountpoint): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 165 | fields = {'path': mountpoint} |
| 166 | stats = os.statvfs(mountpoint) |
| 167 | _inodes_free_metric.set(stats.f_favail, fields=fields) |
| 168 | _inodes_total_metric.set(stats.f_files, fields=fields) |
| 169 | |
| 170 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 171 | def _collect_disk_io_info(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 172 | try: |
| 173 | disk_counters = psutil.disk_io_counters(perdisk=True).iteritems() |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 174 | except RuntimeError as ex: |
| 175 | if "couldn't find any physical disk" in str(ex): |
| 176 | # Disk performance counters aren't enabled on Windows. |
| 177 | pass |
| 178 | else: |
| 179 | raise |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 180 | else: |
| 181 | for disk, counters in disk_counters: |
| 182 | fields = {'disk': disk} |
| 183 | _disk_read_metric.set(counters.read_bytes, fields=fields) |
| 184 | _disk_write_metric.set(counters.write_bytes, fields=fields) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 185 | |
| 186 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 187 | def collect_mem_info(): |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 188 | # We don't report mem.used because (due to virtual memory) it is not |
| 189 | # useful. |
| 190 | mem = psutil.virtual_memory() |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 191 | _mem_free_metric.set(mem.available) |
| 192 | _mem_total_metric.set(mem.total) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 193 | |
| 194 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 195 | def collect_proc_info(): |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 196 | autoserv_count = 0 |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 197 | sysmon_count = 0 |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 198 | total = 0 |
| 199 | for proc in psutil.process_iter(): |
Allen Li | 80dae19 | 2016-11-01 11:58:10 -0700 | [diff] [blame] | 200 | if _is_parent_autoserv(proc): |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 201 | autoserv_count += 1 |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 202 | elif _is_sysmon(proc): |
| 203 | sysmon_count += 1 |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 204 | total += 1 |
Allen Li | 79317bb | 2016-12-16 18:25:07 -0800 | [diff] [blame] | 205 | logger.debug('autoserv_count: %s', autoserv_count) |
| 206 | logger.debug('sysmon_count: %s', sysmon_count) |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 207 | _autoserv_proc_count_metric.set(autoserv_count) |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 208 | _sysmon_proc_count_metric.set(sysmon_count) |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 209 | _proc_count_metric.set(total) |
| 210 | |
| 211 | |
Allen Li | 80dae19 | 2016-11-01 11:58:10 -0700 | [diff] [blame] | 212 | def _is_parent_autoserv(proc): |
| 213 | """Return whether proc is a parent (not forked) autoserv process.""" |
| 214 | return _is_autoserv(proc) and not _is_autoserv(proc.parent()) |
| 215 | |
| 216 | |
| 217 | def _is_autoserv(proc): |
| 218 | """Return whether proc is an autoserv process.""" |
| 219 | # This relies on the autoserv script being run directly. The script should |
| 220 | # be named autoserv exactly and start with a shebang that is /usr/bin/python, |
| 221 | # NOT /bin/env |
| 222 | return proc.name() == 'autoserv' |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 223 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 224 | |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 225 | def _is_sysmon(proc): |
| 226 | """Return whether proc is a sysmon process.""" |
Allen Li | dfdfbda | 2016-12-16 17:41:16 -0800 | [diff] [blame] | 227 | return proc.cmdline()[:3] == ['python', '-m', 'chromite.scripts.sysmon'] |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 228 | |
| 229 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 230 | def collect_load_avg(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 231 | try: |
| 232 | avg1, avg5, avg15 = os.getloadavg() |
| 233 | except OSError: |
| 234 | pass |
| 235 | else: |
| 236 | _load_average_metric.set(avg1, fields={'minutes': 1}) |
| 237 | _load_average_metric.set(avg5, fields={'minutes': 5}) |
| 238 | _load_average_metric.set(avg15, fields={'minutes': 15}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 239 | |
| 240 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 241 | def collect_unix_time(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 242 | _unix_time_metric.set(int(time.time() * 1000)) |