How to mute signalfx detector alerts using cli with the help of bash script

273 Views Asked by At

I have created a simple bash script that help you to mute alert within a few second without going into the console. prerequisite:

  1. jq needs to be installed on local
  2. you should know some portion of your detector. ex- If I want to mute alert with for detector name "prod-test memory utilization" then while mute alert via script I will simply type prod-test
  3. Token should be created on signalfx so that we can authenticate from local

Ref: https://docs.splunk.com/Observability/alerts-detectors-notifications/mute-notifications.html#nav-Mute-alert-notifications

#!/bin/bash
echo -e "Please enter SFx token to authenticate\n"
read TOKEN
echo -e "    \n"
echo -e "Please copy-paste any one muting option from below. \n a) Mute_Indefinitely \n b) Mute_By_Duration \n"
read MUTE_OPTION
echo -e "   \n"
## this is comman name from detector in my case it is prod-test and prod-test2"
echo -e "Please copy-paste the server name which you wish to mute.
 1. prod-test
 2. prod-test2\n"
 read SERVER_NAME
echo "Fatching detectors ids, Please wait for few seconds..."
DETECTOR_ID=$(curl -s -X GET "https://<your sfx url>/v2/detector?limit=2000" -H "Content-Type: application/json" -H "X-SF-TOKEN: "$TOKEN"" | jq -r '.results[] | select(.name | contains("'"$SERVER_NAME"'")).id')


############### MUTE BY DURATION FUNCTION ###############
mute_by_duration () {
 START_TIME=`date  +%s%N | cut -b1-13`
 END_TIME=`date -d "+$min minutes"  +%s%N | cut -b1-13`
curl -X "POST" "https://<your sfx url>/v2/alertmuting" \
        -H 'X-SF-TOKEN: '$TOKEN'' \
        -H 'Content-Type: application/json' \
        -d $'{
        "startTime": "'"$START_TIME"'",
        "stopTime": "'"$END_TIME"'",
        "filters": [
        {
        "property": "sf_detectorId",
        "propertyValue": "'"$i"'"
        }
        ]
        }'
if [ $? -eq 0 ]; then
  echo -e "\nDetector has been muted\n"
fi
}


############# MUTE ALERT INDEFINITELY ################
mute_indefinitely () {
  curl -X "POST" "https://<your sfx url>/v2/alertmuting" \
        -H 'X-SF-TOKEN: '$TOKEN'' \
        -H 'Content-Type: application/json' \
        -d $'{
        "startTime": "",
        "stopTime": "",
        "filters": [
        {
        "property": "sf_detectorId",
        "propertyValue": "'"$i"'"
        }
        ]
        }'
  if [ $? -eq 0 ]; then
    echo -e "\nDetector has been muted\n"
  fi
}


#################### MUTING OPTION ###############
muting_rule() {
case "$MUTE_OPTION" in
   "Mute_Indefinitely") echo -e "\n***** You have selected Mute_Indefinitely option, don't forget to unmute later *****\n";
     for i in $DETECTOR_ID; do mute_indefinitely; done
   ;;
   "Mute_By_Duration") echo -e "\n***** You have selected Mute_By_Duration option, This Alert will auto unmute after $min minutes *****\n";
     for i in $DETECTOR_ID; do mute_by_duration; done
   ;;
   *) echo "Please select correct option"
esac
}


############# SELECTION BASED ON MUTING OPTION #######################
if [ "$MUTE_OPTION" == Mute_By_Duration ]; then
  echo -e "How much minutes you want to mute alert from current time? \n Example: Type "30" to mute for 30 mins \n"
  read  min
  muting_rule
elif [ "$MUTE_OPTION" == Mute_Indefinitely ]; then
  muting_rule
else
  echo "Invalid Option"
fi
0

There are 0 best solutions below