LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-25-2012, 12:04 PM   #1
eyanu
Member
 
Registered: Jul 2012
Location: kampala,Uganda
Distribution: backtrack
Posts: 87

Rep: Reputation: Disabled
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.
 
Old 09-25-2012, 12:14 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.

Last edited by suicidaleggroll; 09-25-2012 at 12:22 PM.
 
Old 09-25-2012, 12:20 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
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.
 
Old 09-25-2012, 01:17 PM   #4
eyanu
Member
 
Registered: Jul 2012
Location: kampala,Uganda
Distribution: backtrack
Posts: 87

Original Poster
Rep: Reputation: Disabled
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?
 
Old 09-25-2012, 01:22 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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

Last edited by suicidaleggroll; 09-25-2012 at 01:23 PM.
 
Old 09-25-2012, 01:24 PM   #6
eyanu
Member
 
Registered: Jul 2012
Location: kampala,Uganda
Distribution: backtrack
Posts: 87

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-25-2012, 01:26 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
1 members found this post helpful.
Old 09-25-2012, 01:28 PM   #8
eyanu
Member
 
Registered: Jul 2012
Location: kampala,Uganda
Distribution: backtrack
Posts: 87

Original Poster
Rep: Reputation: Disabled
Man thanks alot...
 
Old 09-25-2012, 01:34 PM   #9
eyanu
Member
 
Registered: Jul 2012
Location: kampala,Uganda
Distribution: backtrack
Posts: 87

Original Poster
Rep: Reputation: Disabled
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%
 
Old 09-25-2012, 02:21 PM   #10
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
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.
 
Old 09-25-2012, 04:45 PM   #11
eyanu
Member
 
Registered: Jul 2012
Location: kampala,Uganda
Distribution: backtrack
Posts: 87

Original Poster
Rep: Reputation: Disabled
Thanks guys alot...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash check return and email if criteria met sir-lancealot Programming 2 01-04-2012 11:22 AM
[SOLVED] Help Running a Check in Bash Script Duo11 Linux - General 14 04-04-2011 09:15 AM
Bash script to check if process is running and send email depam Linux - Newbie 2 04-08-2009 01:11 AM
Script to check IP address and send to an email kbrajesh Linux - Networking 4 02-08-2007 04:13 AM
bash script to check mount is ok procfs Programming 2 01-17-2007 07:19 PM


All times are GMT -5. The time now is 12:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration