Get EC2 CPU utilization using AWS SDK - amazon-web-services

I need to create a AWS Lambda function in JAVA to print the EC2 instance's CPU Utilisation. How to get the CPU utilization of EC2 instance using AWS JAVA SDK.

Amazon CloudWatch maintains metrics about every Amazon EC2 instance, including CPU Utilization.
By default, metrics are collected at 5-minute intervals at no charge. You can enable detailed monitoring for an instance, which will collect metrics at 1-minute intervals. Additional charges apply.
You can obtain the metrics via a call to Amazon CloudWatch. Use the getMetricData() function.
Note that you actually request a calculated value over a period of time, such as "Average CPU for the previous 5 minutes".

Related

Not getting node metrics for mysqld_exporter and postgres_exporter for RDS instances

I have setup a system for RDS monitoring and used mysqld_exporter and postgres_exporter and exposed their metrics to prometheus server but I am not getting important metrics to monitor like CPU_Utilization, memory available, iops rate, latency etc. I am getting about 1800 metrics but none of them are these I suppose. I think i need node exporter for RDS monitoring for getting cpu utilization but i don't know how to configure node_exporter for RDS instance.
PS: I don't want to use AWS Cloudwatch metrics

AWS Resource Usage Data - CPU, Memory and Disk

I am trying to build an analytics Dashboard using the below Metrics/KPIs for all the EC2 Instance.
Total CPU vs CPUUtilized
Total RAM vs RAMUtilized
Total EBS Volume vs EBSUtilized.
For example, I have lunch an EC2 instance with 4 CPU, 16GiB RAM and 50GB SSD, I would like to know the above KPIs in a time series trend. I am not getting any clue on where to get the data from EC2. Tried the EC2 instance metrics through CloudWatch using boto3 client, however did not get the above Metrics. I would like to know :
Where to find the data with above Metrics ?
Need the above metrics data in s3 on an daily basis.
Similarly is there a way to get similar metrics for AWS RDS and AWS EKS Cluster ?
Thanks!
The Amazon EC2 service collects information about the virtual machine (instance) and sends it to Amazon CloudWatch Logs.
See: List the available CloudWatch metrics for your instances - Amazon Elastic Compute Cloud
Note that it only collects metrics that can be observed from the virtual machine itself -- CPU Utilization, network traffic and Amazon EBS traffic. The EC2 service cannot see what is happening 'inside' the instance, since it is the Operating System that controls memory and manages the contents of the disks.
If you wish to collect metrics from the Operating System, then you would need to Collect metrics and logs from Amazon EC2 instances and on-premises servers with the CloudWatch agent - Amazon CloudWatch. This agent runs in the instance and sends metrics out to CloudWatch.
You can write code that calls the CloudWatch Metrics APIs to retrieve metrics. Note that the metrics returned are calculated over a time period (eg average CPU Utilization over a 5-minute period). It is not possible to retrieve the actual raw datapoints.
See also:
Monitoring Amazon RDS metrics with Amazon CloudWatch - Amazon Relational Database Service
Amazon EKS and Kubernetes Container Insights metrics - Amazon CloudWatch

How to monitor disk utilization in instances inside an auto scaling group

I want to monitor the average Disk Utilization across all the EC2 instances inside an auto scaling group.
I know I can monitor CPU Utilization across all instances inside an auto scaling group because there's a CloudWatch metrics for that. However, I need CloudWatch Agent to monitor the Disk Utilization. Is there a good way to monitor this? Or do I need to implement my own automation using Lambda?
CPU Utilization is managed by the Operating System, which runs 'inside' the EC2 instance. AWS has no access to your Operating System and, therefore, no knowledge of your disk utilization. For the same reason, an AWS Lambda function would not be able to access this information either.
Instead, Collect metrics and logs from Amazon EC2 instances and on-premises servers with the CloudWatch agent - Amazon CloudWatch. The CloudWatch is probably already installed on the instance and can be configured to collect metrics such as:
disk_total
disk_used
disk_used_percent
mem_available
mem_used

how to stop an EC2 instance after checking the memory utilization

I want to stop my EC2 instances if the memory utilization is more than x% from my Lambda function(python) , is their any possibility to check the memory utilization of an EC2 instance
For EC2 by default it will only have the host level metrics be accessible, this includes CPU, Disk Performance and Network Performance but does not include other metrics such as Memory Utilization.
For this you will need to push a custom metric from the EC2 instance into AWS, this can be performed by installing the CloudWatch Agent.
Once you have the memory metric being pushed into CloudWatch you can create an alarm that will trigger on a specific threshold being exceeded, allowing you to trigger an SNS topic. This can have a Lambda subscribe to the topic to be triggered under the condition.
You need to install the CloudWatch agent on the EC2 instances, if it's not there already. Then the memory usage will be a metric in CloudWatch that your Lambda function can query.

AWS EC2 Autoscaling Average CPU utilization v.s. Grafana CPU utilization

We want to use AWS predictive scaling to forecast the load and CPU so this will certainly help us move away from manually launching instances based on load. We created new scaling plan by choosing EC2 Autoscaling group and enabling predictive scaling(forecast only for now). But we noticed that the CPU graph on Grafana is different from AWS Average CPU utilization. Grafana is getting alerts from elasticsearch which gets logs directly from services running in ec2. I am not sure why they don't show the same percentage of CPU Utilization and am wondering why AWS CPU Utilization is lower than the CPU shows on Grafana? If so can autoscaling scales the instances correctly?
AWS Autoscaling group Average CPU utilization
Grafana Averge CPU graph
AWS has its own method of computing CPU Util which is based on "EC2 compute units" so it is possible that value will differ when compared to another way of calculating the same metrics.