-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgentDiskSpace_V5
43 lines (38 loc) · 1.72 KB
/
AgentDiskSpace_V5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
# Define the name of the Docker container to check
DOCKER_CONTAINER_NAME="my-docker-container"
# Define the email addresses to send the alert to (separated by commas)
# Define the disk utilization threshold
THRESHOLD=80
# Get the ID of the running container
DOCKER_CONTAINER_ID=$(docker ps -qf "name=$DOCKER_CONTAINER_NAME")
# Check if the container is running
if [ -n "$DOCKER_CONTAINER_ID" ]; then
# Get the disk usage inside the container
DOCKER_DISK_USAGE=$(docker exec -it $DOCKER_CONTAINER_ID df -h / | awk 'NR==2{print $5}' | cut -d'%' -f1)
# Get the memory usage inside the container
DOCKER_MEMORY_USAGE=$(docker stats --no-stream $DOCKER_CONTAINER_NAME --format "{{.MemUsage}}")
# Get the CPU usage inside the container
DOCKER_CPU_USAGE=$(docker stats --no-stream $DOCKER_CONTAINER_NAME --format "{{.CPUPerc}}")
# Check if the disk usage is above the threshold
if [ "$DOCKER_DISK_USAGE" -gt "$THRESHOLD" ]; then
# Send an email alert in HTML format
echo "Disk usage inside the Docker container is above $THRESHOLD%" | mail -s "Disk usage alert inside Docker container $DOCKER_CONTAINER_NAME" -a "Content-Type: text/html" $ALERT_EMAILS << EOF
<html>
<body>
<p>Disk usage inside the Docker container is above $THRESHOLD%.</p>
<p><strong>Details:</strong></p>
<ul>
<li>Docker container name: $DOCKER_CONTAINER_NAME</li>
<li>Disk usage: $DOCKER_DISK_USAGE%</li>
<li>Memory usage: $DOCKER_MEMORY_USAGE</li>
<li>CPU usage: $DOCKER_CPU_USAGE</li>
</ul>
</body>
</html>
EOF
fi
else
echo "Docker container $DOCKER_CONTAINER_NAME is not running."
fi