Improve ADB based monitoring agent
- Ensure "Mobile Data" option is enabled before starting the test
- Enable logging
- Run as systemd service instead of daemon
Change-Id: Id446071961692afe5bda41e827068ffe11dd2350
diff --git a/edge-monitoring/agent_adb/edge_monitoring_agent_adb.py b/edge-monitoring/agent_adb/edge_monitoring_agent_adb.py
old mode 100644
new mode 100755
index f26f5d1..86c05d5
--- a/edge-monitoring/agent_adb/edge_monitoring_agent_adb.py
+++ b/edge-monitoring/agent_adb/edge_monitoring_agent_adb.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
# Copyright 2020-present Open Networking Foundation
#
@@ -14,12 +14,13 @@
# See the License for the specific language governing permissions and
# limitations under the License
+import sys
import os
import time
import requests
import json
import enum
-import daemon
+import logging
from collections import namedtuple
from pyadb import ADB
@@ -36,6 +37,12 @@
object_hook=lambda d: namedtuple('X', d.keys())(*d.values())
)
+logging.basicConfig(
+ filename=CONF.log_file,
+ format='%(asctime)s [%(levelname)s] %(message)s',
+ level=logging.WARN
+)
+
ADB_GET_COMMANDS = {
"apn_mode": "settings get global airplane_mode_on",
"lte_state": "dumpsys telephony.registry | grep -m1 mDataConnectionState",
@@ -72,9 +79,10 @@
def _run_adb_shell(adb, command):
result, error = adb.shell_command(command)
- if error is not None:
- err = "[ERROR]: " + command + " failed"
- return False, err
+ # error is returned even when succeeded
+ # so ignore error value here
+ if result:
+ logging.debug(result)
time.sleep(2)
result = result[0] if result is not None else None
return True, result
@@ -132,22 +140,35 @@
'''
report the aether network state to the monitoring server
'''
+ logging.info("Sending report %s", edge_status)
try:
result = requests.post(CONF.report_url, json=edge_status)
except requests.exceptions.ConnectionError:
+ logging.error("Failed to report for %s", e)
+ pass
+ try:
+ result.raise_for_status()
+ except requests.exceptions.HTTPError as e:
+ logging.error("Failed to report for %s", e)
pass
-def run():
+def main():
adb = ADB()
if adb.set_adb_path(CONF.adb.path) is False:
- err = "[ERROR]: " + CONF.adb.path + " not found"
- return State.error, err
+ logging.error(CONF.adb.path + " not found")
+ sys.exit(1)
dev = adb.get_devices()
if len(dev) == 0:
- return State.error, "Device not found"
+ logging.error("No device found")
+ sys.exit(1)
+
adb.set_target_device(dev[0])
+ success, result = _run_adb_shell(adb, "svc data enable")
+ if not success:
+ logging.error("Failed to turn data on")
+ sys.exit(1)
while True:
cp_state, err = get_control_plane_state(adb)
@@ -160,10 +181,5 @@
time.sleep(CONF.report_interval)
-def main():
- with daemon.DaemonContext():
- run()
-
-
if __name__ == "__main__":
main()