[lug] Monitoring file descriptors

David L. Anselmi anselmi at anselmi.us
Mon Jul 28 18:56:22 MDT 2008


Ben Whaley wrote:
> Folks,
 > Just trying to get us back on topic ;) Can't we all just be friends?

No!  We can't all just be friends!  Your script sucks!  All your base 
are belong to us!!!
;-)

But seriously, I always like to take things like this apart and put them 
back together.  (So I can't debug them later[1].)  (It's nice to hear 
about solutions like this, so don't take my changes as criticism. 
Decide for yourself which you like better.)

> if [ `cat /proc/sys/fs/file-nr | awk '{ diff=$3 - $1; print diff }'` -lt
> 20000 ]; then echo "Low on file descriptors - `cat /proc/sys/fs/file-nr |
> awk '{ diff=$3 - $1; print diff }'`" | mail -s "ftp1 file descriptors"
> ben at atrust.com; fi

diff=`awk '{ diff=$3 - $1; print diff }' /proc/sys/fs/file-nr` ;
[ "$diff" -lt 2000 ] && echo "Low on file descriptors - $diff" |
   mail -s "ftp1 file descriptors" ben at atrust.com

(the line feeds are gratuitous).  It does the same thing with less awks 
and cats.  It seems worthwhile to save diff rather than recalculate it 
later.

I'm always amused to see scripts at work that say:

cat foo | grep bar | awk '{print $3}'

which can be written as:

awk '/bar/ {print $3}' foo

(which, of course gets the foo and bar backwards so isn't nearly as 
aesthetic.) ;-)

Dave

  1. 
http://nedbatchelder.com/blog/200310/kernighan_on_debugging_clever_code.html



More information about the LUG mailing list