root/resmon_conf.pl

Revision 37cbe43bf668696d2f26324ca62752e777b57d2b, 1.7 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 13 years ago)

Initial revision

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

  • Property mode set to 100755
Line 
1 #!/usr/bin/perl
2
3 use Time::HiRes qw( gettimeofday tv_interval sleep );
4 my @conflist;
5 my $rmseconds;
6 my $rmlast = undef;
7
8 sub set_interval {
9   $rmseconds = shift;
10
11
12 sub wait_interval {
13   $rmlast = [gettimeofday] unless defined($rmlast);
14   my $elapsed = $rmseconds - tv_interval($rmlast);
15   if($elapsed > 0) {
16     sleep($elapsed);
17   }
18   $rmlast = [gettimeofday];
19 }
20
21 my $statusfile;
22 my $sfopened = 0;
23 sub set_statusfile {
24   $statusfile = shift;
25   $statusfile = undef if($statusfile eq '-');
26 }
27
28 sub open_statusfile {
29   return 0 unless $statusfile;
30   if(open(STAT, ">$statusfile.swap")) {
31     $sfopened = 1;
32     chmod 0644, "$statusfile.swap";
33     return 1;
34   }
35   return 0;
36 }
37 sub print_statusfile {
38   my $line = shift;
39   if($sfopened) {
40     print STAT $line;
41   } else {
42     print $line;
43   }
44 }
45 sub close_statusfile {
46   if($sfopened) {
47     close(STAT);
48     unlink("$statusfile");
49     link("$statusfile.swap", "$statusfile");
50     unlink("$statusfile.swap");
51     $sfopened = 0;
52   }
53 }
54 sub parse_config {
55   my $filename = shift;
56   open(CONF, "<$filename");
57   undef(@conflist);
58   while(<CONF>) {
59     next if /^\s*#/;
60     if(/\s*INTERVAL\s+(\d+)\s*;\s*/) {
61       set_interval($1);
62       next;
63     }
64     if(/\s*STATUSFILE\s+(\S+)\s*;\s*/) {
65       set_statusfile($1);
66       next;
67     }
68     if($current) {
69       if(/^\s*(\S+)\s*:\s*(.+)\s*$/) {
70         my %kvs;
71         $kvs{'type'} = $current;
72         $kvs{'object'} = $1;
73         my @params = split(/,/, $2);
74         grep { $kvs{$1} = $2 if /^\s*(\S+)\s*=>\s*(\S+)\s*$/ } @params;
75         push(@conflist, \%kvs);
76       } elsif (/^\s*\}\s*$/) {
77         $current = undef;
78       }
79     } else {
80       if(/\s*(\S+)\s*\{/) {
81         $current = $1;
82         next;
83       }
84     }
85   }
86   if($current) {
87     die "Error while parsing configuration file. $current clause unfinished";
88   }
89   return \@conflist;
90 }
91
92 1;
Note: See TracBrowser for help on using the browser.