QuecManager non-beta

Its about time I did this!
This commit is contained in:
Cameron Thompson
2025-04-02 23:09:08 -04:00
parent c4a340bd36
commit c42907e346
482 changed files with 47267 additions and 109914 deletions

View File

@@ -0,0 +1,63 @@
#!/bin/sh
# Set headers for JSON response
echo "Content-type: application/json"
echo ""
# Disable the service in UCI
uci set quecmanager.quecwatch.enabled='0'
if ! uci commit quecmanager; then
echo '{"status":"error","message":"Failed to update configuration"}'
exit 1
fi
# Function to log cleanup events
log_message() {
local level="$1"
local message="$2"
local LOG_DIR="/tmp/log/quecwatch"
local LOG_FILE="${LOG_DIR}/quecwatch.log"
# Ensure log directory exists
mkdir -p "${LOG_DIR}"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "${timestamp} - [${level}] ${message}" >> "$LOG_FILE"
logger -t quecwatch "${level}: ${message}"
}
# Stop the service
if [ -x "/etc/init.d/quecwatch" ]; then
if ! /etc/init.d/quecwatch stop; then
log_message "ERROR" "Failed to stop service cleanly"
# Force kill any remaining processes
if pkill -f "/www/cgi-bin/services/quecwatch.sh"; then
log_message "INFO" "Forced termination of QuecWatch processes"
fi
else
log_message "INFO" "Service stopped successfully"
fi
# Disable the service
if ! /etc/init.d/quecwatch disable; then
log_message "WARN" "Failed to disable service"
else
log_message "INFO" "Service disabled successfully"
fi
fi
# Clean up temporary files
for file in "/tmp/quecwatch_status.json" "/tmp/quecwatch_retry_count" "/var/run/quecwatch.pid"; do
if [ -f "$file" ]; then
if rm -f "$file"; then
log_message "INFO" "Removed temporary file: $file"
else
log_message "WARN" "Failed to remove temporary file: $file"
fi
fi
done
# Return success
echo '{"status":"success","message":"QuecWatch disabled successfully"}'

View File

@@ -0,0 +1,137 @@
#!/bin/sh
# Set content type to JSON
echo "Content-type: application/json"
echo ""
# Read POST data
read -r POST_DATA
# Function to extract value from JSON post data
extract_json_value() {
local key="$1"
local default="$2"
# Try with jsonfilter
if command -v jsonfilter >/dev/null 2>&1; then
local value=$(echo "$POST_DATA" | jsonfilter -e "@.$key" 2>/dev/null)
[ -n "$value" ] && echo "$value" && return 0
fi
# Fallback to grep
local value=$(echo "$POST_DATA" | grep -o "\"$key\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" | cut -d'"' -f4)
[ -n "$value" ] && echo "$value" && return 0
# Fallback to grep for numbers and booleans
local value=$(echo "$POST_DATA" | grep -o "\"$key\"[[:space:]]*:[[:space:]]*[0-9a-zA-Z]*" | cut -d':' -f2 | tr -d '[:space:]')
[ -n "$value" ] && echo "$value" && return 0
# Return default value
echo "$default"
return 0
}
# Extract parameters from POST data
ping_target=$(extract_json_value "pingTarget" "8.8.8.8")
ping_interval=$(extract_json_value "pingInterval" "60")
ping_failures=$(extract_json_value "pingFailures" "3")
max_retries=$(extract_json_value "maxRetries" "5")
connection_refresh=$(extract_json_value "connectionRefresh" "false")
auto_sim_failover=$(extract_json_value "autoSimFailover" "false")
sim_failover_schedule=$(extract_json_value "simFailoverSchedule" "0")
# Validate numeric values
validate_number() {
local value="$1"
local min="$2"
local max="$3"
local name="$4"
if ! echo "$value" | grep -q '^[0-9]\+$'; then
echo '{"status":"error","message":"'"$name must be a number"'"}'
exit 1
fi
if [ "$value" -lt "$min" ] || [ "$value" -gt "$max" ]; then
echo '{"status":"error","message":"'"$name must be between $min and $max"'"}'
exit 1
fi
}
# Validate boolean values
validate_boolean() {
local value="$1"
local name="$2"
if [ "$value" != "true" ] && [ "$value" != "false" ]; then
echo '{"status":"error","message":"'"$name must be true or false"'"}'
exit 1
fi
}
# Validate parameters
validate_number "$ping_interval" 5 3600 "Ping interval"
validate_number "$ping_failures" 1 10 "Ping failures"
validate_number "$max_retries" 1 20 "Max retries"
validate_number "$sim_failover_schedule" 0 1440 "SIM failover schedule"
validate_boolean "$connection_refresh" "Connection refresh"
validate_boolean "$auto_sim_failover" "Auto SIM failover"
# Function to setup UCI configuration
setup_uci_config() {
# Create section if it doesn't exist
touch /etc/config/quecmanager
if ! uci -q get quecmanager.quecwatch >/dev/null; then
uci set quecmanager.quecwatch=service
fi
# Set UCI values
uci set quecmanager.quecwatch.enabled='1'
uci set quecmanager.quecwatch.ping_target="$ping_target"
uci set quecmanager.quecwatch.ping_interval="$ping_interval"
uci set quecmanager.quecwatch.ping_failures="$ping_failures"
uci set quecmanager.quecwatch.max_retries="$max_retries"
uci set quecmanager.quecwatch.current_retries='0'
uci set quecmanager.quecwatch.connection_refresh="$connection_refresh"
uci set quecmanager.quecwatch.refresh_count='3'
uci set quecmanager.quecwatch.auto_sim_failover="$auto_sim_failover"
uci set quecmanager.quecwatch.sim_failover_schedule="$sim_failover_schedule"
# Commit changes
if ! uci commit quecmanager; then
echo '{"status":"error","message":"Failed to save configuration"}'
exit 1
fi
return 0
}
# Setup configuration
if ! setup_uci_config; then
exit 1
fi
# Enable and start the service
if [ ! -f "/etc/init.d/quecwatch" ]; then
echo '{"status":"error","message":"QuecWatch service script not found"}'
exit 1
fi
# Make sure the service script is executable
chmod +x /etc/init.d/quecwatch
# Enable the service
if ! /etc/init.d/quecwatch enable; then
echo '{"status":"error","message":"Failed to enable QuecWatch service"}'
exit 1
fi
# Start the service
if ! /etc/init.d/quecwatch start; then
echo '{"status":"error","message":"Failed to start QuecWatch service"}'
exit 1
fi
# Return success response
echo '{"status":"success","message":"QuecWatch enabled successfully"}'

View File

@@ -0,0 +1,139 @@
#!/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 quecwatch "$1" "$2"
echo "${value:-$2}"
}
# Function to format boolean for JSON
format_boolean() {
if [ "$1" = "1" ] || [ "$1" = "true" ]; then
echo "true"
else
echo "false"
fi
}
# Function to check if service is running
check_service_status() {
if [ -f "/var/run/quecwatch.pid" ]; then
pid=$(cat /var/run/quecwatch.pid 2>/dev/null)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
echo "running"
return
fi
fi
echo "stopped"
}
# Function to get last log entry
get_last_log() {
local LOG_FILE="/tmp/log/quecwatch/quecwatch.log"
if [ -f "$LOG_FILE" ]; then
tail -n 1 "$LOG_FILE" | sed 's/"/\\"/g'
else
echo "No log entries found"
fi
}
# Function to get current status
get_current_status() {
local STATUS_FILE="/tmp/quecwatch_status.json"
local status="unknown"
local message="Status not available"
local retry="0"
local maxRetries="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)
message=$(cat "$STATUS_FILE" | jsonfilter -e '@.message' 2>/dev/null)
retry=$(cat "$STATUS_FILE" | jsonfilter -e '@.retry' 2>/dev/null)
maxRetries=$(cat "$STATUS_FILE" | jsonfilter -e '@.maxRetries' 2>/dev/null)
timestamp=$(cat "$STATUS_FILE" | jsonfilter -e '@.timestamp' 2>/dev/null)
fi
fi
# Use defaults if extraction failed
[ -z "$status" ] && status="unknown"
[ -z "$message" ] && message="Status not available"
[ -z "$retry" ] && retry="0"
[ -z "$maxRetries" ] && maxRetries="0"
[ -z "$timestamp" ] && timestamp=$(date +%s)
echo "{\"status\":\"$status\",\"message\":\"$message\",\"retry\":$retry,\"maxRetries\":$maxRetries,\"timestamp\":$timestamp}"
}
# Load QuecManager configuration
config_load quecmanager
# Check if QuecWatch section exists
if ! uci -q get quecmanager.quecwatch >/dev/null; then
echo '{"status":"inactive","message":"QuecWatch is not configured"}'
exit 0
fi
# Get enabled status
enabled=$(get_uci_value "enabled" "0")
# Get service status
service_status=$(check_service_status)
# Get current status
current_status=$(get_current_status)
# Get last log entry
last_log=$(get_last_log)
# Fetch all configuration values
ping_target=$(get_uci_value "ping_target" "8.8.8.8")
ping_interval=$(get_uci_value "ping_interval" "60")
ping_failures=$(get_uci_value "ping_failures" "3")
max_retries=$(get_uci_value "max_retries" "5")
current_retries=$(get_uci_value "current_retries" "0")
connection_refresh=$(format_boolean $(get_uci_value "connection_refresh" "false"))
refresh_count=$(get_uci_value "refresh_count" "3")
auto_sim_failover=$(format_boolean $(get_uci_value "auto_sim_failover" "false"))
sim_failover_schedule=$(get_uci_value "sim_failover_schedule" "0")
# Determine the overall status
status="inactive"
if [ "$enabled" = "1" ]; then
if [ "$service_status" = "running" ]; then
status="active"
else
status="error"
fi
fi
# Prepare JSON response
cat <<EOF
{
"status": "$status",
"serviceStatus": "$service_status",
"currentStatus": $current_status,
"config": {
"pingTarget": "$ping_target",
"pingInterval": $ping_interval,
"pingFailures": $ping_failures,
"maxRetries": $max_retries,
"currentRetries": $current_retries,
"connectionRefresh": $connection_refresh,
"refreshCount": $refresh_count,
"autoSimFailover": $auto_sim_failover,
"simFailoverSchedule": $sim_failover_schedule
},
"lastActivity": "$last_log"
}
EOF

View File

@@ -0,0 +1,57 @@
#!/bin/sh
# Set headers for JSON response
echo "Content-type: application/json"
echo ""
# Function to log message
log_message() {
local level="$1"
local message="$2"
local LOG_DIR="/tmp/log/quecwatch"
local LOG_FILE="${LOG_DIR}/quecwatch.log"
# Ensure log directory exists
mkdir -p "${LOG_DIR}"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "${timestamp} - [${level}] ${message}" >> "$LOG_FILE"
logger -t quecwatch "${level}: ${message}"
}
# Reset retry counter
if uci -q get quecmanager.quecwatch >/dev/null; then
# Reset retry counter in UCI
uci set quecmanager.quecwatch.current_retries='0'
# Make sure service is enabled
uci set quecmanager.quecwatch.enabled='1'
# Commit changes
if uci commit quecmanager; then
log_message "INFO" "Retry counter reset to 0 and service enabled"
# Also update the retry count file for immediate effect
echo "0" > "/tmp/quecwatch_retry_count"
chmod 644 "/tmp/quecwatch_retry_count"
# Restart the service if it exists
if [ -x "/etc/init.d/quecwatch" ]; then
if /etc/init.d/quecwatch restart; then
log_message "INFO" "Service restarted successfully"
echo '{"status":"success","message":"Retry counter reset and service restarted successfully"}'
else
log_message "ERROR" "Failed to restart service"
echo '{"status":"warning","message":"Retry counter reset but failed to restart service"}'
fi
else
log_message "ERROR" "Service init script not found"
echo '{"status":"warning","message":"Retry counter reset but service init script not found"}'
fi
else
log_message "ERROR" "Failed to update configuration"
echo '{"status":"error","message":"Failed to update configuration"}'
fi
else
echo '{"status":"error","message":"QuecWatch configuration not found"}'
fi