Last active
July 5, 2018 20:25
-
-
Save jonniedarko/07c2b42d45c58a7786788eec420a4920 to your computer and use it in GitHub Desktop.
Revisions
-
jonniedarko revised this gist
Sep 26, 2016 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ #!/bin/bash # # functions to help with cloudwatch set up # based on https://github.com/swoodford/aws # # Trying to mostly conform to Google's style guide: # https://google.github.io/styleguide/shell.xml -
jonniedarko revised this gist
Sep 26, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ # https://google.github.io/styleguide/shell.xml ################################################# # CONSTANTS & DEFAULTS ################################################# DEFAULT_REGION='us -east-1' -
jonniedarko created this gist
Sep 26, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,418 @@ #!/bin/bash # # functions to help with cloudwatch set up # # Trying to mostly conform to Google's style guide: # https://google.github.io/styleguide/shell.xml ################################################# # CONSTANTS & DEFAULTS # ################################################# DEFAULT_REGION='us -east-1' ################################################## # Create an SNS Topic # Globals: # None # Arguments: # -n | --name : topicname # -r | --region : specifiy region (defaults to us-east-1) # -m | --mail-list : (optional) comma separated list # of email addresses # Returns: # Action - the TopicArn that can be used # as action in Alarms ################################################## jio_cloudwatch_utils__createTopic(){ while [[ $# -gt 0 ]]; do case "${1}" in -n|--name) name=$2 shift ;; -m|--mail-list) emaillist=$2 shift ;; -r|--region) region=$2 shift ;; --) shift break ;; *) shift break ;; esac shift done; if [ -z "$region" ]; then region=$DEFAULT_REGION; fi topicArn=$(aws sns list-topics --region $region | jq -r ".Topics[] | select(.TopicArn | contains(\"$name\") ) | .TopicArn") if [ -z "$topicArn" ]; then topicArn=$(aws sns create-topic --region $region --name "$name" | jq -r '.TopicArn') #echo $topicArn if [ ! -z "$emaillist" ]; then aws sns subscribe --region $region --topic-arn $topicArn --protocol email --notification-endpoint $emaillist > /dev/null fi fi echo $topicArn } ################################################## # Create a Load Balancer Unhealthy Host # Check Alarm # Globals: # None # Arguments: # -n | --name : topicname # -l | --load-balancer-name : comma separated list # of email addresses # -a | --action-arn : action arn # -p | --period : seconds over which the specified # statistic is applied. # -t | --threshold : value against which the # specified statistic is compared # -e | --evaluation-period : The number of periods # over which data is compared to # the specified threshold # Returns: # null ################################################## jio_cloudwatch_utils__load_Balancer_Unhealthy_Host_Check(){ while [[ $# -gt 0 ]]; do case "${1}" in -n|--name) name=$2 shift ;; -r|--region) region=$2 shift ;; -l|--load-balancer-name) load_balancer_name=$2 shift ;; -a|--action-arn) action=$2 shift ;; -p|--period) period=$2 shift ;; -t|--threshold) threshold=$2 shift ;; -e|--evaluation-period) evaluation_period=$2 shift ;; --) shift break ;; *) shift break ;; esac shift done; # required fields if [ -z "$name" ]; then echo 'name is required'; exit 1; fi if [ -z "$load_balancer_name" ]; then echo 'load-balancer-name is required'; exit 1; fi if [ -z "$action" ]; then echo 'action-arn is required'; exit 1; fi # set defaults for option fields if [ -z "$period" ]; then period=60; fi if [ -z "$threshold" ]; then threshold=0; fi if [ -z "$evaluation_period" ]; then evaluation_period=3; fi if [ -z "$region" ]; then region=$DEFAULT_REGION; fi # Load Balancer Unhealthy Host Check aws cloudwatch put-metric-alarm --region $region --alarm-name "$name - Unhealthy Host Check" --alarm-description "$name Load Balancer Unhealthy Host Detected" --metric-name "UnHealthyHostCount" --namespace "AWS/ELB" --statistic "Sum" --period "$period" --threshold "$threshold" --comparison-operator "GreaterThanThreshold" --dimensions Name=LoadBalancerName,Value="$load_balancer_name" --evaluation-periods "$evaluation_period" --alarm-actions $action } ################################################## # Create a Load Balancer High Latency # check alarm # Globals: # None # Arguments: # -n | --name : topicname # -l | --load-balancer-name : comma separated list # of email addresses # -a | --action-arn : action arn # -p | --period : seconds over which the specified # statistic is applied. # -t | --threshold : value against which the # specified statistic is compared # -e | --evaluation-period : The number of periods # over which data is compared to # the specified threshold # Returns: # null ################################################## jio_cloudwatch_utils__Load_Balancer_High_Latency_Alarm_Set(){ while [[ $# -gt 0 ]]; do case "${1}" in -n|--name) name=$2 shift ;; -r|--region) region=$2 shift ;; -l|--load-balancer-name) load_balancer_name=$2 shift ;; -a|--action-arn) action=$2 shift ;; -p|--period) period=$2 shift ;; -t|--threshold) threshold=$2 shift ;; -e|--evaluation-period) evaluation_period=$2 shift ;; --) shift break ;; *) shift break ;; esac shift done; # required fields if [ -z "$name" ]; then echo 'name is required'; exit 1; fi if [ -z "$load_balancer_name" ]; then echo 'load-balancer-name is required'; exit 1; fi if [ -z "$action" ]; then echo 'action-arn is required'; exit 1; fi # set defaults for option fields if [ -z "$period" ]; then period=60; fi if [ -z "$threshold" ]; then threshold=0; fi if [ -z "$evaluation_period" ]; then evaluation_period=3; fi if [ -z "$region" ]; then region=$DEFAULT_REGION; fi # Load Balancer High Latency Check aws cloudwatch put-metric-alarm --region $region --alarm-name "$name - LB High Latency" --alarm-description "$name - Load Balancer High Latency" --metric-name "Latency" --namespace "AWS/ELB" --statistic "Average" --period "$period" --threshold "$threshold" --comparison-operator "GreaterThanThreshold" --dimensions Name=LoadBalancerName,Value="$load_balancer_name" --evaluation-periods "$evaluation_period" --alarm-actions $action } ################################################## # Create cloudwatch for an ec2-instance CPU check # check alarm # Globals: # None # Arguments: # -n | --name : topicname # -i | --instance-ids : comma seperated lis of # ec2-instance ids # -a | --action-arn : action arn # -p | --period : seconds over which the specified # statistic is applied. # -t | --threshold : value against which the # specified statistic is compared # -e | --evaluation-period : The number of periods # over which data is compared to # the specified threshold # Returns: # null ################################################## jio_cloudwatch_utils__instance_cpu_check(){ while [[ $# -gt 0 ]]; do case "${1}" in -n|--name) name=$2 shift ;; -r|--region) region=$2 shift ;; -i|--instance-ids) instance_ids=$2 shift ;; -a|--action-arn) action=$2 shift ;; -p|--period) period=$2 shift ;; -t|--threshold) threshold=$2 shift ;; -e|--evaluation-period) evaluation_period=$2 shift ;; --) shift break ;; *) shift break ;; esac shift done; # required fields if [ -z "$name" ]; then echo 'name is required'; exit 1; fi if [ -z "$instance_ids" ]; then echo 'at least 1 instance-id is required'; exit 1; fi if [ -z "$action" ]; then echo 'action-arn is required'; exit 1; fi # set defaults for option fields if [ -z "$period" ]; then period=60; fi if [ -z "$threshold" ]; then threshold=0; fi if [ -z "$evaluation_period" ]; then evaluation_period=3; fi if [ -z "$region" ]; then region=$DEFAULT_REGION; fi # CPU Check aws cloudwatch put-metric-alarm --region $region --alarm-name "$name CPU Check" --alarm-description "$name - CPU usage" --namespace "AWS/EC2" --dimensions Name=InstanceId,Value="$instance_ids" --metric-name "CPUUtilization" --statistic "Average" --comparison-operator "GreaterThanThreshold" --unit "Percent" --period "$period" --threshold "$threshold" --evaluation-periods "$evaluation_period" --alarm-actions $action } ################################################## # gets the InstanceIds for ec2-instance using # the Tag "Name" # # Globals: # None # Arguments: # -n | --name : topicname # Returns: # comma seperated string of instanceIds ################################################## jio_cloudwatch_utils__getInstanceIds(){ while [[ $# -gt 0 ]]; do case "${1}" in -n|--name) name=$2 shift ;; -r|--region) region=$2 shift ;; -t|--cloudwatch-tag) cloudwatchTags=$2 shift ;; --) shift break ;; *) shift break ;; esac shift done; if [ -z "$region" ]; then region=$DEFAULT_REGION; fi if [ -z "$name" ] && [ -z "$cloudwatchTags" ]; then echo 'a comma seperated list of names or cloudwatch-tags is required'; exit 1; fi if [ ! -z "$name" ]; then nameFilters="Name=tag:Name,Values=$name " else nameFilters="" fi if [ ! -z "$cloudwatchTags" ]; then cwFilters="Name=tag:cloudwatch,Values=$cloudwatchTags" else cwFilters="" fi filters="$nameFilters $cwFilters" aws ec2 describe-instances --region $region --filters $filters | jq -r 'reduce (.Reservations[] | .Instances | map(.InstanceId) | join(",") ) as $item (""; . + $item+",")' | rev | cut -c 2- | rev } ################################################## # reads email address from the appropate # "emaillist" file # the Tag "Name" # # Globals: # None # Arguments: # -t | --cloudwatch-tag (optional) : used to # select correct emaillist file # e.g for `... -t jenkins ...` will read # emaillist-jenkins.txt # Returns: # comma seperated string of instanceIds ################################################## jio_cloudwatch_utils__getNotificationEmailList(){ while [[ $# -gt 0 ]]; do case "${1}" in -t|--cloudwatch-tag) tag=$2 shift ;; --) shift break ;; *) shift break ;; esac shift done; if [ ! -z "$tag" ]; then emails=$(cat emaillist-$tag.txt | grep -v '^$\|^\s*\#' | tr -d "[:blank:]" | tr '\n' , | rev | cut -c 2- | rev) else emails=$(cat emaillist.txt | grep -v '^$\|^\s*\#' | tr -d "[:blank:]" | tr '\n' , | rev | cut -c 2- | rev) fi echo $emails } ######### Example Usage ########## # # arn=$(jio_cloudwatch_utils__createTopic -n Jenkins-Cloudwatch -m [email protected],[email protected]) # echo $arn # jio_cloudwatch_utils__load_Balancer_Unhealthy_Host_Check -n Jenkins -l jenkins-frontend -a $arn # jio_cloudwatch_utils__Load_Balancer_High_Latency_Alarm_Set -n Jenkins -l jenkins-frontend -a $arn # # instance_id=$(wget -q -O - http://instance-data/latest/meta-data/instance-id) # jio_cloudwatch_utils__instance_cpu_check -n jenkins -i $instance_id -a $arn # #################################