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