Last active
March 31, 2024 13:23
-
-
Save kmuto/d2a53cae8a99dc6face81bc32ff6b2d2 to your computer and use it in GitHub Desktop.
遅延アラートクローズスクリプト
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/bash | |
CLOSE_AFTER_MINUTES="60" | |
CLOSE_CHECK_ONLY=true | |
DRYRUN= | |
if [ -z "$(mkr --version 2>/dev/null)" ]; then | |
echo "ERROR: Missing mkr. Install mkr package." | |
exit 1 | |
fi | |
if [ ! -f "/etc/mackerel-agent/mackerel-agent.conf" -a -z "$MACKEREL_APIKEY" ]; then | |
echo "ERROR: Define MACKEREL_APIKEY." | |
exit 1 | |
fi | |
if [ "$CLOSE_CHECK_ONLY" ]; then | |
RESULT=$(mkr alerts -jq '.[] | select(.type == "check") | [.id, .openedAt] | @tsv') | |
else | |
RESULT=$(mkr alerts -jq '.[] | [.id, .openedAt] | @tsv') | |
fi | |
echo "$RESULT" | { | |
CLOSES= | |
while read line; do | |
# alertID openedAt | |
array=($line) # bash | |
if [ "$(date +"%s")" -ge "$(expr ${array[1]} + $CLOSE_AFTER_MINUTES \* 60)" ]; then | |
CLOSES="$CLOSES ${array[0]}" | |
fi | |
done | |
if [ "$CLOSES" ]; then | |
$DRYRUN mkr alerts close --reason "auto closed because more than ${CLOSE_AFTER_MINUTES} minutes passed." $CLOSES | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment