Skip to content

Instantly share code, notes, and snippets.

@edgarsandi
Forked from lichti/new_relic_alert.sh
Created September 5, 2016 21:59
Show Gist options
  • Save edgarsandi/2ebb0c21d37f3830fec2f8d629a6df95 to your computer and use it in GitHub Desktop.
Save edgarsandi/2ebb0c21d37f3830fec2f8d629a6df95 to your computer and use it in GitHub Desktop.
Script to enable or disable newrelic alerts
#!/bin/bash
if [ ! -x /usr/bin/curl ]; then
echo "Please install curl"
exit 1
fi
if [ ! -x /usr/bin/jq ]; then
echo "Please install jq"
exit 1
fi
if [ ! -x /usr/bin/python ]; then
echo "Please install python"
exit 1
fi
if [ $# -eq 0 ]; then
set -- '-h' "$@"
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-k|--key)
API_KEY="$2"
shift
;;
-p|--policy)
POLICY_NAME="$2"
shift
;;
-a|--alert)
ALERT_NAME="$2"
shift
;;
-e|--enable)
ENABLED='true'
;;
-d|--disable)
DISABLED='false'
;;
--debug)
DEBUG=true
;;
-h|--help)
echo "$0"
echo " -k, --key <newrelic api key>"
echo " -p, --policy <policy name>"
echo " -a, --alert <alert name>"
echo " -e, --enable"
echo " -d, --disable"
echo " --debug"
echo " -h, --help"
exit 1
;;
*)
echo "$key: unknown option"
echo "Use: $0 --help"
exit 1
;;
esac
shift
done
if [ -n "$ENABLED" ] && [ -n "$DISABLED" ]; then
echo "Use -d or -e"
exit 1
elif [ -n "$ENABLED" ]; then
ALERT_ENABLED=$ENABLED
elif [ -n "$DISABLED" ]; then
ALERT_ENABLED=$DISABLED
fi
if [ -z "$API_KEY" ]; then echo "key not defined"; exit 1; fi
if [ -z "$POLICY_NAME" ]; then echo "policy name not defined"; exit 1; fi
if [ -z "$ALERT_NAME" ]; then echo "alert name not defined"; exit 1; fi
if [ -z "$ALERT_ENABLED" ]; then echo "you must specify enable or disable"; exit 1; fi
if [ -n "$DEBUG" ]; then
echo "API KEY: $API_KEY"
echo "POLICY NAME: $POLICY_NAME"
echo "ALERT NAME: $ALERT_NAME"
echo "ALERT ENABLED: $ALERT_ENABLED"
fi
POLICY_ESCAPED=$(python -c "import urllib; print urllib.quote('''${POLICY_NAME}''')")
if [ -n "$DEBUG" ]; then echo "POLICY ESCAPED: $POLICY_ESCAPED"; fi
POLICY_ID=$(curl -X GET 'https://api.newrelic.com/v2/alerts_policies.json' -H "X-Api-Key: ${API_KEY}" -s -G -d "filter[name]=${POLICY_ESCAPED}" | jq '.policies[0] .id')
if [ -n "$DEBUG" ]; then echo "POLICY ID: $POLICY_ID"; fi
if [ $POLICY_ID != 'null' ]; then
ALERT_JSON=$(curl -X GET 'https://api.newrelic.com/v2/alerts_conditions.json' -H "X-Api-Key: ${API_KEY}" -s -G -d "policy_id=${POLICY_ID}" | jq --arg name "${ALERT_NAME}" '.conditions[] | select(.name | contains($name))')
if [ -n "$DEBUG" ]; then echo "ALERT_JSON: $ALERT_JSON"; fi
if [ -n "$ALERT_JSON" ]; then
ALERT_ID=$(echo ${ALERT_JSON} | jq '.id')
if [ -n "$DEBUG" ]; then echo "ALERT ID: $ALERT_ID"; fi
ALERT_NEW_JSON=$(echo ${ALERT_JSON} | jq --arg enabled ${ALERT_ENABLED} '.enabled = $enabled | del(.id) | { condition: . }')
if [ -n "$DEBUG" ]; then echo "ALERT NEW JSON: $ALERT_NEW_JSON"; fi
RESPONSE=$(curl -X PUT "https://api.newrelic.com/v2/alerts_conditions/${ALERT_ID}.json" -H "X-Api-Key: ${API_KEY}" -s -i -H 'Content-Type: application/json' -d "${ALERT_NEW_JSON}")
if [ -n "$DEBUG" ]; then echo "RESPONSE: $RESPONSE"; fi
CODE=$(echo "$RESPONSE" | grep 'HTTP' | awk '{print $2}')
if [ $CODE -eq 200 ]; then
exit 0
else
exit 1
fi
else
echo 'Invalid alert name'
exit 1
fi
else
echo 'Invalid policy name'
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment