Check VM is running with ping or better ideas?

virtual<3

New Member
Oct 3, 2017
7
0
1
30
Hi,

I have a few VMs that sometimes randomly stop respond. I have no idea why this happens. I've tried to look at it, but failed to find answer. So I'm thinking about doing script pings VM once every 30minutes to check that it's online, if not then restart VM.


First of all I'm not the best coder, but here's what I've come up with so far:

#! /bin/bash

case "ping -c 1 192.168.1.xxx" in

0) echo "Restarting VM:"
qm restart 10x
;;
1) echo "no problems detected"
;;
esac

Fairly simple bash script which I'm planing to run with cronjob every 30minutes.
Now I want to ask if you have any better suggestions? Is there more optimal way of doing this?
I'm open for any suggestions.

Thank you
 
Often, ping is not enough, even things like qemu-agent that pings the guest OS is not enough. We monitor everything with our regular monitoring (best also with a monitoring of your monitoring). We even have checks that check if all VMs in specific pools are checked in case someone set up a VM and forget to add it to the monitoring.
 
You should monitor the services that the VMs provide, you could use httping to check a webserver for instance. You could use a more featured program like nagios or zabbix to get an overview of your system.
 
More efectivefrom my point of view and this method dosen't depend from nothing (network or something) if you have guest agent installed on that machines you can use this command "qm agent 1271 get-osinfo" and if vm is off you receive this message: VM 1271 is not running

Thx :)
 
Hello, I adjusted a code for my needs based on the VM status

#!/bin/bash
# WatchDog script for Proxmox VMs
# v1.1
# Install using `crontab -e` like this: * * * * * /root/watchdog.sh
# Based on https://forum.proxmox.com/threads/simple-reset-script-ping.49901/
#

# Config
vmName1="AAA"
vmId1="101"
vmIp1="xx.xx.xx"

vmName2="BBB"
vmId2="102"
vmIp2="xx.xx.xx"

statusOk="running"

# Log func
log () {
output="[WatchDog] $1"
echo $output && logger $output
}

# Check VM func
checkVM () {

vmStatus=$(/usr/sbin/qm status $2 -verbose | grep qmpstatus | cut -f2 -d' ')
vmUptime=$(/usr/sbin/qm status $2 -verbose | grep uptime | cut -f2 -d' ')

if [ $vmStatus == $statusOk ]
then
log "$1 seems alive, no action taken. Current uptime: $vmUptime"
else
log "$1 Crashed - Restarting now..."
/usr/sbin/qm start $2
fi

}

# Logic
checkVM $vmName1 $vmId1 $vmIp1
checkVM $vmName2 $vmId2 $vmIp2
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!