#!/bin/sh
# 
# Plugin to monitor the number of open files in the system.
#
# $Log$
# Revision 1.2  2004/05/20 13:57:12  jimmyo
# Set categories to some of the plugins.
#
# Revision 1.1  2004/01/02 18:50:01  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.1  2003/11/24 09:57:27  jimmyo
# Finn-Arne rewrote files_slapd to a generic files_
#
# Revision 1.1  2003/11/10 18:04:32  jimmyo
# Contribution by Finn-Arne Johansen (SF#839212)
#
# Parameters:
# 	
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Magic markers (Used by munin-config and some installation scripts.
# Optional):
#
#%# family=contrib
#%# capabilities=autoconf


PROC=`basename $0 | sed 's/^files_//g'`
TMPFILE="/var/lib/munin/plugin-state/munin-files_${PROC}.state"

if [ "$1" = "autoconf" ]; then
	if [ -x /usr/sbin/lsof ]; then
		echo yes
		exit 0
	else
		echo no
		exit 1
	fi
fi

if [ "$1" = "config" ]; then

	echo 'graph_title slapd open files usage'
	echo 'graph_args --base 1000 -l 0'
	echo 'graph_vlabel number of files'
	echo 'graph_category system'
	echo 'cnt.label Slapd proc count'
	echo 'avg.label avg all slapd proc'
	echo 'max.label max slapd files'
	echo hei | awk  '{printf "max.warning %d\nmax.critical %d\n",900,960}'
	exit 0
fi

if [ "$1" = "collect" ]; then
  for ALL in $(pgrep -u root $PROC) ; do 
    lsof -p $ALL | wc -l 
  done | sort -n | \
    awk '{ C=C+1 ;  S=S + $1 ; M=$1 } END { printf "cnt.value %d\navg.value %d\nmax.value %d\n", C, S/C, M} ' 
else
  [ -f $TMPFILE ] && cat $TMPFILE
  echo "/etc/munin/node.d/slapd_files collect > $TMPFILE" | at +4min 2>/dev/null
fi
