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

Unified 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 comments. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « infra/services/sysmon/__main__.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: infra/services/sysmon/android_device_metrics.py
diff --git a/infra/services/sysmon/android_device_metrics.py b/infra/services/sysmon/android_device_metrics.py
new file mode 100644
index 0000000000000000000000000000000000000000..8883820936e003997835cd72c2d78589349ead5a
--- /dev/null
+++ b/infra/services/sysmon/android_device_metrics.py
@@ -0,0 +1,116 @@
+# Copyright (c) 2016 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.
+
+import json
+import logging
+import os
+import socket
+import time
+
+
+from infra_libs import ts_mon
+from infra_libs.ts_mon.common import interface
+from infra_libs.ts_mon.common import targets
+
+
+ANDROID_DEVICE_FILE_VERSION = 1
+
+ANDROID_PREVIOUS_DEVICE_FILE_VERSION = ANDROID_DEVICE_FILE_VERSION - 1
+ANDROID_DEVICE_FILE = os.path.join(os.path.expanduser('~'),
+ 'android_device_status.json')
+
+# Don't read a file older than this many seconds.
+ANDROID_DEVICE_FILE_STALENESS_S = 120
+
+
+cpu_temp = ts_mon.FloatMetric('dev/cpu/temperature',
+ description='device CPU temperature in deg C')
+batt_temp = ts_mon.FloatMetric('dev/battery/temperature',
+ description='battery temperature in deg C')
+batt_charge = ts_mon.FloatMetric('dev/battery/charge',
+ description='percentage charge of battery')
+dev_status = ts_mon.StringMetric('dev/status',
+ description='operational state of device')
+dev_os = ts_mon.StringMetric('dev/os',
+ description='operating system of the device')
+dev_uptime = ts_mon.FloatMetric('dev/uptime',
+ description='device uptime in seconds')
+
+metric_read_status = ts_mon.StringMetric(
bpastene 2016/05/19 18:36:44 What are your plans for this metric?
ghost stip (do not use) 2016/05/20 21:12:50 potential debugging, also a quick way to see if a
+ 'dev/android_device_metric_read/status',
+ description='status of the last metric read')
+
+
+def get_device_statuses():
+ success, devices = _load_android_device_file()
+ if not success:
bpastene 2016/05/19 18:36:44 Nit: Remove success from the return and replace th
ghost stip (do not use) 2016/05/20 21:12:50 sure, why not
+ return
+
+ for device_name, device in devices.iteritems():
+ fields = {'device_id': device_name}
+
+ # Fields with special handling.
+ cpu_temps = device.get('temp', [])
bpastene 2016/05/19 18:36:44 I don't think this is a list as reported by swarmi
ghost stip (do not use) 2016/05/20 21:12:50 you're right
+
+ battery_temp = device.get('battery', {}).get('temperature')
+ battery_temp = battery_temp / 10.0 if battery_temp else None
bpastene 2016/05/19 18:36:44 I've been burned repeatedly with some devices repo
ghost stip (do not use) 2016/05/20 21:12:50 yikes
+
+ status = device.get('state')
+ status = 'good' if status == 'available' else status
+
+ for metric, value in (
+ (cpu_temp, cpu_temps[0] if cpu_temps else None),
+ (batt_temp, battery_temp),
+ (batt_charge, device.get('battery', {}).get('level')),
+ (dev_status, status),
+ (dev_os, device.get('build', {}).get('build.id')),
+ (dev_uptime, device.get('uptime', None))):
+ if value is not None:
+ metric.set(value, fields=fields)
+
+
+def _load_android_device_file():
+ """Load the android device file and check for errors or staleness."""
+ try:
+ with open(ANDROID_DEVICE_FILE) as f:
+ file_data = f.read()
+ except (IOError, OSError):
+ # File isn't there, not an Android bot.
+ metric_read_status.set('not_found')
+ logging.debug('Android device file %s not found', ANDROID_DEVICE_FILE)
+ return False, None
+
+ try:
+ json_data = json.loads(file_data)
+ except ValueError as e:
+ metric_read_status.set('invalid_json')
+ logging.error('Android device file %s invalid json: %s',
+ ANDROID_DEVICE_FILE, e)
+ return False, None
+
+ if not isinstance(json_data, dict):
+ metric_read_status.set('invalid_json')
+ logging.error('Android device file %s is not a dict', ANDROID_DEVICE_FILE)
+ return False, None
+
+ if json_data.get('version') not in (
+ ANDROID_DEVICE_FILE_VERSION,
+ ANDROID_PREVIOUS_DEVICE_FILE_VERSION):
+ metric_read_status.set('invalid_version')
+ logging.error('Android device file %s is version %s, not %s',
+ ANDROID_DEVICE_FILE, json_data.get('version'),
+ ANDROID_DEVICE_FILE_VERSION)
+ return False, None
+
+ now = time.time()
+ timestamp = json_data.get('timestamp', 0)
+ if now >= timestamp + ANDROID_DEVICE_FILE_STALENESS_S:
+ metric_read_status.set('stale_file')
+ logging.error('Android device file %s is %ss stale (max %ss)',
+ ANDROID_DEVICE_FILE, now - timestamp,
+ ANDROID_DEVICE_FILE_STALENESS_S)
+ return False, None
+
+ metric_read_status.set('good')
+ return True, json_data.get('devices', [])
« no previous file with comments | « infra/services/sysmon/__main__.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698