Created
March 24, 2021 01:15
-
-
Save austinjdean/6581e351f8bf6914af9dccf950424aa3 to your computer and use it in GitHub Desktop.
Simple shell script for sending Valheim server alerts to Discord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/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 |
Hey I don't see your comment anymore on the gist, but check your Valheim
server logs. I haven't worked on this (or run my Valheim server) in a while
and they may have updated the phrasing of those messages, causing the
regexes to fail to match.
…On Wed, Apr 26, 2023 at 3:39 PM Sarang Rajesh ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hey, I'm only getting alerts for Got character ZDOID.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/austinjdean/6581e351f8bf6914af9dccf950424aa3#gistcomment-4549653>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABX5ORK6E2N3PPZD776TZULXDF2YBBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAOBWGI3DINRRU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The world save updates can get pretty annoying as they happen every 20 mins. Comment out line 56 to quiet those.