-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#!/bin/bash | ||
# | ||
# cloudwatch-functions | ||
|
||
cloudwatch-alarms(){ | ||
|
||
# List Cloudwatch Alarms | ||
# | ||
# USAGE: cloudwatch-alarms [filter] | ||
# | ||
# $ things | ||
# thing-1234567890123 Online Amazon Linux 2 192.168.1.10 server001.example.com | ||
# thing-1234567890124 Offline Amazon Linux 2 192.168.1.10 server001.example.com | ||
# thing-1234567890125 Online Amazon Linux 2 192.168.1.10 server001.example.com | ||
# | ||
# *Optionally provide a filter string for a `| grep` effect with tighter columisation:* | ||
# | ||
# $ things Online | ||
# i-1234567890123 Online Microsoft Windows Server 2019 Datacenter 68.0.11111 192.168.1.10 server001.example.com | ||
# i-1234567890124 Online Microsoft Windows Server 2022 Datacenter 68.0.11112 192.168.1.20 winserver002.example.com | ||
|
||
local alarms=$(skim-stdin) | ||
local filters=$(__bma_read_filters $@) | ||
|
||
local arg_filters="${alarms:+--alarm-names "${alarms}"}" | ||
|
||
aws cloudwatch describe-alarms \ | ||
$arg_filters \ | ||
--output text \ | ||
--query " | ||
MetricAlarms[].[ | ||
AlarmName, | ||
StateValue, | ||
ActionsEnabled, | ||
join(',', AlarmActions || \`[]\`) | ||
]" \ | ||
| grep -E -- "$filters" \ | ||
| LC_ALL=C sort -t $'\t' -k 1 \ | ||
| columnise | ||
} | ||
|
||
cloudwatch-alarm-delete() { | ||
local alarms="$(skim-stdin "$@")" | ||
[[ -z $alarms ]] && __bma_usage "alarm_name [alarm_name]" && return 1 | ||
|
||
alarm_names_json=$(printf "%s\n" "$alarms" | jq -R '.' | jq -crs '{"AlarmNames":.}') | ||
|
||
echo "You are about to delete the following Cloudwatch Alarms:" | ||
echo "$alarm_names_json" | jq . | ||
[ -t 0 ] || exec </dev/tty # reattach keyboard to STDIN | ||
local regex_yes="^[Yy]$" | ||
read -p "Are you sure you want to continue? " -n 1 -r | ||
echo | ||
if [[ $REPLY =~ $regex_yes ]]; then | ||
aws cloudwatch delete-alarms --cli-input-json "${alarm_names_json}" | ||
fi | ||
} | ||
|
||
cloudwatch-alarm-actions-disable() { | ||
local alarms="$(skim-stdin "$@")" | ||
[[ -z $alarms ]] && __bma_usage "alarm_name [alarm_name]" && return 1 | ||
|
||
alarm_names_json=$(printf "%s\n" "$alarms" | jq -R '.' | jq -crs '{"AlarmNames":.}') | ||
|
||
echo "You are about to disable alarm actions on the following Cloudwatch Alarms:" | ||
echo "$alarm_names_json" | jq . | ||
[ -t 0 ] || exec </dev/tty # reattach keyboard to STDIN | ||
local regex_yes="^[Yy]$" | ||
read -p "Are you sure you want to continue? " -n 1 -r | ||
echo | ||
if [[ $REPLY =~ $regex_yes ]]; then | ||
aws cloudwatch disable-alarm-actions --cli-input-json "${alarm_names_json}" | ||
fi | ||
} | ||
|
||
cloudwatch-alarm-actions-enable() { | ||
local alarms="$(skim-stdin "$@")" | ||
[[ -z $alarms ]] && __bma_usage "alarm_name [alarm_name]" && return 1 | ||
|
||
alarm_names_json=$(printf "%s\n" "$alarms" | jq -R '.' | jq -crs '{"AlarmNames":.}') | ||
|
||
echo "You are about to enable alarm actions on the following Cloudwatch Alarms:" | ||
echo "$alarm_names_json" | jq . | ||
[ -t 0 ] || exec </dev/tty # reattach keyboard to STDIN | ||
local regex_yes="^[Yy]$" | ||
read -p "Are you sure you want to continue? " -n 1 -r | ||
echo | ||
if [[ $REPLY =~ $regex_yes ]]; then | ||
aws cloudwatch enable-alarm-actions --cli-input-json "${alarm_names_json}" | ||
fi | ||
} |