#!/bin/bash
# editorconfig-checker-disable-file
#
# Plugin to check free ram space
# using check_by_ssh
# by Markus Walther (voltshock@gmx.de)
# The script needs a working check_by_ssh connection and needs to run on the client to check it
#
# Command-Line for check_by_ssh
# command_line $USER1$/check_by_ssh -H $HOSTNAME$ -p $ARG1$ -C "$ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$"
#
# Command-Line for service (example)
# check_by_ssh!82!/nagios/check_ram.sh!20!10
#
##########################################################

case $1 in
  --help | -h )
        echo "Usage: check_ram [warn] [crit]"
        echo " [warn] and [crit] as int"
        echo " Example: check_ram 20 10"
        exit 3
         ;;
  * )
    ;;
esac

warn=$1
crit=$2

if [[ "$1" == disabled ]]
then
        echo "OK: Check disabled"
        exit 0
fi

if [ ! "$1" -o ! "$2" ]; then
        echo "Usage: check_ram [warn] [crit]"
        echo " [warn] and [crit] as int"
        echo " Example: check_ram 20 10"
        echo "Unknown: Options missing: using default (warn=20, crit=10)"
        warn=`echo $((20))`
        crit=`echo $((10))`
fi

full=`free | grep Mem | sed -r 's/\ +/\ /g' | cut -d \  -f 2`
free=`free | grep "buffers/cache" | sed -r 's/\ +/\ /g' | cut -d \  -f 4`
if [ "x$free" = "x" ]; then
    # The output of "free" has changed in winter 2014: It removed the
    # "buffers/cache" line, but added an "available" column. We're going
    # to use that new one.
    free=`free | grep Mem | sed -r 's/\ +/\ /g' | cut -d \  -f 7`
fi

if [ -r /proc/spl/kstat/zfs/arcstats ]
then
    # This system is using ZFS. ZFS does not use the standard I/O
    # caching mechanism of Linux. As a result, ZFS caches appear as "in
    # use by an application", which is not entirely true, since they
    # will be freed when actual applications need memory.
    #
    # Add the current size of the ZFS ARC to the amount of free memory.
    free=$(gawk -vfree="$free" '/^size / { printf "%d\n", free + $3 / 1024 }' \
        /proc/spl/kstat/zfs/arcstats)
fi

if [ "$warn" -lt "$crit" -o "$warn" -eq "$crit" ]; then
   echo "Unknown: [warn] must be larger than [crit]"
        exit 3
fi

use=`echo $(( ($free * 100) / $full ))`

if [ "$use" -gt "$warn" -o "$use" -eq "$warn" ]; then
        echo "OK: $use% free memory"
        exit 0
 elif [ "$use" -lt "$warn" -a "$use" -gt "$crit" ]; then
        echo "Warning: $use% free memory"
        exit 1
 elif [ "$use" -eq "$crit" -o "$use" -lt "$crit" ]; then
        echo "Critical: $use% free memory"
        exit 2
 else
        echo "Unknown"
        exit 3
fi