如何在BASH脚本的帮助下使用CLI静音SignalFX检测器警报

发布于 2025-01-17 20:06:33 字数 3251 浏览 3 评论 0原文

我创建了一个简单的 bash 脚本,可以帮助您在几秒钟内静音警报,而无需进入控制台。 先决条件:

  1. jq 需要安装在本地,
  2. 您应该了解检测器的某些部分。 例如,如果我想使用检测器名称“prod-test内存利用率”来静音警报,那么在通过脚本静音警报时,我只需键入 prod-test
  3. 应该在 signalfx 上创建令牌,以便我们可以从本地

参考进行身份验证: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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文