About the resource load in having the program check once per second, I
Actually though this would be a problem.  I only set it that low to 'test'
how bad of a hit I would take. I was really stunned to see just how
efficient perl is.  When I checked the memory usage, and CPU time that this
process uses when running it is surprisingly tiny.  I mean VERY VERY small.
Less than 1% CPU time, and about 1% of available memory usage.  The server
on which the mail database physically exists is also minimally impacted.
It already handles dozens of such transactions per second, and reports very
low overall usage under these loads. The nice thing about it is when it's
working I can send a command from my backberry, and get a response back in
about 3-5 seconds on average.  This is not at all required, but it is nice
to get that kind of response time.  I also tested it with 10, and 30 second
wait timers and the resource usage numbers came out almost identical. I ran
another test with no wait timer to see just how fast perl is. Giving perl
the ability to use 100% of system resources it did something like 8,000,000
look-ups in about 30 seconds.  This did peg both the server the script runs
on, as well as the mail database server.

The only reason I used the GOTO's at all was because I didn't think of the
infinite while loop trick.  Thanks I'll make that change right away.

As for the 1-3 delete loop it really only needs to be 1-2.  1 to delete the
original inbound email, and 2 to delete send reply mail.  Since I'm using
AllDocuments look-up, notes puts all documents in that database there.
It's a nice easy way to access the whole database from one place.  I did it
to 3 just in case something else came in while it was processing it gives
it the ability to 'clean' out the mailbox.

Chris Benco
[EMAIL PROTECTED]




                                                                                       
                                          
                      James Edward Gray                                                
                                          
                      II                       To:       "Chris Benco" 
<[EMAIL PROTECTED]>                            
                      <james@grayproduc        cc:       [EMAIL PROTECTED]            
                                          
                      tions.net>               Subject:  Re: Program Stability Issue   
                                          
                                                                                       
                                          
                      10/11/2002 11:40                                                 
                                          
                      AM                                                               
                                          
                                                                                       
                                          
                                                                                       
                                          




I'm not familiar with "notes email" or the Win32::OLE module you're
using, so it's unlikely that I can solve your problem.  I do have a few
general observations to offer though.

First, checking something like e-mail every one second, may border on
obsessive compulsive behavior!  <laughs>  Seriously, I do think this
time loop is a bad idea.  This could potentially be a pretty
significant resource drain on the computer doing the checking not to
mention the checked server.  My server would ban me for "flooding"
shortly after I started something like this.  If you need e-mail
service with less than a five minute delay, I think it's time to
consider a different form of communication that would meet your needs
better.

Second, goto programming is BAD, BAD, BAD!  It creates what is often
affectionately called "spaghetti code", where following the individual
strands to their natural end can become maddening.  Your code is short
and simple, and it's still hell to follow because of those gotos.  That
makes it hard to get help, when you send your code to anyone.  A goto
call is VERY rarely ever needed in modern programming languages, so
unlearn it as soon as possible.

All the gotos in your script can be replaced with loops and
subroutines.  You used a foreach loop at one point, so I assume you're
at least passingly familiar with the idea.  Your main WAIT label could
easily be replaced with a while loop, given a condition of 1, which
will always evaluate to true and thus be infinite.  Then, when you need
to get back to the top of the loop, as all those gotos function, just
use a call to next.  For example:

while (1) {                    # replaces WAIT:
             # do some processing
             next if CONDITION;                        # this sends us back
to the while
             # more processing, if next wasn't called
}            # end of loop, repeats back to the while

The other construct you can use to replace goto calls is a subroutine
(or sub) to create a reusable chunk of code that you can call anywhere.
  This is how I would replace your BADPASS label, for example:

sub badpass {                        # doesn't execute at this point, just
declares
             # processing...
             return if CONDITION;          # in a sub, return sends us back
to the calling
code
             # more processing, if we didn't return
}            # end of sub, return to calling code

# we can now call this code later with...

badpass();         # will execute the whole badpass sub, right here

You get the idea.  These are basic Perl concepts that would be in every
book out there, but especially focused on in learning to program Perl
type books.  Learning Perl from O'Reilly is very popular, if you would
like to check one out.

Two other minor observations:  I question the accuracy of your 1 to 3
foreach loop.  It seems like magic numbers chosen instead of based on
criteria like the documents available.  I could be way off here though,
since I don't really know what you're doing.  And two, use diagnostics
enables warnings, so having both is redundant.

Good luck with your problem.

James

On Friday, October 11, 2002, at 09:31  AM, Chris Benco wrote:

> Below is a perl script I've been working on that logs into and checks a
> notes email account for new messages once every second.  When it finds
> a
> new message it tests for a password, and if it success it process's the
> email.  The script will based on what is in the email either run
> another
> script, or do anything that can be done form a command line on the
> system
> it is running on.  It then clears the mailbox, then goes back into the
> loop
> where it checks for any mail.  The original propose for this program
> was to
> allow me to do some diagnostics remotely via email (usually my
> blackberry
> handheld), but I've already found dozens of other uses for it.
>
> The problem is that the script is supposed to loop basically forever
> until
> it finds something.  It does this for awhile.  The program works for
> about
> 24 hours at which point it just stops.  I've tried changing to loop
> time
> from 1 second to 10, and even 30 seconds, but it still crashes after
> about
> 24 hours.  I've never managed to be around when this happens to try to
> figure out what is going on.  Looking for improving programs
> stability, or
> even ideas on how to get it to log what is going wrong.  Only thing I
> can
> think of is having it open a text file, and have every line in the
> program
> log it's execution in some way.   I've also already added lines that
> force
> it back into the waiting loop at almost every stage I can think of it
> possibly halting, but that has had little of any effect it seems.
>
> If anyone has any ideas on how to improve this programs stability, how
> to
> make perl programs more stably in general, or even a possible reason
> why
> this, or any general purpose program might crash for unknown reasons
> let me
> know.  Looking for input of any kind on this issue.
>
> Have been thinking of writing some simple but very system specific
> network
> monitoring apps using perl and I really need to prove to myself that I
> can
> keep a perl script running for weeks at a time before I even start.
>
> #######################################################################
> #############################
>
> use strict;
> use warnings;
> use diagnostics;
> use Win32::OLE;
>
> #Notes database Info
> my $userid = "xxxxxx";
> my $server = "xxxxx/xxx/xxx";
>
> #Log into Notes database
> my $Notes = Win32::OLE->new('Notes.NotesSession');
> my $Database = $Notes->GetDatabase("$server", "mail\\$userid.nsf");
>
> #Loop a check for mail once every second, send a '.' to console for
> monitoring
> WAIT:
>     sleep 1;
>     print ".";
>     my $AllDocuments = $Database->AllDocuments;
>     my $Document = $AllDocuments->GetFirstDocument or goto WAIT;
>
> #Process mail
> my $From = $Document->GetFirstItem('From')->{Text} or goto WAIT;
> my $Password = $Document->GetFirstItem('Subject')->{Text};
> my $Command = $Document->GetFirstItem('Body')->{Text};
> my $Subject = "CMD: $Command";
>
> #Password Check - Password must be in subject line of email
> unless ($Password eq "xyz") {goto BADPASS}
>
> #execute command
> my $Report = `$Command`;
> print "!";
>
> #Send reply mail
> SENDMAIL:
>     my $ReportMail = $Database->CreateDocument('NEW') or goto WAIT;
>     $ReportMail->{Form} = 'Memo';
>     $ReportMail->{Body} = "$Report";
>     $ReportMail->{SendTo} = "$From";
>     $ReportMail->{Subject} = "$Subject";
>     $ReportMail->Save(1, 1);
>     $ReportMail->Send(0);
>
> #Clear the mailbox
> foreach (1..3) {
>     my $AllDoc = $Database->AllDocuments or goto WAIT;
>     my $delete  = $AllDoc->GetFirstDocument or goto WAIT;
>     $delete->Remove(1) or goto WAIT;
> }
>     goto WAIT;
>
> #Password Fails - Reformat reply mail and log the attack
> BADPASS:
>     $Report ="You do not have access to this system.  This attack has
> been
> logged";
>     $Subject = "ACCESS DENIED";
>     open(BAD, ">>BADPASS.TXT") or goto WAIT;
>     print BAD "\n\nACCESS VIOLATION: \nFrom: $From\nSubject:
> $Password\nBody:\n\n$Command \n\n " or goto WAIT;
>     close(BAD); print "#" or goto WAIT;
>     goto SENDMAIL;
>
>
> goto WAIT;
>
> #######################################################################
> ##################
>
> Chris Benco
> Network Administrator
> Austin Powder Company
> 216-464-2400 x277
> [EMAIL PROTECTED]
>
>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to