#!/bin/sh
#
# Munin-plugin to monitor the number of threads on Linux.
#
# Parameters understood:
#
#	config		(required)
#	autoconf	(optional - used by munin-config)
#
# GNU GPL, Lars Strand
#
# Magick markers (optional - used by munin-config and som installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf

if [ "$1" = "autoconf" ]; then
    grep -q '^Threads' /proc/$$/status && echo "yes" || echo "no"
    exit 0
fi

if [ "$1" = "config" ]; then
    echo 'graph_title Number of threads'
    #echo 'graph_args --base 1000 -l 0 '
    echo 'graph_vlabel number of threads'
    echo 'graph_category processes'
    echo 'graph_info This graph shows the number of threads.'
    echo 'threads.label threads'
    echo 'threads.draw LINE2'
    echo 'threads.info The current number of threads.'
    exit 0
fi

echo -n "threads.value "
find /proc -maxdepth 1 -name '[1-9]*' -exec grep 'Threads' {}/status \; | awk ' { sum += $2; } END { print sum; }'
# Alternate command:
#ps --no-headers axm | grep '^    -' | wc -l
