blob: 4deb7bf1b4e5d6015d82b7a536e41c235f28271f [file] [log] [blame]
Allen Liec5beb32016-09-08 15:31:41 -07001# 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 Liec5beb32016-09-08 15:31:41 -07005"""System metrics."""
6
Allen Li13bdf0c2017-03-02 15:18:16 -08007from __future__ import absolute_import
Allen Liec5beb32016-09-08 15:31:41 -07008
9import errno
10import os
Allen Liec5beb32016-09-08 15:31:41 -070011import time
12
Mike Frysingercb56b642019-08-25 15:33:08 -040013import psutil # pylint: disable=import-error
Allen Liec5beb32016-09-08 15:31:41 -070014
15from chromite.lib import cros_logging as logging
Allen Lia9c6e802017-07-11 15:42:47 -070016from chromite.lib import metrics
Allen Liec5beb32016-09-08 15:31:41 -070017
Allen Li79317bb2016-12-16 18:25:07 -080018logger = logging.getLogger(__name__)
19
Allen Liec5beb32016-09-08 15:31:41 -070020
Allen Lia9c6e802017-07-11 15:42:47 -070021_cpu_count_metric = metrics.GaugeMetric(
Allen Lia6b02252016-10-26 14:40:51 -070022 'dev/cpu/count',
23 description='Number of CPU cores.')
Allen Lia9c6e802017-07-11 15:42:47 -070024_cpu_time_metric = metrics.FloatMetric(
Allen Lia6b02252016-10-26 14:40:51 -070025 'dev/cpu/time',
26 description='percentage of time spent by the CPU '
27 'in different states.')
Allen Liec5beb32016-09-08 15:31:41 -070028
Allen Lia9c6e802017-07-11 15:42:47 -070029_disk_free_metric = metrics.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070030 'dev/disk/free',
Allen Li22989bd2017-07-12 10:34:37 -070031 description='Available bytes on disk partition.')
Allen Lia9c6e802017-07-11 15:42:47 -070032_disk_total_metric = metrics.GaugeMetric(
Allen Lia6b02252016-10-26 14:40:51 -070033 'dev/disk/total',
Allen Li22989bd2017-07-12 10:34:37 -070034 description='Total bytes on disk partition.')
Allen Liec5beb32016-09-08 15:31:41 -070035
Allen Lia9c6e802017-07-11 15:42:47 -070036_inodes_free_metric = metrics.GaugeMetric(
Allen Lia6b02252016-10-26 14:40:51 -070037 'dev/inodes/free',
38 description='Number of available inodes on '
39 'disk partition (unix only).')
Allen Lia9c6e802017-07-11 15:42:47 -070040_inodes_total_metric = metrics.GaugeMetric(
Allen Lia6b02252016-10-26 14:40:51 -070041 'dev/inodes/total',
42 description='Number of possible inodes on '
43 'disk partition (unix only)')
Allen Liec5beb32016-09-08 15:31:41 -070044
Allen Lia9c6e802017-07-11 15:42:47 -070045_mem_free_metric = metrics.GaugeMetric(
Allen Lia6b02252016-10-26 14:40:51 -070046 'dev/mem/free',
47 description='Amount of memory available to a '
48 'process (in Bytes). Buffers are considered '
Allen Li22989bd2017-07-12 10:34:37 -070049 'free memory.')
Allen Liec5beb32016-09-08 15:31:41 -070050
Allen Lia9c6e802017-07-11 15:42:47 -070051_mem_total_metric = metrics.GaugeMetric(
Allen Lia6b02252016-10-26 14:40:51 -070052 'dev/mem/total',
Allen Li22989bd2017-07-12 10:34:37 -070053 description='Total physical memory in Bytes.')
Allen Liec5beb32016-09-08 15:31:41 -070054
Allen Li325c0762017-03-02 15:00:19 -080055_BOOT_TIME = psutil.boot_time()
Allen Lic987fc92017-03-02 14:54:51 -080056
Allen Lia9c6e802017-07-11 15:42:47 -070057_disk_read_metric = metrics.CounterMetric(
Allen Li325c0762017-03-02 15:00:19 -080058 'dev/disk/read', start_time=_BOOT_TIME,
Allen Li22989bd2017-07-12 10:34:37 -070059 description='Number of Bytes read on disk.')
Allen Lia9c6e802017-07-11 15:42:47 -070060_disk_write_metric = metrics.CounterMetric(
Allen Li325c0762017-03-02 15:00:19 -080061 'dev/disk/write', start_time=_BOOT_TIME,
Allen Li22989bd2017-07-12 10:34:37 -070062 description='Number of Bytes written on disk.')
Allen Liec5beb32016-09-08 15:31:41 -070063
Allen Lia9c6e802017-07-11 15:42:47 -070064_uptime_metric = metrics.GaugeMetric(
Allen Lia6b02252016-10-26 14:40:51 -070065 'dev/uptime',
Allen Li22989bd2017-07-12 10:34:37 -070066 description='Machine uptime, in seconds.')
Allen Liec5beb32016-09-08 15:31:41 -070067
Allen Lia9c6e802017-07-11 15:42:47 -070068_load_average_metric = metrics.FloatMetric(
Allen Lia6b02252016-10-26 14:40:51 -070069 'dev/proc/load_average',
70 description='Number of processes currently '
71 'in the system run queue.')
Allen Liec5beb32016-09-08 15:31:41 -070072
Allen Lia6b02252016-10-26 14:40:51 -070073# 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 Liec5beb32016-09-08 15:31:41 -070077#
78# It is important to gather this metric right before the flush.
Allen Lia9c6e802017-07-11 15:42:47 -070079_unix_time_metric = metrics.GaugeMetric(
Allen Liec5beb32016-09-08 15:31:41 -070080 'dev/unix_time',
Allen Lia6b02252016-10-26 14:40:51 -070081 description='Number of milliseconds since epoch'
82 ' based on local machine clock.')
Allen Liec5beb32016-09-08 15:31:41 -070083
Allen Lia9c6e802017-07-11 15:42:47 -070084_os_name_metric = metrics.StringMetric(
Allen Lia6b02252016-10-26 14:40:51 -070085 'proc/os/name',
86 description='OS name on the machine')
Allen Liec5beb32016-09-08 15:31:41 -070087
Allen Lia9c6e802017-07-11 15:42:47 -070088_os_version_metric = metrics.StringMetric(
Allen Lia6b02252016-10-26 14:40:51 -070089 'proc/os/version',
90 description='OS version on the machine')
Allen Liec5beb32016-09-08 15:31:41 -070091
Allen Lia9c6e802017-07-11 15:42:47 -070092_os_arch_metric = metrics.StringMetric(
Allen Lia6b02252016-10-26 14:40:51 -070093 'proc/os/arch',
94 description='OS architecture on this machine')
Allen Liec5beb32016-09-08 15:31:41 -070095
Allen Lia9c6e802017-07-11 15:42:47 -070096_python_arch_metric = metrics.StringMetric(
Allen Lia6b02252016-10-26 14:40:51 -070097 'proc/python/arch',
98 description='python userland '
99 'architecture on this machine')
Allen Liec5beb32016-09-08 15:31:41 -0700100
101
Allen Li45ae8392017-03-02 14:19:35 -0800102def collect_uptime():
Allen Li325c0762017-03-02 15:00:19 -0800103 _uptime_metric.set(int(time.time() - _BOOT_TIME))
Allen Liec5beb32016-09-08 15:31:41 -0700104
105
Allen Li45ae8392017-03-02 14:19:35 -0800106def collect_cpu_info():
Allen Lia6b02252016-10-26 14:40:51 -0700107 _cpu_count_metric.set(psutil.cpu_count())
Allen Liec5beb32016-09-08 15:31:41 -0700108
109 times = psutil.cpu_times_percent()
110 for mode in ('user', 'system', 'idle'):
Allen Lia6b02252016-10-26 14:40:51 -0700111 _cpu_time_metric.set(getattr(times, mode), {'mode': mode})
Allen Liec5beb32016-09-08 15:31:41 -0700112
113
Allen Li45ae8392017-03-02 14:19:35 -0800114def collect_disk_info(mountpoints=None):
Allen Liec5beb32016-09-08 15:31:41 -0700115 if mountpoints is None:
116 mountpoints = [disk.mountpoint for disk in psutil.disk_partitions()]
Allen Liec5beb32016-09-08 15:31:41 -0700117 for mountpoint in mountpoints:
Allen Li45ae8392017-03-02 14:19:35 -0800118 _collect_disk_info_single(mountpoint)
119 _collect_fs_inode_info(mountpoint)
120 _collect_disk_io_info()
Allen Liec5beb32016-09-08 15:31:41 -0700121
Allen Liec5beb32016-09-08 15:31:41 -0700122
Allen Li45ae8392017-03-02 14:19:35 -0800123def _collect_disk_info_single(mountpoint):
Allen Lia6b02252016-10-26 14:40:51 -0700124 fields = {'path': mountpoint}
Allen Liec5beb32016-09-08 15:31:41 -0700125
126 try:
Allen Lia6b02252016-10-26 14:40:51 -0700127 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 Li45ae8392017-03-02 14:19:35 -0800141 _collect_fs_inode_info(mountpoint)
Allen Lia6b02252016-10-26 14:40:51 -0700142
143
Allen Li45ae8392017-03-02 14:19:35 -0800144def _collect_fs_inode_info(mountpoint):
Allen Lia6b02252016-10-26 14:40:51 -0700145 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 Li45ae8392017-03-02 14:19:35 -0800151def _collect_disk_io_info():
Allen Lia6b02252016-10-26 14:40:51 -0700152 try:
Mike Frysingerabb7d812020-05-15 00:13:10 -0400153 # pylint: disable=dict-items-not-iterating
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400154 disk_counters = psutil.disk_io_counters(perdisk=True).items()
Allen Liec5beb32016-09-08 15:31:41 -0700155 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 Lia6b02252016-10-26 14:40:51 -0700161 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 Liec5beb32016-09-08 15:31:41 -0700166
167
Allen Li45ae8392017-03-02 14:19:35 -0800168def collect_mem_info():
Allen Liec5beb32016-09-08 15:31:41 -0700169 # We don't report mem.used because (due to virtual memory) it is not
170 # useful.
171 mem = psutil.virtual_memory()
Allen Lia6b02252016-10-26 14:40:51 -0700172 _mem_free_metric.set(mem.available)
173 _mem_total_metric.set(mem.total)
Allen Liec5beb32016-09-08 15:31:41 -0700174
175
Allen Li45ae8392017-03-02 14:19:35 -0800176def collect_load_avg():
Allen Lia6b02252016-10-26 14:40:51 -0700177 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 Liec5beb32016-09-08 15:31:41 -0700185
186
Allen Li45ae8392017-03-02 14:19:35 -0800187def collect_unix_time():
Allen Lia6b02252016-10-26 14:40:51 -0700188 _unix_time_metric.set(int(time.time() * 1000))