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