Last active
January 1, 2019 17:22
-
-
Save ahawkins/b9226a01e74387216234d81fdfb7bc24 to your computer and use it in GitHub Desktop.
Install cron job to report memory utilization to cloudwatch
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 characters
#!/usr/bin/env bash | |
set -xeuo pipefail | |
main() { | |
# TODO: add all your desired regions here | |
local -a regions=(ap-south-1) | |
for region in "${regions[@]}"; do | |
aws --region "${region}" cloudwatch put-metric-alarm \ | |
--alarm-name 'UnderutilizedCPU' \ | |
--alarm-description 'Detects instances with under utilized CPU across long time horizons' \ | |
--metric-name 'CPUUtilization' \ | |
--namespace 'AWS/EC2' \ | |
--statistic Average \ | |
--period 300 \ | |
--evaluation-periods 288 \ | |
--threshold 15 \ | |
--comparison-operator LessThanThreshold | |
aws --region "${region}" cloudwatch put-metric-alarm \ | |
--alarm-name 'UnderutilizedMemory' \ | |
--alarm-description 'Detects instances with under utilized memory across long time horizons' \ | |
--metric-name 'MemoryUtilization' \ | |
--namespace 'System/Linux' \ | |
--statistic Average \ | |
--period 300 \ | |
--evaluation-periods 288 \ | |
--threshold 30 \ | |
--comparison-operator LessThanThreshold | |
done | |
} | |
main "$@" |
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 characters
#!/usr/bin/env bash | |
install_dependencies() { | |
case "$(uname -a | tr '[:upper:]' '[:lower:]')" in | |
*ubuntu*) | |
sudo apt-get update | |
sudo apt-get install -y unzip curl libwww-perl libdatetime-perl | |
;; | |
*) | |
echo "Unknown linux distro!" 1>&2 | |
return 1 | |
esac | |
} | |
install_scripts() { | |
curl http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip -O | |
sudo mkdir /opt | |
sudo unzip CloudWatchMonitoringScripts-1.2.1.zip -d /opt | |
/opt/aws-scripts-mon/mon-put-instance-data.pl --verify --mem-util --verbose | |
} | |
add_cron() { | |
local job="$(mktemp)" | |
echo '*/5 * * * * /opt/aws-scripts-mon/mon-put-instance-data.pl --mem-util --from-cron' > "${job}" | |
case "$(uname -a | tr '[:upper:]' '[:lower:]')" in | |
*ubuntu*) | |
sudo cp "${job}" /etc/cron.d/cloudwatch_metrics | |
;; | |
*) | |
echo "Unknown linux distro!" 1>&2 | |
return 1 | |
esac | |
} | |
main() { | |
install_dependencies "$@" | |
install_scripts "$@" | |
add_cron "$@" | |
echo "Done! Check syslog for cron job logs. View metrics in Cloudwatch" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment