#!/usr/bin/perl -w =information Module Name : autosys_wild_search_2.0.0.pl Author : Sanjoga Sahu. Date of Modification : 12th, Jul, 2009. (v1.0.0) Modifications : 28th, Jan, 2011. (v2.0.0) Modification for getting separate Snapshot file for diffrent search criteria and -f (force) option to get forcefully new snapshot; otherwirse use the old snapshot file. Operating System(s) : Unix/Linux Description : This Script Get the autosys Snapshot and try to match the Search String Provided. =cut use strict; my $Version= "2.0.0"; use strict; use Getopt::Std; use vars qw(%opts); use Data::Dumper; $| = 10; # no buffering getopts('j:s:qfpwvh', \%opts); print STDERR "\n$0 Version $Version\n" and exit 0 if $Version and $opts{v}; &usage if $opts{h}; ##################################################################### #Defineing Variables... #if Autorep -w (wide jr) availabe then set $autorep_opt_w="-w" my $autorep_opt_w=""; if($opts{'w'}) { $autorep_opt_w="-w"; } my $date=`date +%Y%m%d`; chomp $date; #print Dumper(\%opts); if(!defined $opts{s}) { &usage; } #impose previous job info file use. if($opts{'f'}) { $opts{'p'}=0; } else { $opts{'p'}=1; } my $reg_ex_auto; if(defined $opts{'j'}) { $reg_ex_auto=$opts{'j'}; } else { $reg_ex_auto="%"; } my $attr=$opts{s}; $attr=~ s/\./\\./g; $attr=~ s/\*/\.\*/g; $attr=~ s/\%/\.\*/g; my $orig_attr=$attr; my $auto_inp; my $latest_input=0; my ($first_line,$last_line); #print Dumper(\%opts); my $auto_input_file; my $header_str; my $tailer_str="# ==T--H--E--#--E--N--D=="; ##################################################################### print STDERR "START : $0 V$Version\n"; print "\nAutosys Job/Regular Expression : $reg_ex_auto\n"; print "Attribute to Search from jils : $orig_attr\n"; #my $source_dir="$ENV{'HOME'}/aws"; my $source_dir="/home/n209263/aws"; unless(-d $source_dir) { my $rr=system("mkdir -p -m 777 $source_dir"); if($rr !=0) { print STDERR "Unable to Create Snapshot Directory : $source_dir, Exiting...\n"; exit(1); } } if($opts{'q'}) { if($opts{'j'}) { $auto_input_file="$source_dir/AUTOINPUT_jq"; print "Autosys JILs used for Search : YES [All Contents of JILs as Search Criteria]\n"; $header_str="# $date JOB: [$reg_ex_auto] JIL: YES\n"; } else { $auto_input_file="$source_dir/AUTOINPUT_aq"; print "Autosys JILs used for Search : YES [All Contents of JILs as Search Criteria]\n"; $header_str="# $date JOB: [$reg_ex_auto] JIL: YES\n"; } } else { if($opts{'j'}) { $auto_input_file="$source_dir/AUTOINPUT_jj"; print "Autosys JILs used for Search : NO [Only Job Names as Search Criteria]\n"; $header_str="# $date JOB: [$reg_ex_auto] JIL: NO\n"; } else { $auto_input_file="$source_dir/AUTOINPUT_jr"; print "Autosys JILs used for Search : NO [Only Job Names as Search Criteria]\n"; $header_str="# $date JOB: [$reg_ex_auto] JIL: NO\n"; } } if($opts{'f'}) { print STDERR "*~~*~~* [-f] Forcefully Getting SnapShot From Autosys [$auto_input_file] *~~*~~*\n"; } if($opts{'p'}) { my $res=&check_jobinfo_file($auto_input_file); if($res==0) { &parse_jobinfo($auto_input_file); } else { if(-f $auto_input_file) { my $resd=system("rm -rf $auto_input_file"); unless($resd==0) { print STDERR "Unable to Delete, Please Manually move or Delete the $auto_input_file\nExiting...\n"; exit(1); } } &gen_jobinfo($auto_input_file); &parse_jobinfo($auto_input_file); } } else { my $res=&check_jobinfo_file($auto_input_file); if($res==0) { &parse_jobinfo($auto_input_file); } else { &gen_jobinfo($auto_input_file); &parse_jobinfo($auto_input_file); } } print STDERR "END : $0 V$Version\n"; ##################################################################### sub check_jobinfo_file { my ($auto_input_file)=@_; if($opts{'p'}) { if(-f $auto_input_file) { unless(-r $auto_input_file) { print STDERR "Can't open for Reading Job Info File : $auto_input_file.\n\n"; return 1; } else { my $res=&check_for_goodfile($auto_input_file); if($res==0) { print STDERR "Good Job Info File Found and will be used for Search : $auto_input_file\n\n"; return 0; } else { print STDERR "Bad Job Info File Found and will Not be used for Search : $auto_input_file, will Generate New file...\n\n"; return 1; } } } else { print STDERR "Job Info File Not Found : $auto_input_file\n\n"; return 1; } } return 1; } ##################################################################### sub check_for_goodfile { my ($auto_input_file)=@_; my $first_line=`head -1 $auto_input_file`; chomp $first_line; my $last_line=`tail -1 $auto_input_file`; chomp $last_line; unless($opts{'p'}) { if($first_line eq $header_str && $last_line eq $tailer_str) { return 0; } else { return 1; } } else { my $flg_h=0; my $flg_t=0; if($first_line=~ /\#\s\d{8}\sJOB\:\s\[$reg_ex_auto\]\sJIL/) { $flg_h=1; } if($last_line eq $tailer_str) { $flg_t=1; } if($flg_h==1 && $flg_t==1) { return 0; } else { return 1; } } } ##################################################################### sub gen_jobinfo { my ($auto_input_file)=@_; print STDERR "Searching in Autosys, Please Wait..."; if($opts{'q'}) { $auto_inp=`autorep $autorep_opt_w -J $reg_ex_auto -q`; } else { $auto_inp=`autorep $autorep_opt_w -J $reg_ex_auto`; } open (WW,">$auto_input_file")|| die "Can't Open for Write :$auto_input_file :$!\n"; print WW $header_str; print WW $auto_inp; print WW $tailer_str; print STDERR "\rGetting Autosys JIL(s) for \"$reg_ex_auto\" Completed...\n\n"; close WW; system("chmod 777 $auto_input_file"); return; } ##################################################################### sub parse_jobinfo { my ($auto_input_file)=@_; my $val=`wc -l $auto_input_file`; chomp $val; #print STDERR "Number of Lines : $val\n\n"; my $num; if($val =~ /(\d+)\s+/) { $num=$1; } if($num==0) { print STDERR "\nNo Search Result Found for : $reg_ex_auto\n"; #&usage; exit(0); } $/=undef; open(RR,$auto_input_file) || die "Can't Open for Read :$auto_input_file:$!\n"; my $res_j=; my ($line1,$res)=split(/\n/,$res_j,2); #print "----$line1---\n"; close RR; my @jobs; if(defined $opts{q}) { @jobs=split(/insert_job/, $res); } else { @jobs=split(/\n/, $res); } #print STDERR "Generating Search Results using JILs for \"$attr\"...\n"; my $found=0; foreach my $job (@jobs) { my @eachjobs=split(/\n/,$job); my $count=0; my $job_name; my $results; if(defined $opts{q}) { my $fst_line=shift(@eachjobs); $fst_line="insert_job".$fst_line; unshift(@eachjobs,$fst_line); } my $jr; my $jr_flg=0; foreach my $eachline (@eachjobs) { next if($eachline=~ /^$/); next if($eachline=~ /\/\*/); next if($eachline=~ /$tailer_str|^insert_job$/); #print "==----$eachline==\n"; if($count==0) { if($eachline=~/insert_job:\s+(.+)\s+job_type/) { $job_name=&trim($1); } } if(!defined $opts{q}) { if($eachline=~/^(.+?)\s+(\d\d\/\d\d\/\d{4}|---)/g) { $job_name=&trim($1); } } #print "--$job_name++\n"; #$jr=`autorep -J $job_name -w`; if($eachline=~/$attr/i) { $jr=&trim(`autorep -J $job_name $autorep_opt_w | head -5 | tail -1`); $eachline=&trim($eachline); if($opts{'q'} && $jr_flg==0) { $jr=~ s/\s{5,}/ /g; $results .="$jr\n"; $jr_flg=1; } $eachline=~ s/\s{5,}/ /g; $results .="\t$eachline\n"; } $count++; } if(defined $results && $results ne "") { $found++; if($opts{'q'}) { #$results="--$found\. $job_name\n$results\n"; $results="$found\. $results\n"; } else { $results="$found\. $job_name\n$results\n"; } print $results; } } print STDERR "Generating Search Results using JIL(s) for \"$orig_attr\" Completed.\n"; print STDERR "$found Search Result(s) Found...\n\n"; return; } ###################### SUBROUTINE : trim ()########################## #removes prefix and postfixed space/newline/tab from input string sub trim { my ($str)=@_; return undef unless(defined $str); $str=~s/^\s+|\s+$//g; return $str; } sub usage { print STDERR qq( Usage: aws -j %Autosys_Job% -s "keyWord" -q Arguments: -j Regular Expresion of Autosys jobs (Ex: c_arcs%) OR job name (Optional) -s Attribute (keyWord) to search from Autosys [Jobs/JIL(s)].(Mendatory) Regular Expresions : * % -q Search the key word from JIL(s) for specified job(s). (Optional) -p Use existing Job Info File -w Autorep (wide) display -f Cerate Fresh Job Info File [By default search existing Job Info File] -v version -h help ); exit(0); }