LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script to check inodes and Email (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-check-inodes-and-email-4175428962/)

eyanu 09-25-2012 11:04 AM

Bash script to check inodes and Email
 
Hello am trying to write a script that will check inodes of a folder then email me the details, this is what i have so far, though when i run it, it just shows nothing.

Code:

#!/bin/bash
MAILTO="email@domain.com"
USAGE=200000 # inode usage - value in percent
dui=`df -hi | sed -n '2p' |awk '{print $5}' | grep -v U|cut -d% -f1`
if [ $dui -gt $USAGE ]
then
echo "Inode usage on `hostname` is exceeded - `$dui`"
fi

Please advice.

suicidaleggroll 09-25-2012 11:14 AM

The inode usage is in percent, meaning a value between 0 and 100, and you're checking if it's greater than 200000. Of course it will never be true. Unless of course you really are expecting a value >200000%...the highest on any of my machines is 9%. If you post the output of "df -hi" it might clarify some things.

Also, you shouldn't put $dui in back ticks in your echo. Back ticks mean "execute what's inside here as if it were typed on the command line". $dui is just going to contain a number, which can't be executed as if it were a command, so it will fail. Remove the back ticks if you just want to echo the number.

MensaWater 09-25-2012 11:20 AM

You haven't told it to send mail - you've simply defined a variable named MAILTO. You need to pipe your echo into a mail command like mailx.

You should use $() instead of backticks `. $() encapsulation is better because it allows nesting and also is less likely to be confused with single quotes.

You don't need to reinvoke the command in your echo so `$dui`. You've already set the variable earlier so should just call it as $dui in your echo. (i.e. no backticks).

Code:

#!/bin/bash
MAILTO="email@domain.com"
USAGE=200000 # inode usage - value in percent
dui=$(df -hi | sed -n '2p' |awk '{print $5}' | grep -v U|cut -d% -f1)
if [ $dui -gt $USAGE ]
then
echo "Inode usage on $(hostname) is exceeded - $dui" |mailx -s "Inode usage on $(hostname)" $MAILTO
fi

The above echo line will make the first part of the echo the body of the email, what follow -s is the subject and the final item is the recipient which is your $MAILTO variable.

eyanu 09-25-2012 12:17 PM

Hey, thanks guys, i followed your advise and changed USAGE to 20% and also piped it to mailx, but it still doesn't send, i check my mail there is nothing, even in qmail queue there is nothing, is there something else am missing?

suicidaleggroll 09-25-2012 12:22 PM

You shouldn't just write an entire script and hope that it works out of the box. BASH scripts are nice in that each line can be run by itself on the command line to see what it's doing.

What is the output of the following two commands on your box (just run them on the command line, not in a script).
Code:

df -hi
Code:

df -hi | sed -n '2p' | awk '{print $5}' | grep -v U | cut -d% -f1

eyanu 09-25-2012 12:24 PM

This is the result.

~# df -hi
Quote:

Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/md1 117M 3.2M 114M 3% /
none 3.0M 1 3.0M 1% /dev/shm
/dev/md0 128K 48 128K 1% /boot
~# df -hi | sed -n '2p' |awk '{print $5}' | grep -v U|cut -d% -f1
Quote:

3

suicidaleggroll 09-25-2012 12:26 PM

Your inode usage is only 3%, which is why it's not sending any mail. The way the script is written, the inode usage needs to exceed USAGE in order for anything to be sent.

You could arbitrarily lower USAGE to 2 to test the script.

eyanu 09-25-2012 12:28 PM

Man thanks alot...

eyanu 09-25-2012 12:34 PM

So how do i get the inode of a directory, say /var/www/vhosts/ and do a loop that will print each domain name that has exceeded 2%

MensaWater 09-25-2012 01:21 PM

inode percentage is a a percentage of filesystem NOT directory. Although you mount filesystems on directories called mount points it does not mean all directories are mount points.

To really figure out percentage for subdirectories or files inside a mount point you'd have to figure out:
1) How many inodes a specific filesystem allows.
2) How many inodes a given subdirectory or file uses.
3) Do a calculation to determine the percentage 2 is of 1.

eyanu 09-25-2012 03:45 PM

Thanks guys alot...


All times are GMT -5. The time now is 09:53 PM.