| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |
| 168 | 186 |
| 187 def get_os_info(): |
| 188 os_name_data = '' |
| 189 os_version_data = '' |
| 190 |
| 191 os_name_data = platform.system().lower() |
| 192 if 'windows' in os_name_data: |
| 193 os_name_data = 'windows' |
| 194 # os_release will be something like '7', 'vista', or 'xp' |
| 195 os_version_data = platform.release() |
| 196 |
| 197 elif 'linux' in os_name_data: |
| 198 # will return something like ('Ubuntu', '14.04', 'trusty') |
| 199 dist_info_data = platform.dist() |
| 200 os_name_data = dist_info_data[0] |
| 201 os_version_data = dist_info_data[1] |
| 202 |
| 203 # on mac platform.system() reports 'darwin' |
| 204 else: |
| 205 # this tuple is only populated on mac systems |
| 206 mac_ver_data = platform.mac_ver() |
| 207 # [0] will be '10.11.5' or similar on a valid mac or will be '' on a |
| 208 # non-mac |
| 209 os_version_data = mac_ver_data[0] |
| 210 if os_version_data: |
| 211 # we found a valid mac |
| 212 os_name_data = 'mac' |
| 213 else : |
| 214 # not a mac, unable to find platform information, reset |
| 215 os_name_data = '' |
| 216 os_version_data = '' |
| 217 |
| 218 # normalize to lower case |
| 219 os_name_data = os_name_data.lower() |
| 220 os_version_data = os_version_data.lower() |
| 221 |
| 222 python_arch_data = '32' |
| 223 if sys.maxsize > 2**32: |
| 224 python_arch_data = '64' |
| 225 |
| 226 # construct metrics |
| 227 os_name.set(os_name_data) |
| 228 os_version.set(os_version_data) |
| 229 os_arch.set(platform.machine()) |
| 230 python_arch.set(python_arch_data) |
| 231 |
| 232 |
| 233 def clear_os_info(): |
| 234 os_name.reset() |
| 235 os_version.reset() |
| 236 |
| 237 |
| 169 def get_proc_info(): | 238 def get_proc_info(): |
| 170 procs = psutil.pids() | 239 procs = psutil.pids() |
| 171 proc_count.set(len(procs)) | 240 proc_count.set(len(procs)) |
| 172 | 241 |
| 173 if os.name == 'posix': # pragma: no cover | 242 if os.name == 'posix': # pragma: no cover |
| 174 try: | 243 try: |
| 175 avg1, avg5, avg15 = os.getloadavg() | 244 avg1, avg5, avg15 = os.getloadavg() |
| 176 except OSError: # pragma: no cover | 245 except OSError: # pragma: no cover |
| 177 pass | 246 pass |
| 178 else: | 247 else: |
| 179 load_average.set(avg1, fields={'minutes': 1}) | 248 load_average.set(avg1, fields={'minutes': 1}) |
| 180 load_average.set(avg5, fields={'minutes': 5}) | 249 load_average.set(avg5, fields={'minutes': 5}) |
| 181 load_average.set(avg15, fields={'minutes': 15}) | 250 load_average.set(avg15, fields={'minutes': 15}) |
| 182 | 251 |
| 183 | 252 |
| 184 def get_unix_time(): | 253 def get_unix_time(): |
| 185 unix_time.set(int(time.time() * 1000)) | 254 unix_time.set(int(time.time() * 1000)) |
| OLD | NEW |