Perl Directory and Process Watcher

Directory watcher to monitor uploaded files in a server, and do some data extraction after upload process is complete
 

#!/usr/sbin/perl -w

#use strict;
use warnings;
use POSIX;
use feature "switch";
use File::Copy;

my $dir = "/raid/raw";
my $rtstps_dir = "/home/ipopp/drl/rt-stps";
my $processed_file = "";
my $config_file = "";

my $rtstps = 0;
my $rtstps_file = "";

print "starting ... \n";

chdir $rtstps_dir or die "Cannot find RTPSPS directory";

#daemonize();

PARENT: while () {

    sleep(6);

    if (check_rtstps()) {

        print "rt-stps is running, please wait ....! \n";
        next;

    } elsif ($rtstps == 1 && $rtstps_file ne "") {

        move("$dir/$rtstps_file","$dir/arch/$rtstps_file");
        $rtstps = 0;
        $rtstps_file = "";

    }


    if ($processed_file eq "") {

        opendir (DH, $dir) || die "Cannot open directory $dir: $!";

        my @files = readdir(DH);
        my $numbfile = @files;

        if ( $numbfile > 2 ) {

            my $myfiles = get_filename(@files);

            if ($myfiles ne $processed_file) {
                $processed_file = $myfiles;
            }

        }

        closedir(DH);

    } else {
        # check if file still uploaded or not
        my $cnt = 0;
        my $cur_size = 0;
        my $last_size = 0;
        my $cur_file = "$dir/$processed_file";

        CHILD: while() {
            sleep (3);
            $cur_size = (stat($cur_file))[7];
            if ($cur_size == $last_size) {
                $cnt++;
                if($cnt >= 20) {
                    #my @args = ("$rtstps_dir/bin/batch.sh","$rtstps_dir/config/$config_file","$dir/$myfiles");
                    #system(@args);
                    run_rtstps($processed_file);
                    $processed_file = "";
                    last;
                }
            }

            if ($last_size != 0 && $last_size != $cur_size) {
                print "uploading ... \n";
            }

            $last_size = $cur_size;
        }
        $cnt = 0;
        $cur_size = 0;
        $last_size = 0;

    }

    if(!check_rtstps()) {
        print "------------Waiting for new File-----------\n";
    }
   

}


sub get_filename {

    #todo :: 2 or more files hanlder
    #todo :: patern regex for .modis files
       
    foreach my $file (@_) {
        given ($file) {
            when (/Terra/) {
                $config_file = "terra.xml";
                return $file;
                #last;
            }

            when (/Aqua/) {
                $config_file = "aqua.xml";
                return $file;
                #last;   
            }
           
            when (/SNPP/) {
                $config_file = "npp.xml";
                return $file;
                #last;
            }
        }
    }
   
}

sub run_rtstps {
    my @args = ("$rtstps_dir/bin/batch.sh","$rtstps_dir/config/$config_file","$dir/$_[0]");
    system(@args);
    $rtstps = 1;
    $rtstps_file = "$_[0]";
}

#Check if any rtstps process is running
sub check_rtstps {

    my($cmd, $process_name) = ("batch.sh", "batch.sh");

    return (`ps -aef | grep $process_name | grep -v grep`)
}

# Make a process as a daemon
sub daemonize {
    use POSIX;
    POSIX::setsid or die "setsid: $!";
    my $pid = fork ();
    if ($pid < 0) {
        die "fork: $!";
    } elsif ($pid) {
        exit 0;
    }
    chdir "/";
    umask 0;
    foreach (0 .. (POSIX::sysconf (&POSIX::_SC_OPEN_MAX) || 1024))
    { POSIX::close $_ }
    open (STDIN, "</dev/null");
    open (STDOUT, ">/dev/null");
    open (STDERR, ">&STDOUT");
 }

Komentar

Postingan populer dari blog ini

Display Image on Panel after transfered via Socket :: Java

Install and Setup VSFTPD in CentOS 6.5

Centos 6 Kambing Repo