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 contextlib | |
| 6 import json | |
| 7 import logging | |
| 8 import os | |
| 9 import socket | |
| 10 import time | |
| 11 | |
| 12 | |
| 13 from infra_libs import ts_mon | |
| 14 from infra_libs.ts_mon.common import interface | |
| 15 from infra_libs.ts_mon.common import targets | |
| 16 | |
| 17 | |
| 18 ANDROID_DEVICE_FILE_VERSION = 1 | |
| 19 | |
| 20 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
| |
| 21 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
| |
| 22 'android_device_status.json') | |
| 23 | |
| 24 # Don't read a file older than this many seconds. | |
| 25 ANDROID_DEVICE_FILE_STALENESS_S = 120 | |
| 26 | |
| 27 | |
| 28 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
| |
| 29 description='device CPU temperature in deg C') | |
| 30 batt_temp = ts_mon.FloatMetric('dev/battery/temperature', | |
| 31 description='battery temperature in deg C') | |
| 32 batt_charge = ts_mon.FloatMetric('dev/battery/charge', | |
| 33 description='percentage charge of battery') | |
| 34 dev_status = ts_mon.StringMetric('dev/status', | |
| 35 description='operational state of device') | |
| 36 dev_os = ts_mon.StringMetric('dev/os', | |
| 37 description='operating system of the device') | |
| 38 dev_uptime = ts_mon.FloatMetric('dev/uptime', | |
| 39 description='device uptime in seconds') | |
| 40 | |
| 41 metric_read_status = ts_mon.StringMetric( | |
| 42 'dev/android_device_metric_read/status', | |
| 43 description='status of the last metric read') | |
| 44 | |
| 45 | |
| 46 @contextlib.contextmanager | |
| 47 def device_target(): | |
| 48 """Switches metric transmission to the device target.""" | |
| 49 old_target = interface.state.target | |
| 50 | |
| 51 # 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.
| |
| 52 fqdn = socket.getfqdn().lower() # foo-[a|m]N.[chrome|golo].chromium.org | |
| 53 host = fqdn.split('.')[0] # foo-[a|m]N | |
| 54 region = ts_mon.config._default_region(fqdn) | |
| 55 network = ts_mon.config._default_network(host) | |
| 56 | |
| 57 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.
| |
| 58 region, | |
| 59 'temperature_monitor', | |
| 60 network, | |
| 61 host) | |
| 62 | |
| 63 yield | |
| 64 | |
| 65 interface.state.target = old_target | |
| 66 | |
| 67 | |
| 68 def get_device_statuses(): | |
| 69 success, devices = _load_android_device_file() | |
| 70 if not success: | |
| 71 return | |
| 72 | |
| 73 with device_target(): | |
| 74 for device_name, device in devices.iteritems(): | |
| 75 fields = {'device_id': device_name} | |
| 76 | |
| 77 # Fields with special handling. | |
| 78 cpu_temps = device.get('temp', []) | |
| 79 | |
| 80 battery_temp = device.get('battery', {}).get('temperature') | |
| 81 battery_temp = battery_temp / 10.0 if battery_temp else None | |
| 82 | |
| 83 status = device.get('state') | |
| 84 status = 'good' if status == 'available' else status | |
| 85 | |
| 86 for metric, value in ( | |
| 87 (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.
| |
| 88 (batt_temp, battery_temp), | |
| 89 (batt_charge, device.get('battery', {}).get('level')), | |
| 90 (dev_status, status), | |
| 91 (dev_os, device.get('build', {}).get('build.id')), | |
| 92 (dev_uptime, device.get('uptime', None))): | |
| 93 if value is not None: | |
| 94 metric.set(value, fields=fields) | |
| 95 | |
| 96 | |
| 97 def _load_android_device_file(): | |
| 98 """Load the android device file and check for errors or staleness.""" | |
| 99 try: | |
| 100 with open(ANDROID_DEVICE_FILE) as f: | |
| 101 file_data = f.read() | |
| 102 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.
| |
| 103 # File isn't there, not an Android bot. | |
| 104 metric_read_status.set('not_found') | |
| 105 logging.debug('Android device file %s not found', ANDROID_DEVICE_FILE) | |
| 106 return False, None | |
| 107 | |
| 108 try: | |
| 109 json_data = json.loads(file_data) | |
| 110 except ValueError as e: | |
| 111 metric_read_status.set('invalid_json') | |
| 112 logging.error('Android device file %s invalid json: %s', | |
| 113 ANDROID_DEVICE_FILE, e) | |
| 114 return False, None | |
| 115 | |
| 116 if not isinstance(json_data, dict): | |
| 117 metric_read_status.set('invalid_json') | |
| 118 logging.error('Android device file %s is not a dict', ANDROID_DEVICE_FILE) | |
| 119 return False, None | |
| 120 | |
| 121 if json_data.get('version') not in ( | |
| 122 ANDROID_DEVICE_FILE_VERSION, | |
| 123 ANDROID_PREVIOUS_DEVICE_FILE_VERSION): | |
| 124 metric_read_status.set('invalid_version') | |
| 125 logging.error('Android device file %s is version %s, not %s', | |
| 126 ANDROID_DEVICE_FILE, json_data.get('version'), | |
| 127 ANDROID_DEVICE_FILE_VERSION) | |
| 128 return False, None | |
| 129 | |
| 130 now = time.time() | |
| 131 timestamp = json_data.get('timestamp', 0) | |
| 132 if now >= timestamp + ANDROID_DEVICE_FILE_STALENESS_S: | |
| 133 metric_read_status.set('stale_file') | |
| 134 logging.error('Android device file %s is %ss stale (max %ss)', | |
| 135 ANDROID_DEVICE_FILE, now - timestamp, | |
| 136 ANDROID_DEVICE_FILE_STALENESS_S) | |
| 137 return False, None | |
| 138 | |
| 139 metric_read_status.set('good') | |
| 140 return True, json_data.get('devices', []) | |
| OLD | NEW |