ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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.
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...
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.
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 { .... }.
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.
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: $!";
\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.
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.
$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");
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.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.