summaryrefslogtreecommitdiff
path: root/do_the_needful.sh
diff options
context:
space:
mode:
authordoc <doc@filenotfound.org>2025-06-30 20:11:52 +0000
committerdoc <doc@filenotfound.org>2025-06-30 20:11:52 +0000
commit41e897f4945aaf8fbcdf0b12ac2f08c5e6ae0458 (patch)
treedb7c3520fd91abc3cf56b1a52095d23f3a80d059 /do_the_needful.sh
commit of legacy codeHEADmaster
Diffstat (limited to 'do_the_needful.sh')
-rwxr-xr-xdo_the_needful.sh60
1 files changed, 60 insertions, 0 deletions
diff --git a/do_the_needful.sh b/do_the_needful.sh
new file mode 100755
index 0000000..b927847
--- /dev/null
+++ b/do_the_needful.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+# === CONFIG ===
+SWAPPINESS_LEVEL=10
+LOG_CLEANUP_LIMIT_DAYS=14
+APACHE_SERVICES=("apache2" "httpd")
+HOST=$(hostname)
+TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
+
+# === Telegram Config ===
+BOT_TOKEN="8178867489:AAH0VjN7VnZSCIWasSz_y97iBLLjPJA751k"
+CHAT_ID="1559582356"
+
+echo "🔧 [$HOST] Starting health cleanup..."
+
+# 1. Tune swappiness
+echo "→ Setting vm.swappiness to $SWAPPINESS_LEVEL"
+echo "vm.swappiness=$SWAPPINESS_LEVEL" | tee /etc/sysctl.d/99-swappiness.conf > /dev/null
+sysctl -p /etc/sysctl.d/99-swappiness.conf > /dev/null
+
+# 2. Disable Apache if not needed
+apache_disabled=""
+for svc in "${APACHE_SERVICES[@]}"; do
+ if systemctl list-units --type=service --all | grep -q "$svc"; then
+ echo "→ Apache service '$svc' detected"
+ if ! ss -tulpn | grep -q ":80"; then
+ echo " 🔕 Apache appears idle. Disabling..."
+ systemctl disable --now "$svc"
+ apache_disabled="yes"
+ else
+ echo " ⚠️ Apache is running and serving. Skipping stop."
+ fi
+ fi
+done
+
+# 3. Clean logs older than X days
+echo "→ Cleaning logs older than $LOG_CLEANUP_LIMIT_DAYS days in /var/log"
+find /var/log -type f -name "*.log" -mtime +$LOG_CLEANUP_LIMIT_DAYS -exec rm -f {} \;
+
+# 4. Summary Info
+MEM=$(free -h | grep Mem | awk '{print $4 " free"}')
+SWAP=$(free -h | grep Swap | awk '{print $3 " used"}')
+DISK=$(df -h / | awk 'NR==2 {print $4 " free"}')
+LOAD=$(uptime | awk -F'load average:' '{print $2}' | xargs)
+
+MSG="✅ [$HOST] Cleanup completed at $TIMESTAMP
+Memory: $MEM
+Swap: $SWAP
+Disk: $DISK
+Load: $LOAD"
+
+if [ "$apache_disabled" == "yes" ]; then
+ MSG="$MSG
+Apache was detected and disabled ✅"
+fi
+
+# 5. Send Telegram message
+curl -s -X POST https://api.telegram.org/bot$BOT_TOKEN/sendMessage \
+ -d chat_id="$CHAT_ID" \
+ -d text="$MSG"