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 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 5 | """System metrics.""" |
| 6 | |
Allen Li | 13bdf0c | 2017-03-02 15:18:16 -0800 | [diff] [blame] | 7 | from __future__ import absolute_import |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 8 | |
| 9 | import errno |
| 10 | import os |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 11 | import time |
| 12 | |
Mike Frysinger | cb56b64 | 2019-08-25 15:33:08 -0400 | [diff] [blame] | 13 | import psutil # pylint: disable=import-error |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 14 | |
| 15 | from chromite.lib import cros_logging as logging |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 16 | from chromite.lib import metrics |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 17 | |
Allen Li | 79317bb | 2016-12-16 18:25:07 -0800 | [diff] [blame] | 18 | logger = logging.getLogger(__name__) |
| 19 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 20 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 21 | _cpu_count_metric = metrics.GaugeMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 22 | 'dev/cpu/count', |
| 23 | description='Number of CPU cores.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 24 | _cpu_time_metric = metrics.FloatMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 25 | 'dev/cpu/time', |
| 26 | description='percentage of time spent by the CPU ' |
| 27 | 'in different states.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 28 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 29 | _disk_free_metric = metrics.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 30 | 'dev/disk/free', |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 31 | description='Available bytes on disk partition.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 32 | _disk_total_metric = metrics.GaugeMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 33 | 'dev/disk/total', |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 34 | description='Total bytes on disk partition.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 35 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 36 | _inodes_free_metric = metrics.GaugeMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 37 | 'dev/inodes/free', |
| 38 | description='Number of available inodes on ' |
| 39 | 'disk partition (unix only).') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 40 | _inodes_total_metric = metrics.GaugeMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 41 | 'dev/inodes/total', |
| 42 | description='Number of possible inodes on ' |
| 43 | 'disk partition (unix only)') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 44 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 45 | _mem_free_metric = metrics.GaugeMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 46 | 'dev/mem/free', |
| 47 | description='Amount of memory available to a ' |
| 48 | 'process (in Bytes). Buffers are considered ' |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 49 | 'free memory.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 50 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 51 | _mem_total_metric = metrics.GaugeMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 52 | 'dev/mem/total', |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 53 | description='Total physical memory in Bytes.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 54 | |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 55 | _BOOT_TIME = psutil.boot_time() |
Allen Li | c987fc9 | 2017-03-02 14:54:51 -0800 | [diff] [blame] | 56 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 57 | _disk_read_metric = metrics.CounterMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 58 | 'dev/disk/read', start_time=_BOOT_TIME, |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 59 | description='Number of Bytes read on disk.') |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 60 | _disk_write_metric = metrics.CounterMetric( |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 61 | 'dev/disk/write', start_time=_BOOT_TIME, |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 62 | description='Number of Bytes written on disk.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 63 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 64 | _uptime_metric = metrics.GaugeMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 65 | 'dev/uptime', |
Allen Li | 22989bd | 2017-07-12 10:34:37 -0700 | [diff] [blame] | 66 | description='Machine uptime, in seconds.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 67 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 68 | _load_average_metric = metrics.FloatMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 69 | 'dev/proc/load_average', |
| 70 | description='Number of processes currently ' |
| 71 | 'in the system run queue.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 72 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 73 | # ts_mon pipeline uses backend clocks when assigning timestamps to metric |
| 74 | # points. By comparing point timestamp to the point value (i.e. time by |
| 75 | # machine's local clock), we can potentially detect some anomalies (clock |
| 76 | # drift, unusually high metrics pipeline delay, completely wrong clocks, etc). |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 77 | # |
| 78 | # It is important to gather this metric right before the flush. |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 79 | _unix_time_metric = metrics.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 80 | 'dev/unix_time', |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 81 | description='Number of milliseconds since epoch' |
| 82 | ' based on local machine clock.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 83 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 84 | _os_name_metric = metrics.StringMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 85 | 'proc/os/name', |
| 86 | description='OS name on the machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 87 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 88 | _os_version_metric = metrics.StringMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 89 | 'proc/os/version', |
| 90 | description='OS version on the machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 91 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 92 | _os_arch_metric = metrics.StringMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 93 | 'proc/os/arch', |
| 94 | description='OS architecture on this machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 95 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 96 | _python_arch_metric = metrics.StringMetric( |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 97 | 'proc/python/arch', |
| 98 | description='python userland ' |
| 99 | 'architecture on this machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 100 | |
| 101 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 102 | def collect_uptime(): |
Allen Li | 325c076 | 2017-03-02 15:00:19 -0800 | [diff] [blame] | 103 | _uptime_metric.set(int(time.time() - _BOOT_TIME)) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 104 | |
| 105 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 106 | def collect_cpu_info(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 107 | _cpu_count_metric.set(psutil.cpu_count()) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 108 | |
| 109 | times = psutil.cpu_times_percent() |
| 110 | for mode in ('user', 'system', 'idle'): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 111 | _cpu_time_metric.set(getattr(times, mode), {'mode': mode}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 112 | |
| 113 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 114 | def collect_disk_info(mountpoints=None): |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 115 | if mountpoints is None: |
| 116 | mountpoints = [disk.mountpoint for disk in psutil.disk_partitions()] |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 117 | for mountpoint in mountpoints: |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 118 | _collect_disk_info_single(mountpoint) |
| 119 | _collect_fs_inode_info(mountpoint) |
| 120 | _collect_disk_io_info() |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 121 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 122 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 123 | def _collect_disk_info_single(mountpoint): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 124 | fields = {'path': mountpoint} |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 125 | |
| 126 | try: |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 127 | usage = psutil.disk_usage(mountpoint) |
| 128 | except OSError as ex: |
| 129 | if ex.errno == errno.ENOENT: |
| 130 | # This happens on Windows when querying a removable drive that |
| 131 | # doesn't have any media inserted right now. |
| 132 | pass |
| 133 | else: |
| 134 | raise |
| 135 | else: |
| 136 | _disk_free_metric.set(usage.free, fields=fields) |
| 137 | _disk_total_metric.set(usage.total, fields=fields) |
| 138 | |
| 139 | # inode counts are only available on Unix. |
| 140 | if os.name == 'posix': |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 141 | _collect_fs_inode_info(mountpoint) |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 142 | |
| 143 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 144 | def _collect_fs_inode_info(mountpoint): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 145 | fields = {'path': mountpoint} |
| 146 | stats = os.statvfs(mountpoint) |
| 147 | _inodes_free_metric.set(stats.f_favail, fields=fields) |
| 148 | _inodes_total_metric.set(stats.f_files, fields=fields) |
| 149 | |
| 150 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 151 | def _collect_disk_io_info(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 152 | try: |
Mike Frysinger | abb7d81 | 2020-05-15 00:13:10 -0400 | [diff] [blame] | 153 | # pylint: disable=dict-items-not-iterating |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 154 | disk_counters = psutil.disk_io_counters(perdisk=True).items() |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 155 | except RuntimeError as ex: |
| 156 | if "couldn't find any physical disk" in str(ex): |
| 157 | # Disk performance counters aren't enabled on Windows. |
| 158 | pass |
| 159 | else: |
| 160 | raise |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 161 | else: |
| 162 | for disk, counters in disk_counters: |
| 163 | fields = {'disk': disk} |
| 164 | _disk_read_metric.set(counters.read_bytes, fields=fields) |
| 165 | _disk_write_metric.set(counters.write_bytes, fields=fields) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 166 | |
| 167 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 168 | def collect_mem_info(): |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 169 | # We don't report mem.used because (due to virtual memory) it is not |
| 170 | # useful. |
| 171 | mem = psutil.virtual_memory() |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 172 | _mem_free_metric.set(mem.available) |
| 173 | _mem_total_metric.set(mem.total) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 174 | |
| 175 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 176 | def collect_load_avg(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 177 | try: |
| 178 | avg1, avg5, avg15 = os.getloadavg() |
| 179 | except OSError: |
| 180 | pass |
| 181 | else: |
| 182 | _load_average_metric.set(avg1, fields={'minutes': 1}) |
| 183 | _load_average_metric.set(avg5, fields={'minutes': 5}) |
| 184 | _load_average_metric.set(avg15, fields={'minutes': 15}) |
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_unix_time(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 188 | _unix_time_metric.set(int(time.time() * 1000)) |