LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2004, 12:23 PM   #1
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Disk Space with Perl


Hi there,

Trying to learn perl and I'm trying to figure out how to check for disk space availabilty on my system. If one of the partitions are over 90% then e-mail root@localhost. Any help is greatly appreciated!! This is what I have so far and I can't think of what to do next:

#!/usr/bin/perl

$check="`df -k`";
open(INFO, $check) || die "could not open df: $!\n";

if (\/dev/) {
print "$_"; #Just testing

}
close (INFO);

Of course upon running it, my die statement returns. Hmm..Any shed of light on how to achieve this, again, is greatly appreciated. Thanks!

-twantrd
 
Old 09-22-2004, 02:09 PM   #2
Donboy
Member
 
Registered: Aug 2003
Location: Little Rock, Arkansas
Distribution: RH, Fedora, Suse, AIX
Posts: 736

Rep: Reputation: 31
If you just need to check the size of a file, you can do it like this...

$filepath = "/path/to/file";
($filesize, $filedate) = (stat($filepath))[7,9];

Now the $filesize variable is the size of the file. The $filedate is set for the file's last modified date.

If you want to execute your du command, you can do it like this..

$space = `du -cks /home/`;

Or whatever path/arguments you want. The backticks are the key. They execute shell commands on the system and the value of $space is set to the result of the shell command.

Hope that helped.
 
Old 09-22-2004, 06:30 PM   #3
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Thanks for your help. However, I need to check the partition space on all disks like:

/dev/hda1 ... .... 45% /
/dev/hda2 ... .... 80% /home
/dev/hda3 ... .... 92% /var

So pretty much, I need to save the Usage and Mount point values and run a check on them. If any of the Usage values are 90% or over, mail me the percentage and the partition that it is on. That's what I'm stuck at . Can anyone shed some light to this perl newb? Thanks...

-twantrd
 
Old 09-22-2004, 07:56 PM   #4
dannyp
LQ Newbie
 
Registered: Mar 2004
Location: Philadelphia, PA
Posts: 14

Rep: Reputation: 0
Your best bet is to use a shell script for such a small task. But here is the code for perl. I don't know how to get perl to mail to root so you have to create 2 files. The first is the perl script doing the work. Here it is.

Code:
#!/usr/bin/perl

@check=`df -k`;

@high = grep { m/\s(\d+)\%/ and $1 >= 70} @check;

open (OUT, " > temp") || "die couldn't open file temp: $!";


print OUT "@high\n";

close (OUT);

Name that script anything you want. Should try to end it in .pl so you know it is a perl script.

In are example I'll call it, work.pl.

Next create a shell script with the following.

For the example I'll call it, finished.bash

Code:
#!/usr/bin/bash    # or whatever shell you use.

work.pl                  # will run the perl script.

mail root@localhost < temp    # will mail temp to root
That should do it. You have to be root to write to the root mail, so if you want this to run put it in the root cronjob and it can run however much you specify.
 
Old 09-22-2004, 08:01 PM   #5
dannyp
LQ Newbie
 
Registered: Mar 2004
Location: Philadelphia, PA
Posts: 14

Rep: Reputation: 0
oops, replace that 70 with a 90 to get 90% and above, in the perl script
 
Old 09-23-2004, 02:30 AM   #6
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Thanks for the help. Now I need to decipher what that line meant:

@high = grep { m/\s(\d+)\%/ and $1 >= 70} @check;

Thanks though!

-twantrd
 
Old 09-23-2004, 03:40 AM   #7
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Ok,

After trying to decipher it, I think I know what you mean. Let me explain this and tell me if I'm right or wrong. hehe

m/ = matching some sort of expression which ends in /.
\s = I don't know why you added that. Can't figure that one out.
(\d+) = digits only. Can't you just put (\d) and it will work fine? Why have +?
\% = You don't want perl to actually parse this as an operator. This is an escape character.
$1 = whatever matches in the first parenthesis, if it matches it is return True.

So, @high will be an array that contains that explicit search of what is inside @check. However, whenever you use a unix command in perl, you need { }? I see that you did a grep { .... }.

Thank you for your help.

-twantrd
 
Old 09-23-2004, 06:01 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,863

Rep: Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598
Anybody can mail to root user.
 
Old 09-23-2004, 02:30 PM   #9
dannyp
LQ Newbie
 
Registered: Mar 2004
Location: Philadelphia, PA
Posts: 14

Rep: Reputation: 0
Ok here is the breakdown.


m/, you are correct with this meaning( you only need the m when you use another type of brackets to enclose the search. Ex: m% search %. Im just used to using it.

\s, is used to match just a single whitespace, \s* would match all.

\d+, is any digit [0-9] the plus is because your digit won't always be the same.

\%, you are correct on this.

$1, is the first argument sent to grep, so when the match is done if the number value it finds is greater than 90 put it in array high.

I used 2 books for learning perl that I can recommend.

1. Perl By Example by Ellie Quigley

2. Learning Perl by Randal schwartz & Tom Phoenix. I beleive the newsest version is 3rd edition.

As for mailing root, chrism01 is correct anybody can mail root. I still got lots to learn about unix.
 
Old 09-23-2004, 02:38 PM   #10
dannyp
LQ Newbie
 
Registered: Mar 2004
Location: Philadelphia, PA
Posts: 14

Rep: Reputation: 0
I noticed a mess up in the code that you should correct.

In the line

Code:
open (OUT, " > temp") || "die couldn't open file temp: $!";    #the  "  infront of die should                            be in front of couldn't.
 

correct version

open (OUT, " > temp") || die "couldn't open file temp: $!";
now the die will work if it can't write the file.
 
Old 09-23-2004, 02:46 PM   #11
Donboy
Member
 
Registered: Aug 2003
Location: Little Rock, Arkansas
Distribution: RH, Fedora, Suse, AIX
Posts: 736

Rep: Reputation: 31
Quote:
\d+, is any digit [0-9] the plus is because your digit won't always be the same.
You sure about that??? I believe it means to match more than one digit. Like \d would just match a single digit. \d\d\d would match 3 digits. \d+ matches 1 or more digits. That's my understanding anyway.
 
Old 09-23-2004, 04:22 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,863

Rep: Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598Reputation: 2598
FYI, here's the Perl CD Bookshelf online: http://iis1.cps.unizar.es/Oreilly/perl/
 
Old 09-23-2004, 08:06 PM   #13
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Chrism - GREAT SITE!! THANKS!!

DannyP - Thanks for the explanation! Anyhow, I found a way to mail and check for disk space in 1 script instead of 2. I modified yours. Below is the code.

#!/usr/bin/perl

use FileHandle;

$mailfrom = "from\@whatever.domain";
$mailto = "to\@whatever.domain";
@check=`df -k`;

@warn = grep { m/(\d+\%)/ and $1 >=90} @check;

$mail = new FileHandle;
$mail->open("| /usr/sbin/sendmail -t") || die "Cannot open: $!";
$mail->print("From: $mailfrom\n");
$mail->print("To: $mailto\n");
$mail->print("Subject: Disk space is low!\n\n");

foreach (@warn)
{
$mail->print($_, "\n");
}
$mail->close();


-twantrd
 
Old 09-23-2004, 11:37 PM   #14
dannyp
LQ Newbie
 
Registered: Mar 2004
Location: Philadelphia, PA
Posts: 14

Rep: Reputation: 0
chrism01 thanks for the link, Donboy you are right, I was explaining it wrong, I looked it up, guess I should stop going off the top of my head with things.. twantrd script looks good.
 
Old 09-30-2004, 05:44 PM   #15
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
I have just realized that the script i wrote is not complete. If NONE of my partitions are over 90%, it will send me a blank body mail message. Hmmm, I tried using if statements here and there but it fails. Can anyone show me the right direction...im such a noob. THanks...

-twantrd
 
  


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
out of disk space... snoply Linux - Enterprise 6 05-06-2005 08:56 AM
How to get rid of the begining and end space of a string for PERL?? cqmyg5 Slackware 2 04-28-2005 04:34 PM
3Gb of disk space lost! Disk space problem or mother board conflicts with HDD Mistreated Linux - Hardware 4 12-06-2004 03:58 PM
Where is all my disk space going? Baryonic Being Linux - General 6 10-13-2004 07:57 AM
Disk space wastage 73 GB Hard disk rajgopalhg Linux - Hardware 2 10-18-2002 03:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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
Open Source Consulting | Domain Registration