OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 | |
7 """Produce health statistics for all attached Android devices.""" | |
8 | |
9 | |
10 import json | |
11 import re | |
12 import shlex | |
13 import subprocess | |
14 | |
15 | |
16 def sanitize(string): | |
17 """Return a string which is safe for InfluxDB.""" | |
18 return re.sub('\W+', '_', string.strip()) | |
19 | |
20 | |
21 def get_devices(): | |
22 """Return a list of attached-and-ready Android devices.""" | |
23 devices = [] | |
24 for line in subprocess.check_output(['adb', 'devices']).splitlines()[1:]: | |
25 if not line: | |
26 continue | |
27 parts = shlex.split(line) | |
28 if len(parts) != 2: | |
29 continue | |
30 if parts[1] == 'device': | |
31 devices.append(parts[0]) | |
32 return devices | |
33 | |
34 | |
35 def get_device_model(serial): | |
36 """Return the model name for the given device.""" | |
37 cmd = ['adb', '-s', serial, 'shell', 'getprop', 'ro.product.model'] | |
38 return sanitize(subprocess.check_output(cmd)) | |
39 | |
40 | |
41 def get_battery_stats(serial): | |
42 """Obtain and return a dictionary of battery statistics for the device.""" | |
43 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
| |
44 output = subprocess.check_output(cmd) | |
45 parts = re.findall('([a-zA-Z0-9\s]+): (\d+)\s*', output) | |
46 rv = {} | |
47 for k, v in parts: | |
48 rv[sanitize(k)] = int(v) | |
49 return rv | |
50 | |
51 | |
52 def get_temperature(serial): | |
53 """Obtain and return the temperature of the device.""" | |
54 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
| |
55 cmd = ['adb', '-s', serial, 'shell', 'cat', temp_file] | |
56 output = subprocess.check_output(cmd).strip() | |
57 try: | |
58 temp = float(output) | |
59 except Exception: | |
60 return -1 | |
61 # Normalize the temperature, assuming it's 9 < t < 100 degrees C. | |
62 while temp > 100.0: | |
63 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
| |
64 return temp | |
65 | |
66 | |
67 def get_device_stats(serial): | |
68 """Obtain and return a dictionary of device statistics.""" | |
69 return { | |
70 'battery': get_battery_stats(serial), | |
71 'model': get_device_model(serial), | |
72 'temperature': get_temperature(serial), | |
73 } | |
74 | |
75 | |
76 def get_all_device_stats(): | |
77 """Obtain and return statistics for all attached devices.""" | |
78 devices = get_devices() | |
79 stats = {} | |
80 for device in devices: | |
81 stats[device] = get_device_stats(device) | |
82 return stats | |
83 | |
84 | |
85 if __name__ == '__main__': | |
86 print json.dumps(get_all_device_stats(), sort_keys=True, indent=4) | |
OLD | NEW |