Skip to content

Commit 0febab5

Browse files
committed
TF: Add truncate error log service/timer
1 parent 87557f6 commit 0febab5

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

contrib/nginx.conf.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ geo $limit {
1010
default 1;
1111
34.70.28.228/32 0;
1212
127.0.0.1/32 0;
13+
216.38.154.82 0;
1314
}
1415

1516
map $limit $limit_key {

terraform/modules/daemon/cloud-config/daemon.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,60 @@ write_files:
6060
echo "[insert-instance-list] added $ip:$PORT to ${container_name}"
6161
done
6262
63+
- path: /home/bs/truncate-err-log.sh
64+
permissions: 0644
65+
owner: root
66+
content: |
67+
#!/bin/bash
68+
69+
# Path to the file inside the container
70+
FILE="/var/log/nginx/error.log"
71+
# Container name
72+
CONTAINER="${container_name}"
73+
# Maximum file size in bytes (20GB)
74+
MAXSIZE=$((20 * 1024 * 1024 * 1024))
75+
76+
# Check if the container is running
77+
if docker ps | grep -q "$CONTAINER"; then
78+
# Get the file size inside the container
79+
FILESIZE=$(docker exec "$CONTAINER" stat -c %s "$FILE")
80+
# Compare the file size with the maximum allowed size
81+
if [ "$FILESIZE" -gt "$MAXSIZE" ]; then
82+
echo "Truncating $FILE in $CONTAINER..."
83+
docker exec "$CONTAINER" truncate -s 0 "$FILE"
84+
else
85+
echo "$FILE in $CONTAINER is within size limit: $((FILESIZE / 1024 / 1024 / 1024)) GB."
86+
fi
87+
else
88+
echo "Container $CONTAINER is not running."
89+
fi
90+
91+
- path: /etc/systemd/system/truncate-err-log.service
92+
permissions: 0644
93+
owner: root
94+
content: |
95+
[Unit]
96+
Description=Check and truncate Nginx error.log file in container if larger than 20GB
97+
98+
[Service]
99+
Type=oneshot
100+
ExecStart=/bin/bash /home/bs/truncate-err-log.sh
101+
102+
- path: /etc/systemd/system/truncate-err-log.timer
103+
permissions: 0644
104+
owner: root
105+
content: |
106+
[Unit]
107+
Description=Timer for truncating Nginx error.log file in container
108+
109+
[Timer]
110+
# Run every 12h
111+
OnCalendar=*-*-* 00/12:00:00
112+
AccuracySec=1h
113+
114+
[Install]
115+
WantedBy=timers.target
116+
63117
- path: /etc/systemd/system/explorer.service
64118
permissions: 0644
65119
owner: root
@@ -159,6 +213,7 @@ write_files:
159213
160214
runcmd:
161215
- systemctl daemon-reload
216+
- systemctl enable --now truncate-err-log.timer
162217
- systemctl enable --now explorer.service
163218
- systemctl enable --now node-exporter.service
164219
- systemctl enable --now process-exporter.service

0 commit comments

Comments
 (0)