#!/bin/bash

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/icinga/libexec /usr/local/libexec /opt/csw/libexec/nagios-plugins"

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

for USER in $(ps ho user:20 $(pgrep ^gocryptfs) | awk '{print $1}'); do
  PROCESSCOUNT=$(pgrep -U "${USER}" | wc -l)
  if [ ! ${PROCESSCOUNT} -gt 2 ]; then
    echo "gocryptfs for user ${USER} but no other processes"
    exit $STATE_WARNING
  fi
done

echo "OK: no stale gocryptfs processes" 
exit $STATE_OK
