Skip to content

Instantly share code, notes, and snippets.

@austinjdean
Created March 24, 2021 01:15
Show Gist options
  • Select an option

  • Save austinjdean/6581e351f8bf6914af9dccf950424aa3 to your computer and use it in GitHub Desktop.

Select an option

Save austinjdean/6581e351f8bf6914af9dccf950424aa3 to your computer and use it in GitHub Desktop.
Simple shell script for sending Valheim server alerts to Discord
#/bin/bash
# your webhook here
webhookURL="https://discord.com/api/webhooks/123412341234/ldsakjflsadsdkjfdslfkjdslfkj"
main() {
poll_log
echo "$newLog" > /tmp/currentLog
rm -f /tmp/newLog
while true; do
poll_log
if [ "$newHash" = "$currentHash" ]; then
# chill
:
else
# differnet hash means there was an update in the log
# wat is the update?
logUpdate=$(grep -F -x -v -f /tmp/currentLog /tmp/newLog)
# process the log update
echo "$logUpdate" | grep -P 'Got connection SteamID \d+' > /dev/null 2>/dev/null
if [ "$?" = "0" ]; then
message=$(echo "$logUpdate" | grep -P 'Got connection SteamID \d+' | tr -d '\r')
echo "$message"
sendToDiscord "$message"
fi
echo "$logUpdate" | grep -P 'Peer \d+ has wrong password' > /dev/null 2>/dev/null
if [ "$?" = "0" ]; then
message=$(echo "$logUpdate" | grep -P 'Peer \d+ has wrong password' | tr -d '\r')
echo "$message"
sendToDiscord "$message"
fi
echo "$logUpdate" | grep -P 'Got character ZDOID from [^ ]+ :' > /dev/null 2>/dev/null
if [ "$?" = "0" ]; then
message=$(echo "$logUpdate" | grep -P 'Got character ZDOID from [^ ]+ :' | tr -d '\r')
echo "$message"
sendToDiscord "$message"
fi
echo "$logUpdate" | grep -P 'Closing socket \d+' > /dev/null 2>/dev/null
if [ "$?" = "0" ]; then
message=$(echo "$logUpdate" | grep -P 'Closing socket \d+' | tr -d '\r')
echo "$message"
sendToDiscord "$message"
fi
echo "$logUpdate" | grep -P 'World saved' > /dev/null 2>/dev/null
if [ "$?" = "0" ]; then
message=$(echo "$logUpdate" | grep -P 'World saved' | tr -d '\r')
echo "$message"
sendToDiscord "$message"
fi
# update current log
echo "$newLog" > /tmp/currentLog
# to not enter an infinite loop, the "new" hash is now the current hash
currentHash="$newHash"
fi
# =====
sleep 2 # throttle 2 second poll
done
}
poll_log() {
# get a current snapshot of latest log, plus the hash
newLog=$(cat log/console/vhserver-console.log | grep -P '^\d{2}/\d{2}/\d{4}' | tail -n 40)
echo "$newLog" > /tmp/newLog
newHash=$(cat /tmp/newLog | sha1sum | cut -c -40)
}
sendToDiscord() {
# $1 will be a simple string sent to discord
# assemble -d line
data="{\"username\": \"Valheim Server Alert\", \"content\": \""$1"\"}"
curl -X POST \
-H "Content-Type: application/json" \
-d "$data" \
"$webhookURL"
}
main
@austinjdean
Copy link
Copy Markdown
Author

The world save updates can get pretty annoying as they happen every 20 mins. Comment out line 56 to quiet those.

@austinjdean
Copy link
Copy Markdown
Author

austinjdean commented Apr 27, 2023 via email

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