Scanning Operational with monitoring

This commit is contained in:
Adam Outler
2025-09-30 22:01:03 -04:00
parent 044035ef62
commit 0cd1dc8987
15 changed files with 739 additions and 116 deletions

View File

@@ -1,28 +1,88 @@
#!/bin/bash
#!/bin/sh
set -u
# verify container capabilities at startup
/services/capcheck.sh
# Function to clean up background processes
cleanup() {
echo "Caught signal, shutting down services..."
# Kill all background jobs
kill $(jobs -p)
wait
echo "All services stopped."
exit 0
SERVICES=""
FAILED_NAME=""
FAILED_STATUS=0
add_service() {
script="$1"
name="$2"
"$script" &
pid=$!
SERVICES="${SERVICES} ${pid}:${name}"
}
# Trap SIGINT (Ctrl+C) and SIGTERM (docker stop)
trap cleanup SIGINT SIGTERM
remove_service() {
target_pid="$1"
updated=""
for entry in ${SERVICES}; do
pid="${entry%%:*}"
[ -z "${pid}" ] && continue
[ "${pid}" = "${target_pid}" ] && continue
updated="${updated} ${entry}"
done
SERVICES="${updated}"
}
# Start all necessary services for NetAlertX in the background
/services/start-crond.sh &
/services/start-php-fpm.sh &
/services/start-nginx.sh &
/services/start-backend.sh &
shutdown_services() {
for entry in ${SERVICES}; do
pid="${entry%%:*}"
[ -z "${pid}" ] && continue
if kill -0 "${pid}" 2>/dev/null; then
kill "${pid}" 2>/dev/null || true
fi
done
for entry in ${SERVICES}; do
pid="${entry%%:*}"
[ -z "${pid}" ] && continue
wait "${pid}" 2>/dev/null || true
done
echo "All services stopped."
}
# Wait for any background process to exit
wait -n
# Trigger cleanup if any process exits
cleanup
handle_exit() {
if [ -n "${FAILED_NAME}" ]; then
echo "Service ${FAILED_NAME} exited with status ${FAILED_STATUS}."
fi
shutdown_services
exit "${FAILED_STATUS}"
}
on_signal() {
echo "Caught signal, shutting down services..."
FAILED_NAME="signal"
FAILED_STATUS=143
handle_exit
}
trap on_signal INT TERM
[ ! -d "${NETALERTX_PLUGINS_LOG}" ] && mkdir -p "${NETALERTX_PLUGINS_LOG}"
[ ! -f "${LOG_DB_IS_LOCKED}" ] && touch "${LOG_DB_IS_LOCKED}"
[ ! -f "${LOG_EXECUTION_QUEUE}" ] && touch "${LOG_EXECUTION_QUEUE}"
add_service "/services/start-crond.sh" "crond"
add_service "/services/start-php-fpm.sh" "php-fpm"
add_service "/services/start-nginx.sh" "nginx"
add_service "/services/start-backend.sh" "backend"
while [ -n "${SERVICES}" ]; do
for entry in ${SERVICES}; do
pid="${entry%%:*}"
name="${entry#*:}"
[ -z "${pid}" ] && continue
if ! kill -0 "${pid}" 2>/dev/null; then
wait "${pid}" 2>/dev/null
status=$?
FAILED_STATUS=$status
FAILED_NAME="${name}"
remove_service "${pid}"
handle_exit
fi
done
sleep 1
done