forked from munin-monitoring/munin
/
munin-mergedb.pl
173 lines (161 loc) · 4.08 KB
/
munin-mergedb.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/perl
# -*- cperl -*-
#
# Merge munin db (datafile{,.storable} / limits) for multi-update masters
# environment
#
# (c) GPL - Adrien "ze" Urban
use warnings;
use strict;
use Storable;
use Munin::Master::Utils;
# Exemple of config (munin-merge.conf):
# # what to merge ?
# merge_datafile yes
# merge_limits yes
# # destination is the directory having this file
#
# # source directories to merge from (option should be used multiple times)
# merge_source_dbdir /nfs/munin/db/updatehost1
# merge_source_dbdir /nfs/munin/db/updatehost2
# merge_source_dbdir /nfs/munin/db/updatehost3
# merge_source_dbdir /nfs/munin/db/updatehost4
my $configfile_name = 'munin-merge.conf';
my $config_type = {
'merge_source_dbdir' => 'ARRAY',
'merge_datafile' => 'BOOL',
'merge_limits' => 'BOOL',
};
my $config = {
'merge_dbdir' => undef,
'merge_source_dbdir' => [],
'merge_datafile' => 0,
'merge_limits' => 0,
};
sub usage()
{
print STDERR <<EOF;
Usage:
$0 merge_dbdir
merge_dbdir should include a config file named $configfile_name.
This is also a security to avoid accidentaly breaking everything.
EOF
exit 1;
}
sub load_config()
{
my $dbdir = $config->{'merge_dbdir'};
my $file = $dbdir . "/" . $configfile_name;
open FILE, "<", $file or die "open: $!\n";
while (<FILE>) {
chomp;
next if (/^[[:space:]]*#/); # comment
next if (/^[[:space:]]*$/); # empty line
unless (/^[[:space:]]*([^[:space:]]+)[[:space:]]+([^[:space:]]+)[[:space:]]*$/) {
die "$.: Unrecogized line format\n";
}
my ($key, $value) = ($1, $2);
if (not defined $config_type->{$key}) {
die "$.: $key: unrecognized option\n";
}
if ('ARRAY' eq $config_type->{$key}) {
push @{$config->{$key}}, $value;
} elsif ('BOOL' eq $config_type->{$key}) {
if ($value =~ /(yes|y|1|true)/i) {
$config->{$key} = 1;
} elsif ($value =~ /(no?|0|false)/i) {
$config->{$key} = 0;
} else {
die "$.: unrecognized boolean: $value\n";
}
} else {
die "INTERNAL ERROR: $config_type->{$key}: " .
"type not implemented\n";
}
}
close FILE;
}
sub check_sources()
{
if (0 == scalar(@{$config->{'merge_source_dbdir'}})) {
die "No source dbdir. " .
"Should I produce a result from thin air?\n";
}
# no datafile, means it's not really a munin dbdir
for my $srcdir (@{$config->{'merge_source_dbdir'}}) {
unless (-f "$srcdir/datafile") {
die "$srcdir: datafile not found";
}
}
}
sub merge_plaintext($)
{
my $name = shift;
my $data = [];
my $version = undef;
for my $srcdir (@{$config->{'merge_source_dbdir'}}) {
my $srcfile = "$srcdir/$name";
unless (-f $srcfile) {
die "$srcdir: $name not found";
}
open FILE, "<", $srcfile or
die "open: $srcfile: $!\n";
my $ver = <FILE>;
if (defined $version) {
die "$srcfile: versions differs: $version vs $ver\n"
if ($ver ne $version);
} else {
$version = $ver;
}
push @$data, <FILE>;
close FILE;
#print $name, " ", scalar(@$data), " ", $srcdir, "\n";
}
my $dstfile = $config->{'merge_dbdir'} . "/" . $name;
my $dsttmp = $dstfile . ".tmp.$$";
open FILE, ">", $dsttmp or
die "open: $dsttmp: $!\n";
print FILE $version, @$data;
close FILE;
rename $dsttmp, $dstfile or
die "mv $dsttmp $dstfile: $!\n";
}
sub merge_datafile_storable()
{
my $name = 'datafile.storable';
my $data = undef;
for my $srcdir (@{$config->{'merge_source_dbdir'}}) {
my $srcfile = "$srcdir/$name";
unless (-f $srcfile) {
die "$srcdir: $name not found";
}
my $info = retrieve($srcfile);
if (defined $data) {
$data = munin_overwrite($data, $info);
} else {
$data = $info;
}
}
my $dstfile = $config->{'merge_dbdir'} . "/" . $name;
my $dsttmp = $dstfile . ".tmp.$$";
Storable::nstore($data, $dsttmp);
rename $dsttmp, $dstfile or
die "mv $dsttmp $dstfile: $!\n";
}
sub merge_datafile()
{
merge_plaintext('datafile');
merge_datafile_storable();
}
sub merge_limits()
{
merge_plaintext('limits');
}
usage unless (1 == scalar(@ARGV));
$config->{'merge_dbdir'} = shift @ARGV;
load_config;
check_sources;
merge_datafile if ($config->{'merge_datafile'});
merge_limits if ($config->{'merge_limits'});
exit 0;
# vim: syntax=perl ts=8