#!/bin/sh

PATH=/usr/sbin:/bin
export PATH

if [ $# -ne 1 ]
then
    echo usage: $0 poolname >&2
    exit 3
fi

POOL=$1

health=`zpool list -H -o health $POOL`
size=`zpool list -H -o size $POOL`
avail=`zpool list -H -o available $POOL`

bytes () {
    echo $1 | sed -e 's/G/ * 1024M/' -e  's/M/ * 1024K/' -e  's/K/ * 1024/' | bc
}

capacity=`echo 100 - 100 '*' \`bytes $avail\` / \`bytes $size\` | bc`

if [ x"$health" != xONLINE ]
then
    echo "CRITICAL - $POOL state is $health ($capacity% used) | size=$size avail=$avail health=$health"
    exit 2
fi

if [ $capacity -gt 95 ]
then
    echo "CRITICAL - $POOL: $capacity% used | size=$size avail=$avail health=$health"
    exit 2
fi

if [ $capacity -gt 90 ]
then
    echo "WARNING - $POOL: $capacity% used | size=$size avail=$avail health=$health"
    exit 1
fi

echo "OK - $POOL: $capacity% used | size=$size avail=$avail health=$health"
exit 0