Plugin to check open files for a specific user

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
tchandra
Posts: 38
Joined: Fri Oct 18, 2019 5:53 am

Plugin to check open files for a specific user

Post 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
benjaminsmith
Posts: 5324
Joined: Wed Aug 22, 2018 4:39 pm
Location: saint paul

Re: Plugin to check open files for a specific user

Post 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
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
tchandra
Posts: 38
Joined: Fri Oct 18, 2019 5:53 am

Re: Plugin to check open files for a specific user

Post 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
You do not have the required permissions to view the files attached to this post.
User avatar
vtrac
Posts: 903
Joined: Tue Oct 27, 2020 1:35 pm

Re: Plugin to check open files for a specific user

Post 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
tchandra
Posts: 38
Joined: Fri Oct 18, 2019 5:53 am

Re: Plugin to check open files for a specific user

Post 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
You do not have the required permissions to view the files attached to this post.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Plugin to check open files for a specific user

Post 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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
tchandra
Posts: 38
Joined: Fri Oct 18, 2019 5:53 am

Re: Plugin to check open files for a specific user

Post by tchandra »

Hi lmiltchev ,

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

Regards,
Chandra
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Plugin to check open files for a specific user

Post by lmiltchev »

I am glad I could help! :D
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked