-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgentDiskSpace_V7
50 lines (45 loc) · 2.18 KB
/
AgentDiskSpace_V7
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
44
45
46
47
48
49
50
#!/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}}")
# Get the network usage inside the container
DOCKER_NETWORK_USAGE=$(docker stats --no-stream $DOCKER_CONTAINER_NAME --format "{{.NetIO}}")
# Get the block IO usage inside the container
DOCKER_BLOCK_IO_USAGE=$(docker stats --no-stream $DOCKER_CONTAINER_NAME --format "{{.BlockIO}}")
# Check if the disk usage is above the threshold
if [ "$DOCKER_DISK_USAGE" -gt "$THRESHOLD" ]; then
# Send an email alert in HTML format
SUBJECT="Disk usage alert inside Docker container $DOCKER_CONTAINER_NAME"
BODY="<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>
<li>Network usage: $DOCKER_NETWORK_USAGE</li>
<li>Block IO usage: $DOCKER_BLOCK_IO_USAGE</li>
</ul>
</body>
</html>"
# Send email alert with the given details
echo "$BODY" | mail -s "$SUBJECT" -a "Content-Type: text/html" -a "Cc: $ALERT_EMAILS" -a "From: [email protected]" "$ALERT_EMAILS"
fi
else
echo "Docker container $DOCKER_CONTAINER_NAME is not running."
fi