1 |
dpavlin |
1 |
############################################################ |
2 |
|
|
# |
3 |
|
|
# $Id: rrd-common.pm 692 2006-06-26 19:11:14Z nicolaw $ |
4 |
|
|
# rrd-common.pm - Common shared module |
5 |
|
|
# |
6 |
|
|
# Copyright 2007 Nicola Worthington |
7 |
|
|
# |
8 |
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
9 |
|
|
# you may not use this file except in compliance with the License. |
10 |
|
|
# You may obtain a copy of the License at |
11 |
|
|
# |
12 |
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
13 |
|
|
# |
14 |
|
|
# Unless required by applicable law or agreed to in writing, software |
15 |
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
16 |
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 |
|
|
# See the License for the specific language governing permissions and |
18 |
|
|
# limitations under the License. |
19 |
|
|
# |
20 |
|
|
############################################################ |
21 |
|
|
# vim:ts=4:sw=4:tw=78 |
22 |
|
|
|
23 |
|
|
package RRDBrowseCommon; |
24 |
|
|
|
25 |
|
|
use 5.6.1; |
26 |
|
|
use warnings; |
27 |
|
|
use strict; |
28 |
|
|
use Config::General qw(); |
29 |
|
|
use vars qw(@ISA @EXPORT @EXPORT_OK); |
30 |
|
|
|
31 |
|
|
require Exporter; |
32 |
|
|
@ISA = qw(Exporter); |
33 |
|
|
@EXPORT_OK = qw(slurp by_domain alpha_period list_dir graph_def read_graph_data); |
34 |
|
|
@EXPORT = @EXPORT_OK; |
35 |
|
|
|
36 |
|
|
# Slurp in a file from disk, yum yum |
37 |
|
|
sub slurp { |
38 |
|
|
my $rtn = $_[0]; |
39 |
|
|
if (open(FH,'<',$_[0])) { |
40 |
|
|
local $/ = undef; |
41 |
|
|
$rtn = <FH>; |
42 |
|
|
close(FH); |
43 |
|
|
} |
44 |
|
|
return $rtn; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
# Sort by domain |
48 |
|
|
sub by_domain { |
49 |
|
|
sub split_domain { |
50 |
|
|
local $_ = shift || ''; |
51 |
|
|
if (/(.*)\.(\w\w\w+)$/) { |
52 |
|
|
return ($2,$1); |
53 |
|
|
} elsif (/(.*)\.(\w+\.\w\w)$/) { |
54 |
|
|
return ($2,$1); |
55 |
|
|
} |
56 |
|
|
return ($_,''); |
57 |
|
|
} |
58 |
|
|
my @A = split_domain($a); |
59 |
|
|
my @B = split_domain($b); |
60 |
|
|
|
61 |
|
|
($A[0] cmp $B[0]) |
62 |
|
|
|| |
63 |
|
|
($A[1] cmp $B[1]) |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
# Sort by time period |
67 |
|
|
sub alpha_period { |
68 |
|
|
my %order = qw(daily 0 weekly 1 monthly 2 annual 3 3year 4); |
69 |
|
|
($a =~ /^(.+)\-/)[0] cmp ($b =~ /^(.+)\-/)[0] |
70 |
|
|
|| |
71 |
|
|
$order{($a =~ /^.+\-(\w+)\./)[0]} <=> $order{($b =~ /^.+\-(\w+)\./)[0]} |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
# Return a list of items in a directory |
75 |
|
|
sub list_dir { |
76 |
|
|
my $dir = shift; |
77 |
|
|
opendir(DH,$dir) || die "Unable to open file handle for directory '$dir': $!"; |
78 |
|
|
my @items = grep(!/^\./,readdir(DH)); |
79 |
|
|
closedir(DH) || die "Unable to close file handle for directory '$dir': $!"; |
80 |
|
|
return @items; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
# Pull out the most relevent graph definition |
84 |
|
|
sub graph_def { |
85 |
|
|
my ($gdefs,$graph) = @_; |
86 |
|
|
return {} unless defined $graph; |
87 |
|
|
|
88 |
|
|
my $rtn = {}; |
89 |
|
|
for (keys %{$gdefs->{graph}}) { |
90 |
|
|
my $graph_key = qr(^$_$); |
91 |
|
|
if ($graph =~ /$graph_key/) { |
92 |
|
|
$rtn = { %{$gdefs->{graph}->{$_}} }; |
93 |
|
|
my ($var) = $graph =~ /_([^_]+)$/; |
94 |
|
|
for my $key (keys %{$rtn}) { |
95 |
|
|
$rtn->{$key} =~ s/\$1/$var/g; |
96 |
|
|
} |
97 |
|
|
last; |
98 |
|
|
} |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
return $rtn; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
# Read in the graph definition config file |
105 |
|
|
sub read_graph_data { |
106 |
|
|
my $filename = shift || undef; |
107 |
|
|
|
108 |
|
|
my %config = (); |
109 |
|
|
eval { |
110 |
|
|
my $conf = new Config::General( |
111 |
|
|
-ConfigFile => $filename, |
112 |
|
|
-LowerCaseNames => 1, |
113 |
|
|
-UseApacheInclude => 1, |
114 |
|
|
-IncludeRelative => 1, |
115 |
|
|
-MergeDuplicateBlocks => 1, |
116 |
|
|
-AllowMultiOptions => 1, |
117 |
|
|
-MergeDuplicateOptions => 1, |
118 |
|
|
-AutoTrue => 1, |
119 |
|
|
); |
120 |
|
|
%config = $conf->getall; |
121 |
|
|
}; |
122 |
|
|
warn $@ if $@; |
123 |
|
|
|
124 |
|
|
return \%config; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
1; |
128 |
|
|
|
129 |
|
|
|