Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2016 The Chromium 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 import json | |
| 6 import logging | |
| 7 import os | |
| 8 import socket | |
| 9 import time | |
| 10 | |
| 11 | |
| 12 from infra_libs import ts_mon | |
| 13 from infra_libs.ts_mon.common import interface | |
| 14 from infra_libs.ts_mon.common import targets | |
| 15 | |
| 16 | |
| 17 ANDROID_DEVICE_FILE_VERSION = 1 | |
| 18 | |
| 19 ANDROID_PREVIOUS_DEVICE_FILE_VERSION = ANDROID_DEVICE_FILE_VERSION - 1 | |
| 20 ANDROID_DEVICE_FILE = os.path.join(os.path.expanduser('~'), | |
| 21 'android_device_status.json') | |
| 22 | |
| 23 # Don't read a file older than this many seconds. | |
| 24 ANDROID_DEVICE_FILE_STALENESS_S = 120 | |
| 25 | |
| 26 | |
| 27 cpu_temp = ts_mon.FloatMetric('dev/cpu/temperature', | |
| 28 description='device CPU temperature in deg C') | |
| 29 batt_temp = ts_mon.FloatMetric('dev/battery/temperature', | |
| 30 description='battery temperature in deg C') | |
| 31 batt_charge = ts_mon.FloatMetric('dev/battery/charge', | |
| 32 description='percentage charge of battery') | |
| 33 dev_status = ts_mon.StringMetric('dev/status', | |
| 34 description='operational state of device') | |
| 35 dev_os = ts_mon.StringMetric('dev/os', | |
| 36 description='operating system of the device') | |
| 37 dev_uptime = ts_mon.FloatMetric('dev/uptime', | |
| 38 description='device uptime in seconds') | |
| 39 | |
| 40 metric_read_status = ts_mon.StringMetric( | |
|
bpastene
2016/05/19 18:36:44
What are your plans for this metric?
ghost stip (do not use)
2016/05/20 21:12:50
potential debugging, also a quick way to see if a
| |
| 41 'dev/android_device_metric_read/status', | |
| 42 description='status of the last metric read') | |
| 43 | |
| 44 | |
| 45 def get_device_statuses(): | |
| 46 success, devices = _load_android_device_file() | |
| 47 if not success: | |
|
bpastene
2016/05/19 18:36:44
Nit: Remove success from the return and replace th
ghost stip (do not use)
2016/05/20 21:12:50
sure, why not
| |
| 48 return | |
| 49 | |
| 50 for device_name, device in devices.iteritems(): | |
| 51 fields = {'device_id': device_name} | |
| 52 | |
| 53 # Fields with special handling. | |
| 54 cpu_temps = device.get('temp', []) | |
|
bpastene
2016/05/19 18:36:44
I don't think this is a list as reported by swarmi
ghost stip (do not use)
2016/05/20 21:12:50
you're right
| |
| 55 | |
| 56 battery_temp = device.get('battery', {}).get('temperature') | |
| 57 battery_temp = battery_temp / 10.0 if battery_temp else None | |
|
bpastene
2016/05/19 18:36:44
I've been burned repeatedly with some devices repo
ghost stip (do not use)
2016/05/20 21:12:50
yikes
| |
| 58 | |
| 59 status = device.get('state') | |
| 60 status = 'good' if status == 'available' else status | |
| 61 | |
| 62 for metric, value in ( | |
| 63 (cpu_temp, cpu_temps[0] if cpu_temps else None), | |
| 64 (batt_temp, battery_temp), | |
| 65 (batt_charge, device.get('battery', {}).get('level')), | |
| 66 (dev_status, status), | |
| 67 (dev_os, device.get('build', {}).get('build.id')), | |
| 68 (dev_uptime, device.get('uptime', None))): | |
| 69 if value is not None: | |
| 70 metric.set(value, fields=fields) | |
| 71 | |
| 72 | |
| 73 def _load_android_device_file(): | |
| 74 """Load the android device file and check for errors or staleness.""" | |
| 75 try: | |
| 76 with open(ANDROID_DEVICE_FILE) as f: | |
| 77 file_data = f.read() | |
| 78 except (IOError, OSError): | |
| 79 # File isn't there, not an Android bot. | |
| 80 metric_read_status.set('not_found') | |
| 81 logging.debug('Android device file %s not found', ANDROID_DEVICE_FILE) | |
| 82 return False, None | |
| 83 | |
| 84 try: | |
| 85 json_data = json.loads(file_data) | |
| 86 except ValueError as e: | |
| 87 metric_read_status.set('invalid_json') | |
| 88 logging.error('Android device file %s invalid json: %s', | |
| 89 ANDROID_DEVICE_FILE, e) | |
| 90 return False, None | |
| 91 | |
| 92 if not isinstance(json_data, dict): | |
| 93 metric_read_status.set('invalid_json') | |
| 94 logging.error('Android device file %s is not a dict', ANDROID_DEVICE_FILE) | |
| 95 return False, None | |
| 96 | |
| 97 if json_data.get('version') not in ( | |
| 98 ANDROID_DEVICE_FILE_VERSION, | |
| 99 ANDROID_PREVIOUS_DEVICE_FILE_VERSION): | |
| 100 metric_read_status.set('invalid_version') | |
| 101 logging.error('Android device file %s is version %s, not %s', | |
| 102 ANDROID_DEVICE_FILE, json_data.get('version'), | |
| 103 ANDROID_DEVICE_FILE_VERSION) | |
| 104 return False, None | |
| 105 | |
| 106 now = time.time() | |
| 107 timestamp = json_data.get('timestamp', 0) | |
| 108 if now >= timestamp + ANDROID_DEVICE_FILE_STALENESS_S: | |
| 109 metric_read_status.set('stale_file') | |
| 110 logging.error('Android device file %s is %ss stale (max %ss)', | |
| 111 ANDROID_DEVICE_FILE, now - timestamp, | |
| 112 ANDROID_DEVICE_FILE_STALENESS_S) | |
| 113 return False, None | |
| 114 | |
| 115 metric_read_status.set('good') | |
| 116 return True, json_data.get('devices', []) | |
| OLD | NEW |