Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 2 | |
| 3 | # Copyright 2020-present Open Networking Foundation |
| 4 | # |
Andy Bavier | 200bd27 | 2022-06-09 11:15:51 -0700 | [diff] [blame^] | 5 | # SPDX-License-Identifier: Apache-2.0 |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 6 | |
Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 7 | import sys |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 8 | import os |
| 9 | import time |
| 10 | import requests |
| 11 | import json |
| 12 | import enum |
Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 13 | import logging |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 14 | from collections import namedtuple |
| 15 | from pyadb import ADB |
| 16 | |
| 17 | ''' |
| 18 | Check Aether network operational status and report it to |
| 19 | central monitoring server |
| 20 | |
| 21 | 1) check mobile connctivity after toggling the airplane mode |
| 22 | 2) check if ping to 8.8.8.8 works |
| 23 | ''' |
| 24 | |
| 25 | CONF = json.loads( |
| 26 | open(os.getenv('CONFIG_FILE', "./config.json")).read(), |
| 27 | object_hook=lambda d: namedtuple('X', d.keys())(*d.values()) |
| 28 | ) |
| 29 | |
Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 30 | logging.basicConfig( |
| 31 | filename=CONF.log_file, |
| 32 | format='%(asctime)s [%(levelname)s] %(message)s', |
| 33 | level=logging.WARN |
| 34 | ) |
| 35 | |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 36 | ADB_GET_COMMANDS = { |
| 37 | "apn_mode": "settings get global airplane_mode_on", |
| 38 | "lte_state": "dumpsys telephony.registry | grep -m1 mDataConnectionState", |
Jeremy Ronquillo | d996b51 | 2021-02-13 13:45:47 -0800 | [diff] [blame] | 39 | "ping_result": "ping -c 3 8.8.8.8&>/dev/null; echo $?", |
| 40 | "ping_dns_result": "ping -c 10 " + "8.8.8.8" + |
| 41 | " | tail -1 | awk '{print $4}'" |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 42 | } |
| 43 | ADB_APN_COMMANDS = { |
| 44 | "home": "input keyevent 3", |
| 45 | "setting": "am start -a android.settings.AIRPLANE_MODE_SETTINGS", |
| 46 | "toggle": "input tap " + \ |
| 47 | CONF.adb.apn_mode_toggle_location.x + " " + \ |
| 48 | CONF.adb.apn_mode_toggle_location.y |
| 49 | } |
| 50 | |
| 51 | |
| 52 | class State(enum.Enum): |
| 53 | error = "-1" |
| 54 | disconnected = "0" |
| 55 | connecting = "1" |
| 56 | connected = "2" |
| 57 | |
| 58 | @classmethod |
| 59 | def has_value(cls, value): |
| 60 | return value in cls._value2member_map_ |
| 61 | |
| 62 | |
| 63 | edge_status = { |
| 64 | 'name': CONF.edge_name, |
| 65 | 'status': { |
| 66 | 'control_plane': None, |
| 67 | 'user_plane': 'connected' |
Jeremy Ronquillo | d996b51 | 2021-02-13 13:45:47 -0800 | [diff] [blame] | 68 | }, |
| 69 | 'speedtest': { |
| 70 | 'ping': { |
| 71 | 'dns': { |
Jeremy Ronquillo | a944fbc | 2021-03-30 10:57:45 -0700 | [diff] [blame] | 72 | 'min': 0.0, |
| 73 | 'avg': 0.0, |
| 74 | 'max': 0.0, |
| 75 | 'stddev': 0.0 |
Jeremy Ronquillo | d996b51 | 2021-02-13 13:45:47 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | def _run_adb_shell(adb, command): |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 83 | result, error = adb.shell_command(command) |
Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 84 | # error is returned even when succeeded |
| 85 | # so ignore error value here |
| 86 | if result: |
| 87 | logging.debug(result) |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 88 | time.sleep(2) |
| 89 | result = result[0] if result is not None else None |
| 90 | return True, result |
| 91 | |
| 92 | |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 93 | def get_control_plane_state(adb): |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 94 | ''' |
| 95 | check aether control plane works by toggling airplane mode |
| 96 | ''' |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 97 | # get the current airplane mode |
| 98 | success, result = _run_adb_shell(adb, ADB_GET_COMMANDS['apn_mode']) |
| 99 | if not success or result is None: |
| 100 | return State.error, result |
| 101 | apn_mode_on = True if result == "1" else False |
| 102 | |
| 103 | # toggle the airplane mode |
| 104 | for command in ADB_APN_COMMANDS.values(): |
| 105 | success, result = _run_adb_shell(adb, command) |
| 106 | if not success: |
| 107 | return State.error, result |
| 108 | if not apn_mode_on: |
| 109 | success, result = _run_adb_shell(adb, ADB_APN_COMMANDS['toggle']) |
| 110 | if not success: |
| 111 | return State.error, result |
| 112 | |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 113 | # get connection state |
| 114 | state = State.connecting.value |
Hyunsun Moon | a40bda5 | 2021-01-21 12:04:43 -0800 | [diff] [blame] | 115 | retry_count = 10 |
| 116 | while retry_count > 0: |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 117 | success, result = _run_adb_shell(adb, ADB_GET_COMMANDS['lte_state']) |
| 118 | if not success or result is None: |
Hyunsun Moon | a40bda5 | 2021-01-21 12:04:43 -0800 | [diff] [blame] | 119 | continue |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 120 | state = result.split("=")[1] |
Hyunsun Moon | a40bda5 | 2021-01-21 12:04:43 -0800 | [diff] [blame] | 121 | if state == State.connected.value: |
| 122 | break |
Hyunsun Moon | 659f281 | 2021-01-21 13:43:49 -0800 | [diff] [blame] | 123 | time.sleep(1) |
Hyunsun Moon | a40bda5 | 2021-01-21 12:04:43 -0800 | [diff] [blame] | 124 | retry_count -= 1 |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 125 | |
| 126 | if not State.has_value(state): |
| 127 | return State.error, None |
| 128 | return State(state), None |
| 129 | |
| 130 | |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 131 | def get_user_plane_state(adb): |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 132 | ''' |
| 133 | checks aether user plane connectivity with ping to 8.8.8.8 |
| 134 | ''' |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 135 | success, result = _run_adb_shell(adb, ADB_GET_COMMANDS['ping_result']) |
| 136 | if not success or result is None: |
| 137 | return State.error, result |
| 138 | |
| 139 | state = State.connected if result == "0" else State.disconnected |
| 140 | return state, None |
| 141 | |
| 142 | |
Jeremy Ronquillo | d996b51 | 2021-02-13 13:45:47 -0800 | [diff] [blame] | 143 | def run_ping_test(adb, ip, count): |
| 144 | ''' |
| 145 | Runs the ping test |
| 146 | Input: IP to ping, # times to ping |
| 147 | Returns: dict of the min/avg/max/stddev numbers from the ping command result |
| 148 | ''' |
Jeremy Ronquillo | a944fbc | 2021-03-30 10:57:45 -0700 | [diff] [blame] | 149 | result = {'min': 0.0, |
| 150 | 'avg': 0.0, |
| 151 | 'max': 0.0, |
| 152 | 'stddev': 0.0} |
Jeremy Ronquillo | d996b51 | 2021-02-13 13:45:47 -0800 | [diff] [blame] | 153 | try: |
| 154 | commandResult, commandOutput = _run_adb_shell(adb, ADB_GET_COMMANDS['ping_dns_result']) |
| 155 | pingResult = commandOutput.split("/") |
| 156 | result = {'min': float(pingResult[0]), |
| 157 | 'avg': float(pingResult[1]), |
| 158 | 'max': float(pingResult[2]), |
| 159 | 'stddev': float(pingResult[3])} |
| 160 | except Exception as e: |
| 161 | logging.error("Ping test failed for " + ip + ": %s", e) |
| 162 | return result |
| 163 | |
| 164 | |
| 165 | def get_ping_test(adb): |
| 166 | ''' |
| 167 | Each ping test result saves the min/avg/max/stddev to dict. |
| 168 | 1) Performs ping test to Google Public DNS for 10 iterations. |
| 169 | 2) # TODO: Performs ping to device on network. |
| 170 | ''' |
| 171 | speedtest_ping = {} |
| 172 | speedtest_ping['dns'] = run_ping_test(adb, "8.8.8.8", 10) |
| 173 | return speedtest_ping |
| 174 | |
| 175 | |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 176 | def report_aether_network_state(): |
| 177 | ''' |
| 178 | report the aether network state to the monitoring server |
| 179 | ''' |
Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 180 | logging.info("Sending report %s", edge_status) |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 181 | try: |
| 182 | result = requests.post(CONF.report_url, json=edge_status) |
| 183 | except requests.exceptions.ConnectionError: |
Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 184 | logging.error("Failed to report for %s", e) |
| 185 | pass |
| 186 | try: |
| 187 | result.raise_for_status() |
| 188 | except requests.exceptions.HTTPError as e: |
| 189 | logging.error("Failed to report for %s", e) |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 190 | pass |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 191 | |
| 192 | |
Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 193 | def main(): |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 194 | adb = ADB() |
| 195 | if adb.set_adb_path(CONF.adb.path) is False: |
Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 196 | logging.error(CONF.adb.path + " not found") |
| 197 | sys.exit(1) |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 198 | |
| 199 | dev = adb.get_devices() |
| 200 | if len(dev) == 0: |
Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 201 | logging.error("No device found") |
| 202 | sys.exit(1) |
| 203 | |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 204 | adb.set_target_device(dev[0]) |
Hyunsun Moon | 4506e3e | 2020-10-07 01:27:12 +0000 | [diff] [blame] | 205 | success, result = _run_adb_shell(adb, "svc data enable") |
| 206 | if not success: |
| 207 | logging.error("Failed to turn data on") |
| 208 | sys.exit(1) |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 209 | |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 210 | while True: |
Hyunsun Moon | da22c76 | 2021-01-22 13:00:24 -0800 | [diff] [blame] | 211 | _run_adb_shell(adb, "svc power stayon true") |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 212 | cp_state, err = get_control_plane_state(adb) |
| 213 | up_state, err = get_user_plane_state(adb) |
Jeremy Ronquillo | d996b51 | 2021-02-13 13:45:47 -0800 | [diff] [blame] | 214 | speedtest_ping = get_ping_test(adb) |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 215 | |
| 216 | edge_status['status']['control_plane'] = cp_state.name |
| 217 | edge_status['status']['user_plane'] = up_state.name |
Jeremy Ronquillo | d996b51 | 2021-02-13 13:45:47 -0800 | [diff] [blame] | 218 | edge_status['speedtest']['ping'] = speedtest_ping |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 219 | |
| 220 | report_aether_network_state() |
Hyunsun Moon | da22c76 | 2021-01-22 13:00:24 -0800 | [diff] [blame] | 221 | _run_adb_shell(adb, "svc power stayon false") |
| 222 | |
Hyunsun Moon | 53097ea | 2020-09-04 17:20:29 -0700 | [diff] [blame] | 223 | time.sleep(CONF.report_interval) |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 224 | |
| 225 | |
Hyunsun Moon | f32ae9a | 2020-05-28 13:17:45 -0700 | [diff] [blame] | 226 | if __name__ == "__main__": |
| 227 | main() |