Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

disk check using perl

by steadybompipi (Novice)
on Dec 14, 2007 at 16:18 UTC ( [id://657072]=perlquestion: print w/replies, xml ) Need Help??

steadybompipi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, I am very new to perl programming.So kindly pardon me if I am asking stupid question. Well, I am learning perl and was trying to convert some my simple shell script to perl. So, i was thinking to write a script that can check the disk space of the server and grep for any thing that is more then 90% and then it will send an email to me I realised that my doing the below, I could actually print out the disk space. disk = `df -k `; print ( $disk ); so can anyone kindly guide me: 1. how can I ouput it to a file 2.grep the file for anything more then 90% 3.send an email to myself.

Replies are listed 'Best First'.
Re: disk check using perl
by moklevat (Priest) on Dec 14, 2007 at 16:36 UTC
    Rather than trying to parse the the df output yourself, you may find it easier to work with the Filesys::Df module.

    To send an e-mail to yourself, you should look into one of the many modules that exist to make this easy. I like MIME::Lite.

    Update: Here is some (untested) code to work with. You will have to modify the email message with your e-mail addresses, and you might want to tweak the language of the Subject and Data (body text) to suit your style. Otherwise, the script reads the percent disk used from / and e-mails you if the percentage if greater than a threshold amount (in this case 90%)

    #!/usr/bin/perl use strict; use warnings; use Filesys::Df; use MIME::Lite; my $disk_info = df("/"); my $threshold = 90; my $disk_percent; if (defined($disk_info)){ my $disk_percent = $disk_info->{per}; } if ($disk_percent > $threshold) { my $message = MIME::Lite->new ( From =>'diskwatcher@yourcomputer.com', To =>'fixitguy@othercomputer.com', Cc =>'someonewhocares@somedomain.com', Subject =>'ALERT: Im in ur disk usin ur inodes!', Data => "It appears that the disk use threshold is + approaching the limit defined in your helpful script. You currently +have $disk_percent percent used. Perhaps you should check on this." ); $message -> send ; }
      seem like my perl version is old in my box. got the below command when i first run. Can't locate warnings.pm in @INC (@INC contains: /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503 /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .) at test2.pl line 4. BEGIN failed--compilation aborted at test2.pl line 4. hence i delete the user warnings; but it still show me some error. Can't locate Filesys/Df.pm in @INC (@INC contains: /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503 /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .) at test2.pl line 10 kindly advice~

        use warnings; can safely be removed. It's useful during development and debugging but shouldn't much change the operation of a program. As an alternative, you can add '-w' to the shebang line as in:

        #!/usr/bin/perl -w
        90% of every Perl application is already written.
        dragonchild
        perl can't find the module in @INC !
        You must install Filesys::Df module from cpan.org with cpan shell or download the source and install it.
        or
        include for example with use lib 'path_to_your_pm_lib';
        if you don't have the cpan shell :
        Enter from the shell command line
        perl -MCPAN -e 'install Filesys::Df'
        HTH,
        PooLPi
Re: disk check using perl
by andyford (Curate) on Dec 14, 2007 at 17:26 UTC
Re: disk check using perl
by talexb (Chancellor) on Dec 14, 2007 at 21:18 UTC

    I highly recommend Nagios for system monitoring. Our setup monitors a variety of parameters (including free disk space) on about forty servers, as well as the numerous installations of the web application that I wrote and currently support for a variety of clients.

    Some problems just send me E-Mail, and other problems page my Blackberry 24/7.

    Way better than anything I could written myself, and very flexible.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      Thanks everyone, but actually, i just need a simple script without using any module like use Filesys::Df; maybe something like : df = `df -k`; percentage =~ /something something/ if percentage > 90; send mail Well, I prefer to use script then download software for the monitoring as my side need alot of approval to do that. cheers
Re: disk check using perl
by Anonymous Monk on Mar 13, 2014 at 19:25 UTC
    #!/usr/bin/perl -w # check_disk_space.pl # Send an email notice out when mounted file systems are past a given +percentage. # # Perl v5.8.8 # Tested under RHEL4, RHEL5, and RHEL6 # # When this script is run it will check the status of all mounted file + systems # using the df command and if the percentage is past the given critica +l set level # will send out an email notice every time it is run. A set warning pe +rcentage # only send out an email every number_of_days_before_next_warning. # It keeps track of the number_of_days_before_next_warning using lock +files. # # The check_disk_space.xml config file should be in the same directory + as the program, # and be given the same name if the program name is changed. # #check_disk_space.xml # &lt; = <, &gt; = > #- # <config> # <warning_percent_used>90</warning_percent_used> # <critical_percent_used>99</critical_percent_used> # <number_of_days_before_next_warning>7</number_of_days_before_next_w +arning> # <warning_lock_directory>/home/<user>/</warning_lock_directory> # <hide_warning_lockfile>yes</hide_warning_lockfile> # <Include_Hostname_In_Subject>no</Include_Hostname_In_Subject> # <command_df>/bin/df</command_df> # <command_find>/bin/find</command_find> # <command_rm>/bin/rm</command_rm> # <command_date>/bin/date</command_date> # <command_hostname>/bin/hostname</command_hostname> # <command_sendmail>/usr/sbin/sendmail</command_sendmail> # <mail_to>user@mail.com</mail_to> # <mail_from>Disk Check &lt;user@system.com&gt;</mail_from> # <mail_subject_prefix>Disk Check</mail_subject_prefix> # <mail_subject_postfix></mail_subject_postfix> # <mail_x_mailer>Disk Check</mail_x_mailer> # </config> #- # # The following config toggles whether the host appears in the subjec +t line. # A value of "yes" will trigger the hostname include. # <Include_Hostname_In_Subject>no</Include_Hostname_In_Subject> # # The following line troggles weather or not lock files are created wi +th a # hidden dot prefix. A value of "yes" will trigger the hidden file. # <hide_warning_lockfile>yes</hide_warning_lockfile> # # The following line determins where the lock files will be placed. If + left # blank the lock files will be saved in the current directory. Be sure + to # end a directory path with a "/" in the config file. #<warning_lock_directory>/home/<user>/</warning_lock_directory> # # This program doesn't require any special privlege accesses, except r +ead/write # access to the lockfile directory. # # Recommended Crontab Entry: # # Check precentage of disk used every day and send out a email if cr +itical levels are reached. # 0 7 * * * cd /home/<user>/bin; /home/<user>/bin/check_disk_space.pl # # Script requirements: install XML Simple # yum install perl-XML-Simple # # History: # -------------------------------------------------------------------- +------- # 2014-02-24 kienenbe Started, works. # use XML::Simple; use File::Basename; #use Data::Dumper; #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Get the mount points from the df command. #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> sub get_filesystem_data { my @mount_points; my $df_line; my $filesystem; my $junk; my $total_size; my $amount_used; my $amount_avalible; my $percent_used; my @df_output = `$command_df -lh`; foreach $df_line (@df_output) { if ($df_line =~ m#\d\%#) { #print "->$df_line"; # Break out the needed data. ($percent_used,$filesystem) = split(/% /, $df_line); chomp $filesystem; ($junk,$total_size,$amount_used,$amount_avalible,$percent_used) += split(/\s+/, $percent_used); # Record the data for the filesystem mount point. $filesystem_info{"$filesystem"}{"total_size"} = "$total_size"; $filesystem_info{"$filesystem"}{"amount_used"} = "$amount_used"; $filesystem_info{"$filesystem"}{"amount_avalible"} = "$amount_av +alible"; $filesystem_info{"$filesystem"}{"percent_used"} = "$percent_used +"; } } } #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Format the text report output for display. #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # system.mysystem.org # Fri Feb 21 13:47:03 UTC 2014 # # ---- Critical file system space at or above 45% ---- # # Size Used Avail Use% Mounted on + # 1.8T 769G 973G 45% /mnt/backup + # 1.1T 436G 550G 45% /data + # # ---- Warning file system space at or above 8% ---- # # Size Used Avail Use% Mounted on + # 485M 38M 422M 9% /boot sub format_report_output { $warning_duplicate_check = 0; $critical_duplicate_check = 0; my $filename; # Cycle though each filesystem mount point. foreach my $filesystem (keys %filesystem_info) { if ($filesystem_info{"$filesystem"}{"percent_used"} >= $critical_p +ercent_used) { if ($critical_duplicate_check eq 0) { # Only print the date and time stamp if this is the first wa +rning check. if ($warning_duplicate_check eq 0) { $formated_report .= "\n $Hostname\n $TimeStamp\n +"; } $formated_report .= "\n ---- Critical file system space at or +above ${critical_percent_used}% ----\n\n"; $formated_report .= sprintf(" %5s %5s %5s %5s %-60s\n", "S +ize","Used","Avail","Use%","Mounted on"); $critical_duplicate_check = 1; } $formated_report .= sprintf(" %5s %5s %5s %5s %-60s\n", $fil +esystem_info{$filesystem}{total_size},$filesystem_info{$filesystem}{a +mount_used},$filesystem_info{$filesystem}{amount_avalible},"$filesyst +em_info{$filesystem}{percent_used}%",$filesystem); } elsif ($filesystem_info{"$filesystem"}{"percent_used"} >= $warning +_percent_used) { # Concert the file system mount point to a file name. $filename = &convert_filesystem_path_to_name($filesystem); # Remove warning locks for file system mount points past the num +ber_of_days_before_next_warning &remove_warning_lockfile($filename); if (&check_for_warning_lockfile($filename)) { # Do nothing } else { if ($warning_duplicate_check eq 0) { # Only print the date and time stamp if this is the first cr +itical check. if ($critical_duplicate_check eq 0) { $formated_report .= "\n $Hostname\n $TimeStamp\n +"; } $formated_report .= "\n ---- Warning file system space at or + above ${warning_percent_used}% ----\n\n"; $formated_report .= sprintf(" %5s %5s %5s %5s %-60s\n", +"Size","Used","Avail","Use%","Mounted on"); $warning_duplicate_check = 1; } $formated_report .= sprintf(" %5s %5s %5s %5s %-60s\n", $f +ilesystem_info{$filesystem}{total_size},$filesystem_info{$filesystem} +{amount_used},$filesystem_info{$filesystem}{amount_avalible},"$filesy +stem_info{$filesystem}{percent_used}%",$filesystem); # Create a warning lock file for the current file system mount + point. &write_warning_lockfile($filename); } } } } #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Send out the email to the recipients. #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> sub send_email_notice { # Send full report to recepiants(s) # Remove complaint about global empty vars such as ${mail_subject_po +stfix} no warnings 'uninitialized'; if ( $warning_duplicate_check eq 1 or $critical_duplicate_check eq +1) { open (MAIL, "|$command_sendmail -t") || die "Can't open the mail b +inary: ${command_sendmail}!\n"; print MAIL "X-Mailer: ${mail_x_mailer}\n"; print MAIL "Return-Path: root\@${Hostname}\n"; print MAIL "From: ${mail_from} <root\@${Hostname}>\n"; print MAIL "To: ${mail_to}\n"; if ($Include_Hostname_In_Subject eq "yes") { print MAIL "Subject: ${mail_subject_prefix}${Hostname}${mail_sub +ject_postfix}\n\n"; } else { print MAIL "Subject: ${mail_subject_prefix}${mail_subject_postfi +x}\n\n"; } print MAIL "\n${formated_report}\n\n"; close (MAIL); } } #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Conver the file system mount point to a lockfile name. #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> sub convert_filesystem_path_to_name { my $filesystem_path = shift; my $lockfile_name; $filesystem_path =~ s#/#_#g; $filesystem_path =~ s# #_#g; if ($hide_warning_lockfile eq "yes") { $lockfile_name = "." . $program_name . "_warning_lock" . $filesyst +em_path; } else { $lockfile_name = $program_name . "_warning_lock" . $filesystem_pat +h; } return ($lockfile_name); } #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Save the warning lockfile #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Creates the locking file in the format of: # / are converted to underscores for mount point. # <.><progran name><_warning_lock><mountpoint> # # -rwxr-x---. 1 user user 9211 Feb 21 13:46 df.pl* # -rw-rw-r--. 1 user user 0 Feb 21 13:47 .df_warning_lock_boot sub write_warning_lockfile { my $lockfile_name = shift; my $lockfile_directory = "./"; # Check if the $warning_lock_directory has been set. # If so use it instead of the current directory. if ( $warning_lock_directory) { $lockfile_directory = $warning_lock_directory; } # Check for the warning lockfile. # If it already exists exit. if ( -e "${lockfile_directory}${lockfile_name}" ) { return; } open (WARNING_LOCKFILE, '>', ${lockfile_directory}.${lockfile_name}) + or $formated_report .= "\nUnable to save warning lockfile for ${lock +file_directory}${lockfile_name}\n Warnings will be sent out every tim +e the script runs.\n\n"; close (WARNING_LOCKFILE); } #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Check for the warning lockfile #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> sub check_for_warning_lockfile { my $lockfile_name = shift; my $lockfile_directory = "./"; # Check if the $warning_lock_directory has been set. # If so use it instead of the current directory. if ( $warning_lock_directory) { $lockfile_directory = $warning_lock_directory; } # Check for the warning lockfile. if ( -e "${lockfile_directory}${lockfile_name}" ) { return 1; } else { return 0; } } #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> # Remove the warning lockfile #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> sub remove_warning_lockfile { my $lockfile_name = shift; my $lockfile_directory = "./"; # Check if the $warning_lock_directory has been set. # If so use it instead of the current directory. if ( $warning_lock_directory) { $lockfile_directory = $warning_lock_directory; } # If the file doesn't exist just exit. if (! -e "${lockfile_directory}${lockfile_name}" ) { return; } # Remove the warning lockfile. `$command_find ${lockfile_directory}${lockfile_name} -mtime +${numbe +r_of_days_before_next_warning} -type f -exec $command_rm -f {} \\;`; } # Run the main routine. &Start; #>>>>>>>>>>>>> #> Main Start #>>>>>>>>>>>>> sub Start { #declare all major variables used in this subroutine. local (%filesystem_info, $TimeStamp, $Hostname, $formated_report, $w +arning_duplicate_check, $critical_duplicate_check, $program_name); # Obtain configuration from a file with a name inferred from this # script's name. ($program_name) = split(/\./, basename($0)); my $program_full_name =basename($0); # if you don not wish to use XML::Simple comment out next line.. $config = XMLin("${program_name}.xml", SuppressEmpty => 'undef'); # if you don not wish to use XML::Simple configure variable directly + here. # if you don't use the XML config file you must place a "\" in front + of the # "@" in your email address so it is not interpreted as an array var +iable. # ex: local $mail_to = "vadapt.notice\@gm +ail.com"; local $command_date = $config->{'command_date' +}; local $command_df = $config->{'command_df'}; local $command_find = $config->{'command_find' +}; local $command_hostname = $config->{'command_hostn +ame'}; local $command_rm = $config->{'command_rm'}; local $command_sendmail = $config->{'command_sendm +ail'}; local $warning_percent_used = $config->{'warning_perce +nt_used'}; local $critical_percent_used = $config->{'critical_perc +ent_used'}; local $number_of_days_before_next_warning = $config->{'number_of_day +s_before_next_warning'}; local $warning_lock_directory = $config->{'warning_lock_ +directory'}; local $hide_warning_lockfile = $config->{'hide_warning_ +lockfile'}; local $Include_Hostname_In_Subject = $config->{'Include_Hostn +ame_In_Subject'}; local $mail_to = $config->{'mail_to'}; local $mail_from = $config->{'mail_from'}; local $mail_subject_prefix = $config->{'mail_subject_ +prefix'}; local $mail_subject_postfix = $config->{'mail_subject_ +postfix'}; local $mail_x_mailer = $config->{'mail_x_mailer +'}; # Get the hostname local $Hostname = `$command_hostname -f`; chomp $Hostname; # Get the date timestamp $TimeStamp = `$command_date`; chomp $TimeStamp; # Do the work &get_filesystem_data; &format_report_output; &send_email_notice; # uncomment to print report when run. #print $formated_report; #End the program exit; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://657072]
Approved by moklevat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-18 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found