root/lib/Resmon/Config.pm

Revision d11ab612546f405c2b3593affc5f1a324cf4d7c9, 2.3 kB (checked in by Mark Harrison <mark@omniti.com>, 5 years ago)

Another config parsing fix

git-svn-id: https://labs.omniti.com/resmon/trunk@170 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • Property mode set to 100755
Line 
1 package Resmon::Config;
2
3 use strict;
4
5 sub new {
6   my $class = shift;
7   my $filename = shift;
8   my $self = bless {
9     configfile => $filename,
10     modstatus => '',
11     # Defaults
12     timeout => 10
13   }, $class;
14   open(CONF, "<$filename") || return undef;
15
16   my $current;
17   my $line = 0;
18   while(<CONF>) {
19     $line++;
20     next if /^\s*#/;
21     next if /^\s*$/;
22     if($current) {
23       if(/^\s*([^:\s](?:[^:]*[^:\s])?)\s*:\s*(.+)\s*$/) {
24         my %kvs;
25         $kvs{'type'} = $current;
26         $kvs{'object'} = $1;
27         my @params = split(/,/, $2);
28         grep { $kvs{$1} = $2 if /^\s*(\S+)\s*=>\s*(\S(?:.*\S)?)\s*$/ } @params;
29         my $object = bless \%kvs, "Resmon::Module::$current";
30         push(@{$self->{Module}->{$current}}, $object);
31
32         # Test to make sure the module actually works
33         my $coderef;
34         eval { $coderef = Resmon::Module::fetch_monitor($current); };
35         if (!$coderef) {
36             # Try to execute the config_as_hash method. If it fails, then
37             # the module didn't load properly (e.g. syntax error).
38             eval { $object->config_as_hash; };
39             if ($@) {
40                 # Module failed to load, print error and add to failed
41                 # modules list.
42                 print STDERR "Problem loading module $current\n";
43                 print STDERR "This module will not be available\n";
44                 $self->{'modstatus'} .= "$current ";
45             }
46         }
47
48       } elsif (/^\s*\}\s*$/) {
49         $current = undef;
50       } else {
51         die "Syntax Error on line $line\n";
52       }
53     } else {
54       if(/\s*(\S+)\s*\{/) {
55         $current = $1;
56         $self->{Module}->{$current} = [];
57         next;
58       }
59       elsif(/\S*LIB\s+(\S+)\s*;\s*/) {
60         eval "use lib '$1';";
61         next;
62       }
63       elsif(/\S*PORT\s+(\d+)\s*;\s*/) {
64         $self->{port} = $1;
65         next;
66       }
67       elsif(/\S*INTERFACE\s+(\S+)\s*;\s*/) {
68         $self->{interface} = $1;
69         next;
70       }
71       elsif(/\s*INTERVAL\s+(\d+)\s*;\s*/) {
72         $self->{interval} = $1;
73         next;
74       }
75       elsif(/\s*STATUSFILE\s+(\S+)\s*;\s*/) {
76         $self->{statusfile} = $1;
77         next;
78       }
79       elsif(/\s*TIMEOUT\s+(\d+)\s*;\s*/) {
80         $self->{timeout} = $1;
81         next;
82       }
83       else {
84         die "Syntax Error on line $line\n";
85       }
86     }
87   }
88   if($current) {
89     die "unclosed stanza\n";
90   }
91   return $self;
92 }
93
94 1;
Note: See TracBrowser for help on using the browser.