Skip to content

Instantly share code, notes, and snippets.

@jdavidrcamacho
Last active December 16, 2025 10:38
Show Gist options
  • Select an option

  • Save jdavidrcamacho/47f58cfbae40c38cd6d2a3638266cfa3 to your computer and use it in GitHub Desktop.

Select an option

Save jdavidrcamacho/47f58cfbae40c38cd6d2a3638266cfa3 to your computer and use it in GitHub Desktop.
# ============================================================
COMMANDS_LOG="/var/log/commands.log"
OUTPUTS_LOG="/var/log/outputs.log"
if [[ $- == *i* ]] && [ -t 1 ]; then
# 1) OUTPUT logging
if [ -z "${BASH_OUTPUT_LOGGER_SET:-}" ]; then
export BASH_OUTPUT_LOGGER_SET=1
exec /usr/bin/script -q -f -a "$OUTPUTS_LOG" -c "bash" /dev/null
fi
# 2) COMMAND logging
if [ -z "${BASH_COMMAND_LOGGER_SET:-}" ]; then
export BASH_COMMAND_LOGGER_SET=1
shopt -s histappend
export HISTTIMEFORMAT="%F %T "
__fpe_log_last_command() {
local LOG_HOST LOG_IP LAST_CMD
LOG_HOST="$(hostname)"
LOG_IP="$(hostname -I 2>/dev/null | awk '{print $1}')"
LAST_CMD="$(HISTTIMEFORMAT= history 1 | sed 's/^ *[0-9]\+ *//')"
printf "%s host=%q ip=%q user=%q cmd=%q\n" \
"$(date --iso-8601=seconds)" "$LOG_HOST" "$LOG_IP" "$USER" "$LAST_CMD" \
>> "$COMMANDS_LOG"
history -a
}
if [ -n "${PROMPT_COMMAND:-}" ]; then
PROMPT_COMMAND="__fpe_log_last_command; ${PROMPT_COMMAND}"
else
PROMPT_COMMAND="__fpe_log_last_command"
fi
fi
fi
# ============================================================
<system>
log_level info
</system>
# Commands typed
<source>
@type tail
path /var/log/commands.log
pos_file /var/log/fluentd/commands.pos
tag shell_commands
<parse>
@type none
</parse>
read_from_head true
</source>
# Terminal output
<source>
@type tail
path /var/log/outputs.log
pos_file /var/log/fluentd/outputs.pos
tag shell_outputs
<parse>
@type none
</parse>
read_from_head true
</source>
# Clean ANSI escape sequences in outputs
<filter shell_outputs>
@type record_transformer
enable_ruby true
<record>
output_clean ${record["message"].gsub(/\e\[[0-9;?]*[A-Za-z]/, "")}
</record>
</filter>
# Send both streams to stdout + forward
<match shell_commands shell_outputs>
@type copy
<store>
@type stdout
</store>
<store>
@type forward
<server>
host 10.50.20.9
port 24224
</server>
<buffer>
@type memory
flush_mode interval
flush_interval 1s
flush_thread_count 2
chunk_limit_size 256k
retry_forever true
retry_max_interval 5
</buffer>
</store>
</match>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment