| 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 |
}, $class; |
|---|
| 11 |
open(CONF, "<$filename") || return undef; |
|---|
| 12 |
|
|---|
| 13 |
my $current; |
|---|
| 14 |
my $line = 0; |
|---|
| 15 |
while(<CONF>) { |
|---|
| 16 |
$line++; |
|---|
| 17 |
next if /^\s*#/; |
|---|
| 18 |
next if /^\s*$/; |
|---|
| 19 |
if($current) { |
|---|
| 20 |
if(/^\s*(\S+)\s*:\s*(.+)\s*$/) { |
|---|
| 21 |
my %kvs; |
|---|
| 22 |
$kvs{'type'} = $current; |
|---|
| 23 |
$kvs{'object'} = $1; |
|---|
| 24 |
my @params = split(/,/, $2); |
|---|
| 25 |
grep { $kvs{$1} = $2 if /^\s*(\S+)\s*=>\s*(\S+)\s*$/ } @params; |
|---|
| 26 |
my $object = bless \%kvs, "Resmon::Module::$current"; |
|---|
| 27 |
push(@{$self->{Module}->{$current}}, $object); |
|---|
| 28 |
} elsif (/^\s*\}\s*$/) { |
|---|
| 29 |
$current = undef; |
|---|
| 30 |
} else { |
|---|
| 31 |
die "Syntax Error on line $line\n"; |
|---|
| 32 |
} |
|---|
| 33 |
} else { |
|---|
| 34 |
if(/\s*(\S+)\s*\{/) { |
|---|
| 35 |
$current = $1; |
|---|
| 36 |
$self->{Module}->{$current} = []; |
|---|
| 37 |
next; |
|---|
| 38 |
} |
|---|
| 39 |
elsif(/\S*LIB\s+(\S+)\s*;?\s*/) { |
|---|
| 40 |
eval "use lib '$1';"; |
|---|
| 41 |
next; |
|---|
| 42 |
} |
|---|
| 43 |
elsif(/\s*INTERVAL\s+(\d+)\s*;?\s*/) { |
|---|
| 44 |
$self->{interval} = $1; |
|---|
| 45 |
next; |
|---|
| 46 |
} |
|---|
| 47 |
elsif(/\s*STATUSFILE\s+(\S+)\s*;?\s*/) { |
|---|
| 48 |
$self->{statusfile} = $1; |
|---|
| 49 |
next; |
|---|
| 50 |
} |
|---|
| 51 |
else { |
|---|
| 52 |
die "Syntax Error on line $line\n"; |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
} |
|---|
| 56 |
if($current) { |
|---|
| 57 |
die "unclosed stanza\n"; |
|---|
| 58 |
} |
|---|
| 59 |
return $self; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
1; |
|---|