Chromium Code Reviews| Index: infra/services/sysmon/android_device_metrics.py |
| diff --git a/infra/services/sysmon/android_device_metrics.py b/infra/services/sysmon/android_device_metrics.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fd0f0f6c428db4ef35c16a6c7e9f2286afd8b7a7 |
| --- /dev/null |
| +++ b/infra/services/sysmon/android_device_metrics.py |
| @@ -0,0 +1,140 @@ |
| +# Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import contextlib |
| +import json |
| +import logging |
| +import os |
| +import socket |
| +import time |
| + |
| + |
| +from infra_libs import ts_mon |
| +from infra_libs.ts_mon.common import interface |
| +from infra_libs.ts_mon.common import targets |
| + |
| + |
| +ANDROID_DEVICE_FILE_VERSION = 1 |
| + |
| +ANDROID_PREVIOUS_DEVICE_FILE_VERSION = ANDROID_DEVICE_FILE_VERSION - 1 |
|
ghost stip (do not use)
2016/05/16 07:39:16
not used right now, but version bumps + cipd is a
|
| +ANDROID_DEVICE_FILE = os.path.join(os.path.expanduser('~'), |
|
ghost stip (do not use)
2016/05/16 07:39:16
as of https://chromereviews.googleplex.com/4260270
|
| + 'android_device_status.json') |
| + |
| +# Don't read a file older than this many seconds. |
| +ANDROID_DEVICE_FILE_STALENESS_S = 120 |
| + |
| + |
| +cpu_temp = ts_mon.FloatMetric('dev/cpu/temperature', |
|
ghost stip (do not use)
2016/05/16 07:39:16
I tried to match these to https://chromium.googles
|
| + description='device CPU temperature in deg C') |
| +batt_temp = ts_mon.FloatMetric('dev/battery/temperature', |
| + description='battery temperature in deg C') |
| +batt_charge = ts_mon.FloatMetric('dev/battery/charge', |
| + description='percentage charge of battery') |
| +dev_status = ts_mon.StringMetric('dev/status', |
| + description='operational state of device') |
| +dev_os = ts_mon.StringMetric('dev/os', |
| + description='operating system of the device') |
| +dev_uptime = ts_mon.FloatMetric('dev/uptime', |
| + description='device uptime in seconds') |
| + |
| +metric_read_status = ts_mon.StringMetric( |
| + 'dev/android_device_metric_read/status', |
| + description='status of the last metric read') |
| + |
| + |
| +@contextlib.contextmanager |
| +def device_target(): |
| + """Switches metric transmission to the device target.""" |
| + old_target = interface.state.target |
| + |
| + # From https://goo.gl/WuJDSu. |
|
M-A Ruel
2016/05/16 23:01:13
not a fan of using url shortener
ghost stip (do not use)
2016/05/17 22:32:12
Done.
|
| + fqdn = socket.getfqdn().lower() # foo-[a|m]N.[chrome|golo].chromium.org |
| + host = fqdn.split('.')[0] # foo-[a|m]N |
| + region = ts_mon.config._default_region(fqdn) |
| + network = ts_mon.config._default_network(host) |
| + |
| + interface.state.target = targets.DeviceTarget( |
|
ghost stip (do not use)
2016/05/16 07:39:16
I don't know if you can just *do* this, leaving it
Sergey Berezin
2016/05/16 19:01:43
Nope, this is effectively a dangerous no-op. The d
ghost stip (do not use)
2016/05/17 22:32:12
Done.
|
| + region, |
| + 'temperature_monitor', |
| + network, |
| + host) |
| + |
| + yield |
| + |
| + interface.state.target = old_target |
| + |
| + |
| +def get_device_statuses(): |
| + success, devices = _load_android_device_file() |
| + if not success: |
| + return |
| + |
| + with device_target(): |
| + for device_name, device in devices.iteritems(): |
| + fields = {'device_id': device_name} |
| + |
| + # Fields with special handling. |
| + cpu_temps = device.get('temp', []) |
| + |
| + battery_temp = device.get('battery', {}).get('temperature') |
| + battery_temp = battery_temp / 10.0 if battery_temp else None |
| + |
| + status = device.get('state') |
| + status = 'good' if status == 'available' else status |
| + |
| + for metric, value in ( |
| + (cpu_temp, cpu_temps[0] if cpu_temps else None), |
|
ghost stip (do not use)
2016/05/16 07:42:30
maruel: I'm hoping that swarming is smart enough i
M-A Ruel
2016/05/16 23:01:13
Update the bot_config as you need if you find anyt
ghost stip (do not use)
2016/05/17 22:32:12
Acknowledged.
|
| + (batt_temp, battery_temp), |
| + (batt_charge, device.get('battery', {}).get('level')), |
| + (dev_status, status), |
| + (dev_os, device.get('build', {}).get('build.id')), |
| + (dev_uptime, device.get('uptime', None))): |
| + if value is not None: |
| + metric.set(value, fields=fields) |
| + |
| + |
| +def _load_android_device_file(): |
| + """Load the android device file and check for errors or staleness.""" |
| + try: |
| + with open(ANDROID_DEVICE_FILE) as f: |
| + file_data = f.read() |
| + except IOError: |
|
ghost stip (do not use)
2016/05/16 07:39:16
I feel like I'm missing another Error here... mayb
M-A Ruel
2016/05/16 23:01:13
(IOError, OSError)
ghost stip (do not use)
2016/05/17 22:32:13
Done.
|
| + # File isn't there, not an Android bot. |
| + metric_read_status.set('not_found') |
| + logging.debug('Android device file %s not found', ANDROID_DEVICE_FILE) |
| + return False, None |
| + |
| + try: |
| + json_data = json.loads(file_data) |
| + except ValueError as e: |
| + metric_read_status.set('invalid_json') |
| + logging.error('Android device file %s invalid json: %s', |
| + ANDROID_DEVICE_FILE, e) |
| + return False, None |
| + |
| + if not isinstance(json_data, dict): |
| + metric_read_status.set('invalid_json') |
| + logging.error('Android device file %s is not a dict', ANDROID_DEVICE_FILE) |
| + return False, None |
| + |
| + if json_data.get('version') not in ( |
| + ANDROID_DEVICE_FILE_VERSION, |
| + ANDROID_PREVIOUS_DEVICE_FILE_VERSION): |
| + metric_read_status.set('invalid_version') |
| + logging.error('Android device file %s is version %s, not %s', |
| + ANDROID_DEVICE_FILE, json_data.get('version'), |
| + ANDROID_DEVICE_FILE_VERSION) |
| + return False, None |
| + |
| + now = time.time() |
| + timestamp = json_data.get('timestamp', 0) |
| + if now >= timestamp + ANDROID_DEVICE_FILE_STALENESS_S: |
| + metric_read_status.set('stale_file') |
| + logging.error('Android device file %s is %ss stale (max %ss)', |
| + ANDROID_DEVICE_FILE, now - timestamp, |
| + ANDROID_DEVICE_FILE_STALENESS_S) |
| + return False, None |
| + |
| + metric_read_status.set('good') |
| + return True, json_data.get('devices', []) |