blob: 841548dce24510a37ef43a02e1d4736367e2ab97 [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
36def get_os_info():
37 os_info = _get_os_info()
38 _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
44OSInfo = collections.namedtuple('OSInfo', 'name,version')
45
46
47def _get_os_info():
48 """Get OS name and version.
49
50 Returns:
51 OSInfo instance
52 """
53 os_name = platform.system().lower()
54 os_version = ''
55 if 'windows' in os_name:
56 os_name = 'windows'
57 # release will be something like '7', 'vista', or 'xp'
58 os_version = platform.release()
59 elif 'linux' in os_name:
60 # will return something like ('Ubuntu', '14.04', 'trusty')
61 os_name, os_version, _ = platform.dist()
62 else:
63 # On mac platform.system() reports 'darwin'.
64 os_version = _get_mac_version()
65 if os_version:
66 # We found a valid mac.
67 os_name = 'mac'
68 else:
69 # not a mac, unable to find platform information, reset
70 os_name = ''
71 os_version = ''
72
73 os_name = os_name.lower()
74 os_version = os_version.lower()
75 return OSInfo(name=os_name, version=os_version)
76
77
78def _get_mac_version():
79 """Get Mac system version.
80
81 Returns:
82 Version string, which is empty if not a valid Mac system.
83 """
84 # This tuple is only populated on mac systems.
85 mac_ver = platform.mac_ver()
86 # Will be '10.11.5' or similar on a valid mac or will be '' on a non-mac.
87 os_version = mac_ver[0]
88 return os_version
89
90
91def _get_python_arch():
92 if sys.maxsize > 2**32:
93 return '64'
94 else:
95 return '32'