Skip to content

Commit ca92ed4

Browse files
committed
Run obfi_previous_minute inside docker to prevent orphaned tac command
1 parent 9b9db0e commit ca92ed4

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

scripts/monitoring/tests/test_utils_sh.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ def test_log_recent_bot_traffic():
3434
# alias the obfi commands to return some noise
3535
aliases_fp.write(
3636
f"""
37-
obfi_previous_minute() {{
37+
obfi_in_docker() {{
3838
cat scripts/monitoring/tests/sample_covers_nginx_logs.log
3939
}}
40-
export -f obfi_previous_minute
40+
export -f obfi_in_docker
4141
4242
nc() {{
4343
# Read stdin and write to nc_fp
@@ -62,7 +62,7 @@ def test_log_recent_bot_traffic():
6262
expected_output = """
6363
stats.ol-covers0.bot_traffic.gptbot 2 1741054377
6464
stats.ol-covers0.bot_traffic.meta_externalagent 1 1741054377
65-
stats.ol-covers0.bot_traffic.other 0 1741054377
65+
stats.ol-covers0.bot_traffic.other 1 1741054377
6666
stats.ol-covers0.bot_traffic.non_bot 6 1741054377
6767
""".strip()
6868
assert f.read().strip() == expected_output
@@ -76,10 +76,10 @@ def test_log_recent_http_statuses():
7676
# alias the obfi commands to return some noise
7777
aliases_fp.write(
7878
f"""
79-
obfi_previous_minute() {{
79+
obfi_in_docker() {{
8080
cat scripts/monitoring/tests/sample_covers_nginx_logs.log
8181
}}
82-
export -f obfi_previous_minute
82+
export -f obfi_in_docker
8383
8484
nc() {{
8585
# Read stdin and write to nc_fp

scripts/monitoring/utils.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ log_recent_bot_traffic() {
7575
BUCKET="$1"
7676

7777
# Get top bot user agent counts in the last minute
78-
BOT_TRAFFIC_COUNTS=$(obfi_previous_minute | obfi_top_bots)
78+
BOT_TRAFFIC_COUNTS=$(obfi_in_docker obfi_previous_minute | obfi_top_bots)
7979

8080
# Output like this:
8181
# 1412516 bingbot
@@ -101,9 +101,9 @@ log_recent_bot_traffic() {
101101

102102
# Also log other bots as a single metric
103103
OTHER_BOTS_COUNT=$(
104-
obfi_previous_minute | \
104+
obfi_in_docker obfi_previous_minute | \
105105
grep -iE '\b[a-z_-]+(bot|spider|crawler)' | \
106-
grep -viE 'bingbot|claudebot|googlebot|applebot|gptbot|yandex.com/bots|ahrefsbot|amazonbot|petalbot|brightbot|SemanticScholarBot|uptimerobot|seznamebot|OAI-SearchBot|VsuSearchSpider' | \
106+
obfi_top_bots -v | \
107107
wc -l
108108
)
109109

@@ -113,7 +113,7 @@ log_recent_bot_traffic() {
113113

114114
# And finally, also log non bot traffic
115115
NON_BOT_TRAFFIC_COUNT=$(
116-
obfi_previous_minute | \
116+
obfi_in_docker obfi_previous_minute | \
117117
grep -viE '\b[a-z_-]+(bot|spider|crawler)' | \
118118
wc -l
119119
)
@@ -128,7 +128,7 @@ log_recent_http_statuses() {
128128
BUCKET="$1"
129129

130130
# Get top http counts from previous minute
131-
HTTP_STATUS_COUNTS=$(obfi_previous_minute | obfi_top_http_statuses)
131+
HTTP_STATUS_COUNTS=$(obfi_in_docker obfi_previous_minute | obfi_top_http_statuses)
132132
# Output like this:
133133
# 60319 " 200
134134
# 55926 " 302
@@ -157,7 +157,7 @@ log_top_ip_counts() {
157157

158158
# Get top IP counts in the last minute
159159
TOP_IP_COUNTS=$(
160-
obfi_previous_minute | \
160+
obfi_in_docker obfi_previous_minute | \
161161
obfi_top_ips 25 | \
162162
awk '{print $1}'
163163
)

scripts/obfi.sh

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,18 @@ obfi() {
6464
return 0
6565
fi
6666
CMD=${1:-"tail -f"}
67+
CONTAINER="${CONTAINER:-}"
68+
OBFI_SUDO=${OBFI_SUDO-"sudo"}
69+
local sudo_cmd=""
70+
71+
if [ ! -z "$OBFI_SUDO" ]; then
72+
sudo_cmd="$OBFI_SUDO -E"
73+
fi
6774

6875
if command -v docker >/dev/null 2>&1; then
6976
if [ -z "$CONTAINER" ]; then
7077
CONTAINER=$(docker ps --format '{{.Names}}' | grep nginx)
7178
fi
72-
else
73-
CONTAINER=""
7479
fi
7580

7681
if [ ! -z "$CONTAINER" ]; then
@@ -82,8 +87,34 @@ obfi() {
8287
LOG_DIR="${OBFI_LOG_DIR:-/1/var/log/nginx}"
8388
FILE=${2:-"$LOG_DIR/access.log"}
8489
echo "Reading from $FILE" 1>&2
85-
sudo -E $CMD $FILE
90+
$sudo_cmd $CMD $FILE
91+
fi
92+
}
93+
94+
obfi_in_docker() {
95+
if ! command -v docker >/dev/null 2>&1; then
96+
echo "Docker is not installed" 1>&2
97+
return 1
98+
fi
99+
100+
CMD=${1:-"tail -f"}
101+
CONTAINER="${CONTAINER:-}"
102+
103+
if [ -z "$CONTAINER" ]; then
104+
CONTAINER=$(docker ps --format '{{.Names}}' | grep nginx)
105+
fi
106+
107+
if [ -z "$CONTAINER" ]; then
108+
echo "No nginx container found"
109+
return 1
86110
fi
111+
112+
docker exec "$CONTAINER" bash -c "
113+
source /openlibrary/scripts/obfi.sh
114+
export OBFI_SUDO=''
115+
export OBFI_LOG_DIR='/var/log/nginx'
116+
$CMD
117+
"
87118
}
88119

89120
###############################################################

0 commit comments

Comments
 (0)