blob: 966c44b67ca24ea622b4e8c829ca5ec71efd0f4d [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(
Allen Li142dc6d2016-12-16 17:03:45 -080017 'proc/os/name',
18 description='OS name on the machine')
19
Allen Lia9c6e802017-07-11 15:42:47 -070020_os_version_metric = metrics.StringMetric(
Allen Li142dc6d2016-12-16 17:03:45 -080021 'proc/os/version',
22 description='OS version on the machine')
23
Allen Lia9c6e802017-07-11 15:42:47 -070024_os_arch_metric = metrics.StringMetric(
Allen Li142dc6d2016-12-16 17:03:45 -080025 'proc/os/arch',
26 description='OS architecture on this machine')
27
Allen Lia9c6e802017-07-11 15:42:47 -070028_python_arch_metric = metrics.StringMetric(
Allen Li142dc6d2016-12-16 17:03:45 -080029 'proc/python/arch',
30 description='python userland architecture on this machine')
31
32
Allen Li45ae8392017-03-02 14:19:35 -080033def collect_os_info():
Allen Li739def02016-12-16 17:16:59 -080034 os_info = _get_osinfo()
Allen Li142dc6d2016-12-16 17:03:45 -080035 _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 Li739def02016-12-16 17:16:59 -080041class 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 Li142dc6d2016-12-16 17:03:45 -080046
47
Allen Li739def02016-12-16 17:16:59 -080048def _get_osinfo():
Allen Li142dc6d2016-12-16 17:03:45 -080049 """Get OS name and version.
50
51 Returns:
52 OSInfo instance
53 """
Allen Lid78af972017-07-13 12:48:01 -070054 os_name = platform.system()
55 if os_name == 'Linux':
Allen Li739def02016-12-16 17:16:59 -080056 return _get_linux_osinfo()
Allen Li739def02016-12-16 17:16:59 -080057 else:
58 return OSInfo(name='', version='')
Allen Li142dc6d2016-12-16 17:03:45 -080059
Allen Li739def02016-12-16 17:16:59 -080060
Allen Li739def02016-12-16 17:16:59 -080061def _get_linux_osinfo():
62 # will return something like ('Ubuntu', '14.04', 'trusty')
Mike Frysinger8205ef82020-05-14 23:53:12 -040063 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 Li739def02016-12-16 17:16:59 -080069 return OSInfo(name=os_name, version=os_version)
70
71
Allen Li142dc6d2016-12-16 17:03:45 -080072def _get_python_arch():
73 if sys.maxsize > 2**32:
74 return '64'
75 else:
76 return '32'