Last active
March 12, 2025 09:56
-
-
Save anatawa12/95dfcbe21271bbc6b6ad58398f2ba284 to your computer and use it in GitHub Desktop.
A small script to check if Nirila Misskey is healthy, and send alert to a discord webhook if it's not.
This file contains hidden or 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/sh | |
# A small script to check if Nirila Misskey is healthy, and send alert to a discord webhook if it's not. | |
# | |
# Optionally, you can send webhook even if it's healthy to confirm that the script is working. | |
# | |
# This script is depending on /healthz endpoint of Misskey added in 2024.5.0 so your Misskey must be at least that version. | |
# | |
# This script is Published at https://gist.github.com/anatawa12/95dfcbe21271bbc6b6ad58398f2ba284. | |
# You may get the latest version from there. | |
# | |
# Usage: | |
# 1. Copy this script to your server. you can clone the repository, or just download file with wget or curl. | |
# For getting directly you can download from the URL: | |
# wget https://gist.githubusercontent.com/anatawa12/95dfcbe21271bbc6b6ad58398f2ba284/raw/healthcheck.sh | |
# curl -O https://gist.githubusercontent.com/anatawa12/95dfcbe21271bbc6b6ad58398f2ba284/raw/healthcheck.sh | |
# 2. Edit the .env file to set the SERVER and WEBHOOK_URL. If you want to send webhook even if it's healthy, set SEND_ALWAYS to true. | |
# 3. Configure a cron job to run this script periodically. | |
# | |
# Configurations: | |
# required: | |
# - SERVER: The domain of your Misskey instance. This may include protocol, port, and trailing slash if needed. If no protocol is provided, https will be used. | |
# EXAMPLES: | |
# - SERVER=example.com - equivalent to https://example.com/ | |
# - SERVER=example.com/ - equivalent to https://example.com/ | |
# - SERVER=http://example.com - equivalent to http://example.com/ | |
# - SERVER=https://example.com - equivalent to https://example.com/ | |
# - SERVER=https://example.com/ - equivalent to https://example.com/ | |
# - SERVER=https://example.com:3000/ - equivalent to https://example.com:3000/ | |
# - WEBHOOK_URL: The URL of the Discord webhook to send the alert to. | |
# optional: | |
# - LOG: If set to false, the script will not log anything except errors. | |
# If set to true, the script will log the message to stderr. | |
# Default is true. | |
# - SEND_ALWAYS: If set to true, the script will send message to the webhook even if the server is healthy. | |
# Default is false. | |
# - MENTIONS: If set, the script will mention the specified users in the message if the server is unhealthy. | |
# EXAMPLES: | |
# - MENTIONS="<@1234567890>" - mentions user with ID 1234567890 | |
# - MENTIONS="<@&1234567890>" - mentions role with ID 1234567890 | |
# - MENTIONS="<@1234567890> <@&1234567890>" - mentions user and role | |
# | |
# License: MIT License | |
# MIT License | |
# | |
# Copyright (c) 2025 anatawa12 | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# start of the program | |
# load .env | |
. "$(dirname "$0")/.env" | |
# check if SERVER and WEBHOOK_URL are set | |
if [ -z "$SERVER" ] || [ -z "$WEBHOOK_URL" ]; then | |
echo "SERVER and WEBHOOK_URL must be set in .env" >&2 | |
exit 1 | |
fi | |
# check if required commands are available | |
check_command() { | |
if ! command -v "$1" > /dev/null; then | |
echo "$1 is required but not found" >&2 | |
exit 1 | |
fi | |
} | |
check_command curl | |
check_command jq | |
# normalize SERVER | |
# if no protocol is provided, use https | |
if [ "${SERVER%%://*}" = "$SERVER" ]; then | |
SERVER="https://$SERVER" | |
fi | |
# remove trailing slash | |
SERVER=${SERVER%/} | |
# log utility: if $LOG is not 'false', log the message to stdout | |
if [ "$LOG" != "false" ]; then | |
log() { | |
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ") $1" >&2 | |
} | |
else | |
log() { | |
: | |
} | |
fi | |
send_message() { | |
jq -n -R --arg MESSAGE "$1" '{"content": $MESSAGE}' \ | |
| curl --header "Content-Type: application/json" --request POST --data @- "$WEBHOOK_URL" | |
} | |
if curl --show-error --fail --silent --output /dev/null "$SERVER/healthz"; then | |
log "Misskey is healthy" | |
if [ "$SEND_ALWAYS" = "true" ]; then | |
send_message "Misskey at <$SERVER/> is healthy." | |
fi | |
else | |
log "Misskey is unhealthy. Sending alert webhook..." | |
send_message "$MENTIONS Misskey at <$SERVER/> is NOT healthy. Please check the server." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment