Quantcast
Viewing all articles
Browse latest Browse all 12281

Script Monitor - Perl to List Certain Files and Sizes

We're trying to run a scrip monitor on SAM where we can monitor certain files and their sizes (particularly log files). We're trying to use the script below which we found in some forums. But we just need to list the file name and the size since the script monitor of SAM requires the out to be in pairs and maximum 10 pairs. Anyone has any idea on how we can do this?

 

#!/usr/bin/perl

 

 

use 5.010;

use strict;

use warnings;

 

 

use Getopt::Long;

my $dir = "";

my $sortby = "";

my $order = "";

my $result;

 

 

$result = GetOptions (

                    'dir=s'  => \$dir,                # specify derictory

                   'sortby=s'    =>  \$sortby,        # 'name' or 'date'

                   'order=s'     =>  \$order);        # 'asc'- or 'des'-ending order of sorting

 

 

print "derictory = $dir, sortby  = $sortby, order = $order \n\n";

 

 

 

 

opendir (DH, $dir)or die "couldn open dericroty: $!\n";

my @filenames = grep ! /^\./, readdir DH;

closedir (DH);

 

 

if ($sortby eq "name") {

 

 

     if ($order eq "asc") {

         foreach my $name (sort {lc $a cmp lc $b} @filenames) {

         my @statinfo = stat("$dir/$name");

         print "$name\tsize= " . $statinfo[7] . ",\t last modified=" .

         scalar(localtime($statinfo[9])) . "\n";

         }

     }

     elsif ($order eq "des") {

         foreach my $name (sort {lc $b cmp lc $a} @filenames) {

         my @statinfo = stat("$dir/$name");

         print "$name\tsize= " . $statinfo[7] . ",\t last modified=" .

         scalar(localtime($statinfo[9])) . "\n";

         }

     }     

}

 

 

 

 

if ($sortby eq "date") {

    if ($order eq "asc") {

        @filenames = sort { -M "$dir/$a" <=> -M "$dir/$b" } (@filenames);  

        print  join ("\n", @filenames);

    }   

    elsif ($order eq "des") {

        @filenames = sort { -M "$dir/$b" <=> -M "$dir/$a" } (@filenames);  

        print  join ("\n", @filenames);

    }   

}


Viewing all articles
Browse latest Browse all 12281

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>