Skip to content

Instantly share code, notes, and snippets.

@rayjanoka
Last active June 2, 2023 21:30
Show Gist options
  • Save rayjanoka/e39c1c9152be1fa605eff88ff6a04459 to your computer and use it in GitHub Desktop.
Save rayjanoka/e39c1c9152be1fa605eff88ff6a04459 to your computer and use it in GitHub Desktop.
tail/pipe kubectl logs to a slack channel with slackcat
#!/bin/sh
set -e
ARCH=amd64
KUBECTL_VERSION=1.27.2
TETRAGON_VERSION=0.9.0
SLACKCAT_VERSION=1.7.3
SLACK_CHANNEL=test
apk -q add curl grep sed coreutils
curl -sLO https://dl.k8s.io/release/v$${KUBECTL_VERSION}/bin/linux/$${ARCH}/kubectl
chmod +x ./kubectl
curl -sLo slackcat https://github.com/bcicen/slackcat/releases/download/$${SLACKCAT_VERSION}/slackcat-$${SLACKCAT_VERSION}-linux-$${ARCH}
chmod +x ./slackcat
echo "$SLACK_TOKEN" > ~/.slackcat
LOOP_MINS=1
LAST_SCAN=$(date --iso-8601=seconds --date "-$LOOP_MINS min")
MAX_LINES_PER_LOOP=100
MAX_LOG_REQUESTS=200
echo "starting tetragon log to slack..."
while true; do
WAIT_UNTIL=$(date --iso-8601=seconds --date="$LAST_SCAN +$LOOP_MINS min")
if [ "$(date '+%s')" -gt "$(date --date "$WAIT_UNTIL" '+%s')" ]; then
echo "requesting logs since $WAIT_UNTIL..."
START_SCAN=$(date --iso-8601=seconds)
./kubectl logs --since-time "$LAST_SCAN" \
--namespace ${var.namespace} \
--selector app.kubernetes.io/name=tetragon \
--container export-stdout \
--max-log-requests $MAX_LOG_REQUESTS \
--tail $MAX_LINES_PER_LOOP | \
./slackcat --stream --channel $SLACK_CHANNEL
LAST_SCAN="$START_SCAN"
fi
sleep 5
done
@rayjanoka
Copy link
Author

these scopes are required for the slackcat bot app, their auto-setup is broken atm.

["chat:write", "im:read", "mpim:read", "channels:read", "groups:read", "files:write", "users:read"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment