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