
Python3 sample script to get metrics from AWS CloudWatch. In the example, we get the maximum value in the last minute and display only the value, this is necessary if you want to collect metrics for example in Zabbix.
Script:
#!/usr/bin/env python3
import boto3
import datetime
awsRegion = "eu-west-1"
namespace = "AWS/ElastiCache"
metric = "CurrConnections"
statistics = "Maximum"
period = 60 # Seconds
timeRange = 1 # Minutes
client = boto3.client('cloudwatch', region_name=awsRegion)
startTime = (datetime.datetime.utcnow() - datetime.timedelta(minutes=timeRange))
startTime = startTime.strftime("%Y-%m-%dT%H:%M:%S")
endTime = datetime.datetime.utcnow()
endTime = endTime.strftime("%Y-%m-%dT%H:%M:%S")
response = client.get_metric_statistics(
Namespace=namespace,
MetricName=metric,
StartTime=startTime,
EndTime=endTime,
Period=period,
Statistics=[
statistics,
]
)
for cw_metric in response['Datapoints']:
print(cw_metric['Maximum'])