Plugin:check filesize


 * 1) !/bin/bash


 * 1) **** BEGIN LICENSE BLOCK *****
 * 2) Version: MPL 2.0
 * 3) echocat check_filesize.sh, Copyright (c) 2011-2012 echocat
 * 4) This Source Code Form is subject to the terms of the Mozilla Public
 * 5) License, v. 2.0. If a copy of the MPL was not distributed with this
 * 6) file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 7) **** END LICENSE BLOCK *****
 * 1) This Source Code Form is subject to the terms of the Mozilla Public
 * 2) License, v. 2.0. If a copy of the MPL was not distributed with this
 * 3) file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 4) **** END LICENSE BLOCK *****
 * 1) **** END LICENSE BLOCK *****


 * 1) Check if a file size is smaller then given parameters.
 * 2) @author: Daniel Werdermann / dwerdermann@web.de
 * 3) @projectsite: https://github.com/echocat/nagios-plugin-check_filesize
 * 4) @version: 1.3
 * 5) @date: 2012-12-30 14:28:21 CEST
 * 6) changes 1.3
 * 7)  - just update license information
 * 8) changes 1.2
 * 9)  - look for logger path via which command
 * 10)  - fiexes for solaris support
 * 11) changes 1.1
 * 12)  - add license information
 * 1)  - fiexes for solaris support
 * 2) changes 1.1
 * 3)  - add license information

PROGNAME=$(basename $0) WARN_MESG= CRIT_MESG= LOGGER="`which logger` -i -p kern.warn -t"
 * 1) configuration
 * 1) configuration

export PATH="/bin:/usr/local/bin:/sbin:/usr/bin:/usr/sbin:/usr/sfw/bin" LIBEXEC="/opt/nagios/libexec /usr/lib64/nagios/plugins /usr/lib/nagios/plugins /usr/local/nagios/libexec /usr/local/libexec" for i in ${LIBEXEC} ; do [ -r ${i}/utils.sh ] &&. ${i}/utils.sh done

if [ -z "$STATE_OK" ]; then echo "nagios utils.sh not found" &>/dev/stderr exit 1 fi



function log { $LOGGER ${PROGNAME} "$@"; }
 * 1) functions
 * 1) functions

function usage { echo "Usage: $PROGNAME -w BYTESIZE -c BYTESIZE FILE [FILE2 FILE3 ...]" echo "Usage: $PROGNAME -h,--help" echo "Options:" echo " FILES    Check this file(s)" echo " -w Bytes Warn if filesize greater then this" echo " -c Bytes Critical filesize greater this" }

function print_help { echo "" usage echo "" echo "Check if filesize is smaller then given parameters" echo "" echo "This plugin is NOT developped by the Nagios Plugin group." echo "Please do not e-mail them for support on this plugin, since" echo "they won't know what you're talking about." echo "" echo "For contact info, read the plugin itself..." }


 * 1) startup checks
 * 1) startup checks

if [ $# -eq 0 ]; then usage exit $STATE_CRITICAL fi

while [ "$1" != "" ] do case "$1" in		--help) print_help; exit $STATE_OK;;		-h) print_help; exit $STATE_OK;; -w) WARN=$2; shift 2;;		-c) CRIT=$2; shift 2;; /*) FILES="${FILES} $1"; shift;;		*) usage; exit $STATE_UNKNOWN;; esac done

if [ $WARN -gt $CRIT ] ; then log "UNKNOWN: warn value is greater then crit value" exit $STATE_UNKNOWN fi


 * 1) now we check if ...
 * 2)  1) ... file exists
 * 3)  2) ... has the correct filesize
 * 1)  2) ... has the correct filesize

for FILE in ${FILES} ; do if [ ! -f $FILE ]; then log "CRITICAL: $FILE don't exists" CRIT_MESG[${#CRIT_MESG[*]}]="${FILE} don't exists" fi

SIZE=$(stat -c %s $FILE)

if [ $SIZE -gt $CRIT ] ; then log "CRITICAL: ${FILE} has size ${SIZE} Byte. Critical at ${CRIT}." CRIT_MESG[${#CRIT_MESG[*]}]="${FILE} has size ${SIZE} Byte. Critical at ${CRIT}." elif [ $SIZE -gt $WARN ] ; then log "WARN: ${FILE} has size ${SIZE} Byte. Warn at ${CRIT}." WARN_MESG[${#WARN_MESG[*]}]="${FILE} has size ${SIZE} Byte. Warn at ${WARN}." fi done

if [ ${#CRIT_MESG[*]} -ne 0 ]; then echo -n "CRITICAL: " for element in "${CRIT_MESG[@]}"; do		echo -n ${element}" ; " done echo exit $STATE_CRITICAL elif [ ${#WARN_MESG[*]} -ne 0 ]; then echo -n "WARN: " for element in "${WARN_MESG[@]}"; do		echo -n ${element}" ; " done echo exit $STATE_WARNING fi

echo "OK: filesize of ${FILES} is smaller then ${WARN} Bytes." exit $STATE_OK