blob: 665e2e7afaed13bbf003165a7aaebcab343291b8 [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
5# Copyright (c) 2015 The Chromium Authors. All rights reserved.
6# Use of this source code is governed by a BSD-style license that can be
7# found in the LICENSE file.
8
9"""System metrics."""
10
11from __future__ import print_function
12
13import collections
14import platform
15import sys
16
17from infra_libs import ts_mon
18
19_os_name_metric = ts_mon.StringMetric(
20 'proc/os/name',
21 description='OS name on the machine')
22
23_os_version_metric = ts_mon.StringMetric(
24 'proc/os/version',
25 description='OS version on the machine')
26
27_os_arch_metric = ts_mon.StringMetric(
28 'proc/os/arch',
29 description='OS architecture on this machine')
30
31_python_arch_metric = ts_mon.StringMetric(
32 'proc/python/arch',
33 description='python userland architecture on this machine')
34
35
Allen Li45ae8392017-03-02 14:19:35 -080036def collect_os_info():
Allen Li739def02016-12-16 17:16:59 -080037 os_info = _get_osinfo()
Allen Li142dc6d2016-12-16 17:03:45 -080038 _os_name_metric.set(os_info.name)
39 _os_version_metric.set(os_info.version)
40 _os_arch_metric.set(platform.machine())
41 _python_arch_metric.set(_get_python_arch())
42
43
Allen Li739def02016-12-16 17:16:59 -080044class OSInfo(collections.namedtuple('OSInfo', 'name,version')):
45 """namedtuple representing OS info (all fields lowercased)."""
46
47 def __new__(cls, name, version):
48 return super(OSInfo, cls).__new__(cls, name.lower(), version.lower())
Allen Li142dc6d2016-12-16 17:03:45 -080049
50
Allen Li739def02016-12-16 17:16:59 -080051def _get_osinfo():
Allen Li142dc6d2016-12-16 17:03:45 -080052 """Get OS name and version.
53
54 Returns:
55 OSInfo instance
56 """
57 os_name = platform.system().lower()
Allen Li142dc6d2016-12-16 17:03:45 -080058 if 'windows' in os_name:
Allen Li739def02016-12-16 17:16:59 -080059 return _get_windows_osinfo()
Allen Li142dc6d2016-12-16 17:03:45 -080060 elif 'linux' in os_name:
Allen Li739def02016-12-16 17:16:59 -080061 return _get_linux_osinfo()
62 elif _is_mac():
Allen Li142dc6d2016-12-16 17:03:45 -080063 # On mac platform.system() reports 'darwin'.
Allen Li739def02016-12-16 17:16:59 -080064 #
65 # TODO(ayatane): I'm not sure how true the above comment is, but I
66 # have no reason to remove it nor change the existing logic right
67 # now.
68 return _get_mac_osinfo()
69 else:
70 return OSInfo(name='', version='')
Allen Li142dc6d2016-12-16 17:03:45 -080071
Allen Li739def02016-12-16 17:16:59 -080072
73def _get_windows_osinfo():
74 os_name = 'windows'
75 # release will be something like '7', 'vista', or 'xp'
76 os_version = platform.release()
Allen Li142dc6d2016-12-16 17:03:45 -080077 return OSInfo(name=os_name, version=os_version)
78
79
Allen Li739def02016-12-16 17:16:59 -080080def _get_linux_osinfo():
81 # will return something like ('Ubuntu', '14.04', 'trusty')
82 os_name, os_version, _ = platform.dist()
83 return OSInfo(name=os_name, version=os_version)
84
85
86def _get_mac_osinfo():
87 return OSInfo(name='mac', version=_get_mac_version())
88
89
90def _is_mac():
91 """Return whether the current system is a Mac."""
92 return bool(_get_mac_version())
93
94
Allen Li142dc6d2016-12-16 17:03:45 -080095def _get_mac_version():
96 """Get Mac system version.
97
98 Returns:
99 Version string, which is empty if not a valid Mac system.
100 """
Allen Li739def02016-12-16 17:16:59 -0800101 return platform.mac_ver()[0]
Allen Li142dc6d2016-12-16 17:03:45 -0800102
103
104def _get_python_arch():
105 if sys.maxsize > 2**32:
106 return '64'
107 else:
108 return '32'