Display open file handles

Post support questions that directly relate to Linux/Unix operating systems.

Moderators: Developers, Moderators

Post Reply
stigmata
Posts: 49
Joined: Tue Nov 23, 2004 10:26 am

Display open file handles

Post by stigmata »

Hi,

is there any way to monitor the number of open file handles on the linux system?

Regards,
Oliver
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

If you have a script that simply returns this wanted value or an OID for that, the answer ist: YES. There's a way to implement this script at http://www.cacti.net/downloads/docs/htm ... thods.html
Reinhard
stigmata
Posts: 49
Joined: Tue Nov 23, 2004 10:26 am

Post by stigmata »

Hi Reinhard,

currently I'm searching for this script ;)
"lsof | wc -l" <-- I can't believe that this is the best way to get the open files...

Oliver
stigmata
Posts: 49
Joined: Tue Nov 23, 2004 10:26 am

Post by stigmata »

I made this script:

--------------------------------------------------------------------------------
#!/bin/bash

TEMP=`ls /proc/`
TEMP=`echo $TEMP | sed "s/[a-zA-Z]//g" | sed "s/[.]//g"`
NR_HANDLES=0

for PID in $TEMP;
do
NR_PID=`/usr/sbin/lsof -p $PID | wc -l`
NR_HANDLES=$[$NR_HANDLES+$NR_PID]
done

echo $NR_HANDLES
--------------------------------------------------------------------------------
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

Well, seems nice, but has problems with processes like sysrq-trigger (the "-" sign!). You'll have to define a Data Input Method for that, a Data Template and a Graph Template as described in http://www.cacti.net/downloads/docs/htm ... thods.html
Reinhard
stigmata
Posts: 49
Joined: Tue Nov 23, 2004 10:26 am

Post by stigmata »

I changed to a new solution:

#!/bin/bash

TEMP=`ls /proc/`
PID_EXCLUDE=$[$$+1]
TEMP=`echo $TEMP | sed "s/[a-zA-Z]//g" | sed "s/[.]//g" | sed "s/$PID_EXCLUDE//g"`
NR_HANDLES=0


for PIDS in $TEMP;
do

FD_COUNTER=0

for FD in $(ls /proc/$PIDS/fd/);
do
FD_COUNTER=$[FD_COUNTER+1]
done

FD_COUNTER_ALL=$[FD_COUNTER_ALL+FD_COUNTER]

done
echo $FD_COUNTER_ALL


because lsof generates high load on the server....
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

Yes, it's a lot faster. But three runs on my host (laptop) yielded the follwoing
ls: /proc/4962/fd/: No such file or directory
ls: /proc/4963/fd/: No such file or directory
ls: /proc/-/fd/: No such file or directory
1162
[root@gandalf scripts]# . lvm_file_handles.sh
ls: /proc/5193/fd/: No such file or directory
ls: /proc/5194/fd/: No such file or directory
ls: /proc/-/fd/: No such file or directory
2324
[root@gandalf scripts]# . lvm_file_handles.sh
ls: /proc/5422/fd/: No such file or directory
ls: /proc/5423/fd/: No such file or directory
ls: /proc/-/fd/: No such file or directory
3486
So there has to be some more error handling, I suppose ...
Reinhard
stigmata
Posts: 49
Joined: Tue Nov 23, 2004 10:26 am

Post by stigmata »

hmm, strange - on our server this scripts works fine.
sorry thats not running on your laptop.
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

Don't bother for that, just thought it would make the script more stable to work on that. My system runs:
Linux 2.6.9-5.EL #1 Wed Jan 5 19:22:18 EST 2005 i686 GNU/Linux
0.8.6g
CACTID 0.8.6f Copyright 2002-2005 by The Cacti Group
PHP 4.3.9 (cgi) (built: Aug 17 2005 10:22:19)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
RRDtool 1.2.11 Copyright 1997-2005 by Tobias Oetiker <tobi@oetiker.ch>
mysql Ver 14.7 Distrib 4.1.10a, for redhat-linux-gnu (i386)
gcc (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.1)
Did not find time to debug this on my own yet...
Reinhard
wytzevanderploeg
Posts: 1
Joined: Wed Feb 27, 2008 8:42 am

Post by wytzevanderploeg »

Thank you for your script.

I have some entries in /proc/ which contain a '-' (dash) sign. These won't get replaced by sed. (sysrq-trigger, key-users) which result in a '-' sign as given directory and thus giving an error. If you add a dash to the sed script it works really well.

So the TEMP line would become like this:

Code: Select all

TEMP=`echo $TEMP | sed "s/[a-zA-Z-]//g" | sed "s/[.]//g" | sed "s/$PID_EXCLUDE//g"`
UPDATE:
I modified the script a bit. I hope you don't mind:

Code: Select all

#!/bin/bash

NR_HANDLES=0

for PIDS in $(ls /proc/ | sed "s/[^0-9]//g");
do
        if [ -d /proc/$PIDS ]; then
                NR_HANDLES=$[NR_HANDLES+$(sudo ls /proc/$PIDS/fd/ | wc -w)]
        fi
done

echo -n $NR_HANDLES
Because Cacti was running as www-data I had to add the sudo part to be able to list the contents of the fd dirs. The line in /etc/sudoers looks like this:

www-data ALL=(root) NOPASSWD:/bin/ls /proc/*/fd/

I attached the xml file for your convenience. (Exported with Version 0.8.6i)
Attachments
file_handles.xml
(6.33 KiB) Downloaded 831 times
Cactus84
Cacti User
Posts: 55
Joined: Thu Jan 12, 2012 8:22 pm

Re: Display open file handles

Post by Cactus84 »

So I tried this one and the command worked fine in the shell, but on the graph it was way off:
./file_handles.sh
1644
Does anything stand out?

Thanks
Attachments
error_cacti-wrong-values-gtemplate.PNG
error_cacti-wrong-values-gtemplate.PNG (42.8 KiB) Viewed 4158 times
error_cacti-wrong-values-data_template.PNG
error_cacti-wrong-values-data_template.PNG (70.09 KiB) Viewed 4158 times
error_cacti-wrong-values.PNG
error_cacti-wrong-values.PNG (17.24 KiB) Viewed 4158 times
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests