mon-disk-space.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env bash
  2. ## Author: Joseph Snow (snow@sleepless.com) 11/10/2011
  3. ## script to check local disk space and submit data to AWS cloudwatch
  4. ## VARS
  5. export FC_HOME=~/fc
  6. export AWS_CLOUDWATCH_HOME=$FC_HOME/util/CloudWatch-1.0.12.1
  7. export PATH=$AWS_CLOUDWATCH_HOME/bin:$PATH
  8. export JAVA_HOME=/usr/lib/jvm/jre
  9. ## cron example
  10. ## ## this script updates custom disk space stats to AWS cloudwatch. this script should be run every 5 minutes
  11. ## */5 * * * * /home/ec2-user/fc/util/mon-disk-space.sh
  12. ## this scripts expects these vars to be set
  13. ## export AWS_ACCESS_KEY_ID=<YOUR_AWS_ACCESS_KEY_ID>
  14. ## export AWS_SECRET_ACCESS_KEY=<YOUR_AWS_SECRET_ACCESS_KEY>
  15. if test -e "$FC_HOME/fcbackups/.fcbackup.env" ; then
  16. source "$FC_HOME/fcbackups/.fcbackup.env"
  17. fi
  18. path='/'
  19. if [ -n "$1" ]; then
  20. path=$1
  21. fi
  22. # get ec2 instance id
  23. instanceid=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`
  24. freespace=`df --local --block-size=1M $path | grep $path | tr -s ' ' | cut -d ' ' -f 4`
  25. usedpercent=`df --local $path | grep $path | tr -s ' ' | cut -d ' ' -f 5 | grep -o "[0-9]*"`
  26. echo "mon-disk-space AWS cloudwatch custom diskspace monitor"
  27. echo "instanceid: $instanceid"
  28. echo "freespace: $freespace"
  29. echo "usedpercent: $usedpercent"
  30. # send the stats to AWS cloudwatch using the CloudWatch tools
  31. mon-put-data --I $AWS_ACCESS_KEY_ID --S $AWS_SECRET_ACCESS_KEY --region "$EC2_REGION" --metric-name "FreeSpaceMBytes" --namespace "System/Linux" --dimensions "InstanceId=$instanceid,Path=$path" --value "$freespace" --unit "Megabytes"
  32. mon-put-data --I $AWS_ACCESS_KEY_ID --S $AWS_SECRET_ACCESS_KEY --metric-name "UsedSpacePercent" --namespace "System/Linux" --dimensions "InstanceId=$instanceid,Path=$path" --value "$usedpercent" --unit "Percent"
  33. echo "done"