Skip to content
Snippets Groups Projects
check_ceph_diskfree 3.35 KiB
#!/bin/bash
# ======================================================================
#
# Icinga/ Nagios Check
# CEPH DISKFREE
#
# ----------------------------------------------------------------------
#
# REQUIREMENTS:
#   - ceph
#
# SYNTAX:
#   - check_ceph_diskfree [-w WARNINGLIMIT] [-c CRITICALLIMIT]
#     WARNINGLIMIT   integer  usage limit when to warn; default: 70
#     CRITICALLIMIT  integer  usage limit when to alert; default: 90
#
# ----------------------------------------------------------------------
# 2020-03-04  v1.0  <axel.hahn@iml.unibe.ch>
# 2020-03-05  v1.1  <axel.hahn@iml.unibe.ch> switch to ph.* helper functions
# ======================================================================

. `dirname $0`/inc_pluginfunctions

tmpfile=/tmp/ceph_df_output__$$
outfile=/tmp/check_ceph_df_out__$$

typeset -i iWarning=0
typeset -i iCritical=0

# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

# set default / override from command line params
typeset -i iWarnLimit=`     ph.getValueWithParam 70 w "$@"`
typeset -i iCriticalLimit=` ph.getValueWithParam 90 c "$@"`


sudo /bin/ceph df > $tmpfile 2>&1
if [ $? -ne 0 ]; then
    rm -f $tmpfile
	ph.abort "UNKNOWN: ceph is not available or no sudo permissions to execute ceph commands."
fi

rm -f $outfile 2>/dev/null
isHeader=0
area=

while read line
do
    # echo "DEBUG: $line"
    newArea=`echo $line | grep "^[A-Z]*:"`
    lineStatus="          "
    if [ ! -z "$newArea" ]; then
    echo>>$outfile
        area=`echo $newArea | cut -f 1 -d ":"`
        isHeader=1

        # Position of disk usage in the sections
        iPos=0
        test "$area" = "GLOBAL" && iPos=4
        test "$area" = "POOLS"  && iPos=4
        if [ $iPos -eq 0 ]; then
            ph.abort "ERROR: unhandled section: [$area]"
        fi

    else
        if [ $isHeader -eq 1 ]; then
            isHeader=0
        else
            typeset -i percentUsed=`echo $line | awk -v ipos=$iPos '{ print $ipos }' | sed "s#\..*##"`
            if [ $percentUsed -ge $iWarnLimit ]; then
                if [ $percentUsed -ge $iCriticalLimit ]; then
                    iCritical=$iCritical+1
                    lineStatus="CRITICAL  "
                else
                    iWarning=$iWarning+1
                    lineStatus="WARNING   "
                fi
            else
                lineStatus="OK        "
            fi
        fi
    fi
    echo "$lineStatus $line" >> $outfile
done < $tmpfile


if [ $iCritical -gt 0 ]; then
    ph.setStatus "critical"
elif [ $iWarning -gt 0 ]; then
    ph.setStatus "warning"
else
    ph.setStatus "ok"
fi

ph.status "Disksize on Ceph cluster and its pools - critcal: $iCritical ($iCriticalLimit %) .. warnings: $iWarning ($iWarnLimit %)"
cat $outfile

# 3rd line is global size status
sTotal=`sed -n 3p $tmpfile | awk '{ print $1 }'`
sAvail=`sed -n 3p $tmpfile | awk '{ print $2 }'`
sUsed=` sed -n 3p $tmpfile | awk '{ print $3 }'`

iTotal=`ph.toUnit $sTotal ""`
iAvail=`ph.toUnit $sAvail ""`
iUsed=` ph.toUnit $sUsed ""`

ph.perfadd "global-total"    "${iTotal}"    "" "" 0 ${iTotal}
ph.perfadd "global-avail"    "${iAvail}"    "" "" 0 ${iTotal}
ph.perfadd "global-used"     "${iUsed}"     "" "" 0 ${iTotal}


# cleanup
rm -f $tmpfile $outfile 2>/dev/null

ph.exit

# ----------------------------------------------------------------------