From e1ca3c0cea9d0c749a5e189f5457c9be687e49d50a336d0ec77054a20c2e4e7b Mon Sep 17 00:00:00 2001 From: larssand Date: Thu, 18 Jun 2026 22:06:16 +0200 Subject: [PATCH] add sudo --- README.md | 12 ++++++++++++ start.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c81fddb..006f686 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,18 @@ Or use the helper script, which creates/uses `.venv` automatically and runs `pip ./start.sh stop ``` +The script activates `.venv` inside the script process. If you also want your current shell prompt to show the venv, run: + +```bash +source .venv/bin/activate +``` + +For UDP `514`, the script starts only the listener command with `sudo`: + +```bash +FGAI_SYSLOG_PORT=514 ./start.sh +``` + Analyze local logs: ```bash diff --git a/start.sh b/start.sh index 8150d80..93e76bd 100755 --- a/start.sh +++ b/start.sh @@ -19,19 +19,39 @@ usage() { printf ' FGAI_LISTENER_LOG=%s\n' "$LISTENER_LOG" } +activate_venv_for_script() { + # Activation here affects this script process. It cannot modify the parent shell prompt. + # Use "source .venv/bin/activate" manually if you want your interactive prompt to show it. + # shellcheck disable=SC1091 + source "$VENV_DIR/bin/activate" +} + install_deps() { if [ ! -x "$VENV_DIR/bin/python" ]; then printf 'Creating venv: %s\n' "$VENV_DIR" python3 -m venv "$VENV_DIR" fi + activate_venv_for_script printf 'Installing/updating fgAI dependencies...\n' - "$VENV_DIR/bin/python" -m pip install --upgrade pip - "$VENV_DIR/bin/python" -m pip install -e "$ROOT_DIR" + python -m pip install --upgrade pip + python -m pip install -e "$ROOT_DIR" } is_running() { - [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null + [ -f "$PID_FILE" ] && ps -p "$(cat "$PID_FILE")" >/dev/null 2>&1 +} + +needs_privileged_port() { + [ "$PORT" -lt 1024 ] && [ "${EUID:-$(id -u)}" -ne 0 ] +} + +listener_command() { + "$VENV_DIR/bin/fgai" listen-syslog \ + --host "$HOST" \ + --port "$PORT" \ + --output "$LOG_FILE" \ + --quiet } start_listener() { @@ -44,11 +64,20 @@ start_listener() { return 0 fi - nohup "$VENV_DIR/bin/fgai" listen-syslog \ - --host "$HOST" \ - --port "$PORT" \ - --output "$LOG_FILE" \ - --quiet > "$LISTENER_LOG" 2>&1 & + if needs_privileged_port; then + printf 'Port %s needs elevated privileges; starting listener with sudo.\n' "$PORT" + sudo nohup "$VENV_DIR/bin/fgai" listen-syslog \ + --host "$HOST" \ + --port "$PORT" \ + --output "$LOG_FILE" \ + --quiet > "$LISTENER_LOG" 2>&1 & + else + nohup "$VENV_DIR/bin/fgai" listen-syslog \ + --host "$HOST" \ + --port "$PORT" \ + --output "$LOG_FILE" \ + --quiet > "$LISTENER_LOG" 2>&1 & + fi printf '%s\n' "$!" > "$PID_FILE" printf 'Started fgAI syslog listener, pid %s\n' "$(cat "$PID_FILE")" @@ -64,7 +93,9 @@ stop_listener() { return 0 fi - kill "$(cat "$PID_FILE")" + if ! kill "$(cat "$PID_FILE")" 2>/dev/null; then + sudo kill "$(cat "$PID_FILE")" + fi rm -f "$PID_FILE" printf 'Stopped fgAI syslog listener\n' } @@ -74,6 +105,9 @@ status_listener() { printf 'fgAI syslog listener running, pid %s\n' "$(cat "$PID_FILE")" printf 'Input: udp://%s:%s\n' "$HOST" "$PORT" printf 'Syslog file: %s\n' "$LOG_FILE" + if command -v ss >/dev/null 2>&1; then + ss -lunp 2>/dev/null | awk -v port=":$PORT" '$0 ~ port {print}' + fi return 0 fi @@ -103,7 +137,7 @@ case "$command" in ;; analyze) install_deps - "$VENV_DIR/bin/fgai" analyze-logs --logs "$LOG_FILE" + fgai analyze-logs --logs "$LOG_FILE" ;; install) install_deps