Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import errno | 5 import errno |
| 6 import os | 6 import os |
| 7 import logging | 7 import logging |
| 8 import platform | |
| 8 import time | 9 import time |
| 9 | 10 |
| 10 import psutil | 11 import psutil |
| 11 | 12 |
| 12 from infra_libs import ts_mon | 13 from infra_libs import ts_mon |
| 13 | 14 |
| 14 | 15 |
| 15 cpu_count = ts_mon.GaugeMetric('dev/cpu/count', | 16 cpu_count = ts_mon.GaugeMetric('dev/cpu/count', |
| 16 description='Number of CPU cores.') | 17 description='Number of CPU cores.') |
| 17 cpu_time = ts_mon.FloatMetric('dev/cpu/time', | 18 cpu_time = ts_mon.FloatMetric('dev/cpu/time', |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 # tsmon pipeline uses backend clocks when assigning timestamps to metric points. | 79 # tsmon pipeline uses backend clocks when assigning timestamps to metric points. |
| 79 # By comparing point timestamp to the point value (i.e. time by machine's local | 80 # By comparing point timestamp to the point value (i.e. time by machine's local |
| 80 # clock), we can potentially detect some anomalies (clock drift, unusually high | 81 # clock), we can potentially detect some anomalies (clock drift, unusually high |
| 81 # metrics pipeline delay, completely wrong clocks, etc). | 82 # metrics pipeline delay, completely wrong clocks, etc). |
| 82 # | 83 # |
| 83 # It is important to gather this metric right before the flush. | 84 # It is important to gather this metric right before the flush. |
| 84 unix_time = ts_mon.GaugeMetric('dev/unix_time', | 85 unix_time = ts_mon.GaugeMetric('dev/unix_time', |
| 85 description='Number of milliseconds since epoch ' | 86 description='Number of milliseconds since epoch ' |
| 86 'based on local machine clock.') | 87 'based on local machine clock.') |
| 87 | 88 |
| 89 metric_os_name = ts_mon.StringMetric('proc/os/name', | |
| 90 description='OS name on the machine ' | |
|
dsansome
2016/06/29 06:21:29
Align these lines with the ' above
chrishall
2016/07/01 04:27:59
Done.
| |
| 91 'metric:hostname.') | |
| 92 | |
| 93 metric_os_version = ts_mon.StringMetric('proc/os/version', | |
| 94 description='OS version on the machine ' | |
| 95 'metric:hostname.') | |
| 88 | 96 |
| 89 def get_uptime(): | 97 def get_uptime(): |
| 90 uptime.set(int(time.time() - START_TIME)) | 98 uptime.set(int(time.time() - START_TIME)) |
| 91 | 99 |
| 92 | 100 |
| 93 def get_cpu_info(): | 101 def get_cpu_info(): |
| 94 cpu_count.set(psutil.cpu_count()) | 102 cpu_count.set(psutil.cpu_count()) |
| 95 | 103 |
| 96 times = psutil.cpu_times_percent() | 104 times = psutil.cpu_times_percent() |
| 97 for mode in ('user', 'system', 'idle'): | 105 for mode in ('user', 'system', 'idle'): |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 fields = {'interface': nic} | 166 fields = {'interface': nic} |
| 159 for metric, counter_name in metric_counter_names: | 167 for metric, counter_name in metric_counter_names: |
| 160 try: | 168 try: |
| 161 metric.set(getattr(counters, counter_name), fields=fields) | 169 metric.set(getattr(counters, counter_name), fields=fields) |
| 162 except ts_mon.MonitoringDecreasingValueError as ex: # pragma: no cover | 170 except ts_mon.MonitoringDecreasingValueError as ex: # pragma: no cover |
| 163 # This normally shouldn't happen, but might if the network driver module | 171 # This normally shouldn't happen, but might if the network driver module |
| 164 # is reloaded, so log an error and continue instead of raising an | 172 # is reloaded, so log an error and continue instead of raising an |
| 165 # exception. | 173 # exception. |
| 166 logging.error(str(ex)) | 174 logging.error(str(ex)) |
| 167 | 175 |
| 176 def get_os_info(): | |
|
dsansome
2016/06/29 06:21:29
Please add tests for this to test/system_metrics_t
chrishall
2016/07/01 04:27:59
Done.
| |
| 177 os_name = "" | |
| 178 os_version = "" | |
| 179 | |
| 180 os_name = platform.system().lower() | |
| 181 if "windows" in os_name: | |
| 182 os_name = "windows" | |
| 183 # os_release will be something like '7', 'vista', or 'xp' | |
| 184 os_version = platform.release() | |
| 185 | |
| 186 elif "linux" in os_name: | |
| 187 # will return something like ('Ubuntu', '14.04', 'trusty') | |
| 188 dist_info = platform.dist() | |
| 189 os_name = dist_info[0] | |
| 190 os_version = dist_info[1] | |
| 191 | |
| 192 # on mac platform.system() reports 'darwin' | |
| 193 else: | |
| 194 # this tuple is only populated on mac systems | |
| 195 mac_ver = platform.mac_ver() | |
| 196 # will be '10.11.5' or similar on a valid mac | |
| 197 # or will be '' on a non-mac | |
| 198 os_version = mac_ver[0] | |
| 199 if os_version: | |
| 200 # we found a valid mac | |
| 201 os_name = "mac" | |
| 202 else : | |
| 203 # not a mac | |
| 204 # unable to find platform information | |
| 205 # reset | |
| 206 os_name = "" | |
| 207 os_version = "" | |
| 208 | |
| 209 # normalize to lower case | |
| 210 os_name = os_name.lower() | |
| 211 os_version = os_version.lower() | |
| 212 | |
| 213 # construct metrics | |
| 214 metric_os_name.set(os_name) | |
| 215 metric_os_version.set(os_version) | |
| 216 | |
|
dsansome
2016/06/29 06:21:29
Need 2 blank lines between top-level functions
chrishall
2016/07/01 04:27:59
Done.
| |
| 217 def clear_os_info(): | |
| 218 metric_os_name.reset() | |
| 219 metric_os_version.reset() | |
| 168 | 220 |
| 169 def get_proc_info(): | 221 def get_proc_info(): |
| 170 procs = psutil.pids() | 222 procs = psutil.pids() |
| 171 proc_count.set(len(procs)) | 223 proc_count.set(len(procs)) |
| 172 | 224 |
| 173 if os.name == 'posix': # pragma: no cover | 225 if os.name == 'posix': # pragma: no cover |
| 174 try: | 226 try: |
| 175 avg1, avg5, avg15 = os.getloadavg() | 227 avg1, avg5, avg15 = os.getloadavg() |
| 176 except OSError: # pragma: no cover | 228 except OSError: # pragma: no cover |
| 177 pass | 229 pass |
| 178 else: | 230 else: |
| 179 load_average.set(avg1, fields={'minutes': 1}) | 231 load_average.set(avg1, fields={'minutes': 1}) |
| 180 load_average.set(avg5, fields={'minutes': 5}) | 232 load_average.set(avg5, fields={'minutes': 5}) |
| 181 load_average.set(avg15, fields={'minutes': 15}) | 233 load_average.set(avg15, fields={'minutes': 15}) |
| 182 | 234 |
| 183 | 235 |
| 184 def get_unix_time(): | 236 def get_unix_time(): |
| 185 unix_time.set(int(time.time() * 1000)) | 237 unix_time.set(int(time.time() * 1000)) |
| OLD | NEW |