Nice little sh-script to monitor LVM-thin space with warn mail

SimonR

Well-Known Member
Jan 2, 2020
36
6
48
With these little 2 scripts you can monitor your LVM-thin space to be aware of snapshots killing your overprovisioned storage. Save each script as .sh-File, chmod +x the file and it's directly ready to use after finding the attribute output in LVS command. Change the mail-adress, it runs fine, if your postfix is well configured to send mails to the internet. Change the attribute to search for, in my case "twi", change the percentage after -ge to a trigger from 0 to 99.

Find an attribute in the first line output of LVS-command, in my case it is "twi". We only need that to bring back only the first line of the lvs-output, in my case "twi" is enough to separate it from the other lines.

Script 1 here has a trigger of 0% and can be triggered by a cronjob one time a week to give you an overview regularly without any alarm.
disk-usage.sh

Bash:
#!/bin/sh

/usr/sbin/lvs | grep 'twi' | sed 's/\.//g' | awk '{ print $5,$1 }' | while read output;

do

  echo $output

  used=$(echo $output | awk '{print $1}')

  used2=$(($used / 100))

  partition=$(echo $output | awk '{print $2}')

  if [ $used2 -ge 0 ]; then

  echo "LVM-thin \"$partition\" auf $(hostname) nutzt $used2% am $(date)" | /usr/bin/mail -s "LVM-Thin Speicherplatz Info: $used2% genutzt auf $(hostname)" xxx@test.com

else

echo "Disk space usage is in under threshold"

  fi

done



Script 2 here has a trigger of 90% and I trigger it every 45 minutes by a cronjob to send an alarm, if the LVM-thin is used more than 90%.
disk-usage-alarm.sh

Bash:
#!/bin/sh

/usr/sbin/lvs | grep 'twi' | sed 's/\.//g' | awk '{ print $5,$1 }' | while read output;

do

  echo $output

  used=$(echo $output | awk '{print $1}')

  used2=$(($used / 100))

  partition=$(echo $output | awk '{print $2}')

  if [ $used2 -ge 90 ]; then

  echo "LVM-thin \"$partition\" auf $(hostname) nutzt $used2% am $(date)" | /usr/bin/mail -s "LVM-Thin Speicherplatz Alarm: $used2% genutzt auf $(hostname)" xxx@test.com

else

echo "Disk space usage is in under threshold"

  fi

done

Feel free to use it, I wrote this as modification of a df -h monitoring script, but I needed something for LVM-thin to minimize the overprovisioning panic and stop-possibilities of VMs running on the thin-volume.

Lines in crontab -e could be like this, if you save the .sh-Files in /opt/script directory.

0 5 * * 1 bash /opt/script/disk-usage.sh > /dev/null 2>&1
*/45 * * * * bash /opt/script/disk-usage-alarm.sh > /dev/null 2>&1
 

Attachments

  • Screenshot 2022-05-18 210400.png
    Screenshot 2022-05-18 210400.png
    113.8 KB · Views: 20
Last edited:
Would be better to post your scripts inside CODE-Tags so formating would be preserved. Thats makes it easier for other to read or copy your scripts.