Revert "Merge branch 'development-SDXPINN' into SDXPINN"
This reverts commit72d9dee3b4, reversing changes made to114ee81d76.
This commit is contained in:
@@ -2,9 +2,6 @@
|
||||
# AT Queue Manager for OpenWRT with Preemption Support and Token System
|
||||
# Located in /www/cgi-bin/services/at_queue_manager
|
||||
|
||||
# Load centralized logging
|
||||
. /www/cgi-bin/services/quecmanager_logger.sh
|
||||
|
||||
# Constants
|
||||
QUEUE_DIR="/tmp/at_queue"
|
||||
QUEUE_FILE="$QUEUE_DIR/queue"
|
||||
@@ -18,32 +15,6 @@ RESULTS_MAX_AGE=3600 # 1 hour in seconds
|
||||
POLL_INTERVAL=0.01
|
||||
PREEMPTION_THRESHOLD=2 # 3 seconds threshold for preemption
|
||||
TOKEN_TIMEOUT=30 # seconds before token expires
|
||||
SCRIPT_NAME="at_queue_manager"
|
||||
|
||||
# Logging function - uses both centralized and system logging
|
||||
log_at_queue() {
|
||||
local level="$1"
|
||||
local message="$2"
|
||||
|
||||
# Use centralized logging
|
||||
case "$level" in
|
||||
"error")
|
||||
qm_log_error "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
"warn")
|
||||
qm_log_warn "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
"debug")
|
||||
qm_log_debug "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
*)
|
||||
qm_log_info "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
esac
|
||||
|
||||
# Also maintain system logging for compatibility
|
||||
logger -t at_queue -p "daemon.$level" "$message"
|
||||
}
|
||||
|
||||
# Utility function for JSON escaping
|
||||
escape_json() {
|
||||
@@ -68,7 +39,7 @@ acquire_lock() {
|
||||
|
||||
while [ $attempt -lt $timeout ]; do
|
||||
if mkdir "$LOCK_DIR" 2>/dev/null; then
|
||||
log_at_queue "debug" "Lock acquired"
|
||||
logger -t at_queue -p daemon.debug "Lock acquired"
|
||||
return 0
|
||||
fi
|
||||
|
||||
@@ -76,36 +47,42 @@ acquire_lock() {
|
||||
attempt=$((attempt + 1))
|
||||
done
|
||||
|
||||
log_at_queue "error" "Failed to acquire lock after $timeout attempts"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock after $timeout attempts"
|
||||
return 1
|
||||
}
|
||||
|
||||
release_lock() {
|
||||
if rmdir "$LOCK_DIR" 2>/dev/null; then
|
||||
log_at_queue "debug" "Lock released"
|
||||
if [ -d "$LOCK_DIR" ]; then
|
||||
rmdir "$LOCK_DIR" 2>/dev/null
|
||||
logger -t at_queue -p daemon.debug "Lock released"
|
||||
return 0
|
||||
else
|
||||
log_at_queue "error" "Lock directory doesn't exist"
|
||||
return 1
|
||||
fi
|
||||
|
||||
logger -t at_queue -p daemon.error "Lock directory doesn't exist"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Ensure required directories exist
|
||||
initialize_queue() {
|
||||
init_queue_system() {
|
||||
mkdir -p "$QUEUE_DIR" "$RESULTS_DIR"
|
||||
touch "$QUEUE_FILE" "$ACTIVE_FILE"
|
||||
chmod 666 "$QUEUE_FILE" "$ACTIVE_FILE"
|
||||
log_at_queue "info" "Queue system initialized"
|
||||
touch "$QUEUE_FILE"
|
||||
chmod 755 "$QUEUE_DIR"
|
||||
chmod 644 "$QUEUE_FILE"
|
||||
chmod 755 "$RESULTS_DIR"
|
||||
logger -t at_queue -p daemon.info "Queue system initialized"
|
||||
}
|
||||
|
||||
# Cleanup old results and tracking files
|
||||
cleanup_old_results() {
|
||||
local current_time=$(date +%s)
|
||||
|
||||
# Remove old tracking files
|
||||
find "$QUEUE_DIR" -name "start_time.*" -o -name "pid.*" -type f -mmin +60 -delete 2>/dev/null
|
||||
# Clean up old execution tracking files
|
||||
find "$QUEUE_DIR" -name "pid.*" -type f -mmin +60 -delete 2>/dev/null
|
||||
find "$QUEUE_DIR" -name "*.exit" -type f -mmin +60 -delete 2>/dev/null
|
||||
find "$QUEUE_DIR" -name "start_time.*" -type f -mmin +60 -delete 2>/dev/null
|
||||
logger -t at_queue -p daemon.debug "Cleaned up old tracking files"
|
||||
|
||||
log_at_queue "debug" "Cleaned up old tracking files" # Use find with -delete and basic timestamp check for OpenWRT
|
||||
# Use find with -delete and basic timestamp check for OpenWRT
|
||||
find "$RESULTS_DIR" -name "*.json" -type f -mmin +60 -delete 2>/dev/null || {
|
||||
# Fallback method if find fails
|
||||
for file in "$RESULTS_DIR"/*.json; do
|
||||
@@ -122,12 +99,12 @@ cleanup_old_results() {
|
||||
local token_time=$(cat "$TOKEN_FILE" | jsonfilter -e '@.timestamp')
|
||||
if [ $((current_time - token_time)) -gt $TOKEN_TIMEOUT ]; then
|
||||
local token_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id')
|
||||
log_at_queue "warn" "Removing expired token from $token_holder"
|
||||
logger -t at_queue -p daemon.warn "Removing expired token from $token_holder"
|
||||
rm -f "$TOKEN_FILE"
|
||||
fi
|
||||
fi
|
||||
|
||||
log_at_queue "info" "Cleanup: Removed files older than 1 hour"
|
||||
logger -t at_queue -p daemon.info "Cleanup: Removed files older than 1 hour"
|
||||
}
|
||||
|
||||
# Generate unique command ID
|
||||
@@ -145,7 +122,7 @@ start_execution_tracking() {
|
||||
echo "$pid" > "$QUEUE_DIR/pid.$cmd_id"
|
||||
chmod 644 "$QUEUE_DIR/start_time.$cmd_id"
|
||||
chmod 644 "$QUEUE_DIR/pid.$cmd_id"
|
||||
log_at_queue "debug" "Started tracking command $cmd_id (PID: $pid)"
|
||||
logger -t at_queue -p daemon.debug "Started tracking command $cmd_id (PID: $pid)"
|
||||
}
|
||||
|
||||
# Check if running command should be preempted
|
||||
@@ -154,7 +131,7 @@ should_preempt() {
|
||||
local new_priority="$2"
|
||||
|
||||
if [ ! -f "$QUEUE_DIR/start_time.$current_cmd_id" ]; then
|
||||
log_at_queue "debug" "No start time found for $current_cmd_id"
|
||||
logger -t at_queue -p daemon.debug "No start time found for $current_cmd_id"
|
||||
return 1
|
||||
fi
|
||||
|
||||
@@ -167,16 +144,16 @@ should_preempt() {
|
||||
if [ -f "$ACTIVE_FILE" ]; then
|
||||
current_priority=$(cat "$ACTIVE_FILE" | jsonfilter -e '@.priority')
|
||||
else
|
||||
log_at_queue "debug" "No active command found"
|
||||
logger -t at_queue -p daemon.debug "No active command found"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ $execution_time -gt $PREEMPTION_THRESHOLD ] && [ $new_priority -lt $current_priority ]; then
|
||||
log_at_queue "info" "Command $current_cmd_id (priority $current_priority) running for ${execution_time}s is eligible for preemption by priority $new_priority"
|
||||
logger -t at_queue -p daemon.info "Command $current_cmd_id (priority $current_priority) running for ${execution_time}s is eligible for preemption by priority $new_priority"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_at_queue "debug" "Command $current_cmd_id not eligible for preemption (time: ${execution_time}s, current priority: $current_priority, new priority: $new_priority)"
|
||||
logger -t at_queue -p daemon.debug "Command $current_cmd_id not eligible for preemption (time: ${execution_time}s, current priority: $current_priority, new priority: $new_priority)"
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -187,7 +164,7 @@ preempt_command() {
|
||||
|
||||
if [ -f "$pid_file" ]; then
|
||||
local pid=$(cat "$pid_file")
|
||||
log_at_queue "info" "Preempting command $cmd_id (PID: $pid)"
|
||||
logger -t at_queue -p daemon.info "Preempting command $cmd_id (PID: $pid)"
|
||||
|
||||
# Send SIGTERM first
|
||||
kill -TERM $pid 2>/dev/null
|
||||
@@ -198,7 +175,7 @@ preempt_command() {
|
||||
# Force kill if still running
|
||||
if kill -0 $pid 2>/dev/null; then
|
||||
kill -KILL $pid 2>/dev/null
|
||||
log_at_queue "warn" "Forced termination of command $cmd_id"
|
||||
logger -t at_queue -p daemon.warn "Forced termination of command $cmd_id"
|
||||
fi
|
||||
|
||||
# Record preemption result
|
||||
@@ -208,11 +185,11 @@ preempt_command() {
|
||||
rm -f "$pid_file" "$QUEUE_DIR/start_time.$cmd_id" "$QUEUE_DIR/$cmd_id.exit"
|
||||
[ -f "$ACTIVE_FILE" ] && rm -f "$ACTIVE_FILE"
|
||||
|
||||
log_at_queue "info" "Command $cmd_id preemption complete"
|
||||
logger -t at_queue -p daemon.info "Command $cmd_id preemption complete"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_at_queue "warn" "No PID file found for command $cmd_id"
|
||||
logger -t at_queue -p daemon.warn "No PID file found for command $cmd_id"
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -250,7 +227,7 @@ EOF
|
||||
|
||||
printf "%s" "$response" > "$RESULTS_DIR/$cmd_id.json"
|
||||
chmod 644 "$RESULTS_DIR/$cmd_id.json"
|
||||
log_at_queue "info" "Recorded preemption result for command $cmd_id (duration: ${duration}ms)"
|
||||
logger -t at_queue -p daemon.info "Recorded preemption result for command $cmd_id (duration: ${duration}ms)"
|
||||
}
|
||||
|
||||
# Request a token for direct sms_tool execution
|
||||
@@ -261,7 +238,7 @@ request_token() {
|
||||
|
||||
# Acquire lock first
|
||||
if ! acquire_lock; then
|
||||
log_at_queue "error" "Failed to acquire lock for token request"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock for token request"
|
||||
echo "{\"error\":\"Could not acquire lock\",\"status\":\"denied\"}"
|
||||
return 1
|
||||
fi
|
||||
@@ -275,11 +252,11 @@ request_token() {
|
||||
|
||||
# Check for expired token (> TOKEN_TIMEOUT seconds old)
|
||||
if [ $((current_time - timestamp)) -gt $TOKEN_TIMEOUT ]; then
|
||||
log_at_queue "warn" "Found expired token from $current_holder, releasing"
|
||||
logger -t at_queue -p daemon.warn "Found expired token from $current_holder, releasing"
|
||||
rm -f "$TOKEN_FILE"
|
||||
# Check for priority preemption
|
||||
elif [ $priority -lt $current_priority ]; then
|
||||
log_at_queue "info" "Preempting token from $current_holder (priority: $current_priority) for $requestor_id (priority: $priority)"
|
||||
logger -t at_queue -p daemon.info "Preempting token from $current_holder (priority: $current_priority) for $requestor_id (priority: $priority)"
|
||||
rm -f "$TOKEN_FILE"
|
||||
else
|
||||
# Token in use and cannot be preempted
|
||||
@@ -301,7 +278,7 @@ request_token() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_at_queue "info" "Direct execution with higher priority than active queue command"
|
||||
logger -t at_queue -p daemon.info "Direct execution with higher priority than active queue command"
|
||||
fi
|
||||
|
||||
# Grant token
|
||||
@@ -319,7 +296,7 @@ release_token() {
|
||||
local requestor_id="$1"
|
||||
|
||||
if ! acquire_lock; then
|
||||
log_at_queue "error" "Failed to acquire lock for token release"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock for token release"
|
||||
return 1
|
||||
fi
|
||||
|
||||
@@ -328,15 +305,15 @@ release_token() {
|
||||
|
||||
if [ "$current_holder" = "$requestor_id" ]; then
|
||||
rm -f "$TOKEN_FILE"
|
||||
log_at_queue "debug" "Token released by $requestor_id"
|
||||
logger -t at_queue -p daemon.debug "Token released by $requestor_id"
|
||||
release_lock
|
||||
echo "{\"status\":\"released\"}"
|
||||
return 0
|
||||
else
|
||||
log_at_queue "warn" "Token release attempted by $requestor_id but held by $current_holder"
|
||||
logger -t at_queue -p daemon.warn "Token release attempted by $requestor_id but held by $current_holder"
|
||||
fi
|
||||
else
|
||||
log_at_queue "warn" "Token release attempted but no token exists"
|
||||
logger -t at_queue -p daemon.warn "Token release attempted but no token exists"
|
||||
fi
|
||||
|
||||
release_lock
|
||||
@@ -354,11 +331,11 @@ enqueue_command() {
|
||||
# Ensure queue directory exists
|
||||
[ ! -d "$QUEUE_DIR" ] && init_queue_system
|
||||
|
||||
log_at_queue "info" "Enqueuing command: $cmd (priority: $priority, id: $cmd_id)"
|
||||
logger -t at_queue -p daemon.info "Enqueuing command: $cmd (priority: $priority, id: $cmd_id)"
|
||||
|
||||
# Acquire lock for queue modification
|
||||
if ! acquire_lock; then
|
||||
log_at_queue "error" "Failed to acquire lock for enqueuing command"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock for enqueuing command"
|
||||
echo "{\"error\":\"Queue lock acquisition failed\",\"command\":\"$cmd\"}"
|
||||
return 1
|
||||
fi
|
||||
@@ -381,11 +358,11 @@ enqueue_command() {
|
||||
cat "$QUEUE_FILE" >> "$temp_file"
|
||||
mv "$temp_file" "$QUEUE_FILE"
|
||||
chmod 644 "$QUEUE_FILE"
|
||||
log_at_queue "info" "Added high priority command to front of queue"
|
||||
logger -t at_queue -p daemon.info "Added high priority command to front of queue"
|
||||
else
|
||||
# Normal priority - append to queue
|
||||
echo "$entry" >> "$QUEUE_FILE"
|
||||
log_at_queue "info" "Added normal priority command to end of queue"
|
||||
logger -t at_queue -p daemon.info "Added normal priority command to end of queue"
|
||||
fi
|
||||
|
||||
# Release lock
|
||||
@@ -402,7 +379,7 @@ dequeue_command() {
|
||||
|
||||
# Acquire lock
|
||||
if ! acquire_lock; then
|
||||
log_at_queue "error" "Failed to acquire lock for dequeuing command"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock for dequeuing command"
|
||||
return 1
|
||||
fi
|
||||
|
||||
@@ -418,7 +395,7 @@ dequeue_command() {
|
||||
# Release lock
|
||||
release_lock
|
||||
|
||||
log_at_queue "debug" "Dequeued command: $(echo "$cmd_entry" | jsonfilter -e '@.command')"
|
||||
logger -t at_queue -p daemon.debug "Dequeued command: $(echo "$cmd_entry" | jsonfilter -e '@.command')"
|
||||
echo "$cmd_entry"
|
||||
}
|
||||
|
||||
@@ -456,7 +433,7 @@ execute_with_timeout() {
|
||||
# Start execution tracking
|
||||
start_execution_tracking "$cmd_id" "$pid"
|
||||
|
||||
log_at_queue "debug" "Started command execution: $command (PID: $pid)"
|
||||
logger -t at_queue -p daemon.debug "Started command execution: $command (PID: $pid)"
|
||||
|
||||
# Wait for completion with shorter polling interval
|
||||
local start_time=$(date +%s)
|
||||
@@ -470,7 +447,7 @@ execute_with_timeout() {
|
||||
# Cleanup
|
||||
rm -f "$QUEUE_DIR/pid.$cmd_id" "$QUEUE_DIR/$cmd_id.exit" "$output_file" "$QUEUE_DIR/start_time.$cmd_id"
|
||||
|
||||
log_at_queue "debug" "Command completed with exit code $exit_code"
|
||||
logger -t at_queue -p daemon.debug "Command completed with exit code $exit_code"
|
||||
echo "$output"
|
||||
return $exit_code
|
||||
fi
|
||||
@@ -494,7 +471,7 @@ execute_with_timeout() {
|
||||
# Cleanup
|
||||
rm -f "$QUEUE_DIR/pid.$cmd_id" "$QUEUE_DIR/$cmd_id.exit" "$output_file" "$QUEUE_DIR/start_time.$cmd_id"
|
||||
|
||||
log_at_queue "warn" "Command timed out after $timeout seconds"
|
||||
logger -t at_queue -p daemon.warn "Command timed out after $timeout seconds"
|
||||
echo "${partial_output:-Command timed out after $timeout seconds}"
|
||||
fi
|
||||
|
||||
@@ -510,7 +487,7 @@ execute_command() {
|
||||
|
||||
local start_time=$(date +%s%3N)
|
||||
|
||||
log_at_queue "info" "Executing command $cmd_id: $cmd_text (priority: $priority)"
|
||||
logger -t at_queue -p daemon.info "Executing command $cmd_id: $cmd_text (priority: $priority)"
|
||||
|
||||
# Execute command with timeout
|
||||
local result=$(execute_with_timeout "$cmd_text" $MAX_TIMEOUT "$cmd_id")
|
||||
@@ -524,16 +501,16 @@ execute_command() {
|
||||
|
||||
if [ $exit_code -eq 124 ]; then
|
||||
status="timeout"
|
||||
log_at_queue "error" "Command $cmd_id timed out after ${duration}ms"
|
||||
logger -t at_queue -p daemon.error "Command $cmd_id timed out after ${duration}ms"
|
||||
elif echo "$result" | grep -q "OK"; then
|
||||
status="success"
|
||||
log_level="info"
|
||||
log_at_queue "info" "Command $cmd_id completed successfully in ${duration}ms"
|
||||
logger -t at_queue -p daemon.info "Command $cmd_id completed successfully in ${duration}ms"
|
||||
elif echo "$result" | grep -q "CME ERROR"; then
|
||||
status="cme_error"
|
||||
log_at_queue "error" "Command $cmd_id failed with CME ERROR in ${duration}ms"
|
||||
logger -t at_queue -p daemon.error "Command $cmd_id failed with CME ERROR in ${duration}ms"
|
||||
else
|
||||
log_at_queue "error" "Command $cmd_id failed with general error in ${duration}ms"
|
||||
logger -t at_queue -p daemon.error "Command $cmd_id failed with general error in ${duration}ms"
|
||||
fi
|
||||
|
||||
# Clean and escape the output
|
||||
@@ -559,7 +536,7 @@ EOF
|
||||
|
||||
# Acquire lock for writing result
|
||||
if ! acquire_lock; then
|
||||
log_at_queue "error" "Failed to acquire lock for writing result"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock for writing result"
|
||||
else
|
||||
# Save response
|
||||
printf "%s" "$response" > "$RESULTS_DIR/$cmd_id.json"
|
||||
@@ -584,7 +561,7 @@ process_queue() {
|
||||
# Make sure the lock directory doesn't exist at startup
|
||||
[ -d "$LOCK_DIR" ] && rmdir "$LOCK_DIR" 2>/dev/null
|
||||
|
||||
log_at_queue "info" "Started queue processing daemon"
|
||||
logger -t at_queue -p daemon.info "Started queue processing daemon"
|
||||
|
||||
while true; do
|
||||
# Quick cleanup check
|
||||
@@ -602,12 +579,12 @@ process_queue() {
|
||||
|
||||
# Check for expired token
|
||||
if [ $((current_time - token_time)) -gt $TOKEN_TIMEOUT ]; then
|
||||
log_at_queue "warn" "Removing expired token from $token_holder"
|
||||
logger -t at_queue -p daemon.warn "Removing expired token from $token_holder"
|
||||
rm -f "$TOKEN_FILE"
|
||||
else
|
||||
# Log pause status only every 5 seconds to reduce log spam
|
||||
if [ $((current_time - last_log)) -ge 5 ]; then
|
||||
log_at_queue "debug" "Queue processing paused, token held by $token_holder"
|
||||
logger -t at_queue -p daemon.debug "Queue processing paused, token held by $token_holder"
|
||||
last_log=$current_time
|
||||
fi
|
||||
sleep $POLL_INTERVAL
|
||||
@@ -635,64 +612,49 @@ if [ "${SCRIPT_NAME}" != "" ]; then
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# Log the incoming request for debugging
|
||||
log_at_queue "debug" "CGI: Incoming request - QUERY_STRING='$QUERY_STRING', REQUEST_METHOD='$REQUEST_METHOD', HTTP_USER_AGENT='$HTTP_USER_AGENT'"
|
||||
|
||||
# Parse query string for CGI mode
|
||||
eval $(echo "$QUERY_STRING" | sed 's/&/;/g')
|
||||
|
||||
# Handle empty action parameter specifically
|
||||
if [ -z "$action" ]; then
|
||||
if [ -z "$QUERY_STRING" ]; then
|
||||
log_at_queue "warn" "CGI: No query string provided - possible health check or browser prefetch"
|
||||
echo "{\"error\":\"No action specified\",\"help\":\"Valid actions: enqueue, status, request_token, release_token\"}"
|
||||
else
|
||||
log_at_queue "warn" "CGI: Query string present but no action parameter: '$QUERY_STRING'"
|
||||
echo "{\"error\":\"Missing action parameter\",\"query_string\":\"$QUERY_STRING\"}"
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
case "$action" in
|
||||
"enqueue")
|
||||
if [ -n "$command" ]; then
|
||||
log_at_queue "info" "CGI: Received enqueue request for command: $command"
|
||||
logger -t at_queue -p daemon.info "CGI: Received enqueue request for command: $command"
|
||||
enqueue_command "$command" "$priority"
|
||||
else
|
||||
log_at_queue "error" "CGI: Empty command received"
|
||||
logger -t at_queue -p daemon.error "CGI: Empty command received"
|
||||
echo "{\"error\":\"No command specified\"}"
|
||||
fi
|
||||
;;
|
||||
"status")
|
||||
if [ -f "$ACTIVE_FILE" ]; then
|
||||
log_at_queue "debug" "CGI: Status request - queue active"
|
||||
logger -t at_queue -p daemon.debug "CGI: Status request - queue active"
|
||||
cat "$ACTIVE_FILE"
|
||||
else
|
||||
log_at_queue "debug" "CGI: Status request - queue idle"
|
||||
logger -t at_queue -p daemon.debug "CGI: Status request - queue idle"
|
||||
echo "{\"status\":\"idle\"}"
|
||||
fi
|
||||
;;
|
||||
"request_token")
|
||||
if [ -n "$id" ]; then
|
||||
log_at_queue "info" "Token request from $id (priority: ${priority:-10})"
|
||||
logger -t at_queue -p daemon.info "Token request from $id (priority: ${priority:-10})"
|
||||
request_token "$id" "${priority:-10}" "${timeout:-10}"
|
||||
else
|
||||
log_at_queue "error" "Token request missing ID"
|
||||
logger -t at_queue -p daemon.error "Token request missing ID"
|
||||
echo "{\"error\":\"No requestor ID specified\",\"status\":\"denied\"}"
|
||||
fi
|
||||
;;
|
||||
"release_token")
|
||||
if [ -n "$id" ]; then
|
||||
log_at_queue "info" "Token release from $id"
|
||||
logger -t at_queue -p daemon.info "Token release from $id"
|
||||
release_token "$id"
|
||||
else
|
||||
log_at_queue "error" "Token release missing ID"
|
||||
logger -t at_queue -p daemon.error "Token release missing ID"
|
||||
echo "{\"error\":\"No requestor ID specified\",\"status\":\"denied\"}"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
log_at_queue "error" "CGI: Invalid action received: '$action' (QUERY_STRING: '$QUERY_STRING')"
|
||||
echo "{\"error\":\"Invalid action: $action\",\"valid_actions\":[\"enqueue\",\"status\",\"request_token\",\"release_token\"]}"
|
||||
logger -t at_queue -p daemon.error "CGI: Invalid action received: $action"
|
||||
echo "{\"error\":\"Invalid action\"}"
|
||||
;;
|
||||
esac
|
||||
exit 0
|
||||
@@ -707,4 +669,4 @@ fi
|
||||
# If not run as CGI, start queue processing
|
||||
if [ "${SCRIPT_NAME}" = "" ] && [ -z "$1" ]; then
|
||||
process_queue
|
||||
fi
|
||||
fi
|
||||
@@ -1,110 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
# QuecManager Log Cleanup Script
|
||||
# Periodically clean up old log files to prevent /tmp from filling up
|
||||
|
||||
. /www/cgi-bin/services/quecmanager_logger.sh
|
||||
|
||||
# Configuration
|
||||
MAX_LOG_AGE_DAYS=7 # Delete logs older than 7 days
|
||||
MAX_BACKUP_FILES=2 # Keep maximum 2 backup files (.1, .2)
|
||||
CLEANUP_LOG_SIZE=1000 # Run cleanup if any log exceeds 1MB
|
||||
|
||||
# Function to log cleanup activities
|
||||
log_cleanup() {
|
||||
qm_log_info "system" "log_cleanup" "$1"
|
||||
}
|
||||
|
||||
# Initialize
|
||||
qm_init_logs
|
||||
log_cleanup "Starting log cleanup process"
|
||||
|
||||
# Cleanup function
|
||||
perform_cleanup() {
|
||||
local files_cleaned=0
|
||||
local space_freed=0
|
||||
|
||||
# Clean up old backup files
|
||||
if [ -d "$QM_LOG_BASE" ]; then
|
||||
# Remove backup files older than specified days
|
||||
old_backups=$(find "$QM_LOG_BASE" -name "*.1" -o -name "*.2" -type f -mtime +$MAX_LOG_AGE_DAYS 2>/dev/null)
|
||||
for backup_file in $old_backups; do
|
||||
if [ -f "$backup_file" ]; then
|
||||
file_size=$(du -k "$backup_file" 2>/dev/null | cut -f1)
|
||||
rm -f "$backup_file" 2>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
files_cleaned=$((files_cleaned + 1))
|
||||
space_freed=$((space_freed + ${file_size:-0}))
|
||||
log_cleanup "Removed old backup file: $(basename "$backup_file")"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# Force rotation for large log files
|
||||
for category_dir in "$QM_LOG_DAEMONS" "$QM_LOG_SERVICES" "$QM_LOG_SETTINGS" "$QM_LOG_SYSTEM"; do
|
||||
if [ -d "$category_dir" ]; then
|
||||
for logfile in "$category_dir"/*.log; do
|
||||
if [ -f "$logfile" ]; then
|
||||
# Check file size in KB
|
||||
file_size_kb=$(du -k "$logfile" 2>/dev/null | cut -f1)
|
||||
|
||||
if [ "${file_size_kb:-0}" -gt $CLEANUP_LOG_SIZE ]; then
|
||||
log_cleanup "Rotating large log file: $(basename "$logfile") (${file_size_kb}KB)"
|
||||
qm_rotate_log "$logfile"
|
||||
files_cleaned=$((files_cleaned + 1))
|
||||
fi
|
||||
fi
|
||||
done
|
||||
fi
|
||||
done
|
||||
|
||||
# Additional cleanup: remove empty log files
|
||||
empty_logs=$(find "$QM_LOG_BASE" -name "*.log" -type f -size 0 2>/dev/null)
|
||||
for empty_log in $empty_logs; do
|
||||
rm -f "$empty_log" 2>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
files_cleaned=$((files_cleaned + 1))
|
||||
log_cleanup "Removed empty log file: $(basename "$empty_log")"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Log cleanup summary
|
||||
if [ $files_cleaned -gt 0 ]; then
|
||||
log_cleanup "Cleanup completed: $files_cleaned files processed, ${space_freed}KB freed"
|
||||
else
|
||||
log_cleanup "Cleanup completed: no files needed cleaning"
|
||||
fi
|
||||
}
|
||||
|
||||
# Check if we should run cleanup based on disk usage
|
||||
check_disk_usage() {
|
||||
# Check /tmp usage (OpenWrt compatible)
|
||||
local tmp_usage=""
|
||||
|
||||
# Try df first (most common)
|
||||
if command -v df >/dev/null 2>&1; then
|
||||
tmp_usage=$(df /tmp 2>/dev/null | awk 'NR==2 {print $5}' | tr -d '%')
|
||||
fi
|
||||
|
||||
# If we got a valid percentage and it's high, force cleanup
|
||||
if [ -n "$tmp_usage" ] && [ "$tmp_usage" -gt 80 ]; then
|
||||
log_cleanup "High /tmp usage detected (${tmp_usage}%), forcing cleanup"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Always run periodic cleanup
|
||||
return 0
|
||||
}
|
||||
|
||||
# Main execution
|
||||
if check_disk_usage; then
|
||||
perform_cleanup
|
||||
else
|
||||
log_cleanup "Disk usage check passed, skipping cleanup"
|
||||
fi
|
||||
|
||||
# Clean up centralized log helper's old logs too
|
||||
qm_cleanup_logs
|
||||
|
||||
log_cleanup "Log cleanup process completed"
|
||||
@@ -8,17 +8,14 @@ set -eu
|
||||
# Ensure PATH for OpenWrt/BusyBox
|
||||
export PATH="/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
|
||||
|
||||
# Load centralized logging
|
||||
. /www/cgi-bin/services/quecmanager_logger.sh
|
||||
|
||||
# Configuration
|
||||
TMP_DIR="/tmp/quecmanager"
|
||||
OUT_JSON="$TMP_DIR/memory.json"
|
||||
PID_FILE="$TMP_DIR/memory_daemon.pid"
|
||||
LOG_FILE="$TMP_DIR/memory_daemon.log"
|
||||
CONFIG_FILE="/etc/quecmanager/settings/memory_settings.conf"
|
||||
[ -f "$CONFIG_FILE" ] || CONFIG_FILE="/tmp/quecmanager/settings/memory_settings.conf"
|
||||
DEFAULT_INTERVAL=1
|
||||
SCRIPT_NAME="memory_daemon"
|
||||
|
||||
# Ensure temp directory exists
|
||||
ensure_tmp_dir() {
|
||||
@@ -27,7 +24,7 @@ ensure_tmp_dir() {
|
||||
|
||||
# Logging function
|
||||
log() {
|
||||
qm_log_info "daemon" "$SCRIPT_NAME" "$1"
|
||||
printf '%s - %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >> "$LOG_FILE" 2>/dev/null || true
|
||||
}
|
||||
|
||||
# Check if this daemon instance is already running
|
||||
|
||||
@@ -5,22 +5,19 @@ set -eu
|
||||
# Ensure PATH for OpenWrt/BusyBox
|
||||
export PATH="/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
|
||||
|
||||
# Load centralized logging
|
||||
. /www/cgi-bin/services/quecmanager_logger.sh
|
||||
|
||||
TMP_DIR="/tmp/quecmanager"
|
||||
OUT_JSON="$TMP_DIR/ping_latency.json"
|
||||
PID_FILE="$TMP_DIR/ping_daemon.pid"
|
||||
LOG_FILE="$TMP_DIR/ping_daemon.log"
|
||||
CONFIG_FILE="/etc/quecmanager/settings/ping_settings.conf"
|
||||
[ -f "$CONFIG_FILE" ] || CONFIG_FILE="/tmp/quecmanager/settings/ping_settings.conf"
|
||||
DEFAULT_HOST="8.8.8.8"
|
||||
DEFAULT_INTERVAL=5
|
||||
SCRIPT_NAME="ping_daemon"
|
||||
|
||||
ensure_tmp_dir() { [ -d "$TMP_DIR" ] || mkdir -p "$TMP_DIR" || exit 1; }
|
||||
|
||||
log() {
|
||||
qm_log_info "daemon" "$SCRIPT_NAME" "$1"
|
||||
printf '%s - %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >> "$LOG_FILE" 2>/dev/null || true
|
||||
}
|
||||
|
||||
daemon_is_running() {
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
# QuecManager Centralized Logging Helper
|
||||
# OpenWrt/BusyBox compatible logging system
|
||||
# Usage: source this file and use qm_log function
|
||||
|
||||
set -e
|
||||
|
||||
# Base log directory
|
||||
QM_LOG_BASE="/tmp/quecmanager/logs"
|
||||
|
||||
# Log categories
|
||||
QM_LOG_DAEMONS="$QM_LOG_BASE/daemons"
|
||||
QM_LOG_SERVICES="$QM_LOG_BASE/services"
|
||||
QM_LOG_SETTINGS="$QM_LOG_BASE/settings"
|
||||
QM_LOG_SYSTEM="$QM_LOG_BASE/system"
|
||||
|
||||
# Log levels
|
||||
QM_LOG_ERROR="ERROR"
|
||||
QM_LOG_WARN="WARN"
|
||||
QM_LOG_INFO="INFO"
|
||||
QM_LOG_DEBUG="DEBUG"
|
||||
|
||||
# Maximum log file size (in KB) - keep small for OpenWrt
|
||||
QM_LOG_MAX_SIZE=500
|
||||
|
||||
# Initialize log directories
|
||||
qm_init_logs() {
|
||||
mkdir -p "$QM_LOG_DAEMONS" "$QM_LOG_SERVICES" "$QM_LOG_SETTINGS" "$QM_LOG_SYSTEM" 2>/dev/null || true
|
||||
}
|
||||
|
||||
# Get log file path based on category and script name
|
||||
qm_get_logfile() {
|
||||
local category="$1"
|
||||
local script_name="$2"
|
||||
|
||||
case "$category" in
|
||||
"daemon"|"daemons")
|
||||
echo "$QM_LOG_DAEMONS/${script_name}.log"
|
||||
;;
|
||||
"service"|"services")
|
||||
echo "$QM_LOG_SERVICES/${script_name}.log"
|
||||
;;
|
||||
"setting"|"settings")
|
||||
echo "$QM_LOG_SETTINGS/${script_name}.log"
|
||||
;;
|
||||
"system")
|
||||
echo "$QM_LOG_SYSTEM/${script_name}.log"
|
||||
;;
|
||||
*)
|
||||
echo "$QM_LOG_SYSTEM/unknown.log"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Simple log rotation - keep it OpenWrt compatible
|
||||
qm_rotate_log() {
|
||||
local logfile="$1"
|
||||
|
||||
if [ -f "$logfile" ]; then
|
||||
# Get file size in KB (use du for BusyBox compatibility)
|
||||
local size_kb=$(du -k "$logfile" 2>/dev/null | cut -f1)
|
||||
|
||||
if [ "${size_kb:-0}" -gt "$QM_LOG_MAX_SIZE" ]; then
|
||||
# Simple rotation: keep last 2 versions
|
||||
[ -f "${logfile}.1" ] && mv "${logfile}.1" "${logfile}.2" 2>/dev/null || true
|
||||
mv "$logfile" "${logfile}.1" 2>/dev/null || true
|
||||
touch "$logfile" 2>/dev/null || true
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Main logging function
|
||||
# Usage: qm_log "category" "script_name" "level" "message"
|
||||
qm_log() {
|
||||
local category="$1"
|
||||
local script_name="$2"
|
||||
local level="$3"
|
||||
local message="$4"
|
||||
|
||||
# Initialize if needed
|
||||
qm_init_logs
|
||||
|
||||
# Get log file path
|
||||
local logfile=$(qm_get_logfile "$category" "$script_name")
|
||||
|
||||
# Rotate if needed
|
||||
qm_rotate_log "$logfile"
|
||||
|
||||
# Create log entry with OpenWrt compatible date
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S' 2>/dev/null || date)
|
||||
local pid="$$"
|
||||
|
||||
# Write log entry
|
||||
printf '[%s] [%s] [%s] [PID:%s] %s\n' "$timestamp" "$level" "$script_name" "$pid" "$message" >> "$logfile" 2>/dev/null || true
|
||||
}
|
||||
|
||||
# Convenience functions for different log levels
|
||||
qm_log_error() {
|
||||
qm_log "$1" "$2" "$QM_LOG_ERROR" "$3"
|
||||
}
|
||||
|
||||
qm_log_warn() {
|
||||
qm_log "$1" "$2" "$QM_LOG_WARN" "$3"
|
||||
}
|
||||
|
||||
qm_log_info() {
|
||||
qm_log "$1" "$2" "$QM_LOG_INFO" "$3"
|
||||
}
|
||||
|
||||
qm_log_debug() {
|
||||
qm_log "$1" "$2" "$QM_LOG_DEBUG" "$3"
|
||||
}
|
||||
|
||||
# Cleanup old logs (called periodically)
|
||||
qm_cleanup_logs() {
|
||||
# Remove .2 backup files older than 1 day to save space
|
||||
find "$QM_LOG_BASE" -name "*.2" -type f -mtime +1 -delete 2>/dev/null || true
|
||||
}
|
||||
@@ -2,9 +2,6 @@
|
||||
# Updated QuecProfiles daemon with enhanced SA/NSA NR5G band management and TTL support
|
||||
# Including profile application functions and fixed comparison logic
|
||||
|
||||
# Load centralized logging
|
||||
. /www/cgi-bin/services/quecmanager_logger.sh
|
||||
|
||||
# Configuration
|
||||
QUEUE_DIR="/tmp/at_queue"
|
||||
TOKEN_FILE="$QUEUE_DIR/token"
|
||||
@@ -12,39 +9,33 @@ TRACK_FILE="/tmp/quecprofiles_active"
|
||||
CHECK_TRIGGER="/tmp/quecprofiles_check"
|
||||
STATUS_FILE="/tmp/quecprofiles_status.json"
|
||||
APPLIED_FLAG="/tmp/quecprofiles_applied"
|
||||
DEBUG_LOG="/tmp/quecprofiles_debug.log"
|
||||
DETAILED_LOG="/tmp/quecprofiles_detailed.log"
|
||||
DEFAULT_CHECK_INTERVAL=60 # Default check interval in seconds
|
||||
COMMAND_TIMEOUT=10 # Default timeout for AT commands in seconds
|
||||
QUEUE_PRIORITY=3 # Medium-high priority (1 is highest for cell scan)
|
||||
MAX_TOKEN_WAIT=15 # Maximum seconds to wait for token acquisition
|
||||
SCRIPT_NAME="quecprofile"
|
||||
|
||||
# Initialize logging
|
||||
qm_log_info "service" "$SCRIPT_NAME" "Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)"
|
||||
# Initialize log file
|
||||
echo "$(date) - Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)" >"$DEBUG_LOG"
|
||||
echo "$(date) - Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)" >"$DETAILED_LOG"
|
||||
chmod 644 "$DEBUG_LOG" "$DETAILED_LOG"
|
||||
|
||||
# Function to log messages
|
||||
log_message() {
|
||||
local message="$1"
|
||||
local level="${2:-info}"
|
||||
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Use centralized logging
|
||||
case "$level" in
|
||||
"error")
|
||||
qm_log_error "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
"warn")
|
||||
qm_log_warn "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
"debug")
|
||||
qm_log_debug "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
*)
|
||||
qm_log_info "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
esac
|
||||
# Log to system log
|
||||
logger -t quecprofiles_daemon -p "daemon.$level" "$message"
|
||||
|
||||
# Also log to system log for important messages
|
||||
if [ "$level" = "error" ] || [ "$level" = "warn" ] || [ "$level" = "info" ]; then
|
||||
logger -t quecprofiles_daemon -p "daemon.$level" "$message"
|
||||
# Log to debug file
|
||||
echo "[$timestamp] [$level] $message" >>"$DEBUG_LOG"
|
||||
|
||||
# For detailed logs or errors
|
||||
if [ "$level" = "error" ] || [ "$level" = "debug" ]; then
|
||||
echo "[$timestamp] [$level] $message" >>"$DETAILED_LOG"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
@@ -6,22 +6,20 @@
|
||||
# Load UCI configuration functions
|
||||
. /lib/functions.sh
|
||||
|
||||
# Load centralized logging
|
||||
. /www/cgi-bin/services/quecmanager_logger.sh
|
||||
|
||||
# Configuration
|
||||
QUEUE_DIR="/tmp/at_queue"
|
||||
TOKEN_FILE="$QUEUE_DIR/token"
|
||||
LOG_DIR="/tmp/log/quecwatch"
|
||||
LOG_FILE="$LOG_DIR/quecwatch.log"
|
||||
PID_FILE="/var/run/quecwatch.pid"
|
||||
STATUS_FILE="/tmp/quecwatch_status.json"
|
||||
RETRY_COUNT_FILE="/tmp/quecwatch_retry_count"
|
||||
UCI_CONFIG="quecmanager"
|
||||
MAX_TOKEN_WAIT=10 # Maximum seconds to wait for token acquisition
|
||||
TOKEN_PRIORITY=15 # Medium priority (between profiles and metrics)
|
||||
SCRIPT_NAME="quecwatch"
|
||||
|
||||
# Ensure directories exist
|
||||
mkdir -p "$QUEUE_DIR"
|
||||
mkdir -p "$LOG_DIR" "$QUEUE_DIR"
|
||||
|
||||
# Store PID
|
||||
echo "$$" > "$PID_FILE"
|
||||
@@ -31,27 +29,13 @@ chmod 644 "$PID_FILE"
|
||||
log_message() {
|
||||
local level="${2:-info}"
|
||||
local message="$1"
|
||||
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Use centralized logging
|
||||
case "$level" in
|
||||
"error")
|
||||
qm_log_error "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
"warn")
|
||||
qm_log_warn "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
"debug")
|
||||
qm_log_debug "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
*)
|
||||
qm_log_info "service" "$SCRIPT_NAME" "$message"
|
||||
;;
|
||||
esac
|
||||
# Log to file
|
||||
echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
|
||||
|
||||
# Also log to system log for important messages
|
||||
if [ "$level" = "error" ] || [ "$level" = "warn" ] || [ "$level" = "info" ]; then
|
||||
logger -t quecwatch -p "daemon.$level" "$message"
|
||||
fi
|
||||
# Log to system log
|
||||
logger -t quecwatch -p "daemon.$level" "$message"
|
||||
}
|
||||
|
||||
# Function to update status
|
||||
|
||||
Reference in New Issue
Block a user