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 | |
| 9 import sys | |
| 8 import time | 10 import time |
| 9 | 11 |
| 10 import psutil | 12 import psutil |
| 11 | 13 |
| 12 from infra_libs import ts_mon | 14 from infra_libs import ts_mon |
| 13 | 15 |
| 14 | 16 |
| 15 cpu_count = ts_mon.GaugeMetric('dev/cpu/count', | 17 cpu_count = ts_mon.GaugeMetric('dev/cpu/count', |
| 16 description='Number of CPU cores.') | 18 description='Number of CPU cores.') |
| 17 cpu_time = ts_mon.FloatMetric('dev/cpu/time', | 19 cpu_time = ts_mon.FloatMetric('dev/cpu/time', |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 'in the system run queue.') | 78 'in the system run queue.') |
| 77 | 79 |
| 78 # tsmon pipeline uses backend clocks when assigning timestamps to metric points. | 80 # 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 | 81 # 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 | 82 # clock), we can potentially detect some anomalies (clock drift, unusually high |
| 81 # metrics pipeline delay, completely wrong clocks, etc). | 83 # metrics pipeline delay, completely wrong clocks, etc). |
| 82 # | 84 # |
| 83 # It is important to gather this metric right before the flush. | 85 # It is important to gather this metric right before the flush. |
| 84 unix_time = ts_mon.GaugeMetric('dev/unix_time', | 86 unix_time = ts_mon.GaugeMetric('dev/unix_time', |
| 85 description='Number of milliseconds since epoch ' | 87 description='Number of milliseconds since epoch ' |
| 86 'based on local machine clock.') | 88 'based on local machine clock.') |
| 89 | |
| 90 os_name = ts_mon.StringMetric('proc/os/name', | |
| 91 description='OS name on the machine ' | |
| 92 'metric:hostname.') | |
| 93 | |
| 94 os_version = ts_mon.StringMetric('proc/os/version', | |
| 95 description='OS version on the machine ' | |
| 96 'metric:hostname.') | |
| 97 | |
| 98 os_arch = ts_mon.StringMetric('proc/os/arch', | |
| 99 description='OS architecture on this ' | |
| 100 'machine') | |
| 101 | |
| 102 python_arch = ts_mon.StringMetric('proc/python/arch', | |
| 103 description='python userland ' | |
| 104 'architecture on this machine') | |
| 87 | 105 |
| 88 | 106 |
| 89 def get_uptime(): | 107 def get_uptime(): |
| 90 uptime.set(int(time.time() - START_TIME)) | 108 uptime.set(int(time.time() - START_TIME)) |
| 91 | 109 |
| 92 | 110 |
| 93 def get_cpu_info(): | 111 def get_cpu_info(): |
| 94 cpu_count.set(psutil.cpu_count()) | 112 cpu_count.set(psutil.cpu_count()) |
| 95 | 113 |
| 96 times = psutil.cpu_times_percent() | 114 times = psutil.cpu_times_percent() |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 for nic, counters in nics.iteritems(): | 175 for nic, counters in nics.iteritems(): |
| 158 fields = {'interface': nic} | 176 fields = {'interface': nic} |
| 159 for metric, counter_name in metric_counter_names: | 177 for metric, counter_name in metric_counter_names: |
| 160 try: | 178 try: |
| 161 metric.set(getattr(counters, counter_name), fields=fields) | 179 metric.set(getattr(counters, counter_name), fields=fields) |
| 162 except ts_mon.MonitoringDecreasingValueError as ex: # pragma: no cover | 180 except ts_mon.MonitoringDecreasingValueError as ex: # pragma: no cover |
| 163 # This normally shouldn't happen, but might if the network driver module | 181 # 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 | 182 # is reloaded, so log an error and continue instead of raising an |
| 165 # exception. | 183 # exception. |
| 166 logging.error(str(ex)) | 184 logging.error(str(ex)) |
| 167 | 185 |
|
dsansome
2016/07/01 04:30:56
Add an extra \n here
chrishall
2016/07/01 04:53:56
Done.
| |
| 186 def get_os_info(): | |
| 187 os_name_data = "" | |
|
dsansome
2016/07/01 04:30:56
We seem to use single quotes elsewhere in this fil
chrishall
2016/07/01 04:53:56
Done.
| |
| 188 os_version_data = "" | |
| 189 | |
| 190 os_name_data = platform.system().lower() | |
| 191 if "windows" in os_name_data: | |
| 192 os_name_data = "windows" | |
| 193 # os_release will be something like '7', 'vista', or 'xp' | |
| 194 os_version_data = platform.release() | |
| 195 | |
| 196 elif "linux" in os_name_data: | |
| 197 # will return something like ('Ubuntu', '14.04', 'trusty') | |
| 198 dist_info_data = platform.dist() | |
| 199 os_name_data = dist_info_data[0] | |
| 200 os_version_data = dist_info_data[1] | |
| 201 | |
| 202 # on mac platform.system() reports 'darwin' | |
| 203 else: | |
| 204 # this tuple is only populated on mac systems | |
| 205 mac_ver_data = platform.mac_ver() | |
| 206 # [0] will be '10.11.5' or similar on a valid mac or will be '' on a | |
| 207 # non-mac | |
| 208 os_version_data = mac_ver_data[0] | |
| 209 if os_version_data: | |
| 210 # we found a valid mac | |
| 211 os_name_data = "mac" | |
| 212 else : | |
| 213 # not a mac, unable to find platform information, reset | |
| 214 os_name_data = "" | |
| 215 os_version_data = "" | |
| 216 | |
| 217 # normalize to lower case | |
| 218 os_name_data = os_name_data.lower() | |
| 219 os_version_data = os_version_data.lower() | |
| 220 | |
| 221 python_arch_data = "32" | |
| 222 if sys.maxsize > 2**32: | |
| 223 python_arch_data = "64" | |
| 224 | |
| 225 # construct metrics | |
| 226 os_name.set(os_name_data) | |
| 227 os_version.set(os_version_data) | |
| 228 os_arch(platform.machine()) | |
| 229 python_arch(python_arch_data) | |
| 230 | |
| 231 | |
| 232 def clear_os_info(): | |
| 233 os_name.reset() | |
| 234 os_version.reset() | |
| 235 | |
| 168 | 236 |
| 169 def get_proc_info(): | 237 def get_proc_info(): |
| 170 procs = psutil.pids() | 238 procs = psutil.pids() |
| 171 proc_count.set(len(procs)) | 239 proc_count.set(len(procs)) |
| 172 | 240 |
| 173 if os.name == 'posix': # pragma: no cover | 241 if os.name == 'posix': # pragma: no cover |
| 174 try: | 242 try: |
| 175 avg1, avg5, avg15 = os.getloadavg() | 243 avg1, avg5, avg15 = os.getloadavg() |
| 176 except OSError: # pragma: no cover | 244 except OSError: # pragma: no cover |
| 177 pass | 245 pass |
| 178 else: | 246 else: |
| 179 load_average.set(avg1, fields={'minutes': 1}) | 247 load_average.set(avg1, fields={'minutes': 1}) |
| 180 load_average.set(avg5, fields={'minutes': 5}) | 248 load_average.set(avg5, fields={'minutes': 5}) |
| 181 load_average.set(avg15, fields={'minutes': 15}) | 249 load_average.set(avg15, fields={'minutes': 15}) |
| 182 | 250 |
| 183 | 251 |
| 184 def get_unix_time(): | 252 def get_unix_time(): |
| 185 unix_time.set(int(time.time() * 1000)) | 253 unix_time.set(int(time.time() * 1000)) |
| OLD | NEW |