Skip to content

Commit

Permalink
do logging in a more container-friendly way
Browse files Browse the repository at this point in the history
  • Loading branch information
kx1t committed Oct 23, 2023
1 parent e900f63 commit ac25fdd
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 10 deletions.
8 changes: 1 addition & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ RUN set -x && \
TEMP_PACKAGES+=(pkg-config) && \
TEMP_PACKAGES+=(build-essential) && \
KEPT_PACKAGES+=(tcpdump) && \
#TEMP_PACKAGES+=(lsof) && \
#
# KEPT_PACKAGES+=(unzip) && \
# KEPT_PACKAGES+=(psmisc) && \
# KEPT_PACKAGES+=(procps nano) && \
# KEPT_PACKAGES+=(jq) && \
# KEPT_PACKAGES+=(iputils-ping) && \
KEPT_PACKAGES+=(logrotate) && \
#
# Install all the apt packages:
apt-get update -q && \
Expand Down
5 changes: 5 additions & 0 deletions rootfs/etc/default/logrotate.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/var/log/radar1090 {
rotate 0
size 25k
copytruncate
}
Empty file.
2 changes: 2 additions & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/radar1090-log/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
exec /etc/s6-overlay/scripts/radar1090-log
1 change: 1 addition & 0 deletions rootfs/etc/s6-overlay/s6-rc.d/radar1090-log/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
longrun
Empty file.
4 changes: 2 additions & 2 deletions rootfs/etc/s6-overlay/scripts/radar1090
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ s6wrap=(s6wrap --quiet --timestamps --prepend="$(basename "$0")")

# Run radar1090 as a long term service

"${s6wrap[@]}" --args echo "Starting radar 1090 as a service..."
"${s6wrap[@]}" --args echo "starting as a service..."

function valid_ip()
{
Expand Down Expand Up @@ -83,4 +83,4 @@ RADAR_CMD+=("-l ${beast_host}")
chk_enabled "$VERBOSE" && RADAR_CMD+=("-s") || true

"${s6wrap[@]}" --args echo "invoking: ${RADAR_BIN[@]} ${RADAR_CMD[@]}"
"${s6wrap[@]}" --args ${RADAR_BIN[@]} ${RADAR_CMD[@]}
${RADAR_BIN[@]} ${RADAR_CMD[@]} > /var/log/radar1090
69 changes: 69 additions & 0 deletions rootfs/etc/s6-overlay/scripts/radar1090-log
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/command/with-contenv bash
# shellcheck shell=bash disable=SC1091,SC2015,SC2164,SC2068,SC2145

source /scripts/common
s6wrap=(s6wrap --quiet --timestamps --prepend="$(basename "$0")")

#---------------------------------------------------------------------------------------------
# This repository, docker container, and accompanying scripts and documentation is
# Copyright (C) 2022-2023, Ramon F. Kolb (kx1t)
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
#
# Radar1090 is an ADS-B RADAR Feed Service
# Copyright (C) 2023 by Michael J. Tubby B.Sc. MIET G8TIC [email protected] All Rights Reserved.
# No license to the "radar" binary and its source code is implied; contact the author for information.
#---------------------------------------------------------------------------------------------

# Monitor the radar1090 messages and print a summary:
# - every minute if VERBOSE==true
# - every 5 minutes if VERBOSE!=true
# Note that the output of radar1090 writes a log line every second, even though it may take up to 60 seconds for its buffer to be flushed

"${s6wrap[@]}" --args echo "starting as a service..."

VERBOSE_WAIT=60
NON_VERBOSE_WAIT=300
LOGFILE="/var/logs/radar1090"

chk_enabled "$VERBOSE" && WAITTIME="$VERBOSE_WAIT" || WAITTIME="$NON_VERBOSE_WAIT"

# seed random generator:
RANDOM=$(date +%s)
# Add some random time (0-32 secs) to the start of this service:
sleep $((RANDOM/1000))

# Now go at it furreal:
while true; do
# First wait to collect some data. This data is written by the radar1090 binary to /var/logs/radar1090:
sleep "$WAITTIME"
# read the last lines into an array:
readarray -t loglines <<< "$(tail -"$WAITTIME" "$LOGFILE")"
# Immediately clear logs:
logrotate -f /etc/default/logrotate.conf

# Now parse some statistics:
packets=("$(awk '{ print $3 }' <<< "$(printf "%s\n" "${loglines[@]}")")")
dupes=("$(awk '{ print $7 }' <<< "$(printf "%s\n" "${loglines[@]}")")")
bytespersec=("$(awk '{ print $11 }' <<< "$(printf "%s\n" "${loglines[@]}")")")
total_packets="$(bc -l <<< "${packets[@]//[$'\t\r\n']/+}")"
total_dupes="$(bc -l <<< "${dupes[@]//[$'\t\r\n']/+}")"
avg_bps="$(bc -l <<< "scale=0; (${bytespersec[@]//[$'\t\r\n']/+})/${#loglines[@]}/1")"

# Print the stats to the container logs:
"${s6wrap[@]}" --args echo "Traffic statistics over the previous $WAITTIME seconds:"
"${s6wrap[@]}" --args echo "- Total Packets Received from ${BEASTHOST:-ultrafeeder}: $total_packets"
"${s6wrap[@]}" --args echo "- Duplicate Packets Discarded: $total_dupes"
"${s6wrap[@]}" --args echo "- Average bandwitdh user (excluding overhead): $avg_bps bytes/sec"

done
2 changes: 1 addition & 1 deletion rootfs/etc/s6-overlay/scripts/watchdog
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ s6wrap=(s6wrap --quiet --timestamps --prepend="$(basename "$0")")
# Watchdog to ensure there's traffic flowing for radar1090
# Uses TCPDUMP to ensure there are packets sent to the destination port

"${s6wrap[@]}" --args echo "Started as an s6 service"
"${s6wrap[@]}" --args echo "starting as a service..."

MEASURE_TIME="${MEASURE_TIME:-15}" # how long we take samples to check data is flowing
MEASURE_INTERVAL="${MEASURE_INTERVAL:-300}" # wait time between check runs
Expand Down

0 comments on commit ac25fdd

Please sign in to comment.