I am using perl and want to read an external file with the following structure (full code at the end of the post):
servicestatus {
parameter1 = abc
parameter2 = abc
parameter3 = abc
}
servicecomment {
parameter1 = def
parameter2 = abc
parameter3 = ghi
}
those blocks are located in different parts of the file, the status block is in the upper while the comment-block is in the lower part. (each commentblock is related to a certain statusblock)
now i’m trying to scan the file line-by-line, so i’m encountering a statusblock at first, read a certain parameter (i.e. parameter2) and finally search the rest of the file for a commentblock that has an equal parameter.
the script i have written to solve this problem is extremely nested, in addition to that it only finds the first statusblock, its corresponding commentblock and then leaves the loop, although there is much more to find. any help considering this problem is welcomed!
Here is the full code
while ($line = <SF>) {
if ($line =~ /servicestatus/) {
while ($line = <SF>) {
if ($line =~ /service_description/) {
$service_desc = $line;
while ($line = <SF>) {
if ($line =~ /servicecomment/) {
while ($line = <SF>) {
if ($line =~ /service_description/) {
$service_desc2 = $line;
if ($service_desc eq $service_desc2) {
while ($line = <SF>) {
if ($line =~ /comment_id/) {
if ($line =~ m/(\d+)/) {
$comment_id = $1;
while ($line = <SF>) {
if ($line =~ /entry_time/) {
if ($line =~ m/(\d+)/) {
$entry_time = $1;
if ($entry_time < $time_oldest) {
# Kommando wird in die Commandpipe geladen
open(CP, ">>$o_commandpipe") or die("nagios.cmd not found");
# Befehl wird ausgegeben, External Command DEL_SVC_COMMENT wird ausgeführt
print {CP} "[$time_now] DEL_SVC_COMMENT;$comment_id;$time_now\n";
close(CP);
print("Comment $comment_id deleted.\n");
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
close SF;
for a better understanding: this script is for nagios (server monitoring tool) and should delete user-defined comments for certain services after a particular time.
thanks in advance!
A couple of suggestions:
$/ = "\n\n";. Then you could read the file block by block instead of line by line.Here is an example of how you could do this, using a hash to keep track of the ones you want to delete. I use the assumption that all blocks are separated by blank lines, but if that is not true, you could easily adapt this to ikegami’s method of going through the file.