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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl file parsing and extracting certain info part II

Status
Not open for further replies.

dietmarp

Programmer
Dec 27, 2001
53
US
Hi list, need to ask another question - thought I could manage it already myself.
I need to grep the number following the VALUE(=, of a line.

This is how the line looks like.

7: ATTR(PREFIX, "cpqCmcStatusTemp1"), VALUE(=,2);

Trojan helped me very much so far (see thread "Perl file parsing and extracting certain info")
I tried to use his example but didn't get far.
push @strings, $1 if(/^\s+\d+:\s+\$SPECIFIC\s+=\s+(\d+);/);
Any help appreiated
Dietmar
 
What do you want to do with that value?
Just output it in sequence?

This might do what you want:
Code:
#!/usr/bin/perl -w
use strict;
my %hash = ();
open FH, "trap.oid" or die "Failed to open trap.oid";
while(<FH>) {
  /"([^"]+)"\s+"([^"]+)"/
    and $hash{$1} = $2;
}
close FH;
open FH, "trap.cds" or die "Failed to open trap.cds";
my @strings;
while(<FH>) {
  chomp;
  @strings = (), print "$1\n" if(/^CLASS (.*)$/);
  if(/^\s+(\d+):[^"]+"([^"]+)"/) {
    if(exists $hash{$2}) {
      push @strings, $hash{$2};
    } else {
      push @strings, $2;
    }
  }
  push @strings, $1 if(/^\s+\d+:\s+\$SPECIFIC\s+=\s+(\d+);/);
  push @strings, $1 if(/VALUE\(=,(\d+)\);/);
  print join(" ", @strings), "\n" if(/^END/);
}
close FH;


Trojan.
 
Hi Trojan,
I found out,that I need to to a bit more formating to be able to send the traps.

I did a couple changes (print) to the code

#!/usr/bin/perl -w
use strict;
my $tohost = "myserver";
my $fromhost = "hisserver";
my %hash = ();
open FH, "trap.oid" or die "Failed to open trap.oid";
while(<FH>) {
/"([^"]+)"\s+"([^"]+)"/
and $hash{$1} = $2;
}
close FH;
open FH, "trap.cds" or die "Failed to open trap.cds";
my @strings;
while(<FH>) {
chomp;
@strings = (), print "echo \"Sending Trap $1\"\n" if(/^CLASS (.*)$/);
if(/^\s+(\d+):[^"]+"([^"]+)"/) {
if(exists $hash{$2}) {
push @strings, $hash{$2};
push @strings, "Integer 0";
} else {
push @strings, "wsnmptrap -h $tohost $2 6";
}
}
push @strings, "$1 OctetString \"$fromhost\"" if(/^\s+\d+:\s+\$SPECIFIC\s+=\s+(\d+);/);
print join(" ", @strings), "\n" if(/^END/);
}
close FH;


Using this trap.cds input
#########################################################
##### Compaq ConsoleManagementController- (CMC) MIB
#########################################################
CLASS IM_153001_2
SELECT
1: ATTR(=,$ENTERPRISE) , VALUE(PREFIX, "1.3.6.1.4.1.232.153" ) ;
2: $SPECIFIC = 153001;
3: ATTR(=, "sysDescr") ;
4: ATTR(=, "sysContact")");
5: ATTR(=, "sysName");
6: ATTR(=, "sysLocation");
7: ATTR(PREFIX, "cpqCmcStatusTemp1"), VALUE(=,2);
MAP
sub_source = HP_Insight_Manager;
specificTrap = $V2;
hpq_eventID = 153001_2;
severity = HARMLESS;
hpq_contact = $V4;
hostname=$V5;
hpq_location = $V6;
hpq_CmcStatusTemp1 = $V7;
sub_origin = $K7;
hpq_val = $V7;
msg = "The status of Compaq ConsoleManagementController temperature 1: Normal";
hpq_info = PRINTF ( "Location: %s, Message: %s from: %s, contact %s", $V6, $V7, $V5, $V4);
END

and following trap.oid

"sysDescr" "1.3.6.1.2.1.1.1"
"sysContact" "1.3.6.1.2.1.1.4"
"sysName" "1.3.6.1.2.1.1.5"
"ssSubsysName" "1.3.6.1.4.1.36.2.15.21.1.3.2.1.2"
"sysLocation" "1.3.6.1.2.1.1.6"
"cpqCmcStatusTemp1" "1.3.6.1.4.1.232.153.2.2.4.1"

the result now looks like this:
echo "Sending Trap IM_153001_2"
wsnmptrap -h myserver 1.3.6.1.4.1.232.153 6 153001 OctetString "hisserver" 1.3.6.1.2.1.1.1 Integer 0 1.3.6.1.2.1.1.4 Integer 0 1.3.6.1.2.1.1.5 Integer 0 1.3.6.1.2.1.1.6 Integer 0 1.3.6.1.4.1.232.153.2.2.4.1 Integer 0

But this result is not correct. If you look at the example above you'll see that line
7: ATTR(PREFIX, "cpqCmcStatusTemp1"), VALUE(=,2);
contains a VALUE of 2.

So I'd like to check, if the matched line contains a Value ( if line doesnt contain a value I have to set it to 0)

So final output of this example should look like this
wsnmptrap -h myserver 1.3.6.1.4.1.232.153 6 153001 OctetString "hisserver" 1.3.6.1.2.1.1.1 Integer 0 1.3.6.1.2.1.1.4 Integer 0 1.3.6.1.2.1.1.5 Integer 0 1.3.6.1.2.1.1.6 Integer 0 1.3.6.1.4.1.232.153.2.2.4.1 Integer 7

Cheers, Dietmar


 
ups - solved it myself - thanks to trojan

if(/^\s+(\d+):[^"]+"([^"]+)"/) {
if(exists $hash{$2}) {
push @strings, $hash{$2};
if (/VALUE\(=,(\d+)\);/) {
push @strings, "Integer $1";
}
else {
push @strings, "Integer 0";
}
} else {
push @strings, "wsnmptrap -h $tohost $2 6";
}
}

merci bien dietmar
 
Sorry,
Didn't have any time to spare today, World Superbikes (Brands Hatch)!
Gald to see you solved it yourself, it's always better that way, you tend to learn more.


Trojan.
 
Trojan,

Do you follow the bikes? BSB? Did you travel to Mondello this year?

--Paul

cigless ...
 
Sorry mate,
I'm skint. I've not been working for 2 years now.
Nobody want's old timers like me any more! :-(
Did Brands a few years ago when I was working (it's not far from me) but I haven't been since.
Would have been nice.
Just about to catch race 2 from Brands on the TV now though.
What a race we had this morning! Wow! :)

Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top