OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
ghost stip (do not use)
2015/09/08 21:46:36
Add a license directive to the top. see other file
bpastene
2015/09/09 00:02:18
Done.
| |
2 | |
3 """ | |
ghost stip (do not use)
2015/09/08 21:46:36
"""Single line description of the module, ending i
bpastene
2015/09/09 00:02:19
Done.
| |
4 Launches a daemon to monitor the devices' temperatures by | |
5 uploading them to monarch every 30 seconds. | |
6 """ | |
7 | |
8 import json | |
9 import logging | |
10 import os | |
11 import optparse | |
12 import re | |
13 import signal | |
14 import subprocess | |
15 import sys | |
16 import time | |
17 | |
18 # Common name of sensor found in nexus devices to measure cpu (core0) temp | |
ghost stip (do not use)
2015/09/08 21:46:36
comments end in a period.
bpastene
2015/09/09 00:02:18
Done.
| |
19 _CPU_TEMP_SENSOR = 'tsens_tz_sensor0' | |
20 | |
21 # TODO(bpastene): change the following once infra.git becomes a checked | |
22 # out repo slaves instead of a cipd managed package | |
ghost stip (do not use)
2015/09/08 21:46:36
same
bpastene
2015/09/09 00:02:18
Done.
| |
23 | |
24 # Location of the infra-python package's run script | |
ghost stip (do not use)
2015/09/08 21:46:36
same
bpastene
2015/09/09 00:02:18
Done.
| |
25 _RUN_PY = '/opt/infra-python/run.py' | |
26 | |
ghost stip (do not use)
2015/09/08 21:46:36
two spaces for top-level definitions
bpastene
2015/09/09 00:02:18
Done.
| |
27 class SigtermError(Exception): | |
28 """Exception used to catch a sigterm.""" | |
29 pass | |
30 | |
ghost stip (do not use)
2015/09/08 21:46:36
space
bpastene
2015/09/09 00:02:18
Done.
| |
31 def get_device_args(adb_path, metric_prefix, device): | |
32 bat_temp = None | |
33 cpu_temp = None | |
34 # Search for the file that the _CPU_TEMP_SENSOR dumps to and cat it | |
ghost stip (do not use)
2015/09/08 21:46:36
nit: add a . at the end of a comment.
bpastene
2015/09/09 00:02:18
Done.
| |
35 cmd = [adb_path, '-s', device, 'shell', | |
36 'grep -l "%s" /sys/class/thermal/thermal_zone*/type' | |
37 % (_CPU_TEMP_SENSOR)] | |
38 try: | |
39 cpu_temp_files = subprocess.check_output(cmd) | |
40 if (len(cpu_temp_files.splitlines()) == 1): | |
41 cpu_temp_file = re.sub('type$', 'temp', cpu_temp_files.strip()) | |
42 cmd = [adb_path, '-s', device, 'shell', | |
43 'cat %s' % (cpu_temp_file)] | |
44 file_contents = subprocess.check_output(cmd) | |
45 cpu_temp = int(file_contents) | |
46 except (subprocess.CalledProcessError, TypeError, ValueError): | |
47 cpu_temp = None | |
48 | |
49 # Dump system battery info and grab the temp | |
ghost stip (do not use)
2015/09/08 21:46:36
nit: end in period.
bpastene
2015/09/09 00:02:19
Done.
| |
50 cmd = [adb_path, '-s', device, 'shell', 'dumpsys battery'] | |
51 try: | |
52 battery_info = subprocess.check_output(cmd) | |
53 for line in battery_info.splitlines(): | |
54 m = re.match('^\s*temperature: ([0-9]+)\s*$', line) | |
55 if m: | |
56 bat_temp = int(m.group(1)) | |
57 except (subprocess.CalledProcessError, TypeError, ValueError): | |
58 bat_temp = None | |
59 | |
60 cpu_dict = {'name': "%s/%s/cpu_temp" % (metric_prefix, device), | |
61 'value': cpu_temp} | |
62 cpu_temp_args = ['--float', json.dumps(cpu_dict)] if cpu_temp else [] | |
63 battery_dict = {'name': '%s/%s/battery_temp' % (metric_prefix, | |
64 device), 'value': bat_temp} | |
65 bat_temp_args = ['--float', | |
66 json.dumps(battery_dict)] if bat_temp else [] | |
67 return cpu_temp_args + bat_temp_args | |
68 | |
ghost stip (do not use)
2015/09/08 21:46:36
space
bpastene
2015/09/09 00:02:19
Done.
| |
69 def main(adb_path, | |
70 devices_json, | |
71 metric_prefix="android_device"): | |
72 """Polls the devices for their battery and cpu temperatures | |
ghost stip (do not use)
2015/09/08 21:46:36
"""One line description ending in a period.
Fulle
bpastene
2015/09/09 00:02:18
Done.
| |
73 every 30 seconds and uploads them to monarch through infra's | |
74 ts_mon. Fully qualified, the metric names would be | |
75 /chrome/infra/${metric_prefix}/${device_serial}/(battery|cpu)_temp | |
76 | |
77 Args: | |
78 adb_path: Path to adb binary | |
79 devices_json: Json list of device serials to poll | |
80 metric_prefix: Prefix of the metric name. | |
81 """ | |
82 | |
83 def SigtermHandler(_signum, _unused_frame): | |
ghost stip (do not use)
2015/09/08 21:46:36
delete lines 83 - 85
bpastene
2015/09/09 00:02:18
Done.
| |
84 raise SigtermError() | |
85 signal.signal(signal.SIGTERM, SigtermHandler) | |
86 | |
87 devices = json.loads(devices_json) | |
88 try: | |
89 while True: | |
90 upload_cmd_args = [] | |
91 for device in devices: | |
92 upload_cmd_args += get_device_args(adb_path, metric_prefix, device) | |
93 | |
94 cmd = [_RUN_PY, 'infra.tools.send_ts_mon_values'] + upload_cmd_args | |
95 try: | |
96 subprocess.Popen(cmd) | |
97 except OSError: | |
98 logging.exception('Unable to call %s', _RUN_PY) | |
99 | |
100 time.sleep(30) | |
101 except SigtermError: | |
ghost stip (do not use)
2015/09/08 21:46:36
delete lines 100 - 104
bpastene
2015/09/09 00:02:18
Done.
| |
102 logging.info("Got a SIGTERM, shutting down") | |
103 except: # pylint: disable=bare-except | |
104 logging.exception('Unexpected exception in main.') | |
105 | |
106 if __name__ == '__main__': | |
107 sys.exit(main(*sys.argv[1:])) | |
OLD | NEW |