Added cgi-bin scripts
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
#!/bin/sh
|
||||
|
||||
QUEUE_FILE="/tmp/at_pipe.txt"
|
||||
RESULT_FILE="/tmp/at_results.json"
|
||||
LOG_FILE="/var/log/at_commands.log"
|
||||
# Define all lock keywords
|
||||
FETCH_LOCK_KEYWORD="FETCH_DATA_LOCK"
|
||||
SIGNAL_LOCK_KEYWORD="SIGNAL_METRICS_LOCK"
|
||||
# Combine keywords for pattern matching
|
||||
ALL_LOCK_KEYWORDS="${FETCH_LOCK_KEYWORD}\\|${SIGNAL_LOCK_KEYWORD}"
|
||||
|
||||
# Create or clear necessary files
|
||||
touch "${QUEUE_FILE}"
|
||||
[ ! -f "${RESULT_FILE}" ] && echo '[]' > "${RESULT_FILE}"
|
||||
|
||||
# Log messages to the log file
|
||||
log_message() {
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "${LOG_FILE}"
|
||||
}
|
||||
|
||||
# Escape special characters for JSON
|
||||
escape_json() {
|
||||
echo "$1" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g'
|
||||
}
|
||||
|
||||
# Function to check if any lock is present
|
||||
is_system_locked() {
|
||||
grep -q "\"command\":\"\\(${ALL_LOCK_KEYWORDS}\\)\"" "${QUEUE_FILE}"
|
||||
return $?
|
||||
}
|
||||
|
||||
# Process a single command
|
||||
process_command() {
|
||||
local command="$1"
|
||||
local timestamp="$2"
|
||||
local cmd_id="$3"
|
||||
|
||||
log_message "Processing command: ${command} (ID: ${cmd_id})"
|
||||
|
||||
# Check if sms_tool exists and is executable
|
||||
if ! which sms_tool >/dev/null 2>&1; then
|
||||
log_message "Error: sms_tool not found in PATH"
|
||||
result="sms_tool not found"
|
||||
exit_code=1
|
||||
else
|
||||
# Execute the AT command using sms_tool
|
||||
result=$(sms_tool at "${command}" 2>&1)
|
||||
exit_code=$?
|
||||
log_message "Command output: ${result}"
|
||||
log_message "Exit code: ${exit_code}"
|
||||
fi
|
||||
|
||||
# Escape the command and result for JSON
|
||||
escaped_command=$(escape_json "${command}")
|
||||
escaped_result=$(echo "${result}" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g' | tr -d '\r')
|
||||
|
||||
# Generate the result JSON
|
||||
if [ ${exit_code} -eq 0 ]; then
|
||||
log_message "Command successful: ${command}"
|
||||
RESULT_JSON=$(printf '{"id":"%s","status":"success","command":"%s","response":"%s","queued_at":"%s","executed_at":"%s"}' \
|
||||
"${cmd_id}" "${escaped_command}" "${escaped_result}" "${timestamp}" "$(date '+%H:%M:%S')")
|
||||
else
|
||||
log_message "Command failed: ${command}"
|
||||
RESULT_JSON=$(printf '{"id":"%s","status":"error","command":"%s","error":"%s","queued_at":"%s","executed_at":"%s"}' \
|
||||
"${cmd_id}" "${escaped_command}" "${escaped_result}" "${timestamp}" "$(date '+%H:%M:%S')")
|
||||
fi
|
||||
|
||||
# Update the results file safely
|
||||
if ! current_results=$(cat "${RESULT_FILE}" 2>/dev/null); then
|
||||
log_message "Error reading results file, initializing new one"
|
||||
echo '[]' > "${RESULT_FILE}"
|
||||
current_results='[]'
|
||||
fi
|
||||
|
||||
# Append the result JSON to the results file
|
||||
if ! echo "${current_results}" | jq --argjson new "${RESULT_JSON}" '. + [$new]' > "${RESULT_FILE}.tmp"; then
|
||||
log_message "Error updating results file"
|
||||
return 1
|
||||
fi
|
||||
|
||||
mv "${RESULT_FILE}.tmp" "${RESULT_FILE}"
|
||||
log_message "Successfully updated results file"
|
||||
return ${exit_code}
|
||||
}
|
||||
|
||||
# Check if an entry is a lock entry
|
||||
is_lock_entry() {
|
||||
local line="$1"
|
||||
echo "${line}" | grep -q "\"command\":\"\\(${ALL_LOCK_KEYWORDS}\\)\""
|
||||
return $?
|
||||
}
|
||||
|
||||
# Process pending commands in the queue
|
||||
process_pending_commands() {
|
||||
while true; do
|
||||
# Check if any lock is present
|
||||
if is_system_locked; then
|
||||
local lock_type=$(grep -o "\"command\":\"[^\"]*\"" "${QUEUE_FILE}" | grep "${ALL_LOCK_KEYWORDS}")
|
||||
log_message "System is locked: ${lock_type}, waiting..."
|
||||
sleep 0.5
|
||||
continue
|
||||
fi
|
||||
|
||||
# Read the first line from the queue
|
||||
line=$(head -n 1 "${QUEUE_FILE}" 2>/dev/null)
|
||||
|
||||
if [ -n "${line}" ]; then
|
||||
log_message "Processing queue entry: ${line}"
|
||||
|
||||
# Skip processing if it's a lock entry
|
||||
if is_lock_entry "${line}"; then
|
||||
log_message "Found lock entry, skipping"
|
||||
sed -i '1d' "${QUEUE_FILE}"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Validate JSON before processing
|
||||
if ! echo "${line}" | jq empty 2>/dev/null; then
|
||||
log_message "Invalid JSON in queue, skipping line"
|
||||
sed -i '1d' "${QUEUE_FILE}"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Parse the command, timestamp, and ID from the JSON entry
|
||||
command=$(echo "${line}" | jq -r '.command // empty')
|
||||
timestamp=$(echo "${line}" | jq -r '.timestamp // empty')
|
||||
cmd_id=$(echo "${line}" | jq -r '.id // empty')
|
||||
|
||||
if [ -z "${command}" ] || [ -z "${timestamp}" ] || [ -z "${cmd_id}" ]; then
|
||||
log_message "Missing required fields in JSON, skipping"
|
||||
sed -i '1d' "${QUEUE_FILE}"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Process the command
|
||||
process_command "${command}" "${timestamp}" "${cmd_id}"
|
||||
|
||||
# Remove the processed line from the queue
|
||||
sed -i '1d' "${QUEUE_FILE}"
|
||||
|
||||
# Add a small delay between commands
|
||||
sleep 0.1
|
||||
else
|
||||
# No commands in queue, wait briefly before checking again
|
||||
sleep 0.5
|
||||
break
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Main queue monitoring loop
|
||||
process_queue() {
|
||||
log_message "Starting queue processor with multiple lock support"
|
||||
|
||||
while true; do
|
||||
# Process any pending commands
|
||||
process_pending_commands
|
||||
|
||||
# Wait for changes to the queue file
|
||||
inotifywait -q -e modify,create "${QUEUE_FILE}" >/dev/null 2>&1
|
||||
|
||||
# Small delay to allow file to stabilize
|
||||
sleep 0.1
|
||||
done
|
||||
}
|
||||
|
||||
# Start processing the queue
|
||||
log_message "Queue processor started with file monitoring and multiple lock support"
|
||||
process_queue
|
||||
@@ -0,0 +1,38 @@
|
||||
#!/bin/sh
|
||||
# Script for SMS initialization and initial fetch
|
||||
# Check if atinout and jq are installed
|
||||
if ! command -v atinout &> /dev/null || ! command -v jq &> /dev/null; then
|
||||
echo "Error: Required tools (atinout or jq) are not installed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if the device exists
|
||||
if [ ! -c "/dev/smd7" ]; then
|
||||
echo "Error: Device /dev/smd7 not found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Set SMS text mode
|
||||
if ! echo "AT+CMGF=1" | atinout - /dev/smd7 -; then
|
||||
echo "Error: Failed to set SMS text mode"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Wait for 2 seconds
|
||||
sleep 2
|
||||
|
||||
# Fetch initial SMS messages
|
||||
if ! echo "AT+CMGL=\"ALL\"" | atinout - /dev/smd7 - | jq -R -s '
|
||||
split("\n") |
|
||||
map(select(length > 0)) |
|
||||
map(
|
||||
select(startswith("+CMGL:") or (. != "OK" and . != "ERROR"))
|
||||
) |
|
||||
{messages: .}
|
||||
' > /tmp/sms_inbox.json; then
|
||||
echo "Error: Failed to fetch SMS messages"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Exit successfully
|
||||
exit 0
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Script path
|
||||
SCRIPT_PATH=$(readlink -f "$0")
|
||||
# Fix the spacing in the cron line to ensure exactly 5 fields
|
||||
CRON_LINE="0 0 * * * $SCRIPT_PATH"
|
||||
|
||||
# Install crontab if not already present
|
||||
if ! crontab -l | grep -Fq "$SCRIPT_PATH"; then
|
||||
# Get existing crontab - ensuring clean formatting
|
||||
(crontab -l 2>/dev/null | grep -v "$SCRIPT_PATH" || true; echo "$CRON_LINE") | crontab -
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
logger -t log_cleanup "Successfully installed crontab job"
|
||||
else
|
||||
logger -t log_cleanup "Failed to install crontab job"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Clean specified log files using echo redirection
|
||||
echo "" > /tmp/apn_profiles.log
|
||||
echo "" > /tmp/imei_profiles.log
|
||||
echo "" > /var/log/at_commands.log
|
||||
|
||||
# Add error handling
|
||||
if [ $? -ne 0 ]; then
|
||||
logger -t log_cleanup "Failed to clean one or more log files"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
logger -t log_cleanup "Successfully cleaned log files"
|
||||
@@ -0,0 +1,130 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Configuration
|
||||
LOGDIR="/www/signal_graphs"
|
||||
MAX_ENTRIES=10
|
||||
INTERVAL=60
|
||||
QUEUE_FILE="/tmp/at_pipe.txt"
|
||||
FETCH_LOCK_KEYWORD="FETCH_LOCK"
|
||||
PAUSE_FILE="/tmp/signal_logging.pause"
|
||||
|
||||
# Ensure the directory exists
|
||||
mkdir -p "$LOGDIR"
|
||||
|
||||
# Check for stale entries and clean them
|
||||
check_and_clean_stale() {
|
||||
local command_type="$1" # Either "FETCH_LOCK" or "AT_COMMAND"
|
||||
local wait_count=0
|
||||
|
||||
while [ $wait_count -lt 6 ]; do
|
||||
# Check if our type of entry exists
|
||||
if grep -q "\"command\":\"${command_type}\"" "$QUEUE_FILE"; then
|
||||
sleep 1
|
||||
wait_count=$((wait_count + 1))
|
||||
else
|
||||
# Entry is gone, we can proceed
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
# If we get here, entry is stale - remove it
|
||||
logger -t signal_metrics "Removing stale ${command_type} entry after ${wait_count}s"
|
||||
sed -i "/\"command\":\"${command_type}\"/d" "$QUEUE_FILE"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Simplified lock handling
|
||||
handle_lock() {
|
||||
# First check and clean any FETCH_LOCK entries
|
||||
check_and_clean_stale "FETCH_LOCK"
|
||||
|
||||
# Add our own entry
|
||||
printf '{"command":"AT_COMMAND","pid":"%s","timestamp":"%s"}\n' \
|
||||
"$$" \
|
||||
"$(date '+%H:%M:%S')" >>"$QUEUE_FILE"
|
||||
|
||||
# Then check and clean our own entry if it gets stuck
|
||||
check_and_clean_stale "AT_COMMAND"
|
||||
}
|
||||
|
||||
# Clean output function
|
||||
clean_output() {
|
||||
local output=""
|
||||
read -r line
|
||||
|
||||
while read -r line; do
|
||||
case "$line" in
|
||||
"OK" | "")
|
||||
continue
|
||||
;;
|
||||
*)
|
||||
if [ -n "$output" ]; then
|
||||
output="$output\\n$line"
|
||||
else
|
||||
output="$line"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
echo "$output"
|
||||
}
|
||||
|
||||
# Execute AT command
|
||||
execute_at_command() {
|
||||
local COMMAND="$1"
|
||||
handle_lock
|
||||
local OUTPUT=$(sms_tool at "$COMMAND" -t 4 2>/dev/null | clean_output)
|
||||
sed -i "/\"pid\":\"$$\"/d" "$QUEUE_FILE" # Remove our entry
|
||||
echo "$OUTPUT"
|
||||
}
|
||||
|
||||
# Log signal metric
|
||||
log_signal_metric() {
|
||||
[ -f "$PAUSE_FILE" ] && return
|
||||
|
||||
local COMMAND="$1"
|
||||
local FILENAME="$2"
|
||||
local LOGFILE="$LOGDIR/$FILENAME"
|
||||
|
||||
mkdir -p "$(dirname "$LOGFILE")"
|
||||
|
||||
local TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
|
||||
local SIGNAL_OUTPUT=$(execute_at_command "$COMMAND")
|
||||
|
||||
[ ! -s "$LOGFILE" ] && echo "[]" >"$LOGFILE"
|
||||
|
||||
if [ -n "$SIGNAL_OUTPUT" ]; then
|
||||
local TEMP_FILE="${LOGFILE}.tmp.$$"
|
||||
if jq --arg dt "$TIMESTAMP" \
|
||||
--arg out "$SIGNAL_OUTPUT" \
|
||||
'. + [{"datetime": $dt, "output": $out}] | .[-'"$MAX_ENTRIES"':]' \
|
||||
"$LOGFILE" >"$TEMP_FILE"; then
|
||||
mv "$TEMP_FILE" "$LOGFILE"
|
||||
else
|
||||
rm -f "$TEMP_FILE"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Main continuous logging function
|
||||
start_continuous_logging() {
|
||||
sleep 20
|
||||
logger -t signal_metrics "Starting continuous signal metrics logging (PID: $$)"
|
||||
|
||||
trap 'logger -t signal_metrics "Stopping signal metrics logging"; exit 0' INT TERM
|
||||
|
||||
while true; do
|
||||
if [ ! -f "$PAUSE_FILE" ]; then
|
||||
log_signal_metric "AT+QRSRP" "rsrp.json"
|
||||
log_signal_metric "AT+QRSRQ" "rsrq.json"
|
||||
log_signal_metric "AT+QSINR" "sinr.json"
|
||||
log_signal_metric "AT+QGDCNT?;+QGDNRCNT?" "data_usage.json"
|
||||
fi
|
||||
sleep "$INTERVAL"
|
||||
done
|
||||
}
|
||||
|
||||
# Start the continuous logging
|
||||
start_continuous_logging
|
||||
Reference in New Issue
Block a user