Supercharge Your Monitoring

Raj Kumar
Cloud Native Daily
Published in
4 min readApr 15, 2024

Install and Use CloudWatch Agent on Your Instance with Procstat Plugin

Hey there, cloud enthusiasts! Today, we’re diving into the world of AWS CloudWatch and its amazing agent that simplifies monitoring your instances. Whether you’re a seasoned pro or just getting started, this guide will walk you through installing and using the CloudWatch agent with the procstat plugin, allowing you to monitor specific processes in easy-to-follow steps. Let’s get those metrics flowing and keep your resources in tip-top shape!

What is the CloudWatch Agent?

Think of the CloudWatch agent as your trusty sidekick, constantly checking your instance’s health and sending valuable data (metrics and logs) to CloudWatch for analysis. This allows you to monitor things like CPU usage, memory, and application logs, giving you a clear picture of how your instance is performing.

Step 1: Gear Up — Creating an IAM Role

Before we install the agent, we need to create a special IAM role that gives it the permissions it needs to work its magic. Head over to the IAM console in your AWS account and follow these steps .

  • Click on “Roles” in the navigation pane.
  • Hit the “Create role” button.
  • Choose “EC2” under “Choose the service that will use this role”.
  • Click “Next: Permissions”.
  • Search for “CloudWatchAgentServerPolicy” and select the checkbox next to it. This policy grants the agent the necessary permissions.
  • Give your role a descriptive name (e.g., CloudWatchAgentServerRole) and click “Create role”.

Create AWS Instance:

(I have a instance which is running on the Amazon-linux )

Connect IAM Role to the instance

Select the created IAM Role

Step 2: Installing the Agent on Your Instance

  1. Connect to your instance: Use SSH to connect to your instance.
  2. Update package lists: Depending on your OS, you might need to update the package list before installing anything new. sudo yum update
  3. Install the agent: Use the following command to install the CloudWatch agent:
sudo yum install amazon-cloudwatch-agent

Step 3: Configuration Time — Let’s Talk to CloudWatch ️

The CloudWatch agent needs some instructions on what data to collect and where to send it. We’ll use a configuration file for this. Here’s how to set it up:

  1. Navigate to the configuration directory: Use the cd command to move to the directory where the agent configuration files are stored. Typically, it's /opt/aws/amazon-cloudwatch-agent/config.
  2. Create a new configuration file: Use a text editor like nano/vi to create a new file (e.g., my-config.yaml).

Step 4: Craft Your Configuration File (Let’s Monitor a Process!)

Here’s a basic example of a configuration file that collects CPU and memory metrics and adds the procstat plugin to monitor a specific process by its PID (process ID):

agent:
- webserver
logs:
# This section is for configuring log collection (we'll skip for now)
metrics:
- namespace: "CWAgent"
metric_name: "CPUUtilization"
period: 60
- namespace: "CWAgent"
metric_name: "MemoryUtilization"
period: 60
# Procstat plugin configuration to monitor a process by PID
- plugin: "procstat"
# You can find the PID using the "ps aux" command
processes:
- pid: 1044
collectors:
- cpu: {}
- memory: {}

Important Notes:

  • Replace 1044 with the actual PID of the process you want to monitor. You can find the PID using the ps aux command on your instance.

Step 5: Start the Agent and Verify

  1. Start the agent: Use the following command to start the CloudWatch agent:
sudo systemctl start amazon-cloudwatch-agent.service  # For systemd based systems
# OR
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a start # For older init systems
  • Verify the logs: Check the agent logs using the following command:
sudo journalctl -u amazon-cloudwatch-agent.service

Bonus Tip: The procstat plugin offers various collectors to gather different metrics about your process.

With this configuration, the CloudWatch agent will not only collect system-wide metrics but also delve deeper by monitoring the CPU and memory usage of your specific process, providing valuable insights into its performance.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

RAJKUMAR
DevOps | AWS ☁️| LINUX🐧

let’s stay connected

🟡Linkedin: https://www.linkedin.com/in/raj-kumar-devops/

🔴Website: https://bento.me/rajdevops

🟢For more Projects: https://medium.com/@rajdevops

--

--