Using Custom Amazon CloudWatch Metrics

Standard

日志样式如下:

...
[2015-07-16 09:01:29] production.ERROR: Aws ...
...xxx...
[2015-07-16 09:07:23] production.ERROR: [Notification - getDetails] ...
...xxx...
[2015-07-16 09:16:04] production.ERROR: URL: ...
...xxx...
...

放crontab里跑的shell

#!/bin/bash
if  [ ! -n "$1" ] ;then
  echo -e "no argument\nusage: ./SendPHPErrorNumber2Cloudwatch.sh 5"
  exit
fi
periodMinute=$1
 
dateNow=`date "+%Y-%m-%d"`
 
logFile="/var/log/yemaosheng_com_php-log-"${dateNow}".txt"
if [ ! -f "$logFile" ]; then
  echo "No File"
  exit
fi
 
minuteAgo=`date "+%Y-%m-%d %H:%M:%S" -d "${periodMinute} minute ago"`
 
instanceId=`GET http://169.254.169.254/latest/meta-data/instance-id`
 
#tail -n 500 ${logFile} | awk '/^\[/{print $1" "$2}'
#tail -n 500 ${logFile} | grep ERROR
#    sed 's/^.//' | sed 's/.$//'
#    tr -d "[" | tr -d "]"
#    cut -c 2-20
 
errorNum=`tail -n 500 ${logFile} | grep ERROR | cut -c 2-20 | awk '{if ($0>minuteAgo){print minuteAgo} }' minuteAgo="$minuteAgo" | wc -l`
 
/usr/local/bin/aws cloudwatch put-metric-data --metric-name PHPError --namespace LogError --dimensions Name=InstanceId,Value=${instanceId} --value ${errorNum} --timestamp `date "+%Y-%m-%dT%H:%M:%S.000Z"`

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.