Ad Widget

Collapse

Monitoring ProxMox VE virtual machines backup with Zabbix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skygge
    Member
    • Jun 2009
    • 46

    #1

    Monitoring ProxMox VE virtual machines backup with Zabbix

    The script:

    Code:
    #!/bin/bash
    
    # Script for checking ProxMox virtual machines backup. For use in Zabbix. Skygge@2016
    
    #Variables
    
    backupconfig='/etc/pve/vzdump.cron'
    storageconfig='/etc/pve/storage.cfg'
    configdir='/etc/pve/local/qemu-server'
    error=0
    
    # Virtual machines with onboot=yes
    virtualmachines=`find $configdir -type f -name "*.conf" -exec grep -H 'onboot' {} \;|cut -d ":" -f 1|cut -d "/" -f 6|cut -d "." -f 1`
    
    # Backup Configuration for VM's
    backupallmachines=`grep '\-\-all' $backupconfig|wc -l`
    
    
    # Check every VM for configured backup
    if [ "$backupallmachines" = "0" ]; then
    for i in $virtualmachines
    do
        checkbackup=`grep $i $backupconfig |sed 's/  */ /g'|cut -d "-" -f 1|cut -d " " -f 8-|tr -d "\n"`
        if [ "$checkbackup" = "" ];then
            echo "VM $i is not configured for backups."
            error=1
        fi
    done
    fi
    
    # Check every VM for existing backup files and check if they're newer than 7 days
    for k in $virtualmachines
    do
            parameters=`cat $backupconfig |egrep "($k|\-\-all)"|head -1|sed 's/  */ /g'`
    
            if [ "$parameters" = "" ];then
                backup=0
                error=1
            else
                #read backup configuration file into an array and find the backup storage parameter
                IFS=' ' read -r -a array <<< "$parameters"
                for index in "${!array[@]}"
                do
                        if [ "${array[index]}" = "--storage" ]; then
                            z=$((index+1))
                            backupstorage=${array[$z]}
                        fi
                done
            # read physical backup path from storage configuration file for VM $k
            backupdirectory=`cat $storageconfig|grep $backupstorage|grep path|rev|cut -d " " -f 1|rev`
            if [ -d $backupdirectory/dump ]; then
                    # check if backup file(s) exists on backup path for VM $k
                    backup=`ls $backupdirectory/dump/|grep $k|grep -v log|wc -l`
                    if [ "$backup" = "0" ]; then
                          echo "VM $k has no backup file."
                          error=1
                    else
                    # check if backup file is newer than 7 days for VM $k
                    newbackup=`find $backupdirectory/dump/ -type f -name "*$k*" -not -name "*.log" -mtime -7 | sort -nr | head -1|wc -l`
                        if [ "$newbackup" = "0" ]; then
                            echo "VM $k backup is older than 7 days."
                            error=1
                        fi
                    fi
                else
                    echo "backup directory for VM $k does not exists"
                    backup=0
                    error=1
            fi
            fi
    done
    
    if [ "$error" = "0" ]; then
    echo "OK"
    fi
    Script returns "OK" when backup is configured for every VM, backup file exists in configured backup storage and is newer than 7 days.
    If something is wrong with the backups the script displays suitable message.
    Tested in ProxMox VE 2.x, 3.x, 4.x

    Zabbix item type: varchar
    Zabbix trigger: last value != OK

    NOTE:
    Zabbix user requires sudo to run this script (ProxMox config files are unreadable for users).

    Skygge
    Last edited by skygge; 20-04-2016, 10:46.
  • MR_Andrew
    Junior Member
    • Dec 2015
    • 27

    #2
    Hello!

    Dear Skygge,

    Many great thanks for your script! It's really very cool and super helpful!

    But there is one problem that I found during detailed investigation. Problem is here:
    Code:
     storageconfig='/etc/pve/storage.cfg'
    backupdirectory=`cat $storageconfig|grep $backupstorage|grep path|rev|cut -d " " -f 1|rev`
    Investigation.

    Code:
    # cat /etc/pve/storage.cfg
    dir: local
            path /var/lib/vz
            content vztmpl,backup,iso
            maxfiles 10
            shared 0
    
    lvmthin: local-lvm
            thinpool data
            vgname pve
            content rootdir,images
    
    dir: vms
            path /mnt/vms
            content images
            shared 0
    In my case, $backupstorage is local. So,
    Code:
    # cat /etc/pve/storage.cfg | grep local
    dir: local
    lvmthin: local-lvm


    There is no "path" that used next in the script. Your command will works only in case if "backupdirectory" contains name of "backupstorage".

    Example where this will works:

    Code:
    ~$  sudo cat /etc/pve/storage.cfg
    dir: backup
            path /usr/backup
            content iso,vztmpl,images,backup
            maxfiles 5
    
    dir: local
            path /var/lib/vz
            content vztmpl,images,iso,rootdir
            maxfiles 0
    
    $ sudo cat /etc/pve/storage.cfg |grep backup |grep path|rev|cut -d " " -f 1|rev
    /usr/backup
    But here result is broken:
    Code:
    # cat /etc/pve/storage.cfg
    dir: local
            path /var/lib/vz
            content vztmpl,backup,iso
            maxfiles 10
            shared 0
    
    lvmthin: local-lvm
            thinpool data
            vgname pve
            content rootdir,images
    
    dir: vms
            path /mnt/vms
            content images
            shared 0
    
    # cat /etc/pve/storage.cfg |grep local|grep path|rev|cut -d " " -f 1|rev

    Solution: use "grep" with next string that contains path: backupdirectory=`cat $storageconfig|grep -A 1 $backupstorage|grep path|rev|cut -d " " -f 1|rev`

    This will works:
    Code:
    # cat /etc/pve/storage.cfg |grep -A 1 local|grep path|rev|cut -d " " -f 1|rev
    /var/lib/vz

    Thanks!
    Last edited by MR_Andrew; 01-09-2018, 23:00.

    Comment

    Working...