feat(proxmox): check_vm as cronjob

Signed-off-by: Tuan-Dat Tran <tuan-dat.tran@tudattr.dev>
This commit is contained in:
Tuan-Dat Tran
2025-09-02 19:52:49 +02:00
parent 7aa16f3207
commit a1da69ac98
2 changed files with 25 additions and 18 deletions

View File

@@ -1,11 +1,10 @@
#!/bin/bash #!/bin/bash
# Configuration # Configuration
VM_ID=303 VM_ID=$1
TARGET_IP="192.168.20.36" # Replace with the IP of your VM TARGET_IP=$2
PORT=22 PORT=22
CHECK_INTERVAL=300 # 5 minutes in seconds LOG_FILE="/var/log/vm_monitor_${VM_ID}.log"
LOG_FILE="/var/log/vm_monitor.log"
# Function to log messages # Function to log messages
log_message() { log_message() {
@@ -65,19 +64,12 @@ restart_vm() {
log_message "VM $VM_ID has been restarted." log_message "VM $VM_ID has been restarted."
} }
# Main loop # Main execution
log_message "Starting monitoring of VM $VM_ID on port $PORT..." log_message "Starting monitoring of VM $VM_ID on port $PORT..."
log_message "Press Ctrl+C to exit."
while true; do # Check if port 22 is open
# Check if port 22 is open if ! check_port; then
if ! check_port; then restart_vm
restart_vm else
else log_message "Port $PORT is reachable. VM is running normally."
log_message "Port $PORT is reachable. VM is running normally." fi
fi
# Wait for the next check
log_message "Sleeping for $CHECK_INTERVAL seconds..."
sleep $CHECK_INTERVAL
done

View File

@@ -17,6 +17,7 @@
ansible.builtin.shell: | ansible.builtin.shell: |
qm set {{ vm.vmid }} --scsi0 {{ proxmox_storage }}:{{ vm.vmid }}/vm-{{ vm.vmid }}-disk-0.raw --ide2 {{ proxmox_storage }}:cloudinit --boot order=scsi0 qm set {{ vm.vmid }} --scsi0 {{ proxmox_storage }}:{{ vm.vmid }}/vm-{{ vm.vmid }}-disk-0.raw --ide2 {{ proxmox_storage }}:cloudinit --boot order=scsi0
delegate_to: "{{ vm.node }}" delegate_to: "{{ vm.node }}"
changed_when: true
- name: Resize scsi0 disk if needed - name: Resize scsi0 disk if needed
ansible.builtin.shell: | ansible.builtin.shell: |
@@ -86,3 +87,17 @@
# create: true # create: true
# state: present # state: present
# delegate_to: localhost # delegate_to: localhost
- name: Copy VM check script to node
ansible.builtin.copy:
src: check_proxmox_vm.sh
dest: /usr/local/bin/check_proxmox_vm.sh
mode: '0755'
delegate_to: "{{ vm.node }}"
- name: Schedule VM check script
ansible.builtin.cron:
name: "Check VM {{ vm.name }}"
job: "/usr/local/bin/check_proxmox_vm.sh {{ vm.vmid }} {{ vm_found_ip }}"
minute: "*/5"
delegate_to: "{{ vm.node }}"