Monday 21 September 2015

Create cloudwatch memory metric on Linux/Windows

http://docs.aws.amazon.com/cli/latest/reference/cloudwatch/put-metric-data.html 
https://aws.amazon.com/cloudwatch/pricing/

Custom Cloudwatch Metric $0.50/month

Add following to cron (change to your instance-id):
*/5 * * * *  source /etc/profile && aws cloudwatch put-metric-data --metric-name MemoryUsedMB --namespace "System/Linux" --dimensions "InstanceId=`wget -q -O- http://169.254.169.254/latest/meta-data/instance-id`" --value `free -m | grep Mem: | awk ' { print $3-$6-$7 } '`  --unit "Megabytes" --timestamp `date -u +'\%Y-\%m-\%dT\%H:\%M:00'`

See graph on Cloudwatch -> Custom Metric -> System/Linux

Can do similar with Windows:
1. Install AWS CLI:
http://docs.aws.amazon.com/cli/latest/userguide/installing.html#install-msi-on-windows
2. Configure AWS CLI if you are not using IAM instance roles
3. Download wget (so you don't have to hardcode instance-id) to c:\service
         https://eternallybored.org/misc/wget/current/wget.exe 
4. Create scheduled tasks
            set command="powershell c:\service\usedmemory.ps1"
            schtasks /create /sc minute /mo 5 /tn "Usedmemory" /tr %command%
usedmemory.ps1 (create in c:\service) :
$memoryInfo = gwmi -Query "SELECT TotalVisibleMemorySize, FreePhysicalMemory FROM Win32_OperatingSystem"
$totalVisibleMemorySize = $memoryInfo.TotalVisibleMemorySize
$freePhysicalMemory = $memoryInfo.FreePhysicalMemory
$used = ($totalVisibleMemorySize - $freePhysicalMemory) / 1024
$now = get-date -Format U
$d = get-date $now -uformat %Y-%m-%dT%H:%M:00
$instance = /service/wget -q -O- http://169.254.169.254/latest/meta-data/instance-id
echo "$d $instance $used" >> /service/usedmemory.out
aws cloudwatch put-metric-data --metric-name MemoryUsedMB --namespace "System/Windows" --dimensions "InstanceId=$instance" --value $used --unit "Megabytes" --timestamp $d
      
Can then retrieve data from cloudwatch via CLI:

$ aws cloudwatch list-metrics | jq ' .Metrics[] | "\(.Namespace),\(.MetricName),\(.Dimensions[0].Name),\(.Dimensions[0].Value)" ' | grep -i memory | egrep -v 'RDS|ElasticMapReduce|ElastiCache'

$ aws cloudwatch get-metric-statistics --namespace 'System/Linux'  --dimensions "Name=InstanceId,Value=i-db8d5667" --metric-name MemoryUsedMB --start-time `date -u --date="15 days ago" +'%Y-%m-%dT%H:%M:00'` --end-time `date -u +'%Y-%m-%dT%H:%M:00'`  --period 900 --statistic Maximum  | grep -i Maximum | cut -d':' -f2  | cut -d',' -f1 | sort -n | cat -n  | awk ' { print $1 " " $2 } ' | tail
1431 371.0
1432 371.0
1433 371.0
1434 371.0
1435 372.0
1436 374.0
1437 375.0
1438 376.0
1439 419.0
1440 429.0

aws cloudwatch get-metric-statistics --namespace System/Linux --dimensions Name=InstanceId,Value=i-2080d4ee --metric-name MemoryUsedMB --start-time `date -u --date="1 hour ago" +'%Y-%m-%dT%H:%M:00'` --end-time `date -u +'%Y-%m-%dT%H:%M:00'` --period 60 --statistic Average | jq ' .Datapoints[] | "\(.Timestamp),\(.Average)" ' | sort

Requires IAM policy:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1461280108000",
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
See also:
https://aws.amazon.com/blogs/aws/amazon-cloudwatch-monitoring-scripts-for-microsoft-windows/
https://github.com/moomindani/aws-mon-linux

No comments:

Post a Comment