21. September 2018

Using AutoScaling means new instances are created automatically. The IP of the system changes every time. Using Nagios as a monitoring tool becomes very difficult because the host IP’s don’t adapt automatically unless you start scripting something crazy. The easier way is to use CloudWatch to create custom metrics and monitor them with alarms.
How this could work for example with monitoring the mailq of postfix I’ll explain to you here.

Role for instance

First, the instances need a role with appropriate rights. How to assign roles to an instance is explained in another article.
https://www.sysonion.de/2018/09/21/aws-create-role-for-instance/

Push Cloudwatch Metric

If your instance now has the appropriate rights it can execute Cloudwatch commands.
On the instance you need the aws cli.

The first test you can run on the console of your system should look like this.

[root@sysonion]#aws --region eu-central-1 cloudwatch put-metric-data --metric-name "Mail Queue" --dimensions InstanceId=Id-123456 --namespace "Test Space" --value 3

You have to set ID of your instance and value with any number.
In Cloudwatch you have to see now an metric with the namesapce „Test Space“.

Switch on Sum in Statistics. You have to see the value 3 in your metric.

Cloudwatch

If the push now works, a script has to be created, which tells the Clouwatch metric the values in your desired interval.
Every 5 minutes I run a cronjob that executes the following script.

Script Cloudwatch Metric

#!/bin/sh
##### Cloudwatch Monitoring mailq Postfix ##############################
#############################################################
mailq=`mailq | grep -w "Mail queue is empty"` > /dev/null
if [ $? != 0 ];then
	mailq2=`mailq | /usr/bin/tail -n1 | /usr/bin/gawk '{print $5}'`
else
	mailq2=0
fi

instance=`ec2-metadata --instance-id |cut -c 14-`
aws --region eu-central-1 cloudwatch put-metric-data --metric-name "Mail Queue" --dimensions InstanceId=$instance --namespace "Test Space" --value $mailq2
##############################################################

Set alarm

Now you can create an alarm to send notifications vie e-mail or sms.

Cloudwatch Alarm

Your threshold value for your alarm is completely individual. I have set my threshold to 5. This means that at a period of 5 minutes the values are at 5 an alarm will be sent.  So if the mail queue of Postfix has 5 mails hanging in the queue over a period of 5 minutes, the notification by mail goes out.

Cloudwatch Alarm

Leave a Comment