Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2016 The ChromiumOS Authors |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 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( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 17 | "proc/os/name", description="OS name on the machine" |
| 18 | ) |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 19 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 20 | _os_version_metric = metrics.StringMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 21 | "proc/os/version", description="OS version on the machine" |
| 22 | ) |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 23 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 24 | _os_arch_metric = metrics.StringMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | "proc/os/arch", description="OS architecture on this machine" |
| 26 | ) |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 27 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 28 | _python_arch_metric = metrics.StringMetric( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | "proc/python/arch", |
| 30 | description="python userland architecture on this machine", |
| 31 | ) |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 32 | |
| 33 | |
Allen Li | 45ae839 | 2017-03-02 14:19:35 -0800 | [diff] [blame] | 34 | def collect_os_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | os_info = _get_osinfo() |
| 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()) |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 40 | |
| 41 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | class OSInfo(collections.namedtuple("OSInfo", "name,version")): |
| 43 | """namedtuple representing OS info (all fields lowercased).""" |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | """Get OS name and version. |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 51 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 52 | Returns: |
Trent Apted | 66736d8 | 2023-05-25 10:38:28 +1000 | [diff] [blame^] | 53 | OSInfo instance |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | """ |
| 55 | os_name = platform.system() |
| 56 | if os_name == "Linux": |
| 57 | return _get_linux_osinfo() |
| 58 | else: |
| 59 | return OSInfo(name="", version="") |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 60 | |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame] | 61 | |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame] | 62 | def _get_linux_osinfo(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 63 | # will return something like ('Ubuntu', '14.04', 'trusty') |
| 64 | try: |
| 65 | # This is removed in Python 3.8+. |
| 66 | # pylint: disable=deprecated-method |
| 67 | os_name, os_version, _ = platform.dist() |
| 68 | except AttributeError: |
| 69 | os_name = os_version = "" |
| 70 | return OSInfo(name=os_name, version=os_version) |
Allen Li | 739def0 | 2016-12-16 17:16:59 -0800 | [diff] [blame] | 71 | |
| 72 | |
Allen Li | 142dc6d | 2016-12-16 17:03:45 -0800 | [diff] [blame] | 73 | def _get_python_arch(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | if sys.maxsize > 2**32: |
| 75 | return "64" |
| 76 | else: |
| 77 | return "32" |