blob: 4129e7ad89c56d6ddf8a79fd8a4de31e7ac06182 [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 -08008from __future__ import print_function
9
10import collections
11import platform
12import sys
13
Allen Lia9c6e802017-07-11 15:42:47 -070014from chromite.lib import metrics
Allen Li142dc6d2016-12-16 17:03:45 -080015
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 """
54 os_name = platform.system().lower()
Allen Li142dc6d2016-12-16 17:03:45 -080055 if 'windows' in os_name:
Allen Li739def02016-12-16 17:16:59 -080056 return _get_windows_osinfo()
Allen Li142dc6d2016-12-16 17:03:45 -080057 elif 'linux' in os_name:
Allen Li739def02016-12-16 17:16:59 -080058 return _get_linux_osinfo()
59 elif _is_mac():
Allen Li142dc6d2016-12-16 17:03:45 -080060 # On mac platform.system() reports 'darwin'.
Allen Li739def02016-12-16 17:16:59 -080061 #
62 # TODO(ayatane): I'm not sure how true the above comment is, but I
63 # have no reason to remove it nor change the existing logic right
64 # now.
65 return _get_mac_osinfo()
66 else:
67 return OSInfo(name='', version='')
Allen Li142dc6d2016-12-16 17:03:45 -080068
Allen Li739def02016-12-16 17:16:59 -080069
70def _get_windows_osinfo():
71 os_name = 'windows'
72 # release will be something like '7', 'vista', or 'xp'
73 os_version = platform.release()
Allen Li142dc6d2016-12-16 17:03:45 -080074 return OSInfo(name=os_name, version=os_version)
75
76
Allen Li739def02016-12-16 17:16:59 -080077def _get_linux_osinfo():
78 # will return something like ('Ubuntu', '14.04', 'trusty')
79 os_name, os_version, _ = platform.dist()
80 return OSInfo(name=os_name, version=os_version)
81
82
83def _get_mac_osinfo():
84 return OSInfo(name='mac', version=_get_mac_version())
85
86
87def _is_mac():
88 """Return whether the current system is a Mac."""
89 return bool(_get_mac_version())
90
91
Allen Li142dc6d2016-12-16 17:03:45 -080092def _get_mac_version():
93 """Get Mac system version.
94
95 Returns:
96 Version string, which is empty if not a valid Mac system.
97 """
Allen Li739def02016-12-16 17:16:59 -080098 return platform.mac_ver()[0]
Allen Li142dc6d2016-12-16 17:03:45 -080099
100
101def _get_python_arch():
102 if sys.maxsize > 2**32:
103 return '64'
104 else:
105 return '32'