This script is a monitoring tool for a Huawei LTE modem. It's designed to check the connectivity of the network by pinging a target IP. If the target IP does not respond, it attempts to reboot the modem. After a successful reboot, it sends a notification to a specified Discord webhook. Let's break it down:
Importing necessary modules: The script starts by importing necessary Python modules. Some modules used here are os, time, signal, requests, threading, subprocess, logging, deque, argparse, huawei_lte_api.Connection, huawei_lte_api.Client, huawei_lte_api.enums.client, and dotenv.
Environment variables: It then loads environment variables using dotenv module, which reads the environment variables from a .env file.
Function Definitions: The script then defines various functions that are used later in the script:
ping(host): This function uses the ping command to check if a specified host is reachable.
reboot_modem(client): This function uses the Huawei LTE API to reboot the modem.
signal_handler(signum, frame): This function handles signals from the operating system, allowing the script to exit gracefully when it receives a termination signal.
send_discord_message(webhook_url, content): This function sends a message to a Discord webhook.
delayed_send_discord_message(webhook_url, content, delay): This function sends a message to a Discord webhook after a delay, in a new thread.
Environment Variables and Command-line Arguments: It gets settings from environment variables and command-line arguments. These settings include the modem's URL, username, password, target IP, interval between pings, maximum number of reboots, and reboot interval.
Connection Setup: The script then establishes a connection with the modem's web interface, logging into it with the provided username and password. If the connection or login fails, it tries again a certain number of times.
Main Loop: Finally, the main loop of the script continuously pings the target IP. If the ping fails:
It checks if the modem is responding. If it's not, it re-authenticates and tries again.
If the modem is responding, it checks whether it's allowed to reboot the modem (based on the maximum number of reboots and the reboot interval).
If it's allowed to reboot the modem, it does so and sends a delayed message to a Discord webhook. It then waits for the modem to restart, implementing an exponential backoff strategy to increase the wait time if subsequent reboots are needed.
If it's not allowed to reboot the modem (because it has reached the maximum number of reboots), it logs a warning.
If the ping is successful, it waits for the specified interval before pinging again.
This script is designed to be run continuously, and must be stopped manually. It catches termination signals to allow it to exit gracefully when stopped.