QuecManager non-beta
Its about time I did this!
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Cell Lock Apply Script - Called by crontab at start time
|
||||
|
||||
# Configuration
|
||||
UCI_CONFIG="quecmanager"
|
||||
LOG_DIR="/tmp/log/cell_lock"
|
||||
LOG_FILE="$LOG_DIR/cell_lock.log"
|
||||
STATUS_FILE="/tmp/cell_lock_status.json"
|
||||
QUEUE_DIR="/tmp/at_queue"
|
||||
TOKEN_FILE="$QUEUE_DIR/token"
|
||||
MAX_TOKEN_WAIT=15
|
||||
TOKEN_PRIORITY=5
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Function to log messages
|
||||
log_message() {
|
||||
local message="$1"
|
||||
local level="${2:-info}"
|
||||
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Log to file
|
||||
echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
|
||||
|
||||
# Log to system log
|
||||
logger -t cell_lock_apply -p "daemon.$level" "$message"
|
||||
}
|
||||
|
||||
# Function to update status
|
||||
update_status() {
|
||||
local status="$1"
|
||||
local message="$2"
|
||||
local active="${3:-0}"
|
||||
local locked="${4:-0}"
|
||||
|
||||
# Create JSON status
|
||||
cat > "$STATUS_FILE" <<EOF2
|
||||
{
|
||||
"status": "$status",
|
||||
"message": "$message",
|
||||
"active": $active,
|
||||
"locked": $locked,
|
||||
"timestamp": $(date +%s)
|
||||
}
|
||||
EOF2
|
||||
chmod 644 "$STATUS_FILE"
|
||||
|
||||
log_message "Status updated: $status - $message (active=$active, locked=$locked)"
|
||||
}
|
||||
|
||||
# Function to execute AT command
|
||||
execute_at_command() {
|
||||
local cmd="$1"
|
||||
local timeout="${2:-10}"
|
||||
|
||||
# Execute the command with proper timeout
|
||||
local output
|
||||
local status=1
|
||||
|
||||
output=$(sms_tool at "$cmd" -t "$timeout" 2>&1)
|
||||
status=$?
|
||||
|
||||
if [ $status -ne 0 ]; then
|
||||
log_message "AT command failed: $cmd (exit code: $status)" "error"
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "$output"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Main function to apply cell lock
|
||||
apply_cell_lock() {
|
||||
log_message "Applying cell lock at scheduled start time"
|
||||
|
||||
# Mark as active in UCI
|
||||
uci set "$UCI_CONFIG.cell_lock.active=1"
|
||||
uci commit "$UCI_CONFIG"
|
||||
|
||||
# Update status
|
||||
update_status "active" "Cell lock scheduler is active - scheduled start time reached" 1 1
|
||||
|
||||
# Get lock parameters from UCI
|
||||
local lte_lock_params=$(uci -q get "$UCI_CONFIG.cell_lock.lte_lock")
|
||||
local nr5g_lock_params=$(uci -q get "$UCI_CONFIG.cell_lock.nr5g_lock")
|
||||
|
||||
log_message "Lock parameters from UCI: LTE=$lte_lock_params, NR5G=$nr5g_lock_params"
|
||||
|
||||
# Apply locks if parameters exist
|
||||
local success=0
|
||||
|
||||
if [ -n "$lte_lock_params" ]; then
|
||||
log_message "Applying LTE lock: $lte_lock_params"
|
||||
local lte_cmd="AT+QNWLOCK=\"common/4g\",$lte_lock_params"
|
||||
execute_at_command "$lte_cmd" 10
|
||||
if [ $? -eq 0 ]; then
|
||||
log_message "LTE lock applied successfully"
|
||||
success=1
|
||||
else
|
||||
log_message "Failed to apply LTE lock" "error"
|
||||
fi
|
||||
else
|
||||
log_message "No LTE lock parameters found, checking for current lock"
|
||||
# If no parameters set, try to lock to current serving cell
|
||||
local scan_output=$(execute_at_command "AT+QENG=\"servingcell\"" 5)
|
||||
log_message "Current serving cell info: $scan_output"
|
||||
# Parse and apply if possible (simplified for this example)
|
||||
fi
|
||||
|
||||
if [ -n "$nr5g_lock_params" ]; then
|
||||
log_message "Applying NR5G lock: $nr5g_lock_params"
|
||||
local nr5g_cmd="AT+QNWLOCK=\"common/5g\",$nr5g_lock_params"
|
||||
execute_at_command "$nr5g_cmd" 10
|
||||
if [ $? -eq 0 ]; then
|
||||
log_message "NR5G lock applied successfully"
|
||||
success=1
|
||||
else
|
||||
log_message "Failed to apply NR5G lock" "error"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Apply persist settings
|
||||
local lte_persist=$(uci -q get "$UCI_CONFIG.cell_lock.lte_persist")
|
||||
local nr5g_persist=$(uci -q get "$UCI_CONFIG.cell_lock.nr5g_persist")
|
||||
|
||||
# Default to 0 if not set
|
||||
lte_persist="${lte_persist:-0}"
|
||||
nr5g_persist="${nr5g_persist:-0}"
|
||||
|
||||
local persist_cmd="AT+QNWLOCK=\"save_ctrl\",$lte_persist,$nr5g_persist"
|
||||
execute_at_command "$persist_cmd" 10
|
||||
|
||||
# Reset network to apply changes
|
||||
log_message "Resetting network connection to apply changes"
|
||||
execute_at_command "AT+COPS=2" 5
|
||||
sleep 2
|
||||
execute_at_command "AT+COPS=0" 5
|
||||
|
||||
if [ $success -eq 1 ]; then
|
||||
log_message "Cell lock applied at scheduled start time"
|
||||
update_status "active" "Cell lock applied at scheduled start time" 1 1
|
||||
else
|
||||
log_message "Failed to apply cell lock" "error"
|
||||
update_status "error" "Failed to apply cell lock" 1 0
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Execute main function
|
||||
log_message "====== STARTING SCHEDULED CELL LOCK APPLICATION ======" "notice"
|
||||
apply_cell_lock
|
||||
log_message "====== COMPLETED SCHEDULED CELL LOCK APPLICATION ======" "notice"
|
||||
@@ -0,0 +1,359 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Boot-time Cell Lock Checker - Called from init.d script at boot
|
||||
|
||||
# Configuration
|
||||
UCI_CONFIG="quecmanager"
|
||||
LOG_DIR="/tmp/log/cell_lock"
|
||||
LOG_FILE="$LOG_DIR/cell_lock.log"
|
||||
STATUS_FILE="/tmp/cell_lock_status.json"
|
||||
QUEUE_DIR="/tmp/at_queue"
|
||||
TOKEN_FILE="$QUEUE_DIR/token"
|
||||
MAX_TOKEN_WAIT=15
|
||||
TOKEN_PRIORITY=5
|
||||
ROTATE_SIZE=500 # KB before log rotation
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Enhanced log_message function
|
||||
log_message() {
|
||||
local message="$1"
|
||||
local level="${2:-info}"
|
||||
local component="boot_check"
|
||||
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
local pid=$$
|
||||
|
||||
# Check if log file is too large (>500KB) and rotate if needed
|
||||
if [ -f "$LOG_FILE" ] && [ $(du -k "$LOG_FILE" | cut -f1) -gt $ROTATE_SIZE ]; then
|
||||
mv "$LOG_FILE" "$LOG_FILE.old"
|
||||
touch "$LOG_FILE"
|
||||
chmod 644 "$LOG_FILE"
|
||||
fi
|
||||
|
||||
# Format: [timestamp] [level] [component] [pid] message
|
||||
echo "[$timestamp] [$level] [$component] [$pid] $message" >>"$LOG_FILE"
|
||||
|
||||
# Also log to system log with appropriate priority
|
||||
case "$level" in
|
||||
debug) logger -t "cell_lock_$component" -p daemon.debug "$message" ;;
|
||||
info) logger -t "cell_lock_$component" -p daemon.info "$message" ;;
|
||||
notice) logger -t "cell_lock_$component" -p daemon.notice "$message" ;;
|
||||
warn) logger -t "cell_lock_$component" -p daemon.warning "$message" ;;
|
||||
error) logger -t "cell_lock_$component" -p daemon.err "$message" ;;
|
||||
crit) logger -t "cell_lock_$component" -p daemon.crit "$message" ;;
|
||||
*) logger -t "cell_lock_$component" -p daemon.info "$message" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Function to update status
|
||||
update_status() {
|
||||
local status="$1"
|
||||
local message="$2"
|
||||
local active="${3:-0}"
|
||||
local locked="${4:-0}"
|
||||
|
||||
# Create JSON status
|
||||
cat >"$STATUS_FILE" <<EOF2
|
||||
{
|
||||
"status": "$status",
|
||||
"message": "$message",
|
||||
"active": $active,
|
||||
"locked": $locked,
|
||||
"timestamp": $(date +%s)
|
||||
}
|
||||
EOF2
|
||||
chmod 644 "$STATUS_FILE"
|
||||
|
||||
log_message "Status updated: $status - $message (active=$active, locked=$locked)" "debug"
|
||||
}
|
||||
|
||||
# Function to acquire token for AT commands
|
||||
acquire_token() {
|
||||
local requestor_id="CELLLOCK_$(date +%s)_$$"
|
||||
local priority="$TOKEN_PRIORITY"
|
||||
local max_attempts=$MAX_TOKEN_WAIT
|
||||
local attempt=0
|
||||
|
||||
log_message "Attempting to acquire token with priority $priority" "debug"
|
||||
|
||||
while [ $attempt -lt $max_attempts ]; do
|
||||
# Check if token file exists
|
||||
if [ -f "$TOKEN_FILE" ]; then
|
||||
local current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
|
||||
local current_priority=$(cat "$TOKEN_FILE" | jsonfilter -e '@.priority' 2>/dev/null)
|
||||
local timestamp=$(cat "$TOKEN_FILE" | jsonfilter -e '@.timestamp' 2>/dev/null)
|
||||
local current_time=$(date +%s)
|
||||
|
||||
# Check for expired token (> 30 seconds old)
|
||||
if [ $((current_time - timestamp)) -gt 30 ] || [ -z "$current_holder" ]; then
|
||||
# Remove expired token
|
||||
log_message "Found expired token from $current_holder, removing" "debug"
|
||||
rm -f "$TOKEN_FILE" 2>/dev/null
|
||||
elif [ $priority -lt $current_priority ]; then
|
||||
# Preempt lower priority token
|
||||
log_message "Preempting token from $current_holder (priority: $current_priority)" "debug"
|
||||
rm -f "$TOKEN_FILE" 2>/dev/null
|
||||
else
|
||||
log_message "Token held by $current_holder with priority $current_priority, retrying..." "debug"
|
||||
sleep 0.5
|
||||
attempt=$((attempt + 1))
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
|
||||
# Try to create token file
|
||||
echo "{\"id\":\"$requestor_id\",\"priority\":$priority,\"timestamp\":$(date +%s)}" >"$TOKEN_FILE" 2>/dev/null
|
||||
chmod 644 "$TOKEN_FILE" 2>/dev/null
|
||||
|
||||
# Verify we got the token
|
||||
local holder=$(cat "$TOKEN_FILE" 2>/dev/null | jsonfilter -e '@.id' 2>/dev/null)
|
||||
if [ "$holder" = "$requestor_id" ]; then
|
||||
log_message "Successfully acquired token with ID $requestor_id" "debug"
|
||||
echo "$requestor_id"
|
||||
return 0
|
||||
fi
|
||||
|
||||
sleep 0.5
|
||||
attempt=$((attempt + 1))
|
||||
done
|
||||
|
||||
log_message "Failed to acquire token after $max_attempts attempts" "error"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Function to release token
|
||||
release_token() {
|
||||
local requestor_id="$1"
|
||||
|
||||
if [ -f "$TOKEN_FILE" ]; then
|
||||
local current_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id' 2>/dev/null)
|
||||
if [ "$current_holder" = "$requestor_id" ]; then
|
||||
rm -f "$TOKEN_FILE" 2>/dev/null
|
||||
log_message "Released token $requestor_id" "debug"
|
||||
return 0
|
||||
fi
|
||||
log_message "Token held by $current_holder, not by us ($requestor_id)" "warn"
|
||||
else
|
||||
log_message "Token file doesn't exist, nothing to release" "debug"
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
# Function to execute AT command with token
|
||||
execute_at_command() {
|
||||
local cmd="$1"
|
||||
local timeout="${2:-10}"
|
||||
local token_id="$3"
|
||||
|
||||
if [ -z "$token_id" ]; then
|
||||
log_message "No valid token provided for command: $cmd" "error"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_message "Executing AT command: $cmd (timeout: ${timeout}s)" "debug"
|
||||
|
||||
# Execute the command with proper timeout
|
||||
local output
|
||||
local status=1
|
||||
|
||||
output=$(sms_tool at "$cmd" -t "$timeout" 2>&1)
|
||||
status=$?
|
||||
|
||||
if [ $status -ne 0 ]; then
|
||||
log_message "AT command failed: $cmd (exit code: $status)" "error"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_message "AT command executed successfully: $cmd" "debug"
|
||||
echo "$output"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to convert HH:MM to minutes with better error handling
|
||||
time_to_minutes() {
|
||||
local time="$1"
|
||||
|
||||
# Check if time is empty or malformed
|
||||
if [ -z "$time" ] || ! echo "$time" | grep -q '^[0-9]\{1,2\}:[0-9]\{2\}$'; then
|
||||
log_message "Invalid time format: '$time'" "error"
|
||||
echo "0"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local hours=$(echo "$time" | cut -d':' -f1)
|
||||
local minutes=$(echo "$time" | cut -d':' -f2)
|
||||
|
||||
# Remove leading zeros which can cause issues in arithmetic
|
||||
hours=$(echo "$hours" | sed 's/^0*//')
|
||||
minutes=$(echo "$minutes" | sed 's/^0*//')
|
||||
|
||||
# Default to 0 if empty after removing zeros
|
||||
[ -z "$hours" ] && hours=0
|
||||
[ -z "$minutes" ] && minutes=0
|
||||
|
||||
echo $((hours * 60 + minutes))
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to check if current time is within scheduled window
|
||||
is_time_in_range() {
|
||||
local current_minutes="$1"
|
||||
local start_minutes="$2"
|
||||
local end_minutes="$3"
|
||||
|
||||
# Make sure all values are numeric
|
||||
if ! [[ "$current_minutes" =~ ^[0-9]+$ ]] ||
|
||||
! [[ "$start_minutes" =~ ^[0-9]+$ ]] ||
|
||||
! [[ "$end_minutes" =~ ^[0-9]+$ ]]; then
|
||||
log_message "Non-numeric values in time comparison: current=$current_minutes, start=$start_minutes, end=$end_minutes" "error"
|
||||
return 1 # Not in range
|
||||
fi
|
||||
|
||||
# Handle case where end time is on the next day
|
||||
if [ "$end_minutes" -lt "$start_minutes" ]; then
|
||||
if [ "$current_minutes" -ge "$start_minutes" ] || [ "$current_minutes" -lt "$end_minutes" ]; then
|
||||
return 0 # In range
|
||||
fi
|
||||
else
|
||||
if [ "$current_minutes" -ge "$start_minutes" ] && [ "$current_minutes" -lt "$end_minutes" ]; then
|
||||
return 0 # In range
|
||||
fi
|
||||
fi
|
||||
|
||||
return 1 # Not in range
|
||||
}
|
||||
|
||||
# Main function to check at boot time
|
||||
boot_check() {
|
||||
log_message "Performing boot-time cell lock check" "info"
|
||||
|
||||
# Check if scheduling is enabled
|
||||
local enabled=$(uci -q get "$UCI_CONFIG.cell_lock.enabled")
|
||||
if [ "$enabled" != "1" ]; then
|
||||
log_message "Cell lock scheduling is disabled" "info"
|
||||
update_status "disabled" "Cell lock scheduling is disabled" 0 0
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Get schedule from UCI
|
||||
local start_time=$(uci -q get "$UCI_CONFIG.cell_lock.start_time")
|
||||
local end_time=$(uci -q get "$UCI_CONFIG.cell_lock.end_time")
|
||||
|
||||
if [ -z "$start_time" ] || [ -z "$end_time" ]; then
|
||||
log_message "Missing start or end time in configuration" "error"
|
||||
update_status "error" "Missing schedule configuration" 0 0
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Get current time
|
||||
local current_time=$(date "+%H:%M")
|
||||
|
||||
log_message "Current time: $current_time, Start: $start_time, End: $end_time" "info"
|
||||
|
||||
# Convert times to minutes for comparison
|
||||
local current_minutes=$(time_to_minutes "$current_time")
|
||||
local start_minutes=$(time_to_minutes "$start_time")
|
||||
local end_minutes=$(time_to_minutes "$end_time")
|
||||
|
||||
# Get token for AT commands
|
||||
local token_id=$(acquire_token)
|
||||
if [ -z "$token_id" ]; then
|
||||
log_message "Failed to acquire token for boot check" "error"
|
||||
update_status "error" "Failed to acquire token for boot check" 0 0
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Check if current time is in the scheduled range
|
||||
if is_time_in_range "$current_minutes" "$start_minutes" "$end_minutes"; then
|
||||
log_message "Current time IS within scheduled window" "info"
|
||||
|
||||
# Get lock parameters from UCI
|
||||
local lte_lock_params=$(uci -q get "$UCI_CONFIG.cell_lock.lte_lock")
|
||||
local nr5g_lock_params=$(uci -q get "$UCI_CONFIG.cell_lock.nr5g_lock")
|
||||
|
||||
# Apply locks if parameters exist
|
||||
local success=0
|
||||
|
||||
if [ -n "$lte_lock_params" ]; then
|
||||
log_message "Applying LTE lock at boot: $lte_lock_params" "info"
|
||||
local lte_cmd="AT+QNWLOCK=\"common/4g\",$lte_lock_params"
|
||||
execute_at_command "$lte_cmd" 10 "$token_id"
|
||||
if [ $? -eq 0 ]; then
|
||||
log_message "LTE lock applied successfully at boot" "info"
|
||||
success=1
|
||||
else
|
||||
log_message "Failed to apply LTE lock at boot" "error"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -n "$nr5g_lock_params" ]; then
|
||||
log_message "Applying NR5G lock at boot: $nr5g_lock_params" "info"
|
||||
local nr5g_cmd="AT+QNWLOCK=\"common/5g\",$nr5g_lock_params"
|
||||
execute_at_command "$nr5g_cmd" 10 "$token_id"
|
||||
if [ $? -eq 0 ]; then
|
||||
log_message "NR5G lock applied successfully at boot" "info"
|
||||
success=1
|
||||
else
|
||||
log_message "Failed to apply NR5G lock at boot" "error"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Apply persist settings
|
||||
local lte_persist=$(uci -q get "$UCI_CONFIG.cell_lock.lte_persist")
|
||||
local nr5g_persist=$(uci -q get "$UCI_CONFIG.cell_lock.nr5g_persist")
|
||||
|
||||
# Default to 0 if not set
|
||||
lte_persist="${lte_persist:-0}"
|
||||
nr5g_persist="${nr5g_persist:-0}"
|
||||
|
||||
log_message "Setting persistence at boot: LTE=$lte_persist, NR5G=$nr5g_persist" "info"
|
||||
local persist_cmd="AT+QNWLOCK=\"save_ctrl\",$lte_persist,$nr5g_persist"
|
||||
execute_at_command "$persist_cmd" 10 "$token_id"
|
||||
|
||||
# Reset network to apply changes
|
||||
log_message "Resetting network connection to apply changes" "info"
|
||||
execute_at_command "AT+COPS=2" 5 "$token_id"
|
||||
sleep 2
|
||||
execute_at_command "AT+COPS=0" 5 "$token_id"
|
||||
|
||||
# Mark as active
|
||||
uci set "$UCI_CONFIG.cell_lock.active=1"
|
||||
uci commit "$UCI_CONFIG"
|
||||
update_status "active" "Cell lock scheduler is active - applied at boot" 1 1
|
||||
else
|
||||
log_message "Current time is NOT within scheduled window" "info"
|
||||
|
||||
# Remove any existing locks
|
||||
log_message "Removing LTE lock at boot" "info"
|
||||
execute_at_command 'AT+QNWLOCK="common/4g",0' 10 "$token_id"
|
||||
|
||||
log_message "Removing NR5G lock at boot" "info"
|
||||
execute_at_command 'AT+QNWLOCK="common/5g",0' 10 "$token_id"
|
||||
|
||||
# Disable persistence
|
||||
log_message "Disabling lock persistence at boot" "info"
|
||||
execute_at_command 'AT+QNWLOCK="save_ctrl",0,0' 10 "$token_id"
|
||||
|
||||
# Reset network to apply changes
|
||||
log_message "Resetting network connection to apply changes" "info"
|
||||
execute_at_command "AT+COPS=2" 5 "$token_id"
|
||||
sleep 2
|
||||
execute_at_command "AT+COPS=0" 5 "$token_id"
|
||||
|
||||
# Mark as inactive
|
||||
uci set "$UCI_CONFIG.cell_lock.active=0"
|
||||
uci commit "$UCI_CONFIG"
|
||||
update_status "inactive" "Cell lock scheduler is inactive - outside scheduled hours" 0 0
|
||||
fi
|
||||
|
||||
# Release token
|
||||
release_token "$token_id"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Execute main function
|
||||
log_message "====== STARTING BOOT-TIME CELL LOCK CHECK ======" "notice"
|
||||
boot_check
|
||||
log_message "====== COMPLETED BOOT-TIME CELL LOCK CHECK ======" "notice"
|
||||
@@ -0,0 +1,113 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Set headers for JSON response
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Load UCI functions
|
||||
. /lib/functions.sh
|
||||
|
||||
# Function to safely get UCI value with default
|
||||
get_uci_value() {
|
||||
local value
|
||||
config_get value cell_lock "$1" "$2"
|
||||
echo "${value:-$2}"
|
||||
}
|
||||
|
||||
# Function to check if daemon is running
|
||||
check_service_status() {
|
||||
if [ -f "/var/run/cell_lock_scheduler.pid" ]; then
|
||||
pid=$(cat /var/run/cell_lock_scheduler.pid 2>/dev/null)
|
||||
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
||||
echo "running"
|
||||
return
|
||||
fi
|
||||
fi
|
||||
echo "stopped"
|
||||
}
|
||||
|
||||
# Function to get current status with proper JSON handling
|
||||
get_current_status() {
|
||||
local STATUS_FILE="/tmp/cell_lock_status.json"
|
||||
local status="unknown"
|
||||
local message="Status not available"
|
||||
local active="0"
|
||||
local locked="0"
|
||||
local timestamp=$(date +%s)
|
||||
|
||||
if [ -f "$STATUS_FILE" ]; then
|
||||
# Try to extract values from status file
|
||||
if grep -q "status" "$STATUS_FILE"; then
|
||||
status=$(cat "$STATUS_FILE" | jsonfilter -e '@.status' 2>/dev/null)
|
||||
# Extract message and remove any surrounding quotes
|
||||
message=$(cat "$STATUS_FILE" | jsonfilter -e '@.message' 2>/dev/null | sed 's/^"//;s/"$//')
|
||||
active=$(cat "$STATUS_FILE" | jsonfilter -e '@.active' 2>/dev/null)
|
||||
locked=$(cat "$STATUS_FILE" | jsonfilter -e '@.locked' 2>/dev/null)
|
||||
timestamp=$(cat "$STATUS_FILE" | jsonfilter -e '@.timestamp' 2>/dev/null)
|
||||
fi
|
||||
fi
|
||||
|
||||
# Escape quotes and special characters in message
|
||||
message=$(echo "$message" | sed 's/\\/\\\\/g; s/"/\\"/g')
|
||||
|
||||
# Return the status as a JSON object with properly quoted message
|
||||
echo "{\"status\":\"$status\",\"message\":\"$message\",\"active\":$active,\"locked\":$locked,\"timestamp\":$timestamp}"
|
||||
}
|
||||
|
||||
# Load configuration
|
||||
config_load quecmanager
|
||||
|
||||
# Check if cell lock section exists
|
||||
if ! uci -q get quecmanager.cell_lock >/dev/null; then
|
||||
echo '{"status":"inactive","message":"Cell lock is not configured","enabled":false,"startTime":"","endTime":"","active":false,"locked":false}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Get service status
|
||||
service_status=$(check_service_status)
|
||||
|
||||
# Get current status
|
||||
current_status=$(get_current_status)
|
||||
|
||||
# Get configuration values
|
||||
enabled=$(get_uci_value "enabled" "0")
|
||||
start_time=$(get_uci_value "start_time" "")
|
||||
end_time=$(get_uci_value "end_time" "")
|
||||
active=$(get_uci_value "active" "0")
|
||||
lte_params=$(get_uci_value "lte_params" "")
|
||||
nr5g_params=$(get_uci_value "nr5g_params" "")
|
||||
lte_persist=$(get_uci_value "lte_persist" "0")
|
||||
nr5g_persist=$(get_uci_value "nr5g_persist" "0")
|
||||
|
||||
# Convert numeric values to boolean for JSON
|
||||
enabled_bool="false"
|
||||
active_bool="false"
|
||||
locked_bool="false"
|
||||
|
||||
[ "$enabled" = "1" ] && enabled_bool="true"
|
||||
[ "$active" = "1" ] && active_bool="true"
|
||||
|
||||
# Get locked status from current_status
|
||||
locked=$(echo "$current_status" | jsonfilter -e '@.locked' 2>/dev/null)
|
||||
[ "$locked" = "1" ] && locked_bool="true"
|
||||
|
||||
# Extract the message properly from current status
|
||||
message_value=$(echo "$current_status" | jsonfilter -e '@.message' 2>/dev/null | sed 's/^"//;s/"$//')
|
||||
|
||||
# Prepare JSON response in format expected by the component
|
||||
cat <<EOF
|
||||
{
|
||||
"enabled": $enabled_bool,
|
||||
"start_time": "$start_time",
|
||||
"end_time": "$end_time",
|
||||
"active": $active_bool,
|
||||
"status": "$(echo "$current_status" | jsonfilter -e '@.status')",
|
||||
"message": "$message_value",
|
||||
"locked": $locked_bool,
|
||||
"serviceStatus": "$service_status",
|
||||
"lteParams": "$lte_params",
|
||||
"nr5gParams": "$nr5g_params",
|
||||
"ltePersist": "$lte_persist",
|
||||
"nr5gPersist": "$nr5g_persist"
|
||||
}
|
||||
EOF
|
||||
@@ -0,0 +1,303 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Set content type to JSON
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Configuration
|
||||
UCI_CONFIG="quecmanager"
|
||||
STATUS_FILE="/tmp/cell_lock_status.json"
|
||||
LOG_DIR="/tmp/log/cell_lock"
|
||||
LOG_FILE="$LOG_DIR/cell_lock.log"
|
||||
SCRIPTS_DIR="/www/cgi-bin/quecmanager/cell-locking"
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Function to log messages
|
||||
log_message() {
|
||||
local message="$1"
|
||||
local level="${2:-info}"
|
||||
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Log to file
|
||||
echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
|
||||
|
||||
# Log to system log
|
||||
logger -t cell_lock_handler -p "daemon.$level" "$message"
|
||||
}
|
||||
|
||||
# Function to validate time format (HH:MM)
|
||||
validate_time_format() {
|
||||
local time="$1"
|
||||
local name="$2"
|
||||
|
||||
if ! echo "$time" | grep -q '^[0-2][0-9]:[0-5][0-9]$'; then
|
||||
echo "{\"status\":\"error\",\"message\":\"$name must be in format HH:MM (24-hour)\"}"
|
||||
log_message "$name has invalid format: $time" "error"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Further validate hour (00-23)
|
||||
local hour=$(echo "$time" | cut -d':' -f1)
|
||||
if [ "$hour" -gt 23 ]; then
|
||||
echo "{\"status\":\"error\",\"message\":\"Hour in $name must be between 00-23\"}"
|
||||
log_message "$name has invalid hour: $hour" "error"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to store cell lock parameters from current settings
|
||||
store_lock_params() {
|
||||
# Get the current LTE lock status
|
||||
local lte_cmd="AT+QNWLOCK=\"common/4g\""
|
||||
local lte_output=$(sms_tool at "$lte_cmd" -t 5 2>&1)
|
||||
if [ $? -eq 0 ]; then
|
||||
# Extract parameters if locked
|
||||
if ! echo "$lte_output" | grep -q '"common/4g",0'; then
|
||||
local lte_params=$(echo "$lte_output" | grep -o '"common/4g",[^[:space:]]*' | cut -d',' -f2-)
|
||||
uci set "$UCI_CONFIG.cell_lock.lte_lock=$lte_params"
|
||||
log_message "Stored LTE lock params: $lte_params" "info"
|
||||
else
|
||||
# If not locked, clear the parameters
|
||||
uci set "$UCI_CONFIG.cell_lock.lte_lock="
|
||||
log_message "No active LTE lock, cleared parameters" "info"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Get the current NR5G lock status
|
||||
local nr5g_cmd="AT+QNWLOCK=\"common/5g\""
|
||||
local nr5g_output=$(sms_tool at "$nr5g_cmd" -t 5 2>&1)
|
||||
if [ $? -eq 0 ]; then
|
||||
# Extract parameters if locked
|
||||
if ! echo "$nr5g_output" | grep -q '"common/5g",0'; then
|
||||
local nr5g_params=$(echo "$nr5g_output" | grep -o '"common/5g",[^[:space:]]*' | cut -d',' -f2-)
|
||||
uci set "$UCI_CONFIG.cell_lock.nr5g_lock=$nr5g_params"
|
||||
log_message "Stored NR5G lock params: $nr5g_params" "info"
|
||||
else
|
||||
# If not locked, clear the parameters
|
||||
uci set "$UCI_CONFIG.cell_lock.nr5g_lock="
|
||||
log_message "No active NR5G lock, cleared parameters" "info"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Get the persist settings
|
||||
local persist_cmd="AT+QNWLOCK=\"save_ctrl\""
|
||||
local persist_output=$(sms_tool at "$persist_cmd" -t 5 2>&1)
|
||||
if [ $? -eq 0 ]; then
|
||||
# Extract parameters
|
||||
local persist_params=$(echo "$persist_output" | grep -o '"save_ctrl",[^[:space:]]*' | cut -d',' -f2-)
|
||||
local lte_persist=$(echo "$persist_params" | cut -d',' -f1)
|
||||
local nr5g_persist=$(echo "$persist_params" | cut -d',' -f2)
|
||||
|
||||
# Save to UCI
|
||||
uci set "$UCI_CONFIG.cell_lock.lte_persist=$lte_persist"
|
||||
uci set "$UCI_CONFIG.cell_lock.nr5g_persist=$nr5g_persist"
|
||||
log_message "Stored persist settings: LTE=$lte_persist, NR5G=$nr5g_persist" "info"
|
||||
fi
|
||||
|
||||
# Commit changes
|
||||
uci commit "$UCI_CONFIG"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to update crontab
|
||||
update_crontab() {
|
||||
local enabled=$(uci -q get "$UCI_CONFIG.cell_lock.enabled")
|
||||
local start_time=$(uci -q get "$UCI_CONFIG.cell_lock.start_time")
|
||||
local end_time=$(uci -q get "$UCI_CONFIG.cell_lock.end_time")
|
||||
|
||||
if [ -z "$start_time" ] || [ -z "$end_time" ]; then
|
||||
log_message "Missing start or end time" "error"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local start_hour=$(echo "$start_time" | cut -d':' -f1 | sed 's/^0//')
|
||||
local start_min=$(echo "$start_time" | cut -d':' -f2 | sed 's/^0//')
|
||||
local end_hour=$(echo "$end_time" | cut -d':' -f1 | sed 's/^0//')
|
||||
local end_min=$(echo "$end_time" | cut -d':' -f2 | sed 's/^0//')
|
||||
|
||||
# Create new crontab excluding our entries
|
||||
local new_crontab=$(crontab -l | grep -v "apply_lock.sh\|remove_lock.sh")
|
||||
|
||||
if [ "$enabled" = "1" ]; then
|
||||
# Add our entries
|
||||
new_crontab="$new_crontab
|
||||
$start_min $start_hour * * * $SCRIPTS_DIR/apply_lock.sh
|
||||
$end_min $end_hour * * * $SCRIPTS_DIR/remove_lock.sh"
|
||||
fi
|
||||
|
||||
# Apply new crontab
|
||||
echo "$new_crontab" | crontab -
|
||||
log_message "Updated crontab with start=$start_time, end=$end_time" "info"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Handle GET requests for status
|
||||
if [ "$REQUEST_METHOD" = "GET" ]; then
|
||||
log_message "Handling GET request for cell lock status" "debug"
|
||||
|
||||
# Load UCI configuration
|
||||
if [ -f "/etc/config/quecmanager" ]; then
|
||||
ENABLED=$(uci -q get "$UCI_CONFIG.cell_lock.enabled" || echo "0")
|
||||
START_TIME=$(uci -q get "$UCI_CONFIG.cell_lock.start_time" || echo "")
|
||||
END_TIME=$(uci -q get "$UCI_CONFIG.cell_lock.end_time" || echo "")
|
||||
ACTIVE=$(uci -q get "$UCI_CONFIG.cell_lock.active" || echo "0")
|
||||
|
||||
# Convert to JSON boolean format
|
||||
[ "$ENABLED" = "1" ] && ENABLED="true" || ENABLED="false"
|
||||
[ "$ACTIVE" = "1" ] && ACTIVE="true" || ACTIVE="false"
|
||||
|
||||
# Get current status from status file
|
||||
STATUS="disabled"
|
||||
MESSAGE="\"Scheduler is disabled\""
|
||||
|
||||
if [ -f "$STATUS_FILE" ]; then
|
||||
STATUS=$(cat "$STATUS_FILE" | jsonfilter -e '@.status' 2>/dev/null)
|
||||
MESSAGE=$(cat "$STATUS_FILE" | jsonfilter -e '@.message' 2>/dev/null)
|
||||
if [ -n "$MESSAGE" ]; then
|
||||
MESSAGE="\"$MESSAGE\""
|
||||
else
|
||||
MESSAGE="\"Status not available\""
|
||||
fi
|
||||
fi
|
||||
|
||||
# Output JSON response
|
||||
echo "{\"enabled\":$ENABLED,\"start_time\":\"$START_TIME\",\"end_time\":\"$END_TIME\",\"active\":$ACTIVE,\"status\":\"$STATUS\",\"message\":$MESSAGE}"
|
||||
log_message "Returned status response" "debug"
|
||||
else
|
||||
echo "{\"enabled\":false,\"start_time\":\"\",\"end_time\":\"\",\"active\":false,\"status\":\"unknown\",\"message\":\"Configuration not found\"}"
|
||||
log_message "No configuration found" "warn"
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Handle POST requests for enabling/disabling scheduling
|
||||
if [ "$REQUEST_METHOD" = "POST" ]; then
|
||||
log_message "Handling POST request for cell lock scheduling" "debug"
|
||||
|
||||
# Read POST data
|
||||
CONTENT_LENGTH=${CONTENT_LENGTH:-0}
|
||||
if [ $CONTENT_LENGTH -gt 0 ]; then
|
||||
POST_DATA=$(dd bs=1 count=$CONTENT_LENGTH 2>/dev/null)
|
||||
else
|
||||
POST_DATA=""
|
||||
fi
|
||||
|
||||
# Try to parse JSON data
|
||||
if [ -n "$POST_DATA" ] && command -v jsonfilter >/dev/null 2>&1; then
|
||||
log_message "Parsing JSON data: $POST_DATA" "debug"
|
||||
|
||||
# Extract values from JSON
|
||||
ENABLED=$(echo "$POST_DATA" | jsonfilter -e '@.enabled' 2>/dev/null)
|
||||
if [ -z "$ENABLED" ]; then
|
||||
ENABLED=$(echo "$POST_DATA" | jsonfilter -e '@.enable' 2>/dev/null)
|
||||
fi
|
||||
|
||||
START_TIME=$(echo "$POST_DATA" | jsonfilter -e '@.startTime' 2>/dev/null)
|
||||
if [ -z "$START_TIME" ]; then
|
||||
START_TIME=$(echo "$POST_DATA" | jsonfilter -e '@.start_time' 2>/dev/null)
|
||||
fi
|
||||
|
||||
END_TIME=$(echo "$POST_DATA" | jsonfilter -e '@.endTime' 2>/dev/null)
|
||||
if [ -z "$END_TIME" ]; then
|
||||
END_TIME=$(echo "$POST_DATA" | jsonfilter -e '@.end_time' 2>/dev/null)
|
||||
fi
|
||||
|
||||
# Handle enable/disable logic
|
||||
if [ "$ENABLED" = "true" ] || [ "$ENABLED" = "1" ]; then
|
||||
# Validate times for enable request
|
||||
if [ -z "$START_TIME" ] || [ -z "$END_TIME" ]; then
|
||||
echo "{\"status\":\"error\",\"message\":\"Start time and end time are required\"}"
|
||||
log_message "Missing start or end time" "error"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Validate time formats
|
||||
validate_time_format "$START_TIME" "Start time" || exit 1
|
||||
validate_time_format "$END_TIME" "End time" || exit 1
|
||||
|
||||
# Store current cell lock parameters before enabling scheduler
|
||||
store_lock_params
|
||||
|
||||
# Update configuration
|
||||
log_message "Enabling scheduling with start=$START_TIME, end=$END_TIME" "info"
|
||||
uci -q set "$UCI_CONFIG.cell_lock=scheduler"
|
||||
uci set "$UCI_CONFIG.cell_lock.enabled=1"
|
||||
uci set "$UCI_CONFIG.cell_lock.start_time=$START_TIME"
|
||||
uci set "$UCI_CONFIG.cell_lock.end_time=$END_TIME"
|
||||
uci commit "$UCI_CONFIG"
|
||||
|
||||
# Update crontab
|
||||
update_crontab
|
||||
|
||||
# Check if currently in window and apply lock if needed
|
||||
CURRENT_TIME=$(date "+%H:%M")
|
||||
CURRENT_HOUR=$(echo "$CURRENT_TIME" | cut -d':' -f1 | sed 's/^0//')
|
||||
CURRENT_MIN=$(echo "$CURRENT_TIME" | cut -d':' -f2 | sed 's/^0//')
|
||||
START_HOUR=$(echo "$START_TIME" | cut -d':' -f1 | sed 's/^0//')
|
||||
START_MIN=$(echo "$START_TIME" | cut -d':' -f2 | sed 's/^0//')
|
||||
END_HOUR=$(echo "$END_TIME" | cut -d':' -f1 | sed 's/^0//')
|
||||
END_MIN=$(echo "$END_TIME" | cut -d':' -f2 | sed 's/^0//')
|
||||
|
||||
# Convert to minutes for comparison
|
||||
CURRENT_MINUTES=$((CURRENT_HOUR * 60 + CURRENT_MIN))
|
||||
START_MINUTES=$((START_HOUR * 60 + START_MIN))
|
||||
END_MINUTES=$((END_HOUR * 60 + END_MIN))
|
||||
|
||||
# Check if current time is in range
|
||||
IN_RANGE=0
|
||||
if [ $END_MINUTES -lt $START_MINUTES ]; then
|
||||
# Overnight schedule
|
||||
if [ $CURRENT_MINUTES -ge $START_MINUTES ] || [ $CURRENT_MINUTES -lt $END_MINUTES ]; then
|
||||
IN_RANGE=1
|
||||
fi
|
||||
else
|
||||
# Same day schedule
|
||||
if [ $CURRENT_MINUTES -ge $START_MINUTES ] && [ $CURRENT_MINUTES -lt $END_MINUTES ]; then
|
||||
IN_RANGE=1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Apply lock if in range
|
||||
if [ $IN_RANGE -eq 1 ]; then
|
||||
log_message "Current time is within scheduled window, applying lock now" "info"
|
||||
"$SCRIPTS_DIR/apply_lock.sh" &
|
||||
else
|
||||
log_message "Current time is outside scheduled window, will apply at scheduled time" "info"
|
||||
fi
|
||||
|
||||
echo "{\"status\":\"success\",\"message\":\"Scheduling enabled\",\"startTime\":\"$START_TIME\",\"endTime\":\"$END_TIME\"}"
|
||||
log_message "Successfully enabled scheduling" "info"
|
||||
else
|
||||
# Disable scheduling
|
||||
log_message "Disabling scheduling" "info"
|
||||
uci -q set "$UCI_CONFIG.cell_lock=scheduler"
|
||||
uci set "$UCI_CONFIG.cell_lock.enabled=0"
|
||||
uci set "$UCI_CONFIG.cell_lock.active=0"
|
||||
uci commit "$UCI_CONFIG"
|
||||
|
||||
# Update crontab (removes entries)
|
||||
update_crontab
|
||||
|
||||
# Remove any active locks
|
||||
"$SCRIPTS_DIR/remove_lock.sh" &
|
||||
|
||||
echo "{\"status\":\"success\",\"message\":\"Scheduling disabled\"}"
|
||||
log_message "Successfully disabled scheduling" "info"
|
||||
fi
|
||||
else
|
||||
log_message "Failed to parse JSON data or no JSON data received" "error"
|
||||
echo "{\"status\":\"error\",\"message\":\"Invalid request or missing JSON data\"}"
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# If no valid method was handled
|
||||
echo "{\"status\":\"error\",\"message\":\"Invalid request method\"}"
|
||||
log_message "Invalid request method: $REQUEST_METHOD" "error"
|
||||
exit 1
|
||||
@@ -0,0 +1,198 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Cell Lock Remove Script - Called by crontab at end time
|
||||
|
||||
# Configuration
|
||||
UCI_CONFIG="quecmanager"
|
||||
LOG_DIR="/tmp/log/cell_lock"
|
||||
LOG_FILE="$LOG_DIR/cell_lock.log"
|
||||
STATUS_FILE="/tmp/cell_lock_status.json"
|
||||
QUEUE_DIR="/tmp/at_queue"
|
||||
TOKEN_FILE="$QUEUE_DIR/token"
|
||||
MAX_TOKEN_WAIT=15
|
||||
TOKEN_PRIORITY=5
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Function to log messages
|
||||
log_message() {
|
||||
local message="$1"
|
||||
local level="${2:-info}"
|
||||
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Log to file
|
||||
echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
|
||||
|
||||
# Log to system log
|
||||
logger -t cell_lock_remove -p "daemon.$level" "$message"
|
||||
}
|
||||
|
||||
# Function to update status
|
||||
update_status() {
|
||||
local status="$1"
|
||||
local message="$2"
|
||||
local active="${3:-0}"
|
||||
local locked="${4:-0}"
|
||||
|
||||
# Create JSON status
|
||||
cat > "$STATUS_FILE" <<EOF2
|
||||
{
|
||||
"status": "$status",
|
||||
"message": "$message",
|
||||
"active": $active,
|
||||
"locked": $locked,
|
||||
"timestamp": $(date +%s)
|
||||
}
|
||||
EOF2
|
||||
chmod 644 "$STATUS_FILE"
|
||||
|
||||
log_message "Status updated: $status - $message (active=$active, locked=$locked)"
|
||||
}
|
||||
|
||||
# Function to execute AT command
|
||||
execute_at_command() {
|
||||
local cmd="$1"
|
||||
local timeout="${2:-10}"
|
||||
|
||||
# Execute the command with proper timeout
|
||||
local output
|
||||
local status=1
|
||||
|
||||
output=$(sms_tool at "$cmd" -t "$timeout" 2>&1)
|
||||
status=$?
|
||||
|
||||
if [ $status -ne 0 ]; then
|
||||
log_message "AT command failed: $cmd (exit code: $status)" "error"
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "$output"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Main function to remove cell lock
|
||||
remove_cell_lock() {
|
||||
log_message "Removing cell lock at scheduled end time"
|
||||
|
||||
# Mark as inactive in UCI
|
||||
uci set "$UCI_CONFIG.cell_lock.active=0"
|
||||
uci commit "$UCI_CONFIG"
|
||||
|
||||
# Update status
|
||||
update_status "inactive" "Cell lock scheduler is inactive - scheduled end time reached" 0 0
|
||||
|
||||
# Remove LTE lock
|
||||
log_message "Removing LTE lock"
|
||||
execute_at_command 'AT+QNWLOCK="common/4g",0' 10
|
||||
local lte_status=$?
|
||||
|
||||
# Remove NR5G lock
|
||||
log_message "Removing NR5G lock"
|
||||
execute_at_command 'AT+QNWLOCK="common/5g",0' 10
|
||||
local nr5g_status=$?
|
||||
|
||||
# Disable persistence
|
||||
log_message "Disabling lock persistence"
|
||||
execute_at_command 'AT+QNWLOCK="save_ctrl",0,0' 10
|
||||
|
||||
# Reset network to apply changes
|
||||
log_message "Resetting network connection to apply changes"
|
||||
execute_at_command "AT+COPS=2" 5
|
||||
sleep 2
|
||||
execute_at_command "AT+COPS=0" 5
|
||||
|
||||
log_message "Cell lock removed at scheduled end time"
|
||||
update_status "inactive" "Cell lock removed at scheduled end time" 0 0
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Execute main function
|
||||
log_message "====== STARTING SCHEDULED CELL LOCK REMOVAL ======" "notice"
|
||||
remove_cell_lock
|
||||
log_message "====== COMPLETED SCHEDULED CELL LOCK REMOVAL ======" "notice"
|
||||
EOF
|
||||
|
||||
# Copy to quecmanager directory to match crontab
|
||||
cp "$SERVICES_DIR/apply_lock.sh" "$QUECMANAGER_DIR/"
|
||||
cp "$SERVICES_DIR/remove_lock.sh" "$QUECMANAGER_DIR/"
|
||||
else
|
||||
echo "Scripts found in both locations or only in quecmanager directory"
|
||||
fi
|
||||
|
||||
# Make all scripts executable
|
||||
chmod +x "$SERVICES_DIR/"*.sh 2>/dev/null
|
||||
chmod +x "$QUECMANAGER_DIR/"*.sh 2>/dev/null
|
||||
|
||||
echo "All scripts are now executable"
|
||||
|
||||
# 3. Create test status file
|
||||
cat > "/tmp/cell_lock_status.json" << EOF
|
||||
{
|
||||
"status": "active",
|
||||
"message": "Cell lock scheduler is active",
|
||||
"active": 1,
|
||||
"locked": 1,
|
||||
"timestamp": $(date +%s)
|
||||
}
|
||||
EOF
|
||||
chmod 644 "/tmp/cell_lock_status.json"
|
||||
|
||||
echo "Created test status file at /tmp/cell_lock_status.json"
|
||||
|
||||
# 4. Check crontab entries and fix if needed
|
||||
CRONTAB=$(crontab -l)
|
||||
APPLY_ENTRY=$(echo "$CRONTAB" | grep "apply_lock.sh")
|
||||
REMOVE_ENTRY=$(echo "$CRONTAB" | grep "remove_lock.sh")
|
||||
|
||||
echo "Current crontab entries:"
|
||||
echo "$CRONTAB" | grep -E "apply_lock.sh|remove_lock.sh"
|
||||
|
||||
# Extract times from UCI config
|
||||
START_TIME=$(uci -q get quecmanager.cell_lock.start_time)
|
||||
END_TIME=$(uci -q get quecmanager.cell_lock.end_time)
|
||||
|
||||
if [ -n "$START_TIME" ] && [ -n "$END_TIME" ]; then
|
||||
START_HOUR=$(echo "$START_TIME" | cut -d':' -f1 | sed 's/^0//')
|
||||
START_MIN=$(echo "$START_TIME" | cut -d':' -f2 | sed 's/^0//')
|
||||
END_HOUR=$(echo "$END_TIME" | cut -d':' -f1 | sed 's/^0//')
|
||||
END_MIN=$(echo "$END_TIME" | cut -d':' -f2 | sed 's/^0//')
|
||||
|
||||
echo "UCI times: Start=$START_HOUR:$START_MIN, End=$END_HOUR:$END_MIN"
|
||||
|
||||
# Verify that crontab entries match UCI config
|
||||
NEW_CRONTAB=$(crontab -l | grep -v "apply_lock.sh\|remove_lock.sh")
|
||||
NEW_CRONTAB="$NEW_CRONTAB
|
||||
$START_MIN $START_HOUR * * * $QUECMANAGER_DIR/apply_lock.sh
|
||||
$END_MIN $END_HOUR * * * $QUECMANAGER_DIR/remove_lock.sh"
|
||||
|
||||
echo "Setting crontab with correct paths and times..."
|
||||
echo "$NEW_CRONTAB" | crontab -
|
||||
|
||||
echo "Updated crontab entries:"
|
||||
crontab -l | grep -E "apply_lock.sh|remove_lock.sh"
|
||||
else
|
||||
echo "WARNING: Could not find start/end times in UCI config"
|
||||
fi
|
||||
|
||||
# 5. Test run scripts to verify they work
|
||||
echo "Testing apply_lock.sh script..."
|
||||
"$QUECMANAGER_DIR/apply_lock.sh" &
|
||||
PID=$!
|
||||
sleep 2
|
||||
if kill -0 $PID 2>/dev/null; then
|
||||
echo "apply_lock.sh is running correctly"
|
||||
# Let it complete
|
||||
wait $PID
|
||||
else
|
||||
echo "WARNING: apply_lock.sh may have issues"
|
||||
fi
|
||||
|
||||
# 6. Check if log file is being created
|
||||
if [ -f "/tmp/log/cell_lock/cell_lock.log" ]; then
|
||||
echo "Log file exists. Last few entries:"
|
||||
tail -n 10 "/tmp/log/cell_lock/cell_lock.log"
|
||||
else
|
||||
echo "WARNING: Log file not found"
|
||||
fi
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Log rotation script for cell lock logs
|
||||
|
||||
# Configuration
|
||||
LOG_DIR="/tmp/log/cell_lock"
|
||||
LOG_FILE="$LOG_DIR/cell_lock.log"
|
||||
MAX_LOG_SIZE=500 # KB
|
||||
MAX_LOG_FILES=3 # Number of old log files to keep
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Function to log message
|
||||
log_message() {
|
||||
local message="$1"
|
||||
local level="${2:-info}"
|
||||
local component="log_rotation"
|
||||
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Use logger directly
|
||||
logger -t "cell_lock_$component" -p "daemon.$level" "$message"
|
||||
}
|
||||
|
||||
# Check if log file exists and its size
|
||||
if [ -f "$LOG_FILE" ]; then
|
||||
log_size=$(du -k "$LOG_FILE" | cut -f1)
|
||||
|
||||
if [ $log_size -gt $MAX_LOG_SIZE ]; then
|
||||
log_message "Log file size ($log_size KB) exceeds maximum ($MAX_LOG_SIZE KB), rotating" "info"
|
||||
|
||||
# Rotate old logs
|
||||
if [ -f "$LOG_FILE.2" ]; then
|
||||
mv "$LOG_FILE.2" "$LOG_FILE.3"
|
||||
fi
|
||||
|
||||
if [ -f "$LOG_FILE.1" ]; then
|
||||
mv "$LOG_FILE.1" "$LOG_FILE.2"
|
||||
fi
|
||||
|
||||
if [ -f "$LOG_FILE" ]; then
|
||||
mv "$LOG_FILE" "$LOG_FILE.1"
|
||||
fi
|
||||
|
||||
# Create a new empty log file
|
||||
touch "$LOG_FILE"
|
||||
chmod 644 "$LOG_FILE"
|
||||
|
||||
# Log rotation complete
|
||||
log_message "Log rotation completed successfully" "info"
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') [info] [log_rotation] Log file rotated due to size" >> "$LOG_FILE"
|
||||
else
|
||||
log_message "Log file size ($log_size KB) within limits, no rotation needed" "debug"
|
||||
fi
|
||||
else
|
||||
log_message "Log file does not exist, creating it" "info"
|
||||
touch "$LOG_FILE"
|
||||
chmod 644 "$LOG_FILE"
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') [info] [log_rotation] New log file created" >> "$LOG_FILE"
|
||||
fi
|
||||
@@ -0,0 +1,111 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Script to update crontab entries for cell lock scheduling
|
||||
|
||||
# Configuration
|
||||
UCI_CONFIG="quecmanager"
|
||||
LOG_DIR="/tmp/log/cell_lock"
|
||||
LOG_FILE="$LOG_DIR/cell_lock.log"
|
||||
SCRIPTS_DIR="/www/cgi-bin/quecmanager/cell-locking"
|
||||
LOCK_SCRIPT="$SCRIPTS_DIR/apply_lock.sh"
|
||||
UNLOCK_SCRIPT="$SCRIPTS_DIR/remove_lock.sh"
|
||||
TEMP_CRONTAB="/tmp/cell_lock_crontab"
|
||||
ROTATE_SIZE=500 # KB before log rotation
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Enhanced log_message function
|
||||
log_message() {
|
||||
local message="$1"
|
||||
local level="${2:-info}"
|
||||
local component="update_crontab"
|
||||
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
local pid=$$
|
||||
|
||||
# Check if log file is too large (>500KB) and rotate if needed
|
||||
if [ -f "$LOG_FILE" ] && [ $(du -k "$LOG_FILE" | cut -f1) -gt $ROTATE_SIZE ]; then
|
||||
mv "$LOG_FILE" "$LOG_FILE.old"
|
||||
touch "$LOG_FILE"
|
||||
chmod 644 "$LOG_FILE"
|
||||
fi
|
||||
|
||||
# Format: [timestamp] [level] [component] [pid] message
|
||||
echo "[$timestamp] [$level] [$component] [$pid] $message" >> "$LOG_FILE"
|
||||
|
||||
# Also log to system log with appropriate priority
|
||||
case "$level" in
|
||||
debug) logger -t "cell_lock_$component" -p daemon.debug "$message" ;;
|
||||
info) logger -t "cell_lock_$component" -p daemon.info "$message" ;;
|
||||
notice) logger -t "cell_lock_$component" -p daemon.notice "$message" ;;
|
||||
warn) logger -t "cell_lock_$component" -p daemon.warning "$message" ;;
|
||||
error) logger -t "cell_lock_$component" -p daemon.err "$message" ;;
|
||||
crit) logger -t "cell_lock_$component" -p daemon.crit "$message" ;;
|
||||
*) logger -t "cell_lock_$component" -p daemon.info "$message" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Function to update crontab
|
||||
update_crontab() {
|
||||
log_message "Starting crontab update process" "info"
|
||||
|
||||
local enabled=$(uci -q get "$UCI_CONFIG.cell_lock.enabled")
|
||||
|
||||
# Create a clean temporary crontab file
|
||||
crontab -l | grep -v "$SCRIPTS_DIR/" > "$TEMP_CRONTAB" 2>/dev/null
|
||||
|
||||
if [ "$enabled" = "1" ]; then
|
||||
local start_time=$(uci -q get "$UCI_CONFIG.cell_lock.start_time")
|
||||
local end_time=$(uci -q get "$UCI_CONFIG.cell_lock.end_time")
|
||||
|
||||
if [ -z "$start_time" ] || [ -z "$end_time" ]; then
|
||||
log_message "Missing start or end time in configuration" "error"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_message "Scheduling cell locks with start=$start_time, end=$end_time" "info"
|
||||
|
||||
local start_hour=$(echo "$start_time" | cut -d':' -f1)
|
||||
local start_minute=$(echo "$start_time" | cut -d':' -f2)
|
||||
local end_hour=$(echo "$end_time" | cut -d':' -f1)
|
||||
local end_minute=$(echo "$end_time" | cut -d':' -f2)
|
||||
|
||||
# Remove leading zeros
|
||||
start_hour=$(echo "$start_hour" | sed 's/^0//')
|
||||
start_minute=$(echo "$start_minute" | sed 's/^0//')
|
||||
end_hour=$(echo "$end_hour" | sed 's/^0//')
|
||||
end_minute=$(echo "$end_minute" | sed 's/^0//')
|
||||
|
||||
# Add crontab entries for lock and unlock
|
||||
log_message "Adding crontab entry for start time: $start_minute $start_hour * * *" "debug"
|
||||
echo "$start_minute $start_hour * * * $LOCK_SCRIPT" >> "$TEMP_CRONTAB"
|
||||
|
||||
log_message "Adding crontab entry for end time: $end_minute $end_hour * * *" "debug"
|
||||
echo "$end_minute $end_hour * * * $UNLOCK_SCRIPT" >> "$TEMP_CRONTAB"
|
||||
|
||||
log_message "Added crontab entries for start time ($start_time) and end time ($end_time)" "info"
|
||||
else
|
||||
log_message "Cell lock scheduling is disabled, removing crontab entries" "info"
|
||||
fi
|
||||
|
||||
# Apply the new crontab
|
||||
log_message "Applying updated crontab" "debug"
|
||||
crontab "$TEMP_CRONTAB"
|
||||
local crontab_status=$?
|
||||
|
||||
if [ $crontab_status -eq 0 ]; then
|
||||
log_message "Crontab updated successfully" "info"
|
||||
else
|
||||
log_message "Failed to update crontab (status: $crontab_status)" "error"
|
||||
fi
|
||||
|
||||
# Clean up
|
||||
rm -f "$TEMP_CRONTAB"
|
||||
|
||||
return $crontab_status
|
||||
}
|
||||
|
||||
# Execute the function
|
||||
log_message "====== STARTING CRONTAB UPDATE ======" "notice"
|
||||
update_crontab
|
||||
log_message "====== COMPLETED CRONTAB UPDATE ======" "notice"
|
||||
Reference in New Issue
Block a user