Update QuecManager beta to 0.0.8
- Various edits - Addition of QuecWatch for connection monitoring Co-Authored-By: Russel Yasol <73575327+dr-dolomite@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Configuration and log directories
|
||||
CONFIG_DIR="/etc/quecmanager/quecwatch"
|
||||
QUECWATCH_SCRIPT="${CONFIG_DIR}/quecwatch.sh"
|
||||
RCLOCAL="/etc/rc.local"
|
||||
LOG_DIR="/tmp/log/quecwatch"
|
||||
DEBUG_LOG_FILE="${LOG_DIR}/debug.log"
|
||||
|
||||
# Log directory for cleaning process
|
||||
CLEANUP_LOG_FILE="${LOG_DIR}/cleanup.log"
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "${LOG_DIR}"
|
||||
|
||||
# Function to log cleanup events
|
||||
log_cleanup() {
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "${CLEANUP_LOG_FILE}"
|
||||
}
|
||||
|
||||
# Default response headers
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Cleanup function
|
||||
cleanup_quecwatch() {
|
||||
# Start logging cleanup process
|
||||
log_cleanup "Starting QuecWatch cleanup process"
|
||||
|
||||
# Stop any running QuecWatch processes
|
||||
log_cleanup "Stopping QuecWatch processes"
|
||||
pkill -f "${QUECWATCH_SCRIPT}" >> "${CLEANUP_LOG_FILE}" 2>&1
|
||||
|
||||
# Remove QuecWatch script from rc.local
|
||||
if [ -f "${RCLOCAL}" ]; then
|
||||
log_cleanup "Removing QuecWatch entries from rc.local"
|
||||
sed -i '\|/etc/quecmanager/quecwatch/quecwatch.sh|d' "${RCLOCAL}" >> "${CLEANUP_LOG_FILE}" 2>&1
|
||||
fi
|
||||
|
||||
# Remove configuration directory
|
||||
if [ -d "${CONFIG_DIR}" ]; then
|
||||
log_cleanup "Removing configuration directory: ${CONFIG_DIR}"
|
||||
rm -rf "${CONFIG_DIR}" >> "${CLEANUP_LOG_FILE}" 2>&1
|
||||
fi
|
||||
|
||||
# Remove log directory
|
||||
if [ -d "${LOG_DIR}" ]; then
|
||||
log_cleanup "Removing log directory: ${LOG_DIR}"
|
||||
rm -rf "${LOG_DIR}" >> "${CLEANUP_LOG_FILE}" 2>&1
|
||||
fi
|
||||
|
||||
log_cleanup "QuecWatch cleanup completed successfully"
|
||||
|
||||
# Optional: Output JSON response
|
||||
echo '{"status": "success", "message": "QuecWatch disabled and removed"}'
|
||||
}
|
||||
|
||||
# Execute cleanup
|
||||
cleanup_quecwatch
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,214 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Read POST data
|
||||
read -r QUERY_STRING
|
||||
|
||||
# Function to urldecode
|
||||
urldecode() {
|
||||
echo -e "$(echo "$1" | sed 's/+/ /g;s/%\([0-9A-F][0-9A-F]\)/\\x\1/g')"
|
||||
}
|
||||
|
||||
# Configuration directory
|
||||
CONFIG_DIR="/etc/quecmanager/quecwatch"
|
||||
QUECWATCH_CONFIG="${CONFIG_DIR}/quecwatch.conf"
|
||||
QUECWATCH_SCRIPT="${CONFIG_DIR}/quecwatch.sh"
|
||||
RCLOCAL="/etc/rc.local"
|
||||
LOG_DIR="/tmp/log/quecwatch"
|
||||
DEBUG_LOG_FILE="${LOG_DIR}/debug.log"
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "${LOG_DIR}"
|
||||
|
||||
# Extract values from POST data
|
||||
action=$(echo "$QUERY_STRING" | grep -o 'action=[^&]*' | cut -d= -f2)
|
||||
ping_target=$(echo "$QUERY_STRING" | grep -o 'ping_target=[^&]*' | cut -d= -f2)
|
||||
ping_interval=$(echo "$QUERY_STRING" | grep -o 'ping_interval=[^&]*' | cut -d= -f2)
|
||||
ping_failures=$(echo "$QUERY_STRING" | grep -o 'ping_failures=[^&]*' | cut -d= -f2)
|
||||
max_retries=$(echo "$QUERY_STRING" | grep -o 'max_retries=[^&]*' | cut -d= -f2)
|
||||
connection_refresh=$(echo "$QUERY_STRING" | grep -o 'connection_refresh=[^&]*' | cut -d= -f2)
|
||||
|
||||
# URL decode the values
|
||||
action=$(urldecode "$action")
|
||||
ping_target=$(urldecode "$ping_target")
|
||||
ping_interval=$(urldecode "$ping_interval")
|
||||
ping_failures=$(urldecode "$ping_failures")
|
||||
max_retries=$(urldecode "$max_retries")
|
||||
connection_refresh=$(urldecode "$connection_refresh")
|
||||
|
||||
# Default response headers
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Validate inputs
|
||||
if [ -z "$ping_target" ]; then
|
||||
echo '{"status": "error", "message": "Ping target is required"}'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Initialize configuration function
|
||||
initialize_config() {
|
||||
# Create config directory if not exists
|
||||
mkdir -p "${CONFIG_DIR}"
|
||||
|
||||
# Write configuration with defaults
|
||||
cat >"${QUECWATCH_CONFIG}" <<EOL
|
||||
PING_TARGET=${ping_target}
|
||||
PING_INTERVAL=${ping_interval:-30}
|
||||
PING_FAILURES=${ping_failures:-3}
|
||||
MAX_RETRIES=${max_retries:-5}
|
||||
CONNECTION_REFRESH=${connection_refresh:-false}
|
||||
CURRENT_RETRIES=0
|
||||
REFRESH_COUNT=${connection_refresh:+1}
|
||||
ENABLED=true
|
||||
EOL
|
||||
|
||||
chmod 644 "${QUECWATCH_CONFIG}"
|
||||
}
|
||||
|
||||
# Generate monitoring script function
|
||||
generate_monitoring_script() {
|
||||
cat >"${QUECWATCH_SCRIPT}" <<'EOL'
|
||||
#!/bin/sh
|
||||
|
||||
# Load configuration
|
||||
. /etc/quecmanager/quecwatch/quecwatch.conf
|
||||
|
||||
# Log directory
|
||||
LOG_DIR="/tmp/log/quecwatch"
|
||||
mkdir -p "${LOG_DIR}"
|
||||
|
||||
# Function to log events
|
||||
log_event() {
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "${LOG_DIR}/quecwatch.log"
|
||||
}
|
||||
|
||||
# Function to update retry count in config
|
||||
update_retry_count() {
|
||||
local new_retry_count=$1
|
||||
sed -i "s/CURRENT_RETRIES=[0-9]*/CURRENT_RETRIES=${new_retry_count}/" /etc/quecmanager/quecwatch/quecwatch.conf
|
||||
# Reload config to ensure latest values
|
||||
. /etc/quecmanager/quecwatch/quecwatch.conf
|
||||
}
|
||||
|
||||
# Initialize failure and retry counters
|
||||
failure_count=0
|
||||
retry_trigger=0
|
||||
|
||||
# Ensure CURRENT_RETRIES starts at 0
|
||||
update_retry_count 0
|
||||
|
||||
while true; do
|
||||
# Ping the target
|
||||
if ! ping -c ${PING_FAILURES} ${PING_TARGET} > /dev/null 2>&1; then
|
||||
failure_count=$((failure_count + 1))
|
||||
log_event "Ping failed. Failure count: ${failure_count}"
|
||||
|
||||
# Check if failure threshold is reached
|
||||
if [ ${failure_count} -ge ${PING_FAILURES} ]; then
|
||||
# Reset failure count
|
||||
failure_count=0
|
||||
retry_trigger=$((retry_trigger + 1))
|
||||
|
||||
# Update retry count in config
|
||||
update_retry_count ${retry_trigger}
|
||||
|
||||
log_event "Failure threshold reached. Retry trigger: ${retry_trigger}"
|
||||
|
||||
# Check if retry threshold is reached
|
||||
if [ ${retry_trigger} -ge ${MAX_RETRIES} ]; then
|
||||
log_event "Max retries exhausted. Removing QuecWatch."
|
||||
|
||||
# Remove the script from rc.local
|
||||
sed -i '\|/etc/quecmanager/quecwatch/quecwatch.sh|d' /etc/rc.local
|
||||
|
||||
# Perform final system reboot
|
||||
reboot
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Connection refresh logic
|
||||
if [ "${CONNECTION_REFRESH}" = "true" ] && [ "${REFRESH_COUNT}" -gt 0 ]; then
|
||||
# Decrement refresh count
|
||||
. /etc/quecmanager/quecwatch/quecwatch.conf
|
||||
REFRESH_COUNT=$((REFRESH_COUNT - 1))
|
||||
|
||||
# Update config
|
||||
sed -i "s/REFRESH_COUNT=.*/REFRESH_COUNT=${REFRESH_COUNT}/" /etc/quecmanager/quecwatch/quecwatch.conf
|
||||
|
||||
log_event "Attempting connection refresh"
|
||||
# Add your modem connection refresh command here
|
||||
echo AT+COPS=2 | atinout - /dev/smd7 -
|
||||
sleep 2
|
||||
echo AT+COPS=0 | atinout - /dev/smd7 -
|
||||
|
||||
# Verify connection after refresh
|
||||
if ! ping -c 3 ${PING_TARGET} > /dev/null 2>&1; then
|
||||
log_event "Connection refresh failed. Continuing with retry process."
|
||||
# Continue with retry process, no premature reboot or script removal
|
||||
else
|
||||
log_event "Connection refresh successful"
|
||||
# Reset retry trigger if connection is restored
|
||||
retry_trigger=0
|
||||
failure_count=0
|
||||
update_retry_count 0
|
||||
fi
|
||||
else
|
||||
# Perform modem reboot
|
||||
log_event "Rebooting modem. Retry attempts: ${retry_trigger}"
|
||||
# Add your modem reboot command here
|
||||
reboot
|
||||
fi
|
||||
fi
|
||||
else
|
||||
# Reset failure count and retry trigger if connection is good
|
||||
log_event "Modem is connected to the internet"
|
||||
failure_count=0
|
||||
retry_trigger=0
|
||||
update_retry_count 0
|
||||
fi
|
||||
|
||||
# Wait for specified interval before next check
|
||||
sleep ${PING_INTERVAL}
|
||||
done
|
||||
EOL
|
||||
|
||||
chmod +x "${QUECWATCH_SCRIPT}"
|
||||
|
||||
# Run the script
|
||||
"${QUECWATCH_SCRIPT}" &
|
||||
}
|
||||
|
||||
# Enable QuecWatch
|
||||
enable_quecwatch() {
|
||||
# Initialize configuration
|
||||
initialize_config
|
||||
|
||||
# Generate monitoring script
|
||||
generate_monitoring_script
|
||||
|
||||
# Add to rc.local if not already present
|
||||
if ! grep -q "${QUECWATCH_SCRIPT}" "${RCLOCAL}"; then
|
||||
[ -f "${RCLOCAL}" ] || touch "${RCLOCAL}"
|
||||
chmod +x "${RCLOCAL}"
|
||||
sed -i '$i'"${QUECWATCH_SCRIPT} &" "${RCLOCAL}"
|
||||
fi
|
||||
|
||||
# Output success JSON
|
||||
echo '{"status": "success", "message": "QuecWatch enabled", "config": "'${QUECWATCH_CONFIG}'"}'
|
||||
}
|
||||
|
||||
# Log debug information
|
||||
{
|
||||
echo "Timestamp: $(date)"
|
||||
echo "Script Path: $0"
|
||||
echo "Ping Target: $ping_target"
|
||||
echo "Ping Interval: $ping_interval"
|
||||
echo "Ping Failures: $ping_failures"
|
||||
echo "Max Retries: $max_retries"
|
||||
echo "Connection Refresh: $connection_refresh"
|
||||
} >>"$DEBUG_LOG_FILE" 2>&1
|
||||
|
||||
# Enable QuecWatch
|
||||
enable_quecwatch
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,66 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Set headers for JSON response
|
||||
echo "Content-type: application/json"
|
||||
echo ""
|
||||
|
||||
# Configuration file path
|
||||
CONFIG_FILE="/etc/quecmanager/quecwatch/quecwatch.conf"
|
||||
|
||||
# Check if configuration file exists
|
||||
if [ ! -f "$CONFIG_FILE" ]; then
|
||||
echo '{"status": "inactive", "message": "QuecWatch is not configured"}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Function to safely get config value
|
||||
get_config_value() {
|
||||
grep "^$1=" "$CONFIG_FILE" | cut -d'=' -f2
|
||||
}
|
||||
|
||||
# Check if QuecWatch is enabled
|
||||
enabled=$(get_config_value "ENABLED")
|
||||
if [ "$enabled" != "true" ]; then
|
||||
echo '{"status": "inactive", "message": "QuecWatch is disabled"}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Fetch configuration values
|
||||
ping_target=$(get_config_value "PING_TARGET")
|
||||
ping_interval=$(get_config_value "PING_INTERVAL")
|
||||
ping_failures=$(get_config_value "PING_FAILURES")
|
||||
max_retries=$(get_config_value "MAX_RETRIES")
|
||||
current_retries=$(get_config_value "CURRENT_RETRIES")
|
||||
connection_refresh=$(get_config_value "CONNECTION_REFRESH")
|
||||
refresh_count=$(get_config_value "REFRESH_COUNT")
|
||||
|
||||
# Check monitoring script existence
|
||||
QUECWATCH_SCRIPT="/etc/quecmanager/quecwatch/quecwatch.sh"
|
||||
if [ ! -f "$QUECWATCH_SCRIPT" ]; then
|
||||
echo '{"status": "error", "message": "Monitoring script is missing"}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Check log file for recent activity
|
||||
LOG_FILE="/tmp/log/quecwatch/quecwatch.log"
|
||||
last_log=""
|
||||
if [ -f "$LOG_FILE" ]; then
|
||||
last_log=$(tail -n 1 "$LOG_FILE")
|
||||
fi
|
||||
|
||||
# Prepare JSON response
|
||||
cat <<EOF
|
||||
{
|
||||
"status": "active",
|
||||
"config": {
|
||||
"pingTarget": "${ping_target}",
|
||||
"pingInterval": ${ping_interval},
|
||||
"pingFailures": ${ping_failures},
|
||||
"maxRetries": ${max_retries},
|
||||
"currentRetries": ${current_retries},
|
||||
"connectionRefresh": ${connection_refresh},
|
||||
"refreshCount": ${refresh_count:-0}
|
||||
},
|
||||
"lastActivity": "${last_log}"
|
||||
}
|
||||
EOF
|
||||
31
ipk-source/sdxpinn-quecmanager-beta/root/www/cgi-bin/speedtest.sh
Executable file
31
ipk-source/sdxpinn-quecmanager-beta/root/www/cgi-bin/speedtest.sh
Executable file
@@ -0,0 +1,31 @@
|
||||
#!/bin/sh
|
||||
|
||||
SPEEDTEST_OUTPUT=$(speedtest)
|
||||
|
||||
SERVER=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'Server: [^(]' | cut -d':' -f2 | tr -d ' ')
|
||||
SERVER_ID=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'id: [0-9]' | cut -d':' -f2 | tr -d ' ')
|
||||
ISP=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'ISP: [^(]' | cut -d':' -f2 | tr -d ' ')
|
||||
IDLE_LATENCY=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'Idle Latency: [0-9.] ms' | cut -d':' -f2 | tr -d ' ms')
|
||||
DOWNLOAD_LATENCY=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'Download: [0-9.]* ms' | cut -d':' -f2 | tr -d ' ms')
|
||||
DOWNLOAD_SPEED=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'Download: [0-9.]* Mbps' | cut -d':' -f2 | tr -d ' Mbps')
|
||||
DOWNLOAD_DATA_USED=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'data used: [0-9.]* MB' | cut -d':' -f2 | tr -d ' MB')
|
||||
UPLOAD_LATENCY=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'Upload: [0-9.]* ms' | cut -d':' -f2 | tr -d ' ms')
|
||||
UPLOAD_SPEED=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'Upload: [0-9.]* Mbps' | cut -d':' -f2 | tr -d ' Mbps')
|
||||
UPLOAD_DATA_USED=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'data used: [0-9.]* MB' | cut -d':' -f2 | tr -d ' MB')
|
||||
RESULT_URL=$(echo "$SPEEDTEST_OUTPUT" | grep -o -E 'Result URL: [^.]*' | cut -d':' -f2 | tr -d ' ')
|
||||
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
echo "{
|
||||
"server": "$SERVER",
|
||||
"serverId": "$SERVER_ID",
|
||||
"isp": "$ISP",
|
||||
"idleLatency": $IDLE_LATENCY,
|
||||
"downloadLatency": $DOWNLOAD_LATENCY,
|
||||
"downloadSpeed": $DOWNLOAD_SPEED,
|
||||
"downloadDataUsed": $DOWNLOAD_DATA_USED,
|
||||
"uploadLatency": $UPLOAD_LATENCY,
|
||||
"uploadSpeed": $UPLOAD_SPEED,
|
||||
"uploadDataUsed": $UPLOAD_DATA_USED,
|
||||
"resultUrl": "$RESULT_URL"
|
||||
}"
|
||||
Reference in New Issue
Block a user