Added cgi-bin scripts
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Configuration
|
||||
CONFIG_FILE="/etc/keep_alive_schedule.conf"
|
||||
STATUS_FILE="/tmp/keep_alive_status"
|
||||
SPEEDTEST_SCRIPT="/www/cgi-bin/home/speedtest/speedtest.sh"
|
||||
|
||||
# Function to convert HH:MM to minutes since midnight
|
||||
time_to_minutes() {
|
||||
echo "$1" | awk -F: '{print $1 * 60 + $2}'
|
||||
}
|
||||
|
||||
# Function to validate time interval
|
||||
validate_interval() {
|
||||
START_TIME=$1
|
||||
END_TIME=$2
|
||||
INTERVAL_MINUTES=$3
|
||||
|
||||
# Convert times to minutes
|
||||
START_MINUTES=$(time_to_minutes "$START_TIME")
|
||||
END_MINUTES=$(time_to_minutes "$END_TIME")
|
||||
|
||||
# Calculate duration between start and end time
|
||||
if [ $END_MINUTES -lt $START_MINUTES ]; then
|
||||
# Handle case where end time is on the next day
|
||||
DURATION=$((1440 - START_MINUTES + END_MINUTES))
|
||||
else
|
||||
DURATION=$((END_MINUTES - START_MINUTES))
|
||||
fi
|
||||
|
||||
# Check if interval is longer than duration
|
||||
if [ $INTERVAL_MINUTES -gt $DURATION ]; then
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to generate cron time expression
|
||||
generate_cron_time() {
|
||||
START_TIME=$1
|
||||
END_TIME=$2
|
||||
INTERVAL=$3
|
||||
|
||||
START_HOUR=$(echo "$START_TIME" | cut -d: -f1 | sed 's/^0//')
|
||||
START_MIN=$(echo "$START_TIME" | cut -d: -f2)
|
||||
END_HOUR=$(echo "$END_TIME" | cut -d: -f1 | sed 's/^0//')
|
||||
END_MIN=$(echo "$END_TIME" | cut -d: -f2)
|
||||
|
||||
# If end time is less than start time, it means we cross midnight
|
||||
if [ $(time_to_minutes "$END_TIME") -lt $(time_to_minutes "$START_TIME") ]; then
|
||||
# Create two cron entries for before and after midnight
|
||||
echo "*/$INTERVAL $START_HOUR-23 * * * $SPEEDTEST_SCRIPT"
|
||||
echo "*/$INTERVAL 0-$((END_HOUR - 1)) * * * $SPEEDTEST_SCRIPT"
|
||||
else
|
||||
echo "*/$INTERVAL $START_HOUR-$((END_HOUR - 1)) * * * $SPEEDTEST_SCRIPT"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to urldecode
|
||||
urldecode() {
|
||||
echo -e "$(echo "$1" | sed 's/+/ /g;s/%\([0-9A-F][0-9A-F]\)/\\x\1/g')"
|
||||
}
|
||||
|
||||
# Function to save configuration
|
||||
save_config() {
|
||||
echo "START_TIME=$1" >"$CONFIG_FILE"
|
||||
echo "END_TIME=$2" >>"$CONFIG_FILE"
|
||||
echo "INTERVAL=$3" >>"$CONFIG_FILE"
|
||||
echo "ENABLED=1" >>"$CONFIG_FILE"
|
||||
}
|
||||
|
||||
# Function to disable scheduling
|
||||
disable_scheduling() {
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
sed -i 's/ENABLED=1/ENABLED=0/' "$CONFIG_FILE"
|
||||
fi
|
||||
# Remove any existing cron jobs
|
||||
crontab -l | grep -v "$SPEEDTEST_SCRIPT" | crontab -
|
||||
}
|
||||
|
||||
# Function to get current status
|
||||
get_status() {
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
ENABLED=$(grep "ENABLED=" "$CONFIG_FILE" | cut -d'=' -f2)
|
||||
START_TIME=$(grep "START_TIME=" "$CONFIG_FILE" | cut -d'=' -f2)
|
||||
END_TIME=$(grep "END_TIME=" "$CONFIG_FILE" | cut -d'=' -f2)
|
||||
INTERVAL=$(grep "INTERVAL=" "$CONFIG_FILE" | cut -d'=' -f2)
|
||||
|
||||
echo "Status: 200 OK"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
echo "{\"enabled\":$ENABLED,\"start_time\":\"$START_TIME\",\"end_time\":\"$END_TIME\",\"interval\":$INTERVAL}"
|
||||
else
|
||||
echo "Status: 200 OK"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
echo "{\"enabled\":0,\"start_time\":\"\",\"end_time\":\"\",\"interval\":0}"
|
||||
fi
|
||||
}
|
||||
|
||||
# Handle POST requests
|
||||
if [ "$REQUEST_METHOD" = "POST" ]; then
|
||||
# Read POST data
|
||||
read -r POST_DATA
|
||||
|
||||
# Check if disabling is requested
|
||||
echo "$POST_DATA" | grep -q "disable=true"
|
||||
if [ $? -eq 0 ]; then
|
||||
disable_scheduling
|
||||
echo "Status: 200 OK"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
echo "{\"status\":\"success\",\"message\":\"Scheduling disabled\"}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Extract times and interval
|
||||
START_TIME=$(echo "$POST_DATA" | grep -o 'start_time=[^&]*' | cut -d'=' -f2)
|
||||
END_TIME=$(echo "$POST_DATA" | grep -o 'end_time=[^&]*' | cut -d'=' -f2)
|
||||
INTERVAL=$(echo "$POST_DATA" | grep -o 'interval=[^&]*' | cut -d'=' -f2)
|
||||
|
||||
# Decode times
|
||||
START_TIME=$(urldecode "$START_TIME")
|
||||
END_TIME=$(urldecode "$END_TIME")
|
||||
INTERVAL=$(urldecode "$INTERVAL")
|
||||
|
||||
# Validate times
|
||||
if [ -z "$START_TIME" ] || [ -z "$END_TIME" ] || [ -z "$INTERVAL" ]; then
|
||||
echo "Status: 400 Bad Request"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
echo "{\"error\":\"Missing start time, end time, or interval\"}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Validate interval is a number
|
||||
if ! echo "$INTERVAL" | grep -q '^[0-9]\+$'; then
|
||||
echo "Status: 400 Bad Request"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
echo "{\"error\":\"Interval must be a number in minutes\"}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Validate interval
|
||||
if ! validate_interval "$START_TIME" "$END_TIME" "$INTERVAL"; then
|
||||
echo "Status: 400 Bad Request"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
echo "{\"error\":\"Interval is longer than the time between start and end time\"}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create temporary file for new crontab
|
||||
TEMP_CRON=$(mktemp)
|
||||
|
||||
# Get existing crontab entries (excluding our script)
|
||||
crontab -l 2>/dev/null | grep -v "$SPEEDTEST_SCRIPT" >"$TEMP_CRON"
|
||||
|
||||
# Generate and add cron entries
|
||||
generate_cron_time "$START_TIME" "$END_TIME" "$INTERVAL" >>"$TEMP_CRON"
|
||||
|
||||
# Install new crontab
|
||||
crontab "$TEMP_CRON"
|
||||
rm "$TEMP_CRON"
|
||||
|
||||
# Save configuration
|
||||
save_config "$START_TIME" "$END_TIME" "$INTERVAL"
|
||||
|
||||
echo "Status: 200 OK"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
echo "{\"status\":\"success\",\"message\":\"Keep-alive scheduling enabled\"}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Parse query string for GET requests
|
||||
if [ "$REQUEST_METHOD" = "GET" ]; then
|
||||
QUERY_STRING=$(echo "$QUERY_STRING" | sed 's/&/\n/g')
|
||||
for param in $QUERY_STRING; do
|
||||
case "$param" in
|
||||
status=*)
|
||||
get_status
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
done
|
||||
fi
|
||||
|
||||
# If no valid request is made
|
||||
echo "Status: 400 Bad Request"
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
echo "{\"error\":\"Invalid request\"}"
|
||||
exit 1
|
||||
@@ -0,0 +1,61 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Configuration and log directories
|
||||
CONFIG_DIR="/etc/quecmanager/quecwatch"
|
||||
QUECWATCH_SCRIPT="${CONFIG_DIR}/quecwatch.sh"
|
||||
RCLOCAL="/etc/rc.local"
|
||||
LOG_DIR="/tmp/log/quecwatch"
|
||||
DEBUG_LOG_FILE="${LOG_DIR}/debug.log"
|
||||
|
||||
# Log directory for cleaning process
|
||||
CLEANUP_LOG_FILE="${LOG_DIR}/cleanup.log"
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "${LOG_DIR}"
|
||||
|
||||
# Function to log cleanup events
|
||||
log_cleanup() {
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "${CLEANUP_LOG_FILE}"
|
||||
}
|
||||
|
||||
# Default response headers
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Cleanup function
|
||||
cleanup_quecwatch() {
|
||||
# Start logging cleanup process
|
||||
log_cleanup "Starting QuecWatch cleanup process"
|
||||
|
||||
# Stop any running QuecWatch processes
|
||||
log_cleanup "Stopping QuecWatch processes"
|
||||
pkill -f "${QUECWATCH_SCRIPT}" >> "${CLEANUP_LOG_FILE}" 2>&1
|
||||
|
||||
# Remove QuecWatch script from rc.local
|
||||
if [ -f "${RCLOCAL}" ]; then
|
||||
log_cleanup "Removing QuecWatch entries from rc.local"
|
||||
sed -i '\|/etc/quecmanager/quecwatch/quecwatch.sh|d' "${RCLOCAL}" >> "${CLEANUP_LOG_FILE}" 2>&1
|
||||
fi
|
||||
|
||||
# Remove configuration directory
|
||||
if [ -d "${CONFIG_DIR}" ]; then
|
||||
log_cleanup "Removing configuration directory: ${CONFIG_DIR}"
|
||||
rm -rf "${CONFIG_DIR}" >> "${CLEANUP_LOG_FILE}" 2>&1
|
||||
fi
|
||||
|
||||
# Remove log directory
|
||||
if [ -d "${LOG_DIR}" ]; then
|
||||
log_cleanup "Removing log directory: ${LOG_DIR}"
|
||||
rm -rf "${LOG_DIR}" >> "${CLEANUP_LOG_FILE}" 2>&1
|
||||
fi
|
||||
|
||||
log_cleanup "QuecWatch cleanup completed successfully"
|
||||
|
||||
# Optional: Output JSON response
|
||||
echo '{"status": "success", "message": "QuecWatch disabled and removed"}'
|
||||
}
|
||||
|
||||
# Execute cleanup
|
||||
cleanup_quecwatch
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,411 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Read POST data
|
||||
read -r QUERY_STRING
|
||||
|
||||
# Function to urldecode
|
||||
urldecode() {
|
||||
echo -e "$(echo "$1" | sed 's/+/ /g;s/%\([0-9A-F][0-9A-F]\)/\\x\1/g')"
|
||||
}
|
||||
|
||||
# Configuration directory
|
||||
CONFIG_DIR="/etc/quecmanager/quecwatch"
|
||||
QUECWATCH_CONFIG="${CONFIG_DIR}/quecwatch.conf"
|
||||
QUECWATCH_SCRIPT="${CONFIG_DIR}/quecwatch.sh"
|
||||
RCLOCAL="/etc/rc.local"
|
||||
LOG_DIR="/tmp/log/quecwatch"
|
||||
DEBUG_LOG_FILE="${LOG_DIR}/debug.log"
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "${LOG_DIR}"
|
||||
|
||||
# Extract values from POST data
|
||||
action=$(echo "$QUERY_STRING" | grep -o 'action=[^&]*' | cut -d= -f2)
|
||||
ping_target=$(echo "$QUERY_STRING" | grep -o 'ping_target=[^&]*' | cut -d= -f2)
|
||||
ping_interval=$(echo "$QUERY_STRING" | grep -o 'ping_interval=[^&]*' | cut -d= -f2)
|
||||
ping_failures=$(echo "$QUERY_STRING" | grep -o 'ping_failures=[^&]*' | cut -d= -f2)
|
||||
max_retries=$(echo "$QUERY_STRING" | grep -o 'max_retries=[^&]*' | cut -d= -f2)
|
||||
connection_refresh=$(echo "$QUERY_STRING" | grep -o 'connection_refresh=[^&]*' | cut -d= -f2)
|
||||
auto_sim_failover=$(echo "$QUERY_STRING" | grep -o 'auto_sim_failover=[^&]*' | cut -d= -f2)
|
||||
sim_failover_schedule=$(echo "$QUERY_STRING" | grep -o 'sim_failover_schedule=[^&]*' | cut -d= -f2)
|
||||
|
||||
# URL decode the values
|
||||
action=$(urldecode "$action")
|
||||
ping_target=$(urldecode "$ping_target")
|
||||
ping_interval=$(urldecode "$ping_interval")
|
||||
ping_failures=$(urldecode "$ping_failures")
|
||||
max_retries=$(urldecode "$max_retries")
|
||||
connection_refresh=$(urldecode "$connection_refresh")
|
||||
auto_sim_failover=$(urldecode "$auto_sim_failover")
|
||||
sim_failover_schedule=$(urldecode "$sim_failover_schedule")
|
||||
|
||||
# Default response headers
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Validate inputs
|
||||
if [ -z "$ping_target" ]; then
|
||||
echo '{"status": "error", "message": "Ping target is required"}'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Initialize configuration function
|
||||
initialize_config() {
|
||||
# Create config directory if not exists
|
||||
mkdir -p "${CONFIG_DIR}"
|
||||
|
||||
# Write configuration with defaults and user-provided values
|
||||
cat >"${QUECWATCH_CONFIG}" <<EOL
|
||||
# QuecWatch Configuration File
|
||||
# Ping Target (IP or domain to ping)
|
||||
PING_TARGET=${ping_target}
|
||||
# Interval between ping checks (in seconds)
|
||||
PING_INTERVAL=${ping_interval:-30}
|
||||
# Number of consecutive ping failures before taking action
|
||||
PING_FAILURES=${ping_failures:-3}
|
||||
# Maximum number of retry attempts
|
||||
MAX_RETRIES=${max_retries:-5}
|
||||
# Current retry count (should start at 0)
|
||||
CURRENT_RETRIES=0
|
||||
# Enable/Disable Connection Refresh
|
||||
CONNECTION_REFRESH=${connection_refresh:-false}
|
||||
# Number of connection refresh attempts
|
||||
REFRESH_COUNT=${connection_refresh:+3}
|
||||
# Enable/Disable Auto SIM Failover
|
||||
AUTO_SIM_FAILOVER=${auto_sim_failover:-false}
|
||||
# Schedule for checking initial SIM (in minutes)
|
||||
# 0 means no scheduled check
|
||||
SIM_FAILOVER_SCHEDULE=${sim_failover_schedule:-0}
|
||||
# Indicate that QuecWatch is enabled
|
||||
ENABLED=true
|
||||
EOL
|
||||
|
||||
chmod 644 "${QUECWATCH_CONFIG}"
|
||||
}
|
||||
|
||||
# Generate monitoring script function
|
||||
generate_monitoring_script() {
|
||||
cat >"${QUECWATCH_SCRIPT}" <<'EOL'
|
||||
#!/bin/sh
|
||||
|
||||
# Load configuration
|
||||
. /etc/quecmanager/quecwatch/quecwatch.conf
|
||||
|
||||
# Define file paths
|
||||
QUEUE_FILE="/tmp/at_pipe.txt"
|
||||
LOG_FILE="/tmp/log/quecwatch/quecwatch.log"
|
||||
[ ! -f "${QUEUE_FILE}" ] && touch "${QUEUE_FILE}"
|
||||
|
||||
# Enhanced logging function with debug level
|
||||
log_message() {
|
||||
local level="$1"
|
||||
local message="$2"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo "${timestamp} - [${level}] ${message}" >> "$LOG_FILE"
|
||||
logger -t quecwatch "${level}: ${message}"
|
||||
}
|
||||
|
||||
# Check for stale entries and clean them
|
||||
check_and_clean_stale() {
|
||||
local command_type="$1"
|
||||
local wait_count=0
|
||||
|
||||
while [ $wait_count -lt 6 ]; do
|
||||
if grep -q "\"command\":\"${command_type}\"" "$QUEUE_FILE"; then
|
||||
log_message "DEBUG" "Waiting for ${command_type} to clear (attempt ${wait_count})"
|
||||
sleep 1
|
||||
wait_count=$((wait_count + 1))
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
log_message "WARN" "Removing stale ${command_type} entry after ${wait_count}s"
|
||||
sed -i "/\"command\":\"${command_type}\"/d" "$QUEUE_FILE"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Handle lock with debug logging
|
||||
handle_lock() {
|
||||
log_message "DEBUG" "Checking queue file status before lock"
|
||||
if [ -f "$QUEUE_FILE" ]; then
|
||||
log_message "DEBUG" "Current queue content: $(cat $QUEUE_FILE)"
|
||||
else
|
||||
log_message "DEBUG" "Queue file does not exist, creating it"
|
||||
touch "$QUEUE_FILE"
|
||||
fi
|
||||
|
||||
check_and_clean_stale "FETCH_LOCK"
|
||||
|
||||
log_message "DEBUG" "Adding AT_COMMAND entry to queue"
|
||||
printf '{"command":"AT_COMMAND","pid":"%s","timestamp":"%s"}\n' \
|
||||
"$$" \
|
||||
"$(date '+%H:%M:%S')" >> "$QUEUE_FILE"
|
||||
|
||||
check_and_clean_stale "AT_COMMAND"
|
||||
}
|
||||
|
||||
# Execute AT command with enhanced error handling
|
||||
execute_at_command() {
|
||||
local command="$1"
|
||||
local result=""
|
||||
local retry_count=0
|
||||
local max_retries=3
|
||||
|
||||
log_message "DEBUG" "Executing AT command: ${command}"
|
||||
|
||||
while [ $retry_count -lt $max_retries ]; do
|
||||
handle_lock
|
||||
|
||||
result=$(sms_tool at "$command" -t 4 2>&1)
|
||||
local status=$?
|
||||
|
||||
log_message "DEBUG" "Removing our entry from queue"
|
||||
sed -i "/\"pid\":\"$$\"/d" "$QUEUE_FILE"
|
||||
|
||||
if [ $status -eq 0 ] && [ -n "$result" ]; then
|
||||
log_message "DEBUG" "Command successful. Output: $result"
|
||||
echo "$result"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_message "WARN" "Command failed (attempt $((retry_count + 1))): $result"
|
||||
retry_count=$((retry_count + 1))
|
||||
[ $retry_count -lt $max_retries ] && sleep 2
|
||||
done
|
||||
|
||||
log_message "ERROR" "Command failed after $max_retries attempts: $command"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Function to update retry count in config
|
||||
update_retry_count() {
|
||||
local new_retry_count=$1
|
||||
sed -i "s/CURRENT_RETRIES=[0-9]*/CURRENT_RETRIES=${new_retry_count}/" /etc/quecmanager/quecwatch/quecwatch.conf
|
||||
# Reload config to ensure latest values
|
||||
. /etc/quecmanager/quecwatch/quecwatch.conf
|
||||
}
|
||||
|
||||
# Function to get current SIM slot with enhanced error handling
|
||||
get_current_sim() {
|
||||
local output
|
||||
local retry_count=0
|
||||
local max_retries=3
|
||||
|
||||
while [ $retry_count -lt $max_retries ]; do
|
||||
output=$(execute_at_command "AT+QUIMSLOT?")
|
||||
if [ $? -eq 0 ] && echo "$output" | grep -q "+QUIMSLOT:"; then
|
||||
echo "$output" | grep "+QUIMSLOT:" | awk '{print $2}'
|
||||
return 0
|
||||
fi
|
||||
retry_count=$((retry_count + 1))
|
||||
[ $retry_count -lt $max_retries ] && sleep 2
|
||||
done
|
||||
|
||||
log_message "ERROR" "Failed to get current SIM slot after $max_retries attempts"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Function to switch SIM card with enhanced error handling
|
||||
switch_sim_card() {
|
||||
log_message "INFO" "Attempting to switch SIM card"
|
||||
|
||||
# Get current SIM slot
|
||||
current_sim_slot=$(get_current_sim)
|
||||
if [ $? -ne 0 ]; then
|
||||
log_message "ERROR" "Failed to get current SIM slot"
|
||||
return 1
|
||||
fi # Changed from } to fi
|
||||
|
||||
# Toggle between SIM slots
|
||||
new_sim_slot=$((current_sim_slot % 2 + 1))
|
||||
|
||||
log_message "INFO" "Switching from SIM slot ${current_sim_slot} to SIM slot ${new_sim_slot}"
|
||||
if ! execute_at_command "AT+QUIMSLOT=${new_sim_slot}"; then
|
||||
log_message "ERROR" "Failed to switch to SIM slot ${new_sim_slot}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
sleep 10 # Allow time for SIM switch and network registration
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to check internet connectivity
|
||||
check_internet() {
|
||||
ping -c 3 ${PING_TARGET} > /dev/null 2>&1
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to perform connection recovery
|
||||
perform_connection_recovery() {
|
||||
local recovery_attempted=0
|
||||
local recovery_successful=0
|
||||
|
||||
if [ "${CONNECTION_REFRESH}" = "true" ] && [ "${retry_trigger}" -eq 1 ] && [ "${REFRESH_COUNT}" -gt 0 ]; then
|
||||
log_message "INFO" "Attempting connection refresh"
|
||||
|
||||
if ! execute_at_command "AT+COPS=2"; then
|
||||
log_message "ERROR" "Failed to detach from network"
|
||||
return 1
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
|
||||
if ! execute_at_command "AT+COPS=0"; then
|
||||
log_message "ERROR" "Failed to reattach to network"
|
||||
return 1
|
||||
fi # <-- Changed from } to fi
|
||||
|
||||
sleep 5
|
||||
|
||||
if check_internet; then
|
||||
log_message "INFO" "Connection refresh successful"
|
||||
recovery_successful=1
|
||||
return 0
|
||||
fi
|
||||
|
||||
REFRESH_COUNT=$((REFRESH_COUNT - 1))
|
||||
sed -i "s/REFRESH_COUNT=.*/REFRESH_COUNT=${REFRESH_COUNT}/" /etc/quecmanager/quecwatch/quecwatch.conf
|
||||
recovery_attempted=1
|
||||
fi
|
||||
|
||||
[ ${recovery_successful} -eq 1 ] && return 0 || return 1
|
||||
}
|
||||
|
||||
# Store initial SIM slot
|
||||
initial_sim_slot=""
|
||||
if [ "${AUTO_SIM_FAILOVER}" = "true" ]; then
|
||||
initial_sim_slot=$(get_current_sim)
|
||||
if [ $? -eq 0 ]; then
|
||||
log_message "INFO" "Auto SIM failover enabled. Initial SIM slot: ${initial_sim_slot}"
|
||||
else
|
||||
log_message "ERROR" "Failed to get initial SIM slot"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Main monitoring loop
|
||||
failure_count=0
|
||||
retry_trigger=0
|
||||
sim_failover_interval=0
|
||||
|
||||
while true; do
|
||||
if ! check_internet; then
|
||||
failure_count=$((failure_count + 1))
|
||||
log_message "INFO" "Ping failed. Failure count: ${failure_count}"
|
||||
|
||||
if [ ${failure_count} -ge ${PING_FAILURES} ]; then
|
||||
failure_count=0
|
||||
retry_trigger=$((retry_trigger + 1))
|
||||
update_retry_count ${retry_trigger}
|
||||
|
||||
log_message "INFO" "Failure threshold reached. Retry trigger: ${retry_trigger}"
|
||||
|
||||
if [ ${retry_trigger} -ge ${MAX_RETRIES} ]; then
|
||||
if [ "${AUTO_SIM_FAILOVER}" = "true" ]; then
|
||||
log_message "INFO" "Max retries exhausted. Attempting SIM failover."
|
||||
if switch_sim_card && check_internet; then
|
||||
log_message "INFO" "SIM failover successful"
|
||||
retry_trigger=0
|
||||
failure_count=0
|
||||
update_retry_count 0
|
||||
else
|
||||
log_message "ERROR" "SIM failover failed. Performing system reboot."
|
||||
reboot
|
||||
fi
|
||||
else
|
||||
log_message "INFO" "Max retries exhausted. Auto SIM failover disabled. Removing QuecWatch."
|
||||
sed -i '\|/etc/quecmanager/quecwatch/quecwatch.sh|d' /etc/rc.local
|
||||
reboot
|
||||
exit 0
|
||||
fi
|
||||
else
|
||||
if perform_connection_recovery; then
|
||||
retry_trigger=0
|
||||
failure_count=0
|
||||
update_retry_count 0
|
||||
else
|
||||
log_message "ERROR" "Recovery failed. Performing system reboot."
|
||||
reboot
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
else
|
||||
failure_count=0
|
||||
retry_trigger=0
|
||||
update_retry_count 0
|
||||
log_message "INFO" "Modem is connected to the internet"
|
||||
|
||||
if [ "${AUTO_SIM_FAILOVER}" = "true" ] && [ "${SIM_FAILOVER_SCHEDULE}" -gt 0 ]; then
|
||||
current_sim_slot=$(get_current_sim)
|
||||
|
||||
if [ -n "${initial_sim_slot}" ] && [ "${current_sim_slot}" != "${initial_sim_slot}" ]; then
|
||||
sim_failover_interval=$((sim_failover_interval + 1))
|
||||
|
||||
if [ $((sim_failover_interval * PING_INTERVAL)) -ge $((SIM_FAILOVER_SCHEDULE * 60)) ]; then
|
||||
log_message "INFO" "Scheduled check: Attempting to switch back to initial SIM ${initial_sim_slot}"
|
||||
|
||||
if execute_at_command "AT+QUIMSLOT=${initial_sim_slot}"; then
|
||||
sleep 10
|
||||
|
||||
if check_internet; then
|
||||
log_message "INFO" "Initial SIM restored successfully"
|
||||
retry_trigger=0
|
||||
failure_count=0
|
||||
update_retry_count 0
|
||||
else
|
||||
log_message "WARN" "Initial SIM still not working. Switching back to backup SIM."
|
||||
execute_at_command "AT+QUIMSLOT=${current_sim_slot}"
|
||||
sleep 10
|
||||
fi
|
||||
else
|
||||
log_message "ERROR" "Failed to switch to initial SIM"
|
||||
fi
|
||||
|
||||
sim_failover_interval=0
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
sleep ${PING_INTERVAL}
|
||||
done
|
||||
EOL
|
||||
|
||||
chmod +x "${QUECWATCH_SCRIPT}"
|
||||
|
||||
# Run the script
|
||||
"${QUECWATCH_SCRIPT}" &
|
||||
}
|
||||
|
||||
# Enable QuecWatch
|
||||
enable_quecwatch() {
|
||||
initialize_config
|
||||
generate_monitoring_script
|
||||
|
||||
if ! grep -q "${QUECWATCH_SCRIPT}" "${RCLOCAL}"; then
|
||||
[ -f "${RCLOCAL}" ] || touch "${RCLOCAL}"
|
||||
chmod +x "${RCLOCAL}"
|
||||
sed -i '$i'"${QUECWATCH_SCRIPT} &" "${RCLOCAL}"
|
||||
fi
|
||||
|
||||
# Output success JSON
|
||||
echo '{"status": "success", "message": "QuecWatch enabled", "config": "'${QUECWATCH_CONFIG}'"}'
|
||||
}
|
||||
|
||||
# Log debug information
|
||||
{
|
||||
echo "Timestamp: $(date)"
|
||||
echo "Script Path: $0"
|
||||
echo "Ping Target: $ping_target"
|
||||
echo "Ping Interval: $ping_interval"
|
||||
echo "Ping Failures: $ping_failures"
|
||||
echo "Max Retries: $max_retries"
|
||||
echo "Connection Refresh: $connection_refresh"
|
||||
echo "Auto SIM Failover: $auto_sim_failover"
|
||||
echo "SIM Failover Schedule: $sim_failover_schedule"
|
||||
} >>"$DEBUG_LOG_FILE" 2>&1
|
||||
|
||||
# Enable QuecWatch
|
||||
enable_quecwatch
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,79 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Set headers for JSON response
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Configuration file path
|
||||
CONFIG_FILE="/etc/quecmanager/quecwatch/quecwatch.conf"
|
||||
|
||||
# Check if configuration file exists
|
||||
if [ ! -f "$CONFIG_FILE" ]; then
|
||||
echo '{"status": "inactive", "message": "QuecWatch is not configured"}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Function to safely get config value
|
||||
get_config_value() {
|
||||
grep "^$1=" "$CONFIG_FILE" | cut -d'=' -f2
|
||||
}
|
||||
|
||||
# Check if QuecWatch is enabled
|
||||
enabled=$(get_config_value "ENABLED")
|
||||
if [ "$enabled" != "true" ]; then
|
||||
echo '{"status": "inactive", "message": "QuecWatch is disabled"}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Fetch configuration values
|
||||
ping_target=$(get_config_value "PING_TARGET")
|
||||
ping_interval=$(get_config_value "PING_INTERVAL")
|
||||
ping_failures=$(get_config_value "PING_FAILURES")
|
||||
max_retries=$(get_config_value "MAX_RETRIES")
|
||||
current_retries=$(get_config_value "CURRENT_RETRIES")
|
||||
connection_refresh=$(get_config_value "CONNECTION_REFRESH")
|
||||
refresh_count=$(get_config_value "REFRESH_COUNT")
|
||||
|
||||
# New configuration options
|
||||
mobile_data_reconnect=$(get_config_value "MOBILE_DATA_RECONNECT")
|
||||
auto_sim_failover=$(get_config_value "AUTO_SIM_FAILOVER")
|
||||
sim_failover_schedule=$(get_config_value "SIM_FAILOVER_SCHEDULE")
|
||||
|
||||
# Default values if not set
|
||||
mobile_data_reconnect=${mobile_data_reconnect:-false}
|
||||
auto_sim_failover=${auto_sim_failover:-false}
|
||||
sim_failover_schedule=${sim_failover_schedule:-30}
|
||||
|
||||
# Check monitoring script existence
|
||||
QUECWATCH_SCRIPT="/etc/quecmanager/quecwatch/quecwatch.sh"
|
||||
if [ ! -f "$QUECWATCH_SCRIPT" ]; then
|
||||
echo '{"status": "error", "message": "Monitoring script is missing"}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Check log file for recent activity
|
||||
LOG_FILE="/tmp/log/quecwatch/quecwatch.log"
|
||||
last_log=""
|
||||
if [ -f "$LOG_FILE" ]; then
|
||||
last_log=$(tail -n 1 "$LOG_FILE")
|
||||
fi
|
||||
|
||||
# Prepare JSON response
|
||||
cat <<EOF
|
||||
{
|
||||
"status": "active",
|
||||
"config": {
|
||||
"pingTarget": "${ping_target}",
|
||||
"pingInterval": ${ping_interval},
|
||||
"pingFailures": ${ping_failures},
|
||||
"maxRetries": ${max_retries},
|
||||
"currentRetries": ${current_retries},
|
||||
"connectionRefresh": ${connection_refresh},
|
||||
"refreshCount": ${refresh_count:-0},
|
||||
"mobileDataReconnect": ${mobile_data_reconnect},
|
||||
"autoSimFailover": ${auto_sim_failover},
|
||||
"simFailoverSchedule": ${sim_failover_schedule}
|
||||
},
|
||||
"lastActivity": "${last_log}"
|
||||
}
|
||||
EOF
|
||||
Reference in New Issue
Block a user