我正在使用 perl 并想读取具有以下结构的外部文件(完整代码在帖子末尾):
servicestatus {
parameter1 = abc
parameter2 = abc
parameter3 = abc
}
servicecomment {
parameter1 = def
parameter2 = abc
parameter3 = ghi
}
这些块位于文件的不同部分,状态块位于上部,而注释块位于下部。(每个评论块都与某个状态块相关)
现在我试图逐行扫描文件,所以我首先遇到了一个状态块,读取某个参数(即参数2),最后在文件的其余部分搜索具有相同参数的注释块。
我为解决这个问题而编写的脚本是非常嵌套的,除了它只找到第一个状态块,它对应的注释块然后离开循环,虽然还有很多东西要找。欢迎考虑这个问题的任何帮助!
这是完整的代码
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;
为了更好地理解:此脚本适用于 nagios(服务器监控工具),应在特定时间后删除某些服务的用户定义评论。
提前致谢!