Zack Williams | 84cb77a | 2022-01-14 14:46:39 -0800 | [diff] [blame^] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # SPDX-FileCopyrightText: © 2022 Open Networking Foundation <support@opennetworking.org> |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | """ |
| 7 | cbrs_configure.py |
| 8 | |
| 9 | a tool to configure target eNB devices |
| 10 | """ |
| 11 | |
| 12 | import time |
| 13 | import argparse |
| 14 | import base64 |
| 15 | import datetime |
| 16 | import requests |
| 17 | from lxml import etree |
| 18 | |
| 19 | |
| 20 | def timestamp(): |
| 21 | """ replace with logger eventually """ |
| 22 | return datetime.datetime.now().strftime("[%Y-%m-%d %H:%M:%S]") |
| 23 | |
| 24 | |
| 25 | parser = argparse.ArgumentParser() |
| 26 | parser.add_argument("address", help="eNB IP address", type=str) |
| 27 | parser.add_argument( |
| 28 | "-u", "--username", help="eNB UI login username", type=str, default="sc_femto" |
| 29 | ) |
| 30 | parser.add_argument( |
| 31 | "-p", "--password", help="eNB UI login password", type=str, default="scHt3pp" |
| 32 | ) |
| 33 | parser.add_argument( |
| 34 | "-d", "--delay", help="delay in seconds beetween checks", type=int, default=60 |
| 35 | ) |
| 36 | parser.add_argument( |
| 37 | "-c", |
| 38 | "--count", |
| 39 | help="number of checks before sending reboot", |
| 40 | type=int, |
| 41 | default=10, |
| 42 | ) |
| 43 | |
| 44 | |
| 45 | args = parser.parse_args() |
| 46 | |
| 47 | ENB_DOMAIN = f"https://{args.address}/" |
| 48 | CGI_URL = ENB_DOMAIN + "setup.cgi" |
| 49 | LTESTATE_URL = ENB_DOMAIN + "LTE_status.htm" |
| 50 | |
| 51 | USERNAME = bytes(args.username.encode()) |
| 52 | PASSWORD = bytes(args.password.encode()) |
| 53 | |
| 54 | # Disable SSL verification since we signed the certificate by our own |
| 55 | session = requests.session() |
| 56 | session.verify = False |
| 57 | |
| 58 | # The login payload passed to cgi gateway |
| 59 | login_payload = { |
| 60 | "un": base64.b64encode(USERNAME).decode(), |
| 61 | "pw": base64.b64encode(PASSWORD).decode(), |
| 62 | "this_file": "logon.htm", |
| 63 | "next_file": "status.htm", |
| 64 | "todo": "login", |
| 65 | } |
| 66 | |
| 67 | while True: |
| 68 | try: |
| 69 | r = session.post(CGI_URL, data=login_payload, timeout=2) |
| 70 | e = etree.HTML(r.text) |
| 71 | serial_number = e.xpath("//td")[4].text |
| 72 | except requests.exceptions.ConnectTimeout: |
| 73 | print(f"{timestamp()} Unable to connect {ENB_DOMAIN}, wait 10 seconds.") |
| 74 | time.sleep(10) |
| 75 | continue |
| 76 | except IndexError: |
| 77 | print(f"{timestamp()} Invalid XML returned from {CGI_URL}, wait 10 seconds.") |
| 78 | time.sleep(10) |
| 79 | continue |
| 80 | |
| 81 | cell_down_counter = 0 |
| 82 | |
| 83 | while True: |
| 84 | r = session.get(LTESTATE_URL) |
| 85 | if r.status_code != 200: |
| 86 | print(f"{timestamp()} Unable to load {LTESTATE_URL}, retry login.") |
| 87 | time.sleep(2) |
| 88 | break |
| 89 | |
| 90 | # try to parse XML, and cache any errors |
| 91 | cell_state = "BadXML" |
| 92 | try: |
| 93 | e = etree.HTML(r.text) |
| 94 | cell_state = e.xpath("//td")[23].text |
| 95 | except IndexError: |
| 96 | print(f"{timestamp()} Invalid XML returned from {LTESTATE_URL}") |
| 97 | |
| 98 | print(f"{timestamp()} Current cell state: {cell_state}") |
| 99 | |
| 100 | # check i |
| 101 | if cell_state == "Down": |
| 102 | cell_down_counter = cell_down_counter + 1 |
| 103 | else: |
| 104 | cell_down_counter = 0 |
| 105 | |
| 106 | # eNB is down for too long |
| 107 | if cell_down_counter > args.count: |
| 108 | cell_down_total = args.count * args.delay |
| 109 | print( |
| 110 | f" {timestamp()} Cell down reaches {cell_down_total}s, restart eNodeB." |
| 111 | ) |
| 112 | reboot_payload = { |
| 113 | "this_file": "status.htm", |
| 114 | "next_file": "status.htm", |
| 115 | "todo": "reboot", |
| 116 | } |
| 117 | session.post(CGI_URL, data=reboot_payload) |
| 118 | |
| 119 | # allow time for the eNB to restart itself |
| 120 | print( |
| 121 | f" {timestamp()} Waiting {cell_down_total} after eNodeB restart for recovery" |
| 122 | ) |
| 123 | time.sleep(cell_down_total) |
| 124 | break |
| 125 | |
| 126 | time.sleep(args.delay) |