#!/bin/bash
#
# $Id$
#
# checks if any non-volatile partition seems to be running full,
# i.e. has more than a certain usage.
#
# the satisfying usage percentage considered just not worth warning about is 90 


# prepare

. /usr/lib/montools/mt-prepare

RESNAME="Disk space"


# work

PARTLIST=$(mount -t btrfs,ext3,ext4,zfs |awk '{print $3}' |egrep -v 'bind|loop')

for PART in $PARTLIST ; do
  USAGE=$(/bin/df -P ${PART} | sed 's/^.* \([0-9][0-9]*\)% .*$/\1/p;d')
  if [ $USAGE -ge $DISKUSAGE_CRITICAL ] ; then 
    add2criticals \
      "Disc usage critical on ${PART}: ${USAGE}% "'>='" ${DISKUSAGE_CRITICAL}%" \
      "$(df -m ${PART})"
  else
    if [ $USAGE -ge $DISKUSAGE_WARNING ] ; then 
      add2warnings \
        "Disc usage warning on ${PART}: ${USAGE}% "'>='" ${DISKUSAGE_WARNING}%" \
        "$(df -m ${PART})"
    else
      add2infos \
        "Disc usage OK on ${PART}: ${USAGE}% "'<'" ${DISKUSAGE_WARNING}%" \
        "$(df -m ${PART})"
    fi
  fi
  USAGE=$(/bin/df -i ${PART} | sed 's/^.* \([0-9][0-9]*\)% .*$/\1/p;d')
  if [ $USAGE -ge $DISKUSAGE_CRITICAL ] ; then 
    add2criticals \
      "Inode usage critical on ${PART}: ${USAGE}% "'>='" ${DISKUSAGE_CRITICAL}%" \
      "$(df -m ${PART})"
  else
    if [ $USAGE -ge $DISKUSAGE_WARNING ] ; then 
      add2warnings \
        "Inode usage warning on ${PART}: ${USAGE}% "'>='" ${DISKUSAGE_WARNING}%" \
        "$(df -m ${PART})"
    else
      add2infos \
        "Inode usage OK on ${PART}: ${USAGE}% "'<'" ${DISKUSAGE_WARNING}%" \
        "$(df -m ${PART})"
    fi
  fi
done


# notifications

. /usr/lib/montools/mt-notify-exit

