用perl找出文件中的某些字段
时间:2010-04-23 来源:auqfpe
原文件是内容是:
define service{
hostgroup_name linux-server
service_description PORT_80
check_period 24x7
max_check_attempts 3
normal_check_interval 5
retry_check_interval 1
contact_groups admin
notification_period 24x7
notification_options w,c
notification_interval 60
check_command check_port!80
}
define service{
hostgroup_name windows-server
service_description PORT_7001
check_period 24x7
max_check_attempts 3
normal_check_interval 5
retry_check_interval 1
contact_groups admin
notification_period 24x7
notification_options w,c
notification_interval 60
check_command check_port!7001
}
以上是nagios配置文件内容,我现在的目的是要找出所有的service.cfg文件,再将每个文件里的hostgroup_name、service_description、contact_groups、check_command这四项打印出来,做成列表的方式,因为我首先是将各文件的路径用shell中的find . -name services*.cfg找出来放到file.txt文件中,后面的工作就用perl来完成了。
vim find.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use vars qw($hostgroup_name $service_description $contact_groups $check_command);
5
6 open(FILE,"/nagioswork/file.txt")||die "Cannot Open:$!";;
7 my @file = <FILE>;
8 close(FILE);
9 foreach my $i (@file)
10 {
11 chomp($i);
12 open(FIL,"$i")|| die "Cannot Open:$!";
13 my @f = <FIL>;
14 close(FIL);
15 foreach my $line (@f)
16 {
17
18 chomp($line);
19 if ($line =~ /}/)
20 {
21 print "$hostgroup_name\t$service_description\t$contact_groups\t$check_command\n";
22 }
23 my ($j,$name,$instan)=split(/\s+/,$line);
24 next if (!defined($name) || ! defined($instan));
25 if($name =~ "hostgroup_name" )
26 {
27 $hostgroup_name = $instan;
28 }elsif($name =~ "service_description" )
29 {
30 $service_description = $instan;
31 }elsif($name =~ "contact_groups" )
32 {
33 $contact_groups = $instan;
34 }elsif($name =~ "check_command" )
35 {
36 $check_command = $instan;
37 }else
38 {
39 # print "cannot find";
40 }
41 }
42
43 }
这样执行的效果如下:
linux-server PORT_80 sc_admin check_port!80
windows-server PORT_7001 sc_admin check_port!7001
这样就达到目的了,如果哪位高手有捷径,请指教,本人是perl初学者。写在这里,只是备忘记了,呵呵!
define service{
hostgroup_name linux-server
service_description PORT_80
check_period 24x7
max_check_attempts 3
normal_check_interval 5
retry_check_interval 1
contact_groups admin
notification_period 24x7
notification_options w,c
notification_interval 60
check_command check_port!80
}
define service{
hostgroup_name windows-server
service_description PORT_7001
check_period 24x7
max_check_attempts 3
normal_check_interval 5
retry_check_interval 1
contact_groups admin
notification_period 24x7
notification_options w,c
notification_interval 60
check_command check_port!7001
}
以上是nagios配置文件内容,我现在的目的是要找出所有的service.cfg文件,再将每个文件里的hostgroup_name、service_description、contact_groups、check_command这四项打印出来,做成列表的方式,因为我首先是将各文件的路径用shell中的find . -name services*.cfg找出来放到file.txt文件中,后面的工作就用perl来完成了。
vim find.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use vars qw($hostgroup_name $service_description $contact_groups $check_command);
5
6 open(FILE,"/nagioswork/file.txt")||die "Cannot Open:$!";;
7 my @file = <FILE>;
8 close(FILE);
9 foreach my $i (@file)
10 {
11 chomp($i);
12 open(FIL,"$i")|| die "Cannot Open:$!";
13 my @f = <FIL>;
14 close(FIL);
15 foreach my $line (@f)
16 {
17
18 chomp($line);
19 if ($line =~ /}/)
20 {
21 print "$hostgroup_name\t$service_description\t$contact_groups\t$check_command\n";
22 }
23 my ($j,$name,$instan)=split(/\s+/,$line);
24 next if (!defined($name) || ! defined($instan));
25 if($name =~ "hostgroup_name" )
26 {
27 $hostgroup_name = $instan;
28 }elsif($name =~ "service_description" )
29 {
30 $service_description = $instan;
31 }elsif($name =~ "contact_groups" )
32 {
33 $contact_groups = $instan;
34 }elsif($name =~ "check_command" )
35 {
36 $check_command = $instan;
37 }else
38 {
39 # print "cannot find";
40 }
41 }
42
43 }
这样执行的效果如下:
linux-server PORT_80 sc_admin check_port!80
windows-server PORT_7001 sc_admin check_port!7001
这样就达到目的了,如果哪位高手有捷径,请指教,本人是perl初学者。写在这里,只是备忘记了,呵呵!
相关阅读 更多 +
排行榜 更多 +