-   Programming (
-   -   Add logic in the existing code (

dnaqvi 03-02-2011 10:38 AM

Add logic in the existing code
1 Attachment(s)
I have a existing perl script.
This is our space monitor script.

Current Function of the Script:

If any file system exceed 90% following action do as below:

Script will create a output file which contains any new file created in the system in last 24 hours and any file over 100 MB. This file will send via email to the defined users. And file will deleted from the the System via script.

Current FS in the script:

( '/', '/dev', '/opt', '/tmp', '/var', '/usr );

In addition to above we want to do as follows:

We want to add two mounted file system as /mnt/backup_1 & /mnt/backup_2 in the logic.

We want any /mnt/ server will trigger when its over 95%, rest of the FS should be same as before 90%.

We do not want to delete output file from the System. We also do not want to send whole output file via email, we want to send only notification via email which state that specific FS is over 90% or 95% and please check /home/wasadmin/space for the FSWatch.log.

I have attached script.
Please have a look at it.


z1p 03-02-2011 11:16 AM

It seems like the simplest way to modify the script is to take the body of the for loop and create a function that takes a filesystem name and a threshold value as parameters. Then create a second list of file systems and add a second loop for that list using the higher threshold.

Something like the following:



my $mnt_threshold = 95;
@mnt_fs ( '/mnt/backup_1', '/mnt/backup_2');

process_fs {
    my($fs, $threshold) = @_;  #subroutine parameters

    undef @new_files;
    undef @big_files;
    @lines = `df -k $fs`;
    $lines[ -1 ] =~ /(\d+)\%/;
    ...  # The rest of the for loop

foreach my $fs ( @fs ) {
  process_fs ($fs, $threshold);

foreach my $mnt_fs ( @fs ) {
  process_fs ($fs, $mnt_threshold);

To not remove the log file then remove the unlink command the script, but you need to put in place some way to clean up the log files, even if it is to say someone needs to delete the log file after it is read.

As far as sending the email. A quick google of "perl email" will provide lots of ways to do it.

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