Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: infra/services/sysmon/android_device_metrics.py

Issue 1981853002: Send ts_mon metrics from android device data. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Address final comments, fix coverage. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/device_uptime',
38 description='device uptime in seconds')
39
40 metric_read_status = ts_mon.StringMetric(
41 'dev/android_device_metric_read/status',
42 description='status of the last metric read')
43
44
45 def get_device_statuses(device_file=ANDROID_DEVICE_FILE, now=None):
46 now = now or time.time()
47 devices = _load_android_device_file(device_file, now)
48 if not devices:
49 return
50
51 for device_name, device in devices.iteritems():
52 fields = {'device_id': device_name}
53
54 # Fields with special handling.
55
56 battery_temp = device.get('battery', {}).get('temperature')
57 battery_temp = battery_temp / 10.0 if battery_temp else None
58
59 status = device.get('state')
60 status = 'good' if status == 'available' else status
61
62 for metric, value in (
63 (cpu_temp, device.get('temp', {}).get('emmc_therm')),
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'))):
69 if value is not None:
70 metric.set(value, fields=fields)
71
72
73 def _load_android_device_file(device_file, now):
74 """Load the android device file and check for errors or staleness."""
75 try:
76 with open(device_file) as f:
77 file_data = f.read() # pragma: no cover
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', device_file)
82 return []
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 device_file, e)
90 return []
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', device_file)
95 return []
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 device_file, json_data.get('version'),
103 ANDROID_DEVICE_FILE_VERSION)
104 return []
105
106 timestamp = json_data.get('timestamp', 0)
107 if now >= timestamp + ANDROID_DEVICE_FILE_STALENESS_S:
108 metric_read_status.set('stale_file')
109 logging.error('Android device file %s is %ss stale (max %ss)',
110 device_file, now - timestamp,
111 ANDROID_DEVICE_FILE_STALENESS_S)
112 return []
113
114 metric_read_status.set('good')
115 return json_data.get('devices', [])
OLDNEW
« no previous file with comments | « infra/services/sysmon/__main__.py ('k') | infra/services/sysmon/test/android_device_metrics_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698