[PYTHON] Sample script to monitor Load Average on CloudWatch on AWS EC2

#!/usr/bin/python
# coding: utf-8
import os
import commands
import boto.ec2.cloudwatch

AWS_ACCESS_KEY = '********'
AWS_SECRET_ACCESS_KEY = '********'
AWS_REGION_NAME = '********'


def main():
    instance_id = commands.getoutput("wget -q -O - http://169.254.169.254/latest/meta-data/instance-id")
    load_averages = os.getloadavg()

    cloud_watch = boto.ec2.cloudwatch.connect_to_region(
        aws_access_key_id=AWS_ACCESS_KEY ,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
        region_name=AWS_REGION_NAME,
    )

    cloud_watch.put_metric_data(
        namespace='LoadAverage',
        name='LoadAverage',
        value=float(load_averages[0]),
        dimensions={
            'InstanceId': instance_id,
        }
    )

if __name__ == '__main__':
    main()

Recommended Posts

Sample script to monitor Load Average on CloudWatch on AWS EC2
[2020 version] How to install Python3 on AWS EC2
[AWS EC2] How to install Maven on Amazon Linux 2
[AWS] Install node.js on EC2 instance and execute sample program
Building an environment to execute python programs on AWS EC2
Things to note when running Python on EC2 from AWS Lambda
Preparing to run Flask on EC2
How to set up a jupyter notebook on ssh destination (AWS EC2)
Change AWS EC2 instance from t2 to t3
Sample script to trap signals in Python
Sample to reference Poetry project on GitHub
I tried to reduce costs by starting / stopping EC2 collectively on AWS Lambda