Chromium Code Reviews| Index: scripts/android_stats.py |
| diff --git a/scripts/android_stats.py b/scripts/android_stats.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..e723fb090a8af4aebe3e4fea96c4b7ce66136823 |
| --- /dev/null |
| +++ b/scripts/android_stats.py |
| @@ -0,0 +1,86 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2015 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. |
| + |
| + |
| +"""Produce health statistics for all attached Android devices.""" |
| + |
| + |
| +import json |
| +import re |
| +import shlex |
| +import subprocess |
| + |
| + |
| +def sanitize(string): |
| + """Return a string which is safe for InfluxDB.""" |
| + return re.sub('\W+', '_', string.strip()) |
| + |
| + |
| +def get_devices(): |
| + """Return a list of attached-and-ready Android devices.""" |
| + devices = [] |
| + for line in subprocess.check_output(['adb', 'devices']).splitlines()[1:]: |
| + if not line: |
| + continue |
| + parts = shlex.split(line) |
| + if len(parts) != 2: |
| + continue |
| + if parts[1] == 'device': |
| + devices.append(parts[0]) |
| + return devices |
| + |
| + |
| +def get_device_model(serial): |
| + """Return the model name for the given device.""" |
| + cmd = ['adb', '-s', serial, 'shell', 'getprop', 'ro.product.model'] |
| + return sanitize(subprocess.check_output(cmd)) |
| + |
| + |
| +def get_battery_stats(serial): |
| + """Obtain and return a dictionary of battery statistics for the device.""" |
| + cmd = ['adb', '-s', serial, 'shell', 'dumpsys', 'batteryproperties'] |
|
rmistry
2015/12/01 16:33:17
I ran this on N5 and it said:
Can't find service:
borenet
2015/12/01 20:00:07
Yeah, the services are apparently different betwee
djsollen
2015/12/02 15:46:03
I think that batteryproperties should be available
|
| + output = subprocess.check_output(cmd) |
| + parts = re.findall('([a-zA-Z0-9\s]+): (\d+)\s*', output) |
| + rv = {} |
| + for k, v in parts: |
| + rv[sanitize(k)] = int(v) |
| + return rv |
| + |
| + |
| +def get_temperature(serial): |
| + """Obtain and return the temperature of the device.""" |
| + temp_file = '/sys/devices/virtual/thermal/thermal_zone0/temp' |
|
rmistry
2015/12/01 16:33:17
What about the other zones?
On the N5 it is showin
borenet
2015/12/01 20:00:07
Dunno. I'm still hoping to find a better way to d
djsollen
2015/12/02 15:46:03
You could loop through all the zones and print the
borenet
2015/12/02 15:59:26
I think I'll leave as-is for now and update if/whe
|
| + cmd = ['adb', '-s', serial, 'shell', 'cat', temp_file] |
| + output = subprocess.check_output(cmd).strip() |
| + try: |
| + temp = float(output) |
| + except Exception: |
| + return -1 |
| + # Normalize the temperature, assuming it's 9 < t < 100 degrees C. |
| + while temp > 100.0: |
| + temp /= 10 |
|
rmistry
2015/12/01 16:33:17
Why is this necessary? do some devices output 30C
borenet
2015/12/01 20:00:07
Yes. The two devices on my desk report 2780 and 3
|
| + return temp |
| + |
| + |
| +def get_device_stats(serial): |
| + """Obtain and return a dictionary of device statistics.""" |
| + return { |
| + 'battery': get_battery_stats(serial), |
| + 'model': get_device_model(serial), |
| + 'temperature': get_temperature(serial), |
| + } |
| + |
| + |
| +def get_all_device_stats(): |
| + """Obtain and return statistics for all attached devices.""" |
| + devices = get_devices() |
| + stats = {} |
| + for device in devices: |
| + stats[device] = get_device_stats(device) |
| + return stats |
| + |
| + |
| +if __name__ == '__main__': |
| + print json.dumps(get_all_device_stats(), sort_keys=True, indent=4) |