Skip to content

Instantly share code, notes, and snippets.

@kmuto
Last active March 31, 2024 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmuto/d2a53cae8a99dc6face81bc32ff6b2d2 to your computer and use it in GitHub Desktop.
Save kmuto/d2a53cae8a99dc6face81bc32ff6b2d2 to your computer and use it in GitHub Desktop.
遅延アラートクローズスクリプト
#!/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