CHips L MINI SHELL

CHips L pro

Current Path : /proc/3/task/3/root/proc/2/cwd/opt/zabbix_scripts/
Upload File :
Current File : //proc/3/task/3/root/proc/2/cwd/opt/zabbix_scripts/disk.usage.sh

#!/bin/bash
# Name: check_disk_usage.sh
# Author: Paul Jr. Fernandez
# Checks disk usage of partitions without having to mount them. Uses the tune2fs command.

# Changelog:
# 20150711 1.0: 	Intial Version
# 20150712 1.1:		Added data for generating graphs
#

# Declaring Variables

EXIT_OKAY=0
EXIT_WARN=1
EXIT_CRIT=2
EXIT_UNKW=3
EXIT_STAT=$EXIT_UNKW

DEVICE_PATH=()
DISK_USAGE=()
CRITICAL_LIST=()
WARNING_LIST=()

# Input Argument Handling

while getopts ":p:w:c:h" OPT
do
	case $OPT in
		p)
		if ! [ -b $OPTARG ]
		then
			echo "UNKNOWN: $OPTARG is not a block device" 
			exit $EXIT_STAT
		else
			DEVICE_PATH=( ${DEVICE_PATH[@]} $OPTARG )
			FREE_BLOCKS=$( sudo tune2fs -l $OPTARG | awk -F":" '$1 ~ /Free blocks/ { print $2 }' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' )
			BLOCK_COUNT=$( sudo tune2fs -l $OPTARG | awk -F":" '$1 ~ /Block count/ { print $2 }' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' )
			DISK_FREE=$(( $FREE_BLOCKS * 100 / $BLOCK_COUNT ))
			DISK_USAGE=( ${DISK_USAGE[@]}  $(( 100 - $DISK_FREE)) )
		fi
		;;
		w)
		if ! [ -z $WARNING_THRESHOLD ]
		then
			echo "UNKNOWN: '-w' threshold has to be provided only once"
			exit $EXIT_STAT
		else
			WARNING_THRESHOLD=$OPTARG
		fi
		;;
		c)
		if ! [ -z $CRITICAL_THRESHOLD ]
                then
                        echo "UNKNOWN: '-c' threshold has to be provided only once"
                        exit $EXIT_STAT
                else
                        CRITICAL_THRESHOLD=$OPTARG
                fi
                ;;
		\?)
		echo "UNKNOWN: Incorrect options"
		exit $EXIT_STAT
		;;
	esac
done

# Making a copy of the arrays for pnp4nagios graphs

DEV=( ${DEVICE_PATH[@]} )
DU=( ${DISK_USAGE[@]} )

# If Warning or Critical Thresholds are not provided, set the values as 90% and 95% respectively

if [ -z $WARNING_THRESHOLD ]
then
	WARNING_THRESHOLD=90
fi
if [ -z $CRITICAL_THRESHOLD ]
then
	CRITICAL_THRESHOLD=95
fi

# Checking if disk usage violates provided thresholds

for ((i=0;i<${#DEVICE_PATH[@]};i++))
do
	if [[ ${DISK_USAGE[$i]} -ge $WARNING_THRESHOLD ]]
	then
		if [[ ${DISK_USAGE[$i]} -ge $CRITICAL_THRESHOLD ]]
		then
			CRITICAL_LIST=( "${CRITICAL_LIST[@]}" "${DEVICE_PATH[$i]}" "${DISK_USAGE[$i]}" )
			unset DEVICE_PATH[$i]	DISK_USAGE[$i]
			DEVICE_PATH=( ${DEVICE_PATH[@]} )
			DISK_USAGE=( ${DISK_USAGE[@]} )
			i=$(( $i -1 ))
		else
			WARNING_LIST=( "${WARNING_LIST[@]}" "${DEVICE_PATH[$i]}" "${DISK_USAGE[$i]}" )
                        unset DEVICE_PATH[$i]  DISK_USAGE[$i]
                        DEVICE_PATH=( ${DEVICE_PATH[@]} )
                        DISK_USAGE=( ${DISK_USAGE[@]} )
			i=$(( $i -1 ))
		fi
	fi
done

# Output generation

if [ ${#CRITICAL_LIST[@]} -gt 0 ]
then
	echo -n "CRITICAL: Disk Usage above $CRITICAL_THRESHOLD%:"
	for ((i=0;i<${#CRITICAL_LIST[@]};i+=2))
	do
		echo -n ${CRITICAL_LIST[$i]} "("${CRITICAL_LIST[$i+1]}"%), "
	done
	if [ ${#WARNING_LIST[@]} -gt 0 ]
	then
		echo -n "Disk Usage above $WARNING_THRESHOLD%:"
		for ((i=0;i<${#WARNING_LIST[@]};i+=2))
		do
			echo -n ${WARNING_LIST[$i]} "("${WARNING_LIST[$i+1]}"%), "
		done
	fi
	if [ ${#DEVICE_PATH[@]} -gt 0 ]
	then
		echo -n "Disk Usage OK:"
		for ((i=0;i<${#DEVICE_PATH[@]};i++))
		do
			echo -n ${DEVICE_PATH[$i]} "("${DISK_USAGE[$i]}"%), "
		done
	fi
	EXIT_STAT=$EXIT_CRIT
elif [ ${#WARNING_LIST[@]} -gt 0 ]
then
	echo -n "WARNING: Disk Usage above $WARNING_THRESHOLD%:"
	for ((i=0;i<${#WARNING_LIST[@]};i+=2))
        do
        	echo -n ${WARNING_LIST[$i]} "("${WARNING_LIST[$i+1]}"%), "
	done
	if [ ${#DEVICE_PATH[@]} -gt 0 ]
	then
		echo -n "Disk Usage OK:"
		for ((i=0;i<${#DEVICE_PATH[@]};i++))
		do
			echo -n ${DEVICE_PATH[$i]} "("${DISK_USAGE[$i]}"%), "
		done
	fi
	EXIT_STAT=$EXIT_WARN
else
	echo -n "OK: Disk Usage is within thresholds:"
	for ((i=0;i<${#DEVICE_PATH[@]};i++))
	do
		echo -n ${DEVICE_PATH[$i]} "("${DISK_USAGE[$i]}"%), "
	done
	EXIT_STAT=$EXIT_OKAY
fi

# Generating data for PNP4NAGIOS graphs

echo -n "| "
for ((i=0;i<${#DEV[@]};i++))
do
	echo -n "${DEV[$i]}=${DU[$i]};$WARNING_THRESHOLD;$CRITICAL_THRESHOLD;0;100; "
done
echo ""
exit $EXIT_STAT

Copyright 2K16 - 2K18 Indonesian Hacker Rulez