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!

script perl 1

Status
Not open for further replies.

biondo68

Technical User
Jul 4, 2003
103
IT
Hi

I am new in perl script . I read tuttorial and other but now I need help for my first script .

I have file with 13000 rows

Where the row have event with "critical"

I need to overwrite "TRAP" variable with "2"


Where the row have event with "warning"

I need to overwrite "TRAP" variable with "1"


Where the row have event with "informational"

I need to overwrite "TRAP" variable with "0"

thanks



 
It's always useful to provide a few lines of the actual data file so any response can be tailored to suit precisely. However, here's something that may get you started:

Code:
use strict;
my $TRAP;
while (<DATA>) {
	chomp;
	if (/warning/) {
		$TRAP='1';
		print "TRAP = $TRAP\n";
	} elsif (/critical/) {
		$TRAP='2';
		print "TRAP = $TRAP\n";
	} else {
		print "event = normal\n";
	}
}

__DATA__
xxxxx event critical  
xxxxx event warning
xxxxx event normal  
xxxxx event warning
xxxxx event normal  
xxxxx event warning
xxxxx event critical  
xxxxx event normal
 
Hi thanks.

tha variable is "Status Events" MINOR to overwrite "TRAP 1" in "TRAP 0" in the second row "Status Events" CRITICAL o overwrite "TRAP 1" in "TRAP 2"

#
#
#
EVENT switchSNTPServiceUnavailable .1.3.6.1.4.1.232.0.161015 "Status Events" MINOR
FORMAT A SNTP Server was configured, but no SNTP servers were found. $*
EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "A SNTP Server was configured, but no SNTP servers were found. $*"
SDESC
A SNTP Server was configured, but no SNTP servers were found.
#
#
#
EVENT cpqCrEMUTemperatureCriticalTrap .1.3.6.1.4.1.232.141.3.7.0.24 "Status Events" CRITICAL
FORMAT Primary enclosure temperature critical!. $*
EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "Primary enclosure temperature critical!. $*"
SDESC
Primary enclosure temperature critical!.

Thanks
 
Some further clarification would be useful. How many lines of data is that? The display can mess up the actual formatting and I think it may have done so here.

Also, can you put some more punctuation in the sentence 'tha variable is "Status Events" MINOR to overwrite "TRAP 1" in "TRAP 0" in the second row "Status Events" CRITICAL o overwrite "TRAP 1" in "TRAP 2"'. It's somewhat ambiguous as it stands.
 

Ok ,

I try to be clearer....

I have file with 13000 rows.

The variable is "Status Events" MINOR or CRITICAL or MAJOR ,
I need overwrite the second variable "TRAP 1"

"Status Events" MINOR "TRAP 1" in "TRAP 0"
"Status Events" CRITICAL "TRAP 1" in "TRAP 2"
"Status Events" WARNING "TRAP 1" in "TRAP 1"

Exaple row in the file

#
#
#
EVENT switchSNTPServiceUnavailable .1.3.6.1.4.1.232.0.161015 "Status Events" MINOR
FORMAT A SNTP Server was configured, but no SNTP servers were found. $*
EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "A SNTP Server was configured, but no SNTP servers were found. $*"
SDESC
A SNTP Server was configured, but no SNTP servers were found.
#
#
#
EVENT cpqCrEMUTemperatureCriticalTrap .1.3.6.1.4.1.232.141.3.7.0.24 "Status Events" CRITICAL
FORMAT Primary enclosure temperature critical!. $*
EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "Primary enclosure temperature critical!. $*"
SDESC

thanks





 
I apologise for being somewhat dense, but you say "Example row in the file" then actually display 15 separate lines.

What is the actual line? For example, is

Code:
EVENT switchSNTPServiceUnavailable .1.3.6.1.4.1.232.0.161015 "Status Events" MINOR
FORMAT A SNTP Server was configured, but no SNTP servers were found. $*
EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "A SNTP Server was configured, but no SNTP servers were found. $*"
SDESC
A SNTP Server was configured, but no SNTP servers were found.

all one line in your file? Or are the 15 lines actually as shown?
 
The row shown are part of file ..

Example

EVENT switchSNTPServiceUnavailable .1.3.6.1.4.1.232.0.161015 Status Events" MINOR
FORMAT A SNTP Server was configured, but no SNTP servers were found. $*
EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "A SNTP Server was configured, but no SNTP servers were found. $*"
SDESC
A SNTP Server was configured, but no SNTP servers were found.
 
I'm not convinced I fully understand what you are after, but using the assumption that the text in between hashes is one line, have a look at this:

Code:
use strict;
while (<DATA>) {
	chomp;
	my $line=$_;
	if ($line=~m/^\#$/) {
		next;
	} elsif ($line=~m/\"Status Events\"\s(\w*)\s/) {
		if ($1 eq 'MINOR') {
			#$line=~s/\$R TRAP 1 \"A SNTP Server was configured, but no SNTP servers were found. \$*\"/\$R TRAP 0 \"Status Events\" MINOR/;
			$line=~s/TRAP 1 \"A SNTP Server was configured, but no SNTP servers were found. \$\*\"/TRAP 0 "Status Events" MINOR/;
			print "$line\n";
		} elsif ($1 eq 'CRITICAL') {
			$line=~s/TRAP 1 \"Primary enclosure temperature critical!. \$\*\"/TRAP 2 "Status Events" CRITICAL/;
			print "$line\n";
		} elsif ($1 eq 'WARNING') {
			$line=~s/TRAP 1 \"Primary enclosure temperature warning. \$\*\"/TRAP 1 "Status Events" WARNING/;
			print "$line\n";
		}
	}
}
#
EVENT switchSNTPServiceUnavailable .1.3.6.1.4.1.232.0.161015 "Status Events" MINOR FORMAT A SNTP Server was configured, but no SNTP servers were found. $*EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "A SNTP Server was configured, but no SNTP servers were found. $*" SDESC A SNTP Server was configured, but no SNTP servers were found.
#
#
#
EVENT cpqCrEMUTemperatureCriticalTrap .1.3.6.1.4.1.232.141.3.7.0.24 "Status Events" CRITICAL FORMAT Primary enclosure temperature critical!. $*EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "Primary enclosure temperature critical!. $*" SDESC Primary enclosure temperature critical!.
#
#
#
EVENT cpqCrEMUTemperatureCriticalTrap .1.3.6.1.4.1.232.141.3.7.0.24 "Status Events" WARNING FORMAT Primary enclosure temperature critical!. $*EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "Primary enclosure temperature warning!. $*" SDESC Primary enclosure temperature critical!.
#
 
I left out __DATA__

Code:
I'm not convinced I fully understand what you are after, but using the assumption that the text in between hashes is one line, have a look at this:

[code]
use strict;
while (<DATA>) {
	chomp;
	my $line=$_;
	if ($line=~m/^\#$/) {
		next;
	} elsif ($line=~m/\"Status Events\"\s(\w*)\s/) {
		if ($1 eq 'MINOR') {
			#$line=~s/\$R TRAP 1 \"A SNTP Server was configured, but no SNTP servers were found. \$*\"/\$R TRAP 0 \"Status Events\" MINOR/;
			$line=~s/TRAP 1 \"A SNTP Server was configured, but no SNTP servers were found. \$\*\"/TRAP 0 "Status Events" MINOR/;
			print "$line\n";
		} elsif ($1 eq 'CRITICAL') {
			$line=~s/TRAP 1 \"Primary enclosure temperature critical!. \$\*\"/TRAP 2 "Status Events" CRITICAL/;
			print "$line\n";
		} elsif ($1 eq 'WARNING') {
			$line=~s/TRAP 1 \"Primary enclosure temperature warning. \$\*\"/TRAP 1 "Status Events" WARNING/;
			print "$line\n";
		}
	}
}
__DATA__
#
EVENT switchSNTPServiceUnavailable .1.3.6.1.4.1.232.0.161015 "Status Events" MINOR FORMAT A SNTP Server was configured, but no SNTP servers were found. $*EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "A SNTP Server was configured, but no SNTP servers were found. $*" SDESC A SNTP Server was configured, but no SNTP servers were found.
#
#
#
EVENT cpqCrEMUTemperatureCriticalTrap .1.3.6.1.4.1.232.141.3.7.0.24 "Status Events" CRITICAL FORMAT Primary enclosure temperature critical!. $*EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "Primary enclosure temperature critical!. $*" SDESC Primary enclosure temperature critical!.
#
#
#
EVENT cpqCrEMUTemperatureCriticalTrap .1.3.6.1.4.1.232.141.3.7.0.24 "Status Events" WARNING FORMAT Primary enclosure temperature critical!. $*EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "Primary enclosure temperature warning!. $*" SDESC Primary enclosure temperature critical!.
#
 
I think you're barking up the wrong tree there Tony and solving a different problem.

By "row", in some instances I think the OP means "paragraph", so they are multiple lines.

When the preceding line contains "MINOR" he wants to change "TRAP 1" to "TRAP 0", when it contains "WARNING" I presume it stays unchanged, and when it contains "CRITICAL" he wants to change "TRAP 1" to "TRAP 2".

That's my understanding of the problem, correct me if I'm wrong biondo?

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 

Is correct what he says Annihilannic .

Excuse me If I didn't understand ..

Thanks
 
I am often said to be barking.

How about this then:

Code:
use strict;
my ($x,@lines);
while (<DATA>) {
	chomp;
	push(@lines,$_)
}

for ($x=0;$x<$#lines;$x++) {
	if ($lines[$x]=~m/MINOR/) {
		$lines[$x+2]=~s/TRAP 1/TRAP 0/;
	} elsif ($lines[$x]=~m/CRITICAL/) {
		$lines[$x+2]=~s/TRAP 1/TRAP 2/;
	}
	print "$lines[$x]\n";
}


__DATA__
#
#
#
EVENT switchSNTPServiceUnavailable .1.3.6.1.4.1.232.0.161015 "Status Events" MINOR
FORMAT A SNTP Server was configured, but no SNTP servers were found. $*
EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "A SNTP Server was configured, but no SNTP servers were found. $*"
SDESC
A SNTP Server was configured, but no SNTP servers were found.
#
#
#
EVENT cpqCrEMUTemperatureCriticalTrap .1.3.6.1.4.1.232.141.3.7.0.24 "Status Events" CRITICAL
FORMAT Primary enclosure temperature critical!. $*
EXEC /usr/libexec/submit_check_result_via_nsca $R TRAP 1 "Primary enclosure temperature critical!. $*"
SDESC
#
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top