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 | |
| 9 | import collections |
| 10 | import platform |
| 11 | import sys |
| 12 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 13 | from chromite.lib import metrics |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 14 | |
Mike Frysinger | 807d828 | 2022-04-28 22:45:17 -0400 | [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 | """ |
Allen Li | d78af97 | 2017-07-13 12:48:01 -0700 | [diff] [blame] | 54 | os_name = platform.system() |
| 55 | if os_name == 'Linux': |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame] | 56 | return _get_linux_osinfo() |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame] | 57 | else: |
| 58 | return OSInfo(name='', version='') |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 59 | |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame] | 60 | |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame] | 61 | def _get_linux_osinfo(): |
| 62 | # will return something like ('Ubuntu', '14.04', 'trusty') |
Mike Frysinger | 8205ef8 | 2020-05-14 23:53:12 -0400 | [diff] [blame] | 63 | try: |
| 64 | # This is removed in Python 3.8+. |
| 65 | # pylint: disable=deprecated-method |
| 66 | os_name, os_version, _ = platform.dist() |
| 67 | except AttributeError: |
| 68 | os_name = os_version = '' |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame] | 69 | return OSInfo(name=os_name, version=os_version) |
| 70 | |
| 71 | |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 72 | def _get_python_arch(): |
| 73 | if sys.maxsize > 2**32: |
| 74 | return '64' |
| 75 | else: |
| 76 | return '32' |