如何在BASH脚本的帮助下使用CLI静音SignalFX检测器警报
我创建了一个简单的 bash 脚本,可以帮助您在几秒钟内静音警报,而无需进入控制台。 先决条件:
- jq 需要安装在本地,
- 您应该了解检测器的某些部分。 例如,如果我想使用检测器名称“prod-test内存利用率”来静音警报,那么在通过脚本静音警报时,我只需键入 prod-test
- 应该在 signalfx 上创建令牌,以便我们可以从本地
#!/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:
- jq needs to be installed on local
- 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 - Token should be created on signalfx so that we can authenticate from local
#!/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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论