Professional Web Applications Themes

Can do in 1 while loop? - PERL Beginners

Hello Perl Gurus, I wrote a script to search for log suspends and bloking processes in a text file and send me email if it find either of them. My codes below work but it's not efficent. As you can see I open the file and go to while loop twice. Can someone suggest a better way? Thanks. (attached is the text file i open to search.) Below is my codes: #!/bin/perl -w require "/home/sybase/tranl/pl/global.pl"; ## Search for blocking process open (FILE,"<$whodo_out") or die ("Cannot open file: $!"); while (my $line =<FILE>){ chomp($line); $line =~ s/^\s+//; next if ($line =~ ...

  1. #1

    Default Can do in 1 while loop?

    Hello Perl Gurus,

    I wrote a script to search for log suspends and
    bloking processes in a text file and send me email if
    it find either of them. My codes below work but it's
    not efficent. As you can see I open the file and go to
    while loop twice. Can someone suggest a better way?
    Thanks.
    (attached is the text file i open to search.)
    Below is my codes:

    #!/bin/perl -w
    require "/home/sybase/tranl/pl/global.pl";

    ## Search for blocking process

    open (FILE,"<$whodo_out") or die ("Cannot open file:
    $!");
    while (my $line =<FILE>){
    chomp($line);
    $line =~ s/^\s+//;
    next if ($line =~ /^\D/);
    my $blk = substr($line,40,3);
    print " $blk \n";
    if ($blk != 0){
    print 'Alert! Blocking processes';
    system("/usr/bin/mailx -s 'Alert Blocking
    Process' $receipients < $whodo_out");
    }
    print "\n $suspend \n";
    #exit ;
    }#end while

    close (FILE);

    # Search for LOG SUSPEND process

    open (FILE,"<$whodo_out") or die ("Cannot open file:
    $!");
    while (my $line =<FILE>){
    chomp($line);
    $line =~ s/^\s+//;
    next if ($line =~ /^\D/);
    my $log_suspend = substr($line,70,11);
    print "$log_suspend \n";
    if ($log_suspend eq 'LOG SUSPEND'){
    print 'Alert! LOG SUSPEND processes';
    system("/usr/bin/mailx -s 'Alert LOG SUSPEND
    Process' $receipients < $whodo_out");
    }

    }#end while

    close FILE;

    ##



    __________________________________
    Do you Yahoo!?
    Read only the mail you want - Yahoo! Mail SpamGuard.
    http://promotions./new_mail

    spid status loginame hostname blk program dbname cmd cpu io
    ------ ------------ ------------ ---------- ---- ---------------- ------- ---------------- ------ ------------
    2 sleeping NULL 0 master NETWORK HANDLER 0 0
    3 sleeping NULL 0 master DEADLOCK TUNE 0 0
    4 sleeping NULL 0 master MIRROR HANDLER 0 0
    5 sleeping NULL 0 <astc> master ASTC HANDLER 0 0
    6 sleeping NULL 0 master CHECKPOINT SLEEP 0 1502134
    7 sleeping NULL 0 master HOUSEKEEPER 0 886596
    8 sleeping NULL 18 master NETWORK HANDLER 0 0
    13 sleeping loader 0 Mills INSERT 2031 9872
    16 recv sleep sa ASDECS04 0 Rapid SQL Mills AWAITING COMMAND 1 0
    18 running sa titania.ab 0 isql Mills SELECT 218 0
    21 sleeping loader 0 Mills LOG SUSPEND 1930 0

    Loan Guest

  2. #2

    Default Re: Can do in 1 while loop?

    On Mon, 2004-10-11 at 06:26, loan tran wrote: 



    I am not able to get it ?
    Why cant you put This inside your first while loop

    my $log_suspend = substr($line,70,11);
    print "$log_suspend \n";
    if ($log_suspend eq 'LOG SUSPEND'){
    print 'Alert! LOG SUSPEND processes';
    system("/usr/bin/mailx -s 'Alert LOG SUSPEND
    Process' $receipients < $whodo_out");
    }




    Probably after
    "print "\n $suspend \n";"


    Bye
    Ram


    Ramprasad Guest

  3. #3

    Default Re: Can do in 1 while loop?


    --- Ramprasad A Padmanabhan
    <com> wrote:
     
    > if 
    > it's 
    > go to 
    > way? 
    > file: 
    > file: 
    > SUSPEND 
    >
    >
    >
    > I am not able to get it ?
    > Why cant you put This inside your first while loop
    >[/ref]
     

    I have tried your suggestion. Howerver, result i got
    is only the first if run ( reciveice only blocking
    email).

    In my first post I forget a "last" statement in each
    if. (b/c I dont want to receive multible emails if
    there are multily blocking or suspending procecess.)

    So here is the code again:


    open (FILE,"<$whodo_out") or die ("Cannot open file:
    $!");
    while (my $line =<FILE>){
    chomp($line);
    $line =~ s/^\s+//;
    next if ($line =~ /^\D/);
    my $blk = substr($line,40,3);
    if ($blk != 0){
    print "Alert Blocking processes\n";
    system("/usr/bin/mailx -s 'Alert BLOCKING
    Process' $receipients < $whodo_out");
    last;
    }

    }#end while

    close (FILE);


    # Search for LOG SUSPEND process

    open (FILE,"<$whodo_out") or die ("Cannot open file:
    $!");
    while (my $line =<FILE>){
    chomp($line);
    $line =~ s/^\s+//;
    next if ($line =~ /^\D/);
    my $log_suspend = substr($line,70,11);
    if ($log_suspend eq 'LOG SUSPEND'){
    print "Alert! LOG SUSPEND processes\n";
    system("/usr/bin/mailx -s 'Alert LOG SUSPEND
    Process' $receipients < $whodo_out");
    last;
    }
    }#end while

    close FILE;
     




    __________________________________
    Do you Yahoo!?
    Yahoo! Mail Address AutoComplete - You start. We finish.
    http://promotions./new_mail
    Loan Guest

  4. #4

    Default Re: Can do in 1 while loop?


    ----- Original Message -----
    From: "loan tran" <com>
    To: "perl beginners" <org>
    Sent: Monday, October 11, 2004 3:36 PM
    Subject: Re: Can do in 1 while loop?

     
    > > if 
    > > it's 
    > > go to 
    > > way? 
    > > file: 
    > > file: 
    > > SUSPEND 
    > >
    > >
    > >
    > > I am not able to get it ?
    > > Why cant you put This inside your first while loop
    > >[/ref]

    >
    > I have tried your suggestion. Howerver, result i got
    > is only the first if run ( reciveice only blocking
    > email).
    >
    > In my first post I forget a "last" statement in each
    > if. (b/c I dont want to receive multible emails if
    > there are multily blocking or suspending procecess.)
    >
    > So here is the code again:
    >
    >[/ref]
    You should post your beggin code as well, so you can clearify what modules
    you are using.
     
    You should change the above logic, match will occure with spaces and line
    terminators
     
    You can do both of your string manipulation statments here, unless you are
    only showing partial code. 

    Here is one way you can try it.#Untested

    #!PERL
    use warnings;
    use strict;

    my $whodo_out = $ARGV[0] || die "NO FILE TO P\a\n";
    my $receipients = $ARGV[1] || die "NO RECEIPIENTS ADDRESS\a\n";
    open FILE,$whodo_out || die "Cannot open file: $!\n";

    while (my $line =<FILE>){

    next if ($line =~ /^[\s\D]+);
    chomp $line; # not sure if you need this chomp at all
    $line =~ s/^\s+//;

    my $blk = substr($line,40,3);
    my $log_suspend = substr($line,70,11);

    if ($blk != 0){
    print "Alert Blocking processes\n";
    system "/usr/bin/mailx -s 'Alert BLOCKING Process' $receipients <
    $whodo_out";
    last;
    }

    if ($log_suspend eq 'LOG SUSPEND'){
    print "Alert! LOG SUSPEND processes\n";
    system "/usr/bin/mailx -s 'Alert LOG SUSPEND Process' $receipients <
    $whodo_out";
    last;
    }

    }#end while

    close FILE;

    HTH,
    Mark G.
     
    >
    >
    >
    >
    > __________________________________
    > Do you Yahoo!?
    > Yahoo! Mail Address AutoComplete - You start. We finish.
    > http://promotions./new_mail
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >
    >[/ref]


    Mark Guest

Similar Threads

  1. Loop option set, but flash doesn't loop
    By uj666 in forum Macromedia Flash Player
    Replies: 6
    Last Post: October 21st, 04:23 AM
  2. loop list and compare to query loop
    By boxhead in forum Coldfusion - Advanced Techniques
    Replies: 4
    Last Post: September 9th, 08:38 AM
  3. Film loop rollovers working with tell sprite, but only if Loop is checked
    By Schiemer webforumsuser@macromedia.com in forum Macromedia Director Basics
    Replies: 0
    Last Post: November 27th, 06:02 AM
  4. Urgent: Repeat loop and Film loop clash!
    By Jai G in forum Macromedia Director Lingo
    Replies: 4
    Last Post: July 31st, 04:39 AM
  5. Help with loop inside loop and mysql queries
    By Petre Agenbag in forum PHP Development
    Replies: 1
    Last Post: July 16th, 07:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139