Create custom Cloudwatch metrics on AWZ cloud Ahmad Karawash
[email protected]
12/23/2015
1
Amazon Metrics • Metrics are used to monitor various resources in Amazon Web Services like EBS volumes, EC2 instances and RDS instances.
12/23/2015
2
AWS resources covered by CloudWatch
Amazon Ec2 EBS Volumes AutoScaling Groups Elastic load Balancers Amazon Route 53 RDS DB instances DynamoDB tables ElastiCache clusters RedShift clusters SQS queues SNS topics Storage Gateways 12/23/2015
3
Example of available metrics • Memory Utilization – Memory allocated by applications and the operating system, exclusive of caches and buffers, in percentages. • Memory Used – Memory allocated by applications and the operating system, exclusive of caches and buffers, in megabytes. • Memory Available – System memory available for applications and the operating system, in megabytes. • Disk Space Utilization – Disk space usage as percentages. • Disk Space Used – Disk space usage in gigabytes. • Disk Space Available – Available disk space in gigabytes. • Swap Space Utilization – Swap space usage as a percentage. • Swap Space Used – Swap space usage in megabytes. 12/23/2015
4
Cloudwatch advantage • AWS provides some additional monitoring scripts for adding custom Cloudwatch metrics.
12/23/2015
5
Use case Example • A user might have to take automated actions based on a particular parameter. This parameter can be the number of active users in a system which can be stored in an RDS instance. A script can be used to measure this value and if the number of users is zero for a particular time period, an alarm needs to be triggered and the EC2 instance needs to be terminated.
12/23/2015
6
Prerequisites • Install python • Sudo apt-get install python python dev • Sudo apt-get install python-pip
• Install boto • Sudo pip install boto
• Create RDS Database or local Database for the new custom metric, for example RoomTemperatureDatabase.
12/23/2015
7
put_metric_data • put_metric_data ( $namespace, $metric_data, $opt ): Publishes metric data points to Amazon CloudWatch. Amazon Cloudwatch associates the data points with the specified metric. If the specified metric does not exist, Amazon CloudWatch creates the metric. • If you create a metric with the PutMetricData action, allow up to fifteen minutes for the metric to appear in calls to the ListMetrics action.
12/23/2015
8
RoomTemperatureMeasure Database
12/23/2015
9
Create a Custom Metric and connect to external database Run the python code from Ubuntu console: (how to run check: https://www.youtube.com/watch?v=zKUu4in858A} 1. from boto.ec2.cloudwatch import connect_to_region 2. import MySQLdb 3. import boto.ec2.cloudwatch 4. db=MySQLdb.connect(“Public DNS","username","password","databasename") 5. cursor=db.cursor() 6. n=cursor.execute(" select value from Temperature ORDER BY id DESC LIMIT 1 ") 7. n1=cursor.fetchone()
12/23/2015
10
Create a Custom Metric and connect to external database 8. 9. 10. 11. 12. 13.
s=str(n1) table=string.maketrans( '', '', ) number=s.translate(table,"(){},L") numbers=int(number) reg='ap-west-1' conn_cw=boto.ec2.cloudwatch.connect_to_region(reg, aws_access_key_id=’your_access_key’,aws_secret_access_key=’your_secret_k ey’) 14. conn_cw.put_metric_data(namespace='my_namespace',name='my_metric',val ue=numbers,dimensions={'InstanceId':‘instance-val'}) 12/23/2015
11
Create a Custom Cloudwatch Metric • After you connect successively to the database, “put-metric-data” method will add instance of a new custom metric into you cloudwatch
12/23/2015
12
How to check whether the custom metric was created
12/23/2015
13
How to check whether the custom metric was created
12/23/2015
14
Creating a CloudWatch alarm for the custom metric • Now you can monitor you own metric using Cloudwatch • For example, you can add alarm: 1. from boto.ec2.cloudwatch import connect_to_region 2. import boto.ec2.cloudwatch 3. metric=cw.list_metrics(dimensions={'InstanceId':’ia1b2c3d4’},metric_name='my_metric')[0] 4. metric.create_alarm(name='my_alarm', comparison='