blob: fbf43096ca0665e8f66d3146f753c1e2fcd838c5 [file] [log] [blame]
Allen Li142dc6d2016-12-16 17:03:45 -08001# 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 Li142dc6d2016-12-16 17:03:45 -08005"""System metrics."""
6
Allen Li13bdf0c2017-03-02 15:18:16 -08007from __future__ import absolute_import
Allen Li142dc6d2016-12-16 17:03:45 -08008
9import collections
10import platform
11import sys
12
Allen Lia9c6e802017-07-11 15:42:47 -070013from chromite.lib import metrics
Allen Li142dc6d2016-12-16 17:03:45 -080014
Mike Frysinger807d8282022-04-28 22:45:17 -040015
Allen Lia9c6e802017-07-11 15:42:47 -070016_os_name_metric = metrics.StringMetric(
Alex Klein1699fab2022-09-08 08:46:06 -060017 "proc/os/name", description="OS name on the machine"
18)
Allen Li142dc6d2016-12-16 17:03:45 -080019
Allen Lia9c6e802017-07-11 15:42:47 -070020_os_version_metric = metrics.StringMetric(
Alex Klein1699fab2022-09-08 08:46:06 -060021 "proc/os/version", description="OS version on the machine"
22)
Allen Li142dc6d2016-12-16 17:03:45 -080023
Allen Lia9c6e802017-07-11 15:42:47 -070024_os_arch_metric = metrics.StringMetric(
Alex Klein1699fab2022-09-08 08:46:06 -060025 "proc/os/arch", description="OS architecture on this machine"
26)
Allen Li142dc6d2016-12-16 17:03:45 -080027
Allen Lia9c6e802017-07-11 15:42:47 -070028_python_arch_metric = metrics.StringMetric(
Alex Klein1699fab2022-09-08 08:46:06 -060029 "proc/python/arch",
30 description="python userland architecture on this machine",
31)
Allen Li142dc6d2016-12-16 17:03:45 -080032
33
Allen Li45ae8392017-03-02 14:19:35 -080034def collect_os_info():
Alex Klein1699fab2022-09-08 08:46:06 -060035 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 Li142dc6d2016-12-16 17:03:45 -080040
41
Alex Klein1699fab2022-09-08 08:46:06 -060042class OSInfo(collections.namedtuple("OSInfo", "name,version")):
43 """namedtuple representing OS info (all fields lowercased)."""
Allen Li739def02016-12-16 17:16:59 -080044
Alex Klein1699fab2022-09-08 08:46:06 -060045 def __new__(cls, name, version):
46 return super(OSInfo, cls).__new__(cls, name.lower(), version.lower())
Allen Li142dc6d2016-12-16 17:03:45 -080047
48
Allen Li739def02016-12-16 17:16:59 -080049def _get_osinfo():
Alex Klein1699fab2022-09-08 08:46:06 -060050 """Get OS name and version.
Allen Li142dc6d2016-12-16 17:03:45 -080051
Alex Klein1699fab2022-09-08 08:46:06 -060052 Returns:
53 OSInfo instance
54 """
55 os_name = platform.system()
56 if os_name == "Linux":
57 return _get_linux_osinfo()
58 else:
59 return OSInfo(name="", version="")
Allen Li142dc6d2016-12-16 17:03:45 -080060
Allen Li739def02016-12-16 17:16:59 -080061
Allen Li739def02016-12-16 17:16:59 -080062def _get_linux_osinfo():
Alex Klein1699fab2022-09-08 08:46:06 -060063 # 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 Li739def02016-12-16 17:16:59 -080071
72
Allen Li142dc6d2016-12-16 17:03:45 -080073def _get_python_arch():
Alex Klein1699fab2022-09-08 08:46:06 -060074 if sys.maxsize > 2**32:
75 return "64"
76 else:
77 return "32"