Page 1 of 1

Plugin to check open files for a specific user

Posted: Tue Mar 02, 2021 9:46 pm
by tchandra
Hi Team,

we need to add the alert in Nagios monitoring to monitor open files for specific user in a linux server.
please suggest me on this .

Thanks in advance.

Regards,
Chandra

Re: Plugin to check open files for a specific user

Posted: Wed Mar 03, 2021 5:16 pm
by benjaminsmith
HI Chandra,

Haven't had a chance to test this yet, but there's a community plugin on the Nagios Exchange available for this type of check.

https://exchange.nagios.org/directory/P ... sh/details

We have a few guides with directions on how to test and configure a community plugin. Let me know if you need any assistance or have questions.

Managing Plugins In Nagios XI

Third-Party Plugins in Nagios XI

Benjamin

Re: Plugin to check open files for a specific user

Posted: Fri Mar 12, 2021 12:06 am
by tchandra
Hi Benjamin,

Thanks for your response.
Actually , we are trying to get monitor for number of open files for a jboss user.
However, we already tried with plug in "check_open_files.sh". But we are getting error while executing.
please find attachment for error reference.

we used the below script.
#!/bin/bash
#
# Inspired by: http://pissedoffadmins.com/nagios/nagio ... check.html
#
# Author: Gregor Binder
# Mail: [email protected]

SUDO=/bin/sudo
LSOF=/sbin/lsof
AWK=/bin/awk
WC=/bin/wc

ERROR_CODE=-1
if [ -z "$1" ]; then
echo "Usage: $0 username"
echo " username: Username to check for to much open files"
exit $ERROR_CODE
else
USER=$1
fi

function checkExitStatus {
if [ $1 -ne 0 ]; then
echo "!!! command failure !!! $2"
exit -1
fi
}

# check if the username is valid
$LSOF -u jboss &> /dev/null
checkExitStatus $? "Username wrong"

# check if ah PID is available for the user
PID=`ps -u $USER | tail -1 | awk '{print $1}'`
checkExitStatus $? "No PID found"
OPEN_F=`$SUDO cat /proc/$PID/limits | grep "open files" | awk '{print $5}'`

LSOF=`$SUDO $LSOF -u $USER | $WC -l`
PERCDONE_PRE=$(echo "scale=2;(($LSOF/$OPEN_F) * 100)" |bc)
PERCDONE=`echo $PERCDONE_PRE | cut -d. -f1`

if [ $PERCDONE -lt 84 ]; then
ERROR_CODE=0
printf "FILES OK - $PERCDONE %% with $LSOF files open|files=$LSOF;;;\n"
else
if [ $PERCDONE -ge 85 ] && [ $PERCDONE -le 94 ]; then
ERROR_CODE=1
printf "FILES WARN - $PERCDONE %% with $LSOF files open|files=$LSOF;;;\n"
elif [ $PERCDONE -ge 95 ]; then
ERROR_CODE=2
printf "FILES CRIT - $PERCDONE %% with $LSOF files open|files=$LSOF;;;\n"
fi
fi

exit $ERROR_CODE

Re: Plugin to check open files for a specific user

Posted: Fri Mar 12, 2021 5:02 pm
by vtrac
Hi,
Please install "lsof" to run this script.

Code: Select all

yum install lsof
Also, I looked at the URL below, you must add the followings to the sudoer file
https://github.com/wefixit-AT/nagios_check_open_files

Code: Select all

Cmnd_Alias NRPE_CHECK_OPEN_FILES = /bin/cat /proc/*/limits, /sbin/lsof -u *

nrpe    ALL=(ALL) NOPASSWD: NRPE_CHECK_OPEN_FILES
Here's the output of the command:

Code: Select all

[root@VT-NagiosXI-61 ~]# ./check_open_file.sh apache
FILES OK - 53 % with 2206 files open|files=2206;;;

Regards,
Vinh

Re: Plugin to check open files for a specific user

Posted: Tue Mar 16, 2021 10:37 pm
by tchandra
Hi ,

LSOF command already installed in server .
And we added the two lines in sudoers file , but after added lines sudo users facing issues to login and showing wrong entry in sudoers file.
And then, i created one script with lsof command to check open files for jboss user and getting the output.
But , not able to set thresholds for this.
Please find attachments.

So, need help to set thresholds for this or script to monitor for open files for a particular user

Re: Plugin to check open files for a specific user

Posted: Wed Mar 17, 2021 1:12 pm
by lmiltchev
So, need help to set thresholds for this or script to monitor for open files for a particular user
You could use something like this:

Code: Select all

#!/bin/bash

count=`lsof -u jboss | wc -l`
warn=$1
crit=$2

if (("$count" <= "$warn")); then
  echo "OK: $count open files is within the accepted thresholds"
  exit 0
fi

if (("$count" <= "$crit")) && (("$count" > "$warn")); then
  echo "Warning: $count open files exceed the warning threshold of $warn"
  exit 1
fi

if (("$count" > "$crit")); then
  echo "Critical: $count open files exceed the critical threshold of $crit"
  exit 2
fi
Then, you can run your check and pass two thresholds (warning and critical).

Example:

Code: Select all

./check_open_files_jboss1.sh 1000 2000
I am not a bash expert, so this script may not be perfect, but at least it could get you started.

Hope this helps.

Re: Plugin to check open files for a specific user

Posted: Wed Mar 17, 2021 9:59 pm
by tchandra
Hi lmiltchev ,

Thanks for your help.
Used script & Set the thresholds , its working fine.

Regards,
Chandra

Re: Plugin to check open files for a specific user

Posted: Thu Mar 18, 2021 8:58 am
by lmiltchev
I am glad I could help! :D