#!/bin/sh
# 
# Plugin to monitor the number of open files in the system.
#
# client-conf.d/-options:
#       
#       smbstatus       -- path to smbstatus executable
#       ignoreipcshare  -- ignore IPC$ whan counting shares
#                          any string will do
#
# Parameters:
# 	
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Magic markers (Used by munin-config and some installation scripts.
# Optional):
#
#%# family=contrib
#%# capabilities=autoconf

SMBSTATUS=${smbstatus:-`which smbstatus`}
SMBSTATUS=${SMBSTATUS:-/usr/bin/smbstatus}

if [ "$1" = "autoconf" ]; then
	if [ -x $SMBSTATUS ]; then
		echo yes
		exit 0
	else
		echo no '(smbstatus not found)'
		exit 1
	fi
fi

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

	echo 'graph_title Samba status'
	echo 'graph_args -l 0'
	echo 'graph_vlabel number'
	echo 'graph_category samba'
	echo 'proc.label processes'
	echo 'lock.label locked files'
	echo 'share.label Open shares'
	echo 'max.warning 900'
	echo 'max.critical 960'
	exit 0
fi

$SMBSTATUS -p 2>/dev/null | awk 'BEGIN {lines=0} $1 ~ /^[0-9]+/ {lines++} END {print "proc.value " lines}'
$SMBSTATUS -L 2>/dev/null | awk 'BEGIN {lines=0} $1 ~ /^[0-9]+/ {lines++} END {print "lock.value " lines}'
if [ -n "$ignoreipcshare" ]; then
	IGNORE='$1=="IPC$" {next}'
fi
$SMBSTATUS -S 2>/dev/null | awk '
BEGIN {lines=0;state=0}
$1=="Service"&&state==0 {state=1;next}
/^--*$/&&state==1 {state=2;next}
state<2 {next}
/^$/ {next}           
'"$IGNORE"'
{lines++}
END {print "share.value " lines}'
