blob: 671ebac988f746eddf38d21107d36d63a6c0458f [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
Allen Li13bdf0c2017-03-02 15:18:16 -08009from __future__ import unicode_literals
Allen Li142dc6d2016-12-16 17:03:45 -080010
11import collections
12import platform
13import sys
14
15from infra_libs import ts_mon
16
17_os_name_metric = ts_mon.StringMetric(
18 'proc/os/name',
19 description='OS name on the machine')
20
21_os_version_metric = ts_mon.StringMetric(
22 'proc/os/version',
23 description='OS version on the machine')
24
25_os_arch_metric = ts_mon.StringMetric(
26 'proc/os/arch',
27 description='OS architecture on this machine')
28
29_python_arch_metric = ts_mon.StringMetric(
30 'proc/python/arch',
31 description='python userland architecture on this machine')
32
33
Allen Li45ae8392017-03-02 14:19:35 -080034def collect_os_info():
Allen Li739def02016-12-16 17:16:59 -080035 os_info = _get_osinfo()
Allen Li142dc6d2016-12-16 17:03:45 -080036 _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())
40
41
Allen Li739def02016-12-16 17:16:59 -080042class OSInfo(collections.namedtuple('OSInfo', 'name,version')):
43 """namedtuple representing OS info (all fields lowercased)."""
44
45 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():
Allen Li142dc6d2016-12-16 17:03:45 -080050 """Get OS name and version.
51
52 Returns:
53 OSInfo instance
54 """
55 os_name = platform.system().lower()
Allen Li142dc6d2016-12-16 17:03:45 -080056 if 'windows' in os_name:
Allen Li739def02016-12-16 17:16:59 -080057 return _get_windows_osinfo()
Allen Li142dc6d2016-12-16 17:03:45 -080058 elif 'linux' in os_name:
Allen Li739def02016-12-16 17:16:59 -080059 return _get_linux_osinfo()
60 elif _is_mac():
Allen Li142dc6d2016-12-16 17:03:45 -080061 # On mac platform.system() reports 'darwin'.
Allen Li739def02016-12-16 17:16:59 -080062 #
63 # TODO(ayatane): I'm not sure how true the above comment is, but I
64 # have no reason to remove it nor change the existing logic right
65 # now.
66 return _get_mac_osinfo()
67 else:
68 return OSInfo(name='', version='')
Allen Li142dc6d2016-12-16 17:03:45 -080069
Allen Li739def02016-12-16 17:16:59 -080070
71def _get_windows_osinfo():
72 os_name = 'windows'
73 # release will be something like '7', 'vista', or 'xp'
74 os_version = platform.release()
Allen Li142dc6d2016-12-16 17:03:45 -080075 return OSInfo(name=os_name, version=os_version)
76
77
Allen Li739def02016-12-16 17:16:59 -080078def _get_linux_osinfo():
79 # will return something like ('Ubuntu', '14.04', 'trusty')
80 os_name, os_version, _ = platform.dist()
81 return OSInfo(name=os_name, version=os_version)
82
83
84def _get_mac_osinfo():
85 return OSInfo(name='mac', version=_get_mac_version())
86
87
88def _is_mac():
89 """Return whether the current system is a Mac."""
90 return bool(_get_mac_version())
91
92
Allen Li142dc6d2016-12-16 17:03:45 -080093def _get_mac_version():
94 """Get Mac system version.
95
96 Returns:
97 Version string, which is empty if not a valid Mac system.
98 """
Allen Li739def02016-12-16 17:16:59 -080099 return platform.mac_ver()[0]
Allen Li142dc6d2016-12-16 17:03:45 -0800100
101
102def _get_python_arch():
103 if sys.maxsize > 2**32:
104 return '64'
105 else:
106 return '32'