root/lib/Resmon/Config.pm

Revision b3c640775d36906eb2e9f8a2b1cbf1bbb0ba8fd4, 1.3 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

make this stuff object oriented.. but Resmon::Modules should really by Resmon::Module

git-svn-id: https://labs.omniti.com/resmon/trunk@20 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   }, $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::Modules::$current";
27         push(@{$self->{Modules}->{$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->{Modules}->{$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;
Note: See TracBrowser for help on using the browser.