#!/bin/bash

#######
## Description: Plugin to check for snapshots older then 24 hours
## Author: Sebastian Klee
## Version: 1.0
## Date: 03.05.2022
#######


yesterday=$(date +'%Y-%m-%d %H:%M:%S' -d "yesterday")
warning=0

# The for loop will be repeated for each found snapshot 
for i in $(lvdisplay -C | grep 'snapshot' | awk '{print $1}'); do
      # We filter the output of the lvdisplay command, we only need the creation date of the snapshot, the filtered out creation date will be assigned to the variable "date_snapshot"
      date_snapshot=$(lvdisplay /dev/vgvm/"$i" | grep -E 'Creation.*time' | awk 'BEGIN {OFS=" "} {print $6, $7}')
      # If the creation date of the snapshot is older then yesterday's date, the value of the variable "warning" is incremented by 1
      if [[ $date_snapshot < $yesterday ]]; then
        ((warning+=1))
      fi

done


if [[ $warning -gt 0 ]]; then
        echo "A snapshot older than 24 Hours exists ($warning)" 
        exit 2
else
        echo "No snapshot older than 24 hours"
        exit 0
fi

