Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CannonLock committed Sep 13, 2024
1 parent 4ca22c0 commit b950ff2
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .github/workflows/image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Build image

on:
push:
branches: ["main"]
tags:
- 'v*.*.*' # glob for semver tags (including prereleases)
pull_request:
branches: [main]

jobs:
call-build-image:
uses: UW-Macrostrat/build-push/build-push.yaml@main
with:
image: 'hub.opensciencegrid.org/macrostrat/traefik-log-exporter'
username: ${{ vars.HARBOR_CLI_NAME }}
password: ${{ secrets.HARBOR_CLI_SECRET }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test-logs
.env
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "import-script"]
path = import-script
url = git@github.com:matomo-org/matomo-log-analytics.git
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM python:3.8

# Install cron
RUN apt-get update && apt-get install -y cron

# Set the working directory
WORKDIR /app

# Copy the necessary files
COPY ./import-script/import_logs.py import_logs.py
COPY ./import.sh import.sh
COPY ./logrotate.sh logrotate.sh

# Make the scripts executable
RUN chmod +x import.sh logrotate.sh

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/import-cron

# Apply cron job
RUN crontab /etc/cron.d/import-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Start the cron service and the application
CMD cron && tail -f /var/log/cron.log
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# matomo-log-scraper
# matomo-log-scraper

This service runs as a sidecar to the Traefik pod. It rotates the logs daily and sends them to the Matomo server for analysis.

It keeps 30 days of logs compressed in the PVC on the cluster for debugging purposes.

# Testing

```bash
source .env

# Construct the command with the environment variables
python3 import-script/import_logs.py "$LOG_PATH" --dry-run --url "$MATOMO_URL" --token-auth "$API_TOKEN"
```
2 changes: 2 additions & 0 deletions crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Run logrotate.sh daily at midnight
0 0 * * * /app/logrotate.sh >> /var/log/cron.log 2>&1
3 changes: 3 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
LOG_PATH=./test-logs/traefik.json
API_TOKEN=
MATOMO_URL=https://analytics.macrostrat.org
1 change: 1 addition & 0 deletions import-script
Submodule import-script added at d1cd2f
14 changes: 14 additions & 0 deletions import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

# Define the log file and the rotated log file
ROTATED_LOG_FILE="$LOG_PATH.1"

# Check if the rotated log file exists
if [ -f "$ROTATED_LOG_FILE" ]; then
echo "Log rotation successful. Processing the rotated log file."
# Call the import script
python3 /app/import_logs.py "$ROTATED_LOG_FILE" --url "$MATOMO_URL" --token-auth "$API_TOKEN"
else
echo "Rotated log file not found."
exit 1
fi
20 changes: 20 additions & 0 deletions logrotate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

# Create a temporary logrotate configuration file
cat <<EOF > /tmp/logrotate.conf
$LOG_PATH {
daily
rotate 31
compress
delaycompress
postrotate
/app/import.sh
endscript
}
EOF

# Run logrotate with the temporary configuration file
/usr/sbin/logrotate /tmp/logrotate.conf

# Clean up the temporary configuration file
rm /tmp/logrotate.conf

0 comments on commit b950ff2

Please sign in to comment.