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 | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 63 | BOOT_TIME = psutil.boot_time() |
| 64 | _net_up_metric = ts_mon.CounterMetric( |
| 65 | 'dev/net/bytes/up', start_time=BOOT_TIME, |
| 66 | description='Number of bytes sent on interface.', |
| 67 | units=ts_mon.MetricsDataUnits.BYTES) |
| 68 | _net_down_metric = ts_mon.CounterMetric( |
| 69 | 'dev/net/bytes/down', start_time=BOOT_TIME, |
| 70 | description='Number of Bytes received on ' |
| 71 | 'interface.', |
| 72 | units=ts_mon.MetricsDataUnits.BYTES) |
| 73 | _net_err_up_metric = ts_mon.CounterMetric( |
| 74 | 'dev/net/err/up', start_time=BOOT_TIME, |
| 75 | description='Total number of errors when ' |
| 76 | 'sending (per interface).') |
| 77 | _net_err_down_metric = ts_mon.CounterMetric( |
| 78 | 'dev/net/err/down', start_time=BOOT_TIME, |
| 79 | description='Total number of errors when ' |
| 80 | 'receiving (per interface).') |
| 81 | _net_drop_up_metric = ts_mon.CounterMetric( |
| 82 | 'dev/net/drop/up', start_time=BOOT_TIME, |
| 83 | description='Total number of outgoing ' |
| 84 | 'packets that have been dropped.') |
| 85 | _net_drop_down_metric = ts_mon.CounterMetric( |
| 86 | 'dev/net/drop/down', start_time=BOOT_TIME, |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 87 | description='Total number of incoming ' |
| 88 | 'packets that have been dropped.') |
| 89 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 90 | _disk_read_metric = ts_mon.CounterMetric( |
| 91 | 'dev/disk/read', start_time=BOOT_TIME, |
| 92 | description='Number of Bytes read on disk.', |
| 93 | units=ts_mon.MetricsDataUnits.BYTES) |
| 94 | _disk_write_metric = ts_mon.CounterMetric( |
| 95 | 'dev/disk/write', start_time=BOOT_TIME, |
| 96 | description='Number of Bytes written on disk.', |
| 97 | units=ts_mon.MetricsDataUnits.BYTES) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 98 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 99 | _uptime_metric = ts_mon.GaugeMetric( |
| 100 | 'dev/uptime', |
| 101 | description='Machine uptime, in seconds.', |
| 102 | units=ts_mon.MetricsDataUnits.SECONDS) |
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 | _proc_count_metric = ts_mon.GaugeMetric( |
| 105 | 'dev/proc/count', |
| 106 | description='Number of processes currently running.') |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 107 | _autoserv_proc_count_metric = ts_mon.GaugeMetric( |
| 108 | 'dev/proc/autoserv_count', |
| 109 | description='Number of autoserv processes currently running.') |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 110 | _sysmon_proc_count_metric = ts_mon.GaugeMetric( |
| 111 | 'dev/proc/sysmon_count', |
| 112 | description='Number of sysmon processes currently running.') |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 113 | _load_average_metric = ts_mon.FloatMetric( |
| 114 | 'dev/proc/load_average', |
| 115 | description='Number of processes currently ' |
| 116 | 'in the system run queue.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 117 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 118 | # ts_mon pipeline uses backend clocks when assigning timestamps to metric |
| 119 | # points. By comparing point timestamp to the point value (i.e. time by |
| 120 | # machine's local clock), we can potentially detect some anomalies (clock |
| 121 | # drift, unusually high metrics pipeline delay, completely wrong clocks, etc). |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 122 | # |
| 123 | # It is important to gather this metric right before the flush. |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 124 | _unix_time_metric = ts_mon.GaugeMetric( |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 125 | 'dev/unix_time', |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 126 | description='Number of milliseconds since epoch' |
| 127 | ' based on local machine clock.') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 128 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 129 | _os_name_metric = ts_mon.StringMetric( |
| 130 | 'proc/os/name', |
| 131 | description='OS name on the machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 132 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 133 | _os_version_metric = ts_mon.StringMetric( |
| 134 | 'proc/os/version', |
| 135 | description='OS version on the machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 136 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 137 | _os_arch_metric = ts_mon.StringMetric( |
| 138 | 'proc/os/arch', |
| 139 | description='OS architecture on this machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 140 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 141 | _python_arch_metric = ts_mon.StringMetric( |
| 142 | 'proc/python/arch', |
| 143 | description='python userland ' |
| 144 | 'architecture on this machine') |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 145 | |
| 146 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 147 | def collect_uptime(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 148 | _uptime_metric.set(int(time.time() - BOOT_TIME)) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 149 | |
| 150 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 151 | def collect_cpu_info(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 152 | _cpu_count_metric.set(psutil.cpu_count()) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 153 | |
| 154 | times = psutil.cpu_times_percent() |
| 155 | for mode in ('user', 'system', 'idle'): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 156 | _cpu_time_metric.set(getattr(times, mode), {'mode': mode}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 157 | |
| 158 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 159 | def collect_disk_info(mountpoints=None): |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 160 | if mountpoints is None: |
| 161 | mountpoints = [disk.mountpoint for disk in psutil.disk_partitions()] |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 162 | for mountpoint in mountpoints: |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 163 | _collect_disk_info_single(mountpoint) |
| 164 | _collect_fs_inode_info(mountpoint) |
| 165 | _collect_disk_io_info() |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 166 | |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 167 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 168 | def _collect_disk_info_single(mountpoint): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 169 | fields = {'path': mountpoint} |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 170 | |
| 171 | try: |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 172 | usage = psutil.disk_usage(mountpoint) |
| 173 | except OSError as ex: |
| 174 | if ex.errno == errno.ENOENT: |
| 175 | # This happens on Windows when querying a removable drive that |
| 176 | # doesn't have any media inserted right now. |
| 177 | pass |
| 178 | else: |
| 179 | raise |
| 180 | else: |
| 181 | _disk_free_metric.set(usage.free, fields=fields) |
| 182 | _disk_total_metric.set(usage.total, fields=fields) |
| 183 | |
| 184 | # inode counts are only available on Unix. |
| 185 | if os.name == 'posix': |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 186 | _collect_fs_inode_info(mountpoint) |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 187 | |
| 188 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 189 | def _collect_fs_inode_info(mountpoint): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 190 | fields = {'path': mountpoint} |
| 191 | stats = os.statvfs(mountpoint) |
| 192 | _inodes_free_metric.set(stats.f_favail, fields=fields) |
| 193 | _inodes_total_metric.set(stats.f_files, fields=fields) |
| 194 | |
| 195 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 196 | def _collect_disk_io_info(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 197 | try: |
| 198 | disk_counters = psutil.disk_io_counters(perdisk=True).iteritems() |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 199 | except RuntimeError as ex: |
| 200 | if "couldn't find any physical disk" in str(ex): |
| 201 | # Disk performance counters aren't enabled on Windows. |
| 202 | pass |
| 203 | else: |
| 204 | raise |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 205 | else: |
| 206 | for disk, counters in disk_counters: |
| 207 | fields = {'disk': disk} |
| 208 | _disk_read_metric.set(counters.read_bytes, fields=fields) |
| 209 | _disk_write_metric.set(counters.write_bytes, fields=fields) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 210 | |
| 211 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 212 | def collect_mem_info(): |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 213 | # We don't report mem.used because (due to virtual memory) it is not |
| 214 | # useful. |
| 215 | mem = psutil.virtual_memory() |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 216 | _mem_free_metric.set(mem.available) |
| 217 | _mem_total_metric.set(mem.total) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 218 | |
| 219 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 220 | def collect_net_info(): |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 221 | metric_counter_names = [ |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 222 | (_net_up_metric, 'bytes_sent'), |
| 223 | (_net_down_metric, 'bytes_recv'), |
| 224 | (_net_err_up_metric, 'errout'), |
| 225 | (_net_err_down_metric, 'errin'), |
| 226 | (_net_drop_up_metric, 'dropout'), |
| 227 | (_net_drop_down_metric, 'dropin'), |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 228 | ] |
| 229 | |
| 230 | nics = psutil.net_io_counters(pernic=True) |
| 231 | for nic, counters in nics.iteritems(): |
Allen Li | 10eb79a | 2016-10-19 11:11:53 -0700 | [diff] [blame] | 232 | # TODO(ayatane): Use a different way of identifying virtual interfaces |
| 233 | if nic.startswith('veth'): |
| 234 | # Skip virtual interfaces |
| 235 | continue |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 236 | fields = {'interface': nic} |
| 237 | for metric, counter_name in metric_counter_names: |
| 238 | try: |
| 239 | metric.set(getattr(counters, counter_name), fields=fields) |
| 240 | except ts_mon.MonitoringDecreasingValueError as ex: |
| 241 | # This normally shouldn't happen, but might if the network |
| 242 | # driver module is reloaded, so log an error and continue |
| 243 | # instead of raising an exception. |
Allen Li | 79317bb | 2016-12-16 18:25:07 -0800 | [diff] [blame] | 244 | logger.warning(str(ex)) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 245 | |
| 246 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 247 | def collect_proc_info(): |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 248 | autoserv_count = 0 |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 249 | sysmon_count = 0 |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 250 | total = 0 |
| 251 | for proc in psutil.process_iter(): |
Allen Li | 80dae19 | 2016-11-01 11:58:10 -0700 | [diff] [blame] | 252 | if _is_parent_autoserv(proc): |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 253 | autoserv_count += 1 |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 254 | elif _is_sysmon(proc): |
| 255 | sysmon_count += 1 |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 256 | total += 1 |
Allen Li | 79317bb | 2016-12-16 18:25:07 -0800 | [diff] [blame] | 257 | logger.debug('autoserv_count: %s', autoserv_count) |
| 258 | logger.debug('sysmon_count: %s', sysmon_count) |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 259 | _autoserv_proc_count_metric.set(autoserv_count) |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 260 | _sysmon_proc_count_metric.set(sysmon_count) |
Allen Li | efe7adf | 2016-10-27 11:36:04 -0700 | [diff] [blame] | 261 | _proc_count_metric.set(total) |
| 262 | |
| 263 | |
Allen Li | 80dae19 | 2016-11-01 11:58:10 -0700 | [diff] [blame] | 264 | def _is_parent_autoserv(proc): |
| 265 | """Return whether proc is a parent (not forked) autoserv process.""" |
| 266 | return _is_autoserv(proc) and not _is_autoserv(proc.parent()) |
| 267 | |
| 268 | |
| 269 | def _is_autoserv(proc): |
| 270 | """Return whether proc is an autoserv process.""" |
| 271 | # This relies on the autoserv script being run directly. The script should |
| 272 | # be named autoserv exactly and start with a shebang that is /usr/bin/python, |
| 273 | # NOT /bin/env |
| 274 | return proc.name() == 'autoserv' |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 275 | |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 276 | |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 277 | def _is_sysmon(proc): |
| 278 | """Return whether proc is a sysmon process.""" |
Allen Li | dfdfbda | 2016-12-16 17:41:16 -0800 | [diff] [blame] | 279 | return proc.cmdline()[:3] == ['python', '-m', 'chromite.scripts.sysmon'] |
Allen Li | 0937a52 | 2016-11-23 13:34:48 -0800 | [diff] [blame] | 280 | |
| 281 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 282 | def collect_load_avg(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 283 | try: |
| 284 | avg1, avg5, avg15 = os.getloadavg() |
| 285 | except OSError: |
| 286 | pass |
| 287 | else: |
| 288 | _load_average_metric.set(avg1, fields={'minutes': 1}) |
| 289 | _load_average_metric.set(avg5, fields={'minutes': 5}) |
| 290 | _load_average_metric.set(avg15, fields={'minutes': 15}) |
Allen Li | ec5beb3 | 2016-09-08 15:31:41 -0700 | [diff] [blame] | 291 | |
| 292 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame^] | 293 | def collect_unix_time(): |
Allen Li | a6b0225 | 2016-10-26 14:40:51 -0700 | [diff] [blame] | 294 | _unix_time_metric.set(int(time.time() * 1000)) |