Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Improve my program:(working with files, email contents) 2

Status
Not open for further replies.

Ru55ell

IS-IT--Management
Mar 13, 2003
137
US
Hi, I made a program that works with my HP Openview to send a list of events for the last 48 hours. My program runs a system command to list the events in a file. Then my program runs a system command (findstr) and writes to another file containing the lines I want to send. It then reads the results and emails the file contents. This works, however I got the bug and want to improve the results. I included below a sample of the file contents and my program.
I’d like to improve it by removing the extra information and deliver only parts of each line.
For example the first two lines,

1152897223 4 Fri 07 14 13:13:43 2006 Node1 M NW IC-3 Node Up Node1 Se0;1 17.1.0.58916864 4026
1152928755 4 Fri 07 14 21:59:15 2006 SomeBank_Fidelity M NW IC-3 Node Down SomeBank_Fidelity Se1/0.2;3 17.1.0.58916865 77020


I really only want to see

Fri 07 14 13:13:43 2006 Node1 NW IC-3 Node Up
Fri 07 14 21:59:15 2006 SomeBank_Fidelity NW IC-3 Node Down

The words on the lines are separated by spaces and one by a tab. How can I achieve this? I’m a relative novice. My programs for the most part are a patchwork other people’s programs.

Below I included my program and a sample of the results the find string windows command.


The file:

1152897223 4 Fri 07 14 13:13:43 2006 Node1 M NW IC-3 Node Up Node1 Se0;1 17.1.0.58916864 4026
1152928755 4 Fri 07 14 21:59:15 2006 SomeBank_Fidelity M NW IC-3 Node Down SomeBank_Fidelity Se1/0.2;3 17.1.0.58916865 77020
1152929469 4 Fri 07 14 22:11:09 2006 Another_NodeM NW IC-3 Node Up Another_Node Se0/0;1 17.1.0.58916864 34518
1152930212 4 Fri 07 14 22:23:32 2006 DMZ_1_ROUTER M NW IC-1 Node Up DMZ_1_ROUTER Se5/0.21;1 17.1.0.58916864 2019
1152930349 4 Fri 07 14 22:25:49 2006 SomeBank_Fidelity M NW IC-3 Node Up SomeBank_Fidelity Se1/0.2;1 17.1.0.58916864 77020
1152930350 4 Fri 07 14 22:25:50 2006 DMZ_2_ROUTER M NW IC-1 Node Up DMZ_2_ROUTER Se0/0.9;1 17.1.0.58916864 2542


My program/script

#this worked using command C:\>c:\\ovscripts\\perl "C:\\ovscripts\\scripts\\NWALARMem.pl"

BEGIN {
if ($^O eq "MSWin32") {
@INC = (
# Set the path to your Perl Scripting directory if different
'c:\ovscripts\perl5\site_perl\5.005',
'c:\ovscripts\perl5\site_perl\5.005\MSWin32-x86');
}
}


system ("ovdumpevents -l 2880 > RecentAlarms.txt");
system ('findstr /c:"NW IC" RecentAlarms.txt >fndnw.txt');


open (ALARMS, 'fndnw.txt');

my @data = ("\n", <ALARMS>);


use Net::SMTP;
use strict;

my $MAILHOST = 'mail.smtpgate.soandso.net';
my $smtp = Net::SMTP->new($MAILHOST, Timeout => 10) or die "Unable to connect to mail host : $MAILHOST - $!\n";


my @receipiants = ('Ru55ell@WhereIwork.com','someone.else@WhereIwork');

my $FROM = 'HP.Openview@WhereIwork ';

foreach my $receipiants (@receipiants)

{

$smtp->mail("$receipiants");
$smtp->to("$receipiants");
$smtp->data();
$smtp->datasend("From: $FROM\n");
$smtp->datasend("To: $receipiants\n");
$smtp->datasend("Subject: NW Activity for last 48 hrs");
$smtp->datasend("\n@data");
$smtp->datasend();
$smtp->dataend();

}
$smtp->quit;
 
This seems to work for the most part:

Code:
while(<DATA>){
   /^\d+\s+\d+\s+(\w+ \d+ \d+ \d\d:\d\d:\d\d \d\d\d\d) (\w+)\s+\w\s+(.+)\2/;
   print "$1 $2 $3\n";
}

__DATA__
1152897223 4 Fri 07 14 13:13:43 2006 Node1    M NW IC-3 Node Up  Node1 Se0;1 17.1.0.58916864 4026
1152928755 4 Fri 07 14 21:59:15 2006 SomeBank_Fidelity    M NW IC-3 Node Down  SomeBank_Fidelity Se1/0.2;3 17.1.0.58916865 77020
1152929469 4 Fri 07 14 22:11:09 2006 Another_NodeM NW IC-3 Node Up  Another_Node Se0/0;1 17.1.0.58916864 34518
1152930212 4 Fri 07 14 22:23:32 2006 DMZ_1_ROUTER    M NW IC-1 Node Up  DMZ_1_ROUTER Se5/0.21;1 17.1.0.58916864 2019
1152930349 4 Fri 07 14 22:25:49 2006 SomeBank_Fidelity    M NW IC-3 Node Up  SomeBank_Fidelity Se1/0.2;1 17.1.0.58916864 77020
1152930350 4 Fri 07 14 22:25:50 2006 DMZ_2_ROUTER    M NW IC-1 Node Up  DMZ_2_ROUTER Se0/0.9;1 17.1.0.58916864 2542

it fails on this line:

1152929469 4 Fri 07 14 22:11:09 2006 Another_NodeM NW IC-3 Node Up Another_Node Se0/0;1 17.1.0.58916864 34518

because there is no space after 'Another_Node' and 'M'

if theletter 'M' is consistent throughout the file that can be fixed.
 
Thanks KevinADC,
I edited the data before posting and caused the inconsistency. It should work. I really appreciate your help and will start working on it right away. I’ll post the results.
 
I did it! Thanks KevinADC
I changed one charater range from KevinADC's suggestion and it works like a charm. I used the "/S+" instead of the "/w+" in the field where my device names are. Some of my device names contain special charaters and the "/S" returns any non-white space. Thanks a ton! I'll post the finished perl program onece I get back to work and edit my script.

new line
/^\d+\s+\d+\s+(\w+ \d+ \d+ \d\d:\d\d:\d\d \d\d\d\d) (\S+)\s+\w\s+(.+)\2/;

Kevins's great work!
/^\d+\s+\d+\s+(\w+ \d+ \d+ \d\d:\d\d:\d\d \d\d\d\d) (\w+)\s+\w\s+(.+)\2/;
 
Another way to do it would be to split the string along spaces and use an array slice to capture the values:

Code:
while(<DATA>){
   my @vals = (split/\s+/)[2,3,4,5,6,7,9,10,11,12];
   print join(' ', @vals) . "\n";
}

__DATA__
1152897223 4 Fri 07 14 13:13:43 2006 Node1    M NW IC-3 Node Up  Node1 Se0;1 17.1.0.58916864 4026
1152928755 4 Fri 07 14 21:59:15 2006 SomeBank_Fidelity    M NW IC-3 Node Down  SomeBank_Fidelity Se1/0.2;3 17.1.0.58916865 77020
1152929469 4 Fri 07 14 22:11:09 2006 Another_Node M NW IC-3 Node Up  Another_Node Se0/0;1 17.1.0.58916864 34518
1152930212 4 Fri 07 14 22:23:32 2006 DMZ_1_ROUTER    M NW IC-1 Node Up  DMZ_1_ROUTER Se5/0.21;1 17.1.0.58916864 2019
1152930349 4 Fri 07 14 22:25:49 2006 SomeBank_Fidelity    M NW IC-3 Node Up  SomeBank_Fidelity Se1/0.2;1 17.1.0.58916864 77020
1152930350 4 Fri 07 14 22:25:50 2006 DMZ_2_ROUTER    M NW IC-1 Node Up  DMZ_2_ROUTER Se0/0.9;1 17.1.0.58916864 2542
 
Thanks Raklet,
I like it! Decisions, decisions..
 
as long as the data in the lines is consistent using split would probably be best, but if you need to do any checking or validating of the lines to make sure they have the correct type of data then you should stick with a regexp, but you can wrap it in a conditional to skip unwanted lines

Code:
while(<DATA>) {
   if(/^\d+\s+\d+\s+(\w+ \d+ \d+ \d\d:\d\d:\d\d \d\d\d\d) (\S+)\s+\w\s+(.+)\2/) {
      print "$1 $2 $3\n";
   }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top