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