proxmox monitori

This commit is contained in:
Marius
2025-09-30 02:06:34 +03:00
parent e3085884d9
commit 24c8c75eb6
7 changed files with 425 additions and 0 deletions

65
proxmox/vm107-monitor.sh Normal file
View File

@@ -0,0 +1,65 @@
#!/bin/bash
HOSTNAME=$(hostname)
DATE=$(date '+%Y-%m-%d %H:%M:%S')
VM_ID=107
LOG_FILE="/var/log/vm107-monitor.log"
check_vm_status() {
local vm_status=$(qm status $VM_ID 2>/dev/null | grep -oP 'status: \K.*')
if [ -z "$vm_status" ]; then
echo "ERROR:VM_NOT_FOUND"
return 1
fi
if [ "$vm_status" = "running" ]; then
if ps aux | grep -E "(kvm|qemu).*-id $VM_ID" | grep -qv grep; then
echo "RUNNING:OK"
return 0
else
echo "ERROR:PROCESS_MISSING"
return 1
fi
elif [ "$vm_status" = "stopped" ]; then
echo "STOPPED:VM_STOPPED"
return 1
elif [ "$vm_status" = "internal-error" ]; then
echo "ERROR:INTERNAL_ERROR"
return 1
else
echo "ERROR:UNKNOWN_STATE:$vm_status"
return 1
fi
}
RESULT=$(check_vm_status)
CHECK_EXIT=$?
STATUS=$(echo "$RESULT" | cut -d: -f1)
ERROR_TYPE=$(echo "$RESULT" | cut -d: -f2)
echo "$(date '+%Y-%m-%d %H:%M:%S'): VM $VM_ID status: $STATUS ($ERROR_TYPE)" >> "$LOG_FILE"
if [ $CHECK_EXIT -ne 0 ]; then
SUBJECT="VM $VM_ID $STATUS on $HOSTNAME"
BODY="VM $VM_ID Status: $STATUS\n"
BODY+="Error Type: $ERROR_TYPE\n"
BODY+="Check Time: $DATE\n"
BODY+="Host: $HOSTNAME\n\n"
if [ "$STATUS" = "STOPPED" ]; then
BODY+="VM is stopped. To start: qm start $VM_ID"
elif [ "$ERROR_TYPE" = "INTERNAL_ERROR" ]; then
BODY+="VM has internal-error (KVM crash)."
elif [ "$ERROR_TYPE" = "PROCESS_MISSING" ]; then
BODY+="VM marked as running but process missing."
fi
# Trimite email simplu prin mail command
echo -e "$BODY" | mail -s "$SUBJECT" root
echo "$(date '+%Y-%m-%d %H:%M:%S'): Email sent for VM $VM_ID $STATUS" >> "$LOG_FILE"
fi
exit $CHECK_EXIT