blob: b73a45cb2265d3c33926cedc41536e778693b3bc [file] [log] [blame]
# Copyright 2016 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""System metrics."""
from __future__ import absolute_import
import collections
import platform
import sys
from chromite.lib import metrics
_os_name_metric = metrics.StringMetric(
description='OS name on the machine')
_os_version_metric = metrics.StringMetric(
description='OS version on the machine')
_os_arch_metric = metrics.StringMetric(
description='OS architecture on this machine')
_python_arch_metric = metrics.StringMetric(
description='python userland architecture on this machine')
def collect_os_info():
os_info = _get_osinfo()
class OSInfo(collections.namedtuple('OSInfo', 'name,version')):
"""namedtuple representing OS info (all fields lowercased)."""
def __new__(cls, name, version):
return super(OSInfo, cls).__new__(cls, name.lower(), version.lower())
def _get_osinfo():
"""Get OS name and version.
OSInfo instance
os_name = platform.system()
if os_name == 'Linux':
return _get_linux_osinfo()
return OSInfo(name='', version='')
def _get_linux_osinfo():
# will return something like ('Ubuntu', '14.04', 'trusty')
# This is removed in Python 3.8+.
# pylint: disable=deprecated-method
os_name, os_version, _ = platform.dist()
except AttributeError:
os_name = os_version = ''
return OSInfo(name=os_name, version=os_version)
def _get_python_arch():
if sys.maxsize > 2**32:
return '64'
return '32'