counting down in a for

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default counting down in a for


    Hi all

    there is probably a much better way of doing all this i am working on
    the
    checkResults sub.
    it should find the pollerAudit log created by the main process
    (discover) the file format for the log file is
    pollerAudit.M.D.Y.h.m.s.lan (MDYhms being month day year hrs min sec)
    but as we do not know what the file name will be i want to search for
    it, it will be between startTime and EndTime but closest to endTime

    so i thort i should use a for loop form Max to Min as below, but its all
    getting rather messy.

    is there some CSPAN module that can help me out?
    or has any one got any suggestions for a betta way to do this.
    i have included the full proggi below for reff...


    sub checkResults {
    my($NH_HOME,$reslutsLog)=("nethealth1","");
    my($lowTime,$highTime)=($_[0],$_[1]) ;
    my($fileDate)=strftime("%m.%d.%Y",localtime($lowTi me));
    my($lowFileName)=strftime("%H%M%S",localtime($lowT ime));
    my($highFileName)=strftime("%H%M%S",localtime($hig hTime)); for (
    my ($fTime) = $highFileName; $fTime = $lowFileName; $fTime--
    ) {
    my($fullFileName)="pollerAudit.$fileDate.$_.log";
    if (-e "/$NH_HOME/log/$fullFileName") {
    open UPDATELOG, "$NH_HOME/log/$fullFileName";
    my($reslutsLog) = <UPDATELOG>;
    close UPDATELOG;
    };

    };
    print "$reslutsLog";


    }


    oh and before any one starts sorry about my bad spellings.

    ---
    fnord
    yes im a _Concord_ Engineer, no it never flown!

    --------------------------------------------------------------------------
    #!/usr/local/bin/perl
    #
    use strict;
    use warnings;
    use POSIX qw(strftime);

    ################################################## ##########
    # main discover
    #
    #
    ################################################## ##########


    my ($dataFileName)=$ARGV[0]; # get file name of discover file
    my ($SdtArfName)=$ARGV[1]; # get std rule name


    open DFILE, "$dataFileName" # open datafile and read in or die
    or die "no file specified command format is \"autoDiscover
    FILE-NAME Server \" $!";
    my (@dataFile) = <DFILE>;
    close DFILE;

    foreach my $site (@dataFile) { # loop for each line/site in dataFile
    chomp $site ;
    my ($siteIP,$siteString)=makeArf("$site","$SdtArfName "); # make
    ARF file then return IP and Cominity String

    print " Discovering $siteIP with $siteString \n ";

    my ($startTime)=time; # log start time of discover
    `nhAutoDiscover -noCommit -l \"$siteIP\" -c \"$siteString\"
    -mode
    \"lanwan,router\" -nmsSource \"NH:Discover\" -dciRule
    \"$siteIP.arf\" > tmp.$siteIP.Results \n `; # this is the command used
    to do the discover
    my ($endTime)=time; # log start time of discover
    checkResults("$startTime","$endTime"); # run the results sub };

    sub checkResults {
    my($NH_HOME,$reslutsLog)=("nethealth1","");
    my($lowTime,$highTime)=($_[0],$_[1]) ;
    my($fileDate)=strftime("%m.%d.%Y",localtime($lowTi me));
    my($lowFileName)=strftime("%H%M%S",localtime($lowT ime));
    my($highFileName)=strftime("%H%M%S",localtime($hig hTime)); for (
    my ($fTime) = $highFileName; $fTime = $lowFileName; $fTime--
    ) {
    my($fullFileName)="pollerAudit.$fileDate.$_.log";
    if (-e "/$NH_HOME/log/$fullFileName") {
    open UPDATELOG, "$NH_HOME/log/$fullFileName";
    my($reslutsLog) = <UPDATELOG>;
    close UPDATELOG;
    };

    };
    print "$reslutsLog";


    }



    sub makeArf {
    # Start of sub that makes arf files
    ################################################## ##########
    # this function creates a arf rule file from input Data
    # Version 0.1 6/11/03 original
    # 0.2 21/11/03 tryed to convert to sub
    # 0.3 42/11/03 finished converting to sub
    ################################################## ##########

    my ($csvData)="$_[0]";
    my ($siteLink,$siteNoOfPVCs,$siteAllPVCs)=split(/:/,$csvData,3); #split
    up main / pvc info
    my
    ($siteIP,$siteString,$siteName,$siteGroup,$siteCCT Reff,$siteACRate)=split(/,/,$siteLink,6);
    #split up main info
    my @sitePVCs = split(/;/,$siteAllPVCs,$siteNoOfPVCs);

    my $siteARFFILE = "$siteIP.arf";
    open ARFFILE, ">$siteARFFILE"
    or die "can not open '$siteARFFILE': $!";

    # print header
    print ARFFILE <<EOF;
    ################################################## #################### #
    # Discover Rule for: $siteIP
    #
    ################################################## ####################
    \n EOF

    # open standard file and print
    my ($standardFile)="standard.$_[1].txt";
    open IN, "$standardFile" # open file with standard bits
    or die "can not open standard arf rules file: $! ";
    my @standardRules = <IN>;
    close IN;
    print ARFFILE "@standardRules\n"; #print standard bits



    # print -Cpu- rule
    print ARFFILE <<EOF;
    name matches ".*-Cpu-.*": {
    setName ("$siteName-RH-Cpu");
    setGroup ("$siteGroup");
    setAlias ("RH-Cpu");
    }\n
    EOF

    # print -RH rule
    print ARFFILE <<EOF;
    name matches ".*-RH": {
    setName ("$siteName-RH") ;
    setGroup ("$siteGroup") ;
    setAlias ("RH") ;
    }\n
    EOF

    # print RH-Serial rule
    print ARFFILE <<EOF;
    name matches ".*RH-Serial.*": {
    setName("$siteName-RH-WAN\$2");
    setGroup("$siteGroup");
    setAlias("$siteCCTReff");
    setSpeedIn("$siteACRate");
    setSpeedOut("$siteACRate");
    setDeviceSpeedIn("$siteACRate");
    setDeviceSpeedOut("$siteACRate");
    }\n
    EOF


    # print -Serial rule
    print ARFFILE <<EOF;
    name matches ".*-Serial.*": {
    setName ("$siteName-WAN\$2") ;
    setGroup ("$siteGroup") ;
    setAlias ("$siteCCTReff") ;
    setSpeedIn ("$siteACRate") ;
    setSpeedOut ("$siteACRate") ;
    setDeviceSpeedIn ("$siteACRate") ;
    setDeviceSpeedOut ("$siteACRate") ;
    }\n
    EOF

    foreach my $PVC (@sitePVCs){ # loop for each PVC

    my ($PVCdlci,$PVCname,$PCVreff,$PVCcir)=split(/,/,$PVC,4); #
    split
    out pvc info

    # print PVC rules
    print ARFFILE <<EOF;
    name matches ".*-dlci-$PVCdlci": {
    setName ("$siteName-$PVCname") ;
    setGroup ("$siteGroup") ;
    setAlias ("$PCVreff") ;
    setSpeedIn ("$PVCcir") ;
    setSpeedOut ("$PVCcir") ;
    setDeviceSpeedIn ("$PVCcir") ;
    setDeviceSpeedOut ("$PVCcir") ;
    }\n
    EOF

    }

    close(ARFFILE)
    or die("can not close '$siteARFFILE': $!");

    return ($siteIP,$siteString); # return the IP and comunity string
    to main ruteen
    }; #end of makeArf Sub

    --


    --



    drowl@23.me.uk Guest

  2. Similar Questions and Discussions

    1. Trouble counting to EIGHT?
      Because couting is ever so hard for you guys at Macromedia who are working on the Linux version of Flash Player, here is a reminder of how to count...
    2. Duplicate counting
      Hello all, I wrote a function which counts price of product from retail price and discount. It works. But I need to count price with tax of same...
    3. Counting (easy!)
      I'm sure this is easy but I'm a newbie. I was doing control statements (for, while,etc.) like this: for ($count = 1; $count <= 5; $count++) {...
    4. Counting (easy!) (YES!!)
      I think I figured it out! A FIRST!! for ($i = 1; $i <= 5; $i++){ sleep 1; print "$i\n"; } I prints out like this: 1...2...3...4...5
    5. counting duplicates
      Jane, SELECT field1,count(field1) 'Counter' FROM <tgablename> GROUP BY field1 -- Dinesh. SQL Server FAQ at http://www.tkdinesh.com
  3. #2

    Default Re: counting down in a for


    On Dec 11, 2003, at 11:49 AM, [email]drowl@23.me.uk[/email] wrote:
    [..]
    > there is probably a much better way of doing all this i am working on
    > the checkResults sub.
    > it should find the pollerAudit log created by the main process
    > (discover) the file format for the log file is
    > pollerAudit.M.D.Y.h.m.s.lan (MDYhms being month day year hrs min sec)
    [..]

    This Might be a good time to step back and
    look at this problem in terms of 'algorithm'
    then worry about the code side. Since, while,
    yes, I can 'read' your code, it may help to begin
    with 'what do you really want/need'?

    You are passing in two time values into a function,
    so that you can try to find the 'files' that would
    be made between those times, yes? In particular,
    you are looking for the most recent one, yes?

    IF you only need the most recent one, then your basic
    iteration loop could be simplified with say

    for ( my ($fTime) = $highFileName; $fTime = $lowFileName; $fTime--)
    {
    #my($fullFileName)="pollerAudit.$fileDate.$_.log";
    # make one substitution
    my($fullFileName)="/$NH_HOME/log/pollerAudit.$fileDate.$fTime.log";
    if (-e "$fullFileName") {
    open UPDATELOG, "$fullFileName";
    my($reslutsLog) = <UPDATELOG>;
    close UPDATELOG;
    last; # break out of the loop we found one
    };
    };

    The question really is how many files will be in the
    'log' directory - who is cleaning them up - and the
    actual 'delta' between the $highFileName time value,
    and the actual 'youngest file' in the directory.

    Why not Open the directory with say opendir()
    and get the list of files in it? then use
    the readdir() and 'grep' type tricks:

    opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
    @today_files = grep {$_ =~ /^pollerAudit.$fileDate.*\.log$/ }
    readdir(DIR);
    closedir DIR;

    Then all you need to do is 'sort' that @today_files array...
    You might think of solving that problem with 'map',
    since you could then have a hash of 'existing files'
    and would have made one read of 'the directory block'
    and the rest is in core memory manipulations...


    ciao
    drieux

    ---

    Drieux Guest

  4. #3

    Default Re: counting down in a for

    At 20:45 11/12/2003, you wrote:

    >On Dec 11, 2003, at 11:49 AM, [email]drowl@23.me.uk[/email] wrote:
    >[..]
    >> there is probably a much better way of doing all this i am working on
    >>the checkResults sub.
    >>it should find the pollerAudit log created by the main process
    >>(discover) the file format for the log file is
    >>pollerAudit.M.D.Y.h.m.s.lan (MDYhms being month day year hrs min sec)
    >[..]
    >
    >This Might be a good time to step back and
    >look at this problem in terms of 'algorithm'
    >then worry about the code side. Since, while,
    >yes, I can 'read' your code, it may help to begin
    >with 'what do you really want/need'?
    >
    >You are passing in two time values into a function,
    >so that you can try to find the 'files' that would
    >be made between those times, yes? In particular,
    >you are looking for the most recent one, yes?
    yes this is right i need the most recent one... as after the log is written it takes a few or more seconds for the command to return.
    the "discover" or delta can be from a few seconds to a few minits (depending on the number of elements configured on the device)


    >IF you only need the most recent one, then your basic
    >iteration loop could be simplified with say
    >
    > for ( my ($fTime) = $highFileName; $fTime = $lowFileName; $fTime--)
    > {
    > #my($fullFileName)="pollerAudit.$fileDate.$_.log";
    > # make one substitution
    > my($fullFileName)="/$NH_HOME/log/pollerAudit.$fileDate.$fTime.log";
    > if (-e "$fullFileName") {
    > open UPDATELOG, "$fullFileName";
    > my($reslutsLog) = <UPDATELOG>;
    > close UPDATELOG;
    > last; # break out of the loop we found one
    > };
    > };
    this looks good to me i shall try it out tomorro, but i had a look on CPAN and found a golb module that may help???
    >The question really is how many files will be in the
    >'log' directory - who is cleaning them up - and the
    >actual 'delta' between the $highFileName time value,
    >and the actual 'youngest file' in the directory.
    there are a "lot" of file in the dir, they are cycled / cleaned up, by a scheduled job every week,
    but some will be a month old (ones the result from a monthly scheduled job)
    >Why not Open the directory with say opendir()
    >and get the list of files in it? then use
    >the readdir() and 'grep' type tricks:
    >
    > opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
    > @today_files = grep {$_ =~ /^pollerAudit.$fileDate.*\.log$/ } readdir(DIR);
    > closedir DIR;

    humm if i have this list, can i "grep" through and find the one that contains "key" (ie host name)
    that would do the trick...

    >Then all you need to do is 'sort' that @today_files array...
    >You might think of solving that problem with 'map',
    >since you could then have a hash of 'existing files'
    >and would have made one read of 'the directory block'
    >and the rest is in core memory manipulations...
    >

    this could get messy as i could be running say 20 discovers and only after each one will the log file be ceated.
    +new log could be created by non discover activities.

    thanks for your help

    Ritchie
    >ciao
    >drieux
    >
    >---
    >
    >
    >--
    >To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    >For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    ><http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >
    >
    >
    >
    >---
    >Incoming mail is certified Virus Free.
    >Checked by AVG anti-virus system ([url]http://www.grisoft.com[/url]).
    >Version: 6.0.537 / Virus Database: 332 - Release Date: 06/11/2003

    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system ([url]http://www.grisoft.com[/url]).
    Version: 6.0.537 / Virus Database: 332 - Release Date: 06/11/2003

    DrOwl@23.me.uk Guest

  5. #4

    Default Re: counting down in a for

    <DrOwl@23.me.uk> wrote:
    >
    > At 20:45 11/12/2003, you wrote:
    >
    > >On Dec 11, 2003, at 11:49 AM, [email]drowl@23.me.uk[/email] wrote:
    > >[..]
    > >> there is probably a much better way of doing all this i am working on
    > >>the checkResults sub.
    > >>it should find the pollerAudit log created by the main process
    > >>(discover) the file format for the log file is
    > >>pollerAudit.M.D.Y.h.m.s.lan (MDYhms being month day year hrs min sec)
    > >[..]
    > >
    > >This Might be a good time to step back and
    > >look at this problem in terms of 'algorithm'
    > >then worry about the code side. Since, while,
    > >yes, I can 'read' your code, it may help to begin
    > >with 'what do you really want/need'?
    > >
    > >You are passing in two time values into a function,
    > >so that you can try to find the 'files' that would
    > >be made between those times, yes? In particular,
    > >you are looking for the most recent one, yes?
    >
    > yes this is right i need the most recent one... as after the log is written it takes a few or more seconds for the command to
    return.
    > the "discover" or delta can be from a few seconds to a few minits (depending on the number of elements configured on the device)
    >
    >
    >
    > >IF you only need the most recent one, then your basic
    > >iteration loop could be simplified with say
    > >
    > > for ( my ($fTime) = $highFileName; $fTime = $lowFileName; $fTime--)
    > > {
    > > #my($fullFileName)="pollerAudit.$fileDate.$_.log";
    > > # make one substitution
    > > my($fullFileName)="/$NH_HOME/log/pollerAudit.$fileDate.$fTime.log";
    > > if (-e "$fullFileName") {
    > > open UPDATELOG, "$fullFileName";
    > > my($reslutsLog) = <UPDATELOG>;
    > > close UPDATELOG;
    > > last; # break out of the loop we found one
    > > };
    > > };
    >
    foreach my $fTime ( reverse $lowFileName .. $highFileName ) {
    :
    }

    will implement the loop you wanted. I don't guarantee that this is the
    way to go though!

    HTH,

    Rob


    Rob Dixon Guest

  6. #5

    Default Re: counting down in a for


    On Dec 11, 2003, at 1:20 PM, [email]DrOwl@23.me.uk[/email] wrote:
    [..]
    >> Why not Open the directory with say opendir()
    >> and get the list of files in it? then use
    >> the readdir() and 'grep' type tricks:
    >>
    >> opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
    >> @today_files = grep {$_ =~ /^pollerAudit.$fileDate.*\.log$/ }
    >> readdir(DIR);
    >> closedir DIR;
    >
    > humm if i have this list, can i "grep" through and find the one that
    > contains "key" (ie host name)
    > that would do the trick...
    [..]

    Let's think about the problem here a bit more.

    Step back and ask yourself

    how does the (-e $filename) trick work?

    Well it sort of depends upon the OS, yes, but basically
    it's going to have to do the following basic solution

    a. get the directory portion of $filename
    b. open the directory 'block'
    c. rummage around in the list from the dirblock
    to compare the 'file' portion of filename

    so given our time constraint, a 'few seconds' to a 'few minutes'
    we are talking about something on the order of n waltz's
    through the dirBlock filename list to (n*60) waltz to do a bunch
    of cmp of strings...

    So it would seem a bit more efficient to say,
    hey, why don't we just open the dirBlock once?
    I have perl and a strong RegEx Engine...

    Now we move into the basic clock problem. Clearly
    if we start a poll run at 2003.12.31.2359.59, and it runs
    for more than one second we will be looking for the new
    log file to be in, 2004.01.01.00NN.NN ....

    The gooder news, of course is that we can also tighten
    up our basic 'grep' string some here as well.

    my $delta = ($endtime - $starttime)

    if ( $old_Y_M_D eq $new_YM_D )
    the YMD values are the same
    grow out the filename
    else
    handle different YMD values

    if ($old_hour = $new_hour)
    grow filname to Hour value

    if ( $delta < 60 )
    if ($old_minute = $new_minute )
    grow filename down to minute level

    What you are also beginning to bump your head into is the
    underlying problem of using the file system a bit more
    effectively to help streamline your process. If
    you could fan out "N" discovery processes each writing
    to "N" directories that are Each on "N" different
    disk controllers.... Their writes would be simpler,
    and Your Reads would be simpler too...

    Just a bit of longer term strategic thinking....

    ciao
    drieux

    ---

    Drieux Guest

Posting Permissions

  • You may not post new threads
  • You may 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