Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending alert when file size is over 80% out of the space 2

Status
Not open for further replies.

moretips

MIS
Sep 25, 2000
25
US
I wish to have a script which will check some file size, if the file size is over 80% out of the space, it will send alert to me through the email.

Can anyone give some idea how to do it ? thanks for advance !
 
At least on Linux, do "perldoc -f stat" to learn how to use the "stat" function to get statistics of a file, including the file size.

Do "perldoc -q mail" to search for any perldoc info on "mail" - it gives you a lot of info, but on Perl 5.6, here's a section I found when I scrolled down the output:

How do I send mail?

Use the `sendmail' program directly:

open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
or die "Can't fork for sendmail: $!\n";
print SENDMAIL <<&quot;EOF&quot;;
From: User Originating Mail <me\@host>
To: Final Destination <you\@otherhost>
Subject: A relevant subject line

Body of the message goes here after the blank line
in as many lines as you like.
EOF
close(SENDMAIL) or warn &quot;sendmail didn't close nicely&quot;;

The -oi option prevents sendmail from interpreting a line
consisting of a single dot as &quot;end of message&quot;. The -t
option says to use the headers to decide who to send the
message to, and -odq says to put the message into the
queue. This last option means your message won't be
immediately delivered, so leave it out if you want
immediate delivery.
---------------------------------------

and that's just a small section - there's more on &quot;How do I send mail&quot;.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Hardy Merrill :

thanks for ur help ! Let me explain my problem.

I wish to write a script which will check my file size. IF the file size is more than 80% of the space , it will send me alert by mail.

For example, directory A has 500MB, if the space has less than 20% out of 500MB, it should sent me an alert.

My questions is how to check the space ?

In my database system, there r a lot of chuncks, i don't want it causing problem b'cos the db space has been fully used ( 0 free space). I wish to receive alert so that i can have a look b4 it cause problem.


 
i assume your on a unix system. the command 'df' tells you how much space is free. call this command in your script (with backtics, or open a pipe from it), parse the contents so as to have it understand which file allocations are assigned to what numbers, then have it email you the results when on of the ratios of &quot;free space/total space is > 0.8&quot; The hardest part of that is probably the parsing the return from 'df', and i'm not currently on a unix machine, otherwise i'd start you off.
good luck
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
On my Redhat 6.1 Linux system, this is what I get when I do a &quot;df /tmp&quot;:

Filesystem 1k-blocks Used Available Use% Mounted on
/dev/sda1 3731736 2366984 1175188 67% /


your code would look something like this...

### note that this next command uses backticks
$df_output = `df /dir/you/want/to/test`

### now you want to throw out the first line of header,
### and on the remaining line do some regex to give
### you the number before the percent sign

What you want is the 67 - you'll have to create a regular expression to parse out the number right before the percent sign, and if that number is greater than 80, then send an email using instructions that I printed above. I don't have time right now to play around with finding the right regex to do this.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Thanks to hmerill n stillflame !

i tried to use the df command, but this command only give me the size of the folder.

Instead, i use this&quot; onstat -d&quot; command. Below is part of my script:

onstat -d | grep &quot;\/dev\/my_file_name&quot; | space={$6/$5}*100

echo &quot;$space&quot;

----------------

onstat -d command will get this result :

--------------------------------------------------------------------------------------
chunks chk/dbs offset size free bpages flags pathname

xxxxx 1 1 0 51200 49800 PO- /dev/file_name
xxxxx 2 2 0 10240 35987 PO- /dev/other_file_name
---------------------------------------------------------------------------------------------


So, in order to get the %, i have to divide column 6 ($6) and column 5 ($5) and * 100.

But the result i get is : {/}*100.

May i know anything wrong with my script ?
FYI, this is HP unix box which use Informix as the database.

 
Seems like this is easier than this thread indicates. The following is done on a Sun/Solaris box. I used to work on HP-UX boxes. They may use a switch other that -k with the df command, but they can produce similar output, including the amount of space left in a file system. 'bdf' will also work. Anyway, you can see the general approach below.....


Code:
#!/usr/local/bin/perl
# do df on the file system in question
# root (/), in this example
# change the '/' to what ever mount point you want to check
# split the results of the backticks on a new line char
@df = split(/\n/,`df -k /`); 

# you should get something like this back (no leading #s)
# Filesystem          kbytes      used     avail    capacity   Mounted on
# /dev/dsk/c0t0d0s0  3339774   1359852   1946525        42%    /

# split the '/dev/dsk...' line on multiple spaces
@fields = split(/ +/,$df[1]);

$percent_used = $fields[4];
chop($percent_used); # get rid of percent sign
print &quot;%used - $percent_used %\n&quot;;

if ($percent_used > 79)
{
# do the mail stuff.
}

HTH


keep the rudder amid ship and beware the odd typo
 
For the percentage you need to do this:

$5
------ * 100 = percentage
$5 + $6


onstat -d | grep &quot;\/dev\/my_file_name&quot; | space={$5/($5+$6)}*100

only I'm not sure if that works - I don't have the onstat function on my machine. Are you sure $5 and $6 actually represent the 5th and 6th columns of the onstat command?

*In Perl*, I would execute the onstat command with backticks, which will bring the output of that command back into Perl, and then work with the lines in Perl.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
The reason why i use &quot; onstat -d &quot; instead of &quot;df&quot; or &quot;bdf&quot; is this &quot;onstat -d &quot; will let me know the chunk usage in my server. The other 2 command only give me the size of the whole directory.

However onstat -d didn't give the % of the usage, so i have to divide them. But the script that i wrote give me the wrong answer : {/}*100

I think that is something wrong with my column position.
i am quite blur with the spilt function. :-(
 
instead of splitting it right now, type this on the command line:[tt]
onstat -d > temp.txt[/tt]

then study temp.txt carefully to learn what you need to do in order to correctly parse out the information. maybe you'll need a full blown regex, or maybe just splitting on whitespace will work, but you'll need to understand the syntax of the output before anything will work.
any way, sorry i don't have the exact answer, but my current system in not set up to be able to answer this question. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
why don't you pull the output from onstat into an array and parse it with Perl?

something like....

#!/usr/local/bin/perl
@onstat = split(/\n/,`onstat -d /`);
foreach (@onstat)
{
if (/^--|^chunks|^\n/) { next; }
@fields = split(/\s+/,$_);
$percent_used = ($fields[5]/$fields[4]) * 100;
$pathname = $fields[7];
print &quot;Usage for $pathname: $percent_used\n&quot;;
}

a little verbose and rudimentary, but, maybe it is step toward success....

HTH


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top