Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [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 collections |
| 14 | import platform |
| 15 | import sys |
| 16 | |
| 17 | from infra_libs import ts_mon |
| 18 | |
| 19 | _os_name_metric = ts_mon.StringMetric( |
| 20 | 'proc/os/name', |
| 21 | description='OS name on the machine') |
| 22 | |
| 23 | _os_version_metric = ts_mon.StringMetric( |
| 24 | 'proc/os/version', |
| 25 | description='OS version on the machine') |
| 26 | |
| 27 | _os_arch_metric = ts_mon.StringMetric( |
| 28 | 'proc/os/arch', |
| 29 | description='OS architecture on this machine') |
| 30 | |
| 31 | _python_arch_metric = ts_mon.StringMetric( |
| 32 | 'proc/python/arch', |
| 33 | description='python userland architecture on this machine') |
| 34 | |
| 35 | |
| 36 | def get_os_info(): |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame^] | 37 | os_info = _get_osinfo() |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 38 | _os_name_metric.set(os_info.name) |
| 39 | _os_version_metric.set(os_info.version) |
| 40 | _os_arch_metric.set(platform.machine()) |
| 41 | _python_arch_metric.set(_get_python_arch()) |
| 42 | |
| 43 | |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame^] | 44 | class OSInfo(collections.namedtuple('OSInfo', 'name,version')): |
| 45 | """namedtuple representing OS info (all fields lowercased).""" |
| 46 | |
| 47 | def __new__(cls, name, version): |
| 48 | return super(OSInfo, cls).__new__(cls, name.lower(), version.lower()) |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 49 | |
| 50 | |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame^] | 51 | def _get_osinfo(): |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 52 | """Get OS name and version. |
| 53 | |
| 54 | Returns: |
| 55 | OSInfo instance |
| 56 | """ |
| 57 | os_name = platform.system().lower() |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 58 | if 'windows' in os_name: |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame^] | 59 | return _get_windows_osinfo() |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 60 | elif 'linux' in os_name: |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame^] | 61 | return _get_linux_osinfo() |
| 62 | elif _is_mac(): |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 63 | # On mac platform.system() reports 'darwin'. |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame^] | 64 | # |
| 65 | # TODO(ayatane): I'm not sure how true the above comment is, but I |
| 66 | # have no reason to remove it nor change the existing logic right |
| 67 | # now. |
| 68 | return _get_mac_osinfo() |
| 69 | else: |
| 70 | return OSInfo(name='', version='') |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 71 | |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame^] | 72 | |
| 73 | def _get_windows_osinfo(): |
| 74 | os_name = 'windows' |
| 75 | # release will be something like '7', 'vista', or 'xp' |
| 76 | os_version = platform.release() |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 77 | return OSInfo(name=os_name, version=os_version) |
| 78 | |
| 79 | |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame^] | 80 | def _get_linux_osinfo(): |
| 81 | # will return something like ('Ubuntu', '14.04', 'trusty') |
| 82 | os_name, os_version, _ = platform.dist() |
| 83 | return OSInfo(name=os_name, version=os_version) |
| 84 | |
| 85 | |
| 86 | def _get_mac_osinfo(): |
| 87 | return OSInfo(name='mac', version=_get_mac_version()) |
| 88 | |
| 89 | |
| 90 | def _is_mac(): |
| 91 | """Return whether the current system is a Mac.""" |
| 92 | return bool(_get_mac_version()) |
| 93 | |
| 94 | |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 95 | def _get_mac_version(): |
| 96 | """Get Mac system version. |
| 97 | |
| 98 | Returns: |
| 99 | Version string, which is empty if not a valid Mac system. |
| 100 | """ |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame^] | 101 | return platform.mac_ver()[0] |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def _get_python_arch(): |
| 105 | if sys.maxsize > 2**32: |
| 106 | return '64' |
| 107 | else: |
| 108 | return '32' |