-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull-stack-health.sh
More file actions
48 lines (39 loc) · 1.34 KB
/
full-stack-health.sh
File metadata and controls
48 lines (39 loc) · 1.34 KB
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
#!/bin/bash
LOG_FILE="/var/log/full-stack-health.log"
DATE=$(date "+%Y-%m-%d %H:%M:%S")
REDIS_HOST="127.0.0.1"
REDIS_PORT="6379"
NODE_APP="my-app.js"
MEM_LIMIT=500
echo "=== Full Stack Health Report - $DATE ===" >> $LOG_FILE
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
MEM=$(free | awk '/Mem/ {printf("%.0f"), $3/$2 * 100}')
DISK=$(df / | awk 'END{print $5+0}')
echo "CPU Usage: $CPU%" >> $LOG_FILE
echo "Memory Usage: $MEM%" >> $LOG_FILE
echo "Disk Usage: $DISK%" >> $LOG_FILE
if systemctl is-active --quiet postgresql; then
echo "PostgreSQL: Running" >> $LOG_FILE
else
echo "PostgreSQL: DOWN!" >> $LOG_FILE
fi
if systemctl is-active --quiet redis-server; then
echo "Redis: Running" >> $LOG_FILE
MEM_USAGE=$(redis-cli -h $REDIS_HOST -p $REDIS_PORT info memory | grep used_memory: | awk -F: '{print $2}')
MEM_MB=$((MEM_USAGE/1024/1024))
echo "Redis Memory Usage: ${MEM_MB}MB" >> $LOG_FILE
if [ $MEM_MB -gt $MEM_LIMIT ]; then
redis-cli -h $REDIS_HOST -p $REDIS_PORT FLUSHALL
echo "Redis cache cleared!" >> $LOG_FILE
fi
else
echo "Redis: DOWN!" >> $LOG_FILE
fi
if pgrep -f $NODE_APP > /dev/null; then
echo "Node.js app: Running" >> $LOG_FILE
else
echo "Node.js app: DOWN!" >> $LOG_FILE
fi
echo "" >> $LOG_FILE
# Run every 30 mins
# -> */30 * * * * /home/ubuntu/scripts/full-stack-health.sh