Aws cloud watch
- role having cloudwatch and ssm permissions
- enable the cloudwatch agent (role auto created)
- after agent installed we need enable detail Monitoring
sudo apt update && sudo apt install stress -y
stress --vm 2 --vm-bytes 1G --timeout 300s ## add load cpu
Aws Monitoring
services
- cloud watch – logs, metrics, alarms and dashboard
- CloudTrail – Auditing (Account)
- XRAY- Tracing
- SNS- simple notification service (email/sms)
- SSM – Install/configure
All logs will store in ec2 in /var/log/
root@ip-172-31-24-181:/var/log# ls -l
total 540
lrwxrwxrwx 1 root root 39 Nov 18 05:16 README -> ../../usr/share/doc/systemd/README.logs
-rw-r--r-- 1 root root 736 Nov 18 05:21 alternatives.log
drwx------ 3 root root 4096 Nov 19 02:11 amazon
-rw-r----- 1 root adm 0 Nov 19 02:09 apport.log
drwxr-xr-x 2 root root 4096 Nov 19 02:16 apt
-rw-r----- 1 syslog adm 8083 Nov 19 02:37 auth.log
-rw-rw---- 1 root utmp 0 Nov 18 05:20 btmp
drwxr-x--- 2 _chrony _chrony 4096 Nov 19 02:09 chrony
-rw-r----- 1 root adm 3091 Nov 19 02:09 cloud-init-output.log
-rw-r----- 1 syslog adm 155093 Nov 19 02:09 cloud-init.log
drwxr-xr-x 2 root root 4096 Sep 28 14:32 dist-upgrade
-rw-r----- 1 root adm 50655 Nov 19 02:09 dmesg
-rw-r--r-- 1 root root 41173 Nov 19 02:16 dpkg.log
drwxr-sr-x+ 3 root systemd-journal 4096 Nov 19 02:09 journal
-rw-r----- 1 syslog adm 66980 Nov 19 02:16 kern.log
drwxr-xr-x 2 landscape landscape 4096 Nov 19 02:14 landscape
-rw-rw-r-- 1 root utmp 292292 Nov 19 02:14 lastlog
drwx------ 2 root root 4096 Nov 19 02:09 private
-rw-r----- 1 syslog adm 163699 Nov 19 02:40 syslog
drwxr-xr-x 2 root root 4096 Nov 19 02:09 sysstat
drwxr-x--- 2 root adm 4096 Nov 19 02:09 unattended-upgrades
-rw-rw-r-- 1 root utmp 1152 Nov 19 02:14 wtmp
Task
- create 2-ec2 instance and enable cloud watch and
- create sample dashboard with defult metrics
- run below scrpit genearte logs
#!/usr/bin/env python3
import json
import random
import time
import socket
import logging
from logging.handlers import RotatingFileHandler
from datetime import datetime, timezone
hostname = socket.gethostname()
# Default Linux log directory
LOG_FILE = "/var/log/dummy_metrics.log"
# Log rotation: 10 MB per file, keep 5 backups
handler = RotatingFileHandler(
LOG_FILE,
maxBytes=10 * 1024 * 1024,
backupCount=5
)
logging.basicConfig(
level=logging.INFO,
handlers=[handler],
format="%(message)s"
)
cpu = random.uniform(10, 40)
memory = random.uniform(30, 60)
disk = random.uniform(40, 70)
while True:
cpu = max(1, min(100, cpu + random.uniform(-10, 10)))
memory = max(1, min(100, memory + random.uniform(-3, 3)))
disk = max(1, min(100, disk + random.uniform(-1, 1)))
metrics = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"instance_id": hostname,
"cpu_percent": round(cpu, 2),
"memory_percent": round(memory, 2),
"disk_percent": round(disk, 2),
"network_in_mb": round(random.uniform(1, 100), 2),
"network_out_mb": round(random.uniform(1, 80), 2),
"disk_read_mb": round(random.uniform(1, 50), 2),
"disk_write_mb": round(random.uniform(1, 30), 2),
"http_requests": random.randint(10, 500),
"latency_ms": random.randint(50, 2000),
"status_code": random.choices(
[200, 404, 500],
weights=[90, 7, 3]
)[0]
}
# Write JSON log
logging.info(json.dumps(metrics))
time.sleep(5)
sudo chmod +x /opt/dummy_metrics.py
sudo nohup python3 /opt/dummy_metrics.py \
>/dev/null 2>&1 &