/usr/share/perl5/Munin/Master/Update.pm is in munin 2.0.25-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | package Munin::Master::Update;
# $Id$
use warnings;
use strict;
use English qw(-no_match_vars);
use Carp;
use Time::HiRes;
use Log::Log4perl qw( :easy );
use Munin::Common::Defaults;
use Munin::Master::Config;
use Munin::Master::Logger;
use Munin::Master::UpdateWorker;
use Munin::Master::ProcessManager;
use Munin::Master::Utils;
my $config = Munin::Master::Config->instance()->{config};
sub new {
my ($class) = @_;
# This steals the groups from the master instance of the config.
my $gah = $config->get_groups_and_hosts();
my $self = bless {
STATS => undef,
old_service_configs => {},
old_version => undef,
service_configs => {},
workers => [],
failed_workers => [],
group_repository => Munin::Master::GroupRepository->new($gah),
config_dump_file => "$config->{dbdir}/datafile",
}, $class;
}
sub run {
my ($self) = @_;
$self->_create_rundir_if_missing();
$self->_do_with_lock_and_timing(sub {
INFO "[INFO]: Starting munin-update";
$self->{old_service_configs} = $self->_read_old_service_configs();
$self->{workers} = $self->_create_workers();
$self->_run_workers();
# I wonder if the following should really be done with timing. - janl
$self->_write_new_service_configs_locked();
});
}
sub _read_old_service_configs {
# Read the datafile containing old configurations. This should
# not fail in case of problems with the file. In such a case the
# file should simply be ingored and a new one written. Lets hope
# it does not repeat itself then.
my ($self) = @_;
# Get old service configuration from the config instance since the
# syntaxes are identical.
my $oldconfig = Munin::Master::Config->instance()->{oldconfig};
my $datafile = $oldconfig->{config_file} = $config->{dbdir}.'/datafile';
my $file;
if (-e $datafile ) {
if (! open( $file, '<', $datafile)) {
WARN "[Warning] Cannot open datafile $datafile";
return {};
}
eval {
$oldconfig->parse_config($file);
};
if ($EVAL_ERROR) {
WARN "[Warning] Could not parse datafile $datafile: $EVAL_ERROR";
}
}
return $oldconfig;
}
sub _create_rundir_if_missing {
my ($self) = @_;
unless (-d $config->{rundir}) {
mkdir $config->{rundir}, oct(700)
or croak "Failed to create rundir (".$config->{rundir}."): $!";
}
}
sub _create_workers {
my ($self) = @_;
# FIX log skipped and queued workers:
# logger("Skipping '$name' (update disabled by config)");
# logger("Queuing '$name' for update.");
my @hosts = $self->{group_repository}->get_all_hosts();
if (%{$config->{limit_hosts}}) {
@hosts = grep { $config->{limit_hosts}{$_->{host_name}} } @hosts
}
@hosts = grep { $_->{update} } @hosts;
return [ map { Munin::Master::UpdateWorker->new($_) } @hosts ];
}
sub _do_with_lock_and_timing {
my ($self, $block) = @_;
my $lock = "$config->{rundir}/munin-update.lock";
munin_runlock($lock);
my $update_time = Time::HiRes::time;
if (!open ($self->{STATS}, '>', "$config->{dbdir}/munin-update.stats.tmp")) {
WARN "[WARNING] Could not open STATS to $config->{dbdir}/munin-update.stats.tmp: $!";
# Use /dev/null instead - if the admin won't fix he won't care
open($self->{STATS}, '>', "/dev/null") or
LOGCROAK "[FATAL] Could not open STATS to /dev/null (fallback for not being able to open $config->{dbdir}/munin-update.stats.tmp): $!";
}
# Place global munin-update timeout here.
my $retval = $block->();
$update_time = sprintf("%.2f", (Time::HiRes::time - $update_time));
print { $self->{STATS} } "UT|$update_time\n";
close ($self->{STATS});
$self->{STATS} = undef;
rename ("$config->{dbdir}/munin-update.stats.tmp", "$config->{dbdir}/munin-update.stats");
INFO "[INFO]: Munin-update finished ($update_time sec)";
munin_removelock($lock);
return $retval;
}
sub _run_workers {
my ($self) = @_;
if ($config->{fork}) {
my $pm = Munin::Master::ProcessManager
->new($self->_create_self_aware_worker_result_handler(),
$self->_create_self_aware_worker_exception_handler());
$pm->add_workers(@{$self->{workers}});
$pm->start_work();
}
else {
for my $worker (@{$self->{workers}}) {
my $res ;
eval {
# do_work fails hard on a number of conditions
$res = $worker->do_work();
};
$res=undef if $EVAL_ERROR;
my $worker_id = $worker->{ID};
if (defined($res)) {
$self->_handle_worker_result([$worker_id, $res]);
} else {
# Need to handle connection failure same as other
# failures. do_connect fails softly.
WARN "[WARNING] Failed worker ".$worker_id."\n";
push @{$self->{failed_workers}}, $worker_id;
}
}
}
}
sub _create_self_aware_worker_result_handler {
my ($self) = @_;
return sub { $self->_handle_worker_result(@_); };
}
sub _handle_worker_result {
my ($self, $res) = @_;
if (!defined($res)) {
# no result? problem
LOGCROAK("[FATAL] Handle_worker_result got handed a failed worker result");
}
my ($worker_id, $time_used, $service_configs)
= ($res->[0], $res->[1]{time_used}, $res->[1]{service_configs});
my $update_time = sprintf("%.2f", $time_used);
INFO "[INFO]: Munin-update finished for node $worker_id ($update_time sec)";
if (! defined $self->{STATS} ) {
# This is may only be the case when we get connection refused
ERROR "[BUG!] Did not collect any stats for $worker_id. If this message appears in your logs a lot please email munin-users. Thanks.";
} else {
printf { $self->{STATS} } "UD|%s|%.2f\n", $worker_id, $time_used;
}
$self->{service_configs}{$worker_id} = $service_configs;
}
sub _create_self_aware_worker_exception_handler {
my ($self) = @_;
return sub {
my ($worker, $reason) = @_;
my $worker_id = $worker->{ID};
DEBUG "[DEBUG] In exception handler for failed worker $worker_id";
push @{$self->{failed_workers}}, $worker_id;
};
}
# FIX merge with UpdateWorker::_get_rrd_file_name
# FIX seems like dead code? Or only used in ensure_?
sub _get_rrd_file_name {
my ($self, $host, $service, $ds_name, $ds_type) = @_;
my $type_id = lc(substr(($ds_type), 0, 1));
my ($g, $h) = split /;/, $host;
# // ... perl mode
my $file = sprintf("%s-%s-%s-%s.rrd",
$h,
$service,
$ds_name,
$type_id);
# Not really a danger (we're not doing this stuff via the shell),
# so more to avoid confusion with silly filenames.
($g, $file) = map {
my $p = $_;
$p =~ tr/\//_/;
$p =~ s/^\./_/g;
$p;
} ($g, $file);
my $rrd_file = File::Spec->catfile($config->{dbdir},
$g,
$file);
croak "RRD file '$rrd_file' not found" unless -e $rrd_file;
return $rrd_file;
}
sub _write_new_service_configs_locked {
my ($self) = @_;
my $lock_file = "$config->{rundir}/munin-datafile.lock";
munin_runlock($lock_file);
my $config_dump_file = $self->{config_dump_file};
my $config_dump_file_tmp = "$config_dump_file.$$";
open my $dump, '>', $config_dump_file_tmp
or croak "Fatal error: Could not open '$config_dump_file_tmp' for writing: $!";
$self->_write_new_service_configs($dump);
close $dump
or croak "Fatal error: Could not close '$config_dump_file_tmp': $!";
rename $config_dump_file_tmp, $config_dump_file
or croak "Fatal error: Could not rename '$config_dump_file_tmp' to '$config_dump_file': $!";
munin_removelock($lock_file);
}
sub _write_new_service_configs {
my ($self, $io) = @_;
my $datafile_hash = {};
print $io "version $Munin::Common::Defaults::MUNIN_VERSION\n";
$datafile_hash->{version} = $Munin::Common::Defaults::MUNIN_VERSION;
$self->_print_service_configs_for_not_updated_services($io, $datafile_hash);
$self->_print_old_service_configs_for_failed_workers($io, $datafile_hash);
for my $host (keys %{$self->{service_configs}}) {
for my $service (keys %{$self->{service_configs}{$host}{data_source}}) {
for my $attr (@{$self->{service_configs}{$host}{global}{$service}}) {
print $io "$host:$service.$attr->[0] $attr->[1]\n";
munin_set_var_path($datafile_hash, "$host:$service.$attr->[0]", $attr->[1]);
}
for my $data_source (keys %{$self->{service_configs}{$host}{data_source}{$service}}) {
for my $attr (keys %{$self->{service_configs}{$host}{data_source}{$service}{$data_source}}) {
print $io "$host:$service.$data_source.$attr $self->{service_configs}{$host}{data_source}{$service}{$data_source}{$attr}\n";
munin_set_var_path($datafile_hash, "$host:$service.$data_source.$attr", $self->{service_configs}{$host}{data_source}{$service}{$data_source}{$attr});
}
}
}
}
# Also write the binary (Storable) version
munin_writeconfig_storable($config->{dbdir}.'/datafile.storable', $datafile_hash);
}
sub _print_service_configs_for_not_updated_services {
my ($self, $handle, $datafile_hash) = @_;
my @hosts = $self->{group_repository}->get_all_hosts();
for my $workerdata (@hosts) {
my $worker = $workerdata->get_full_path();
my @data = grep { /\.update$/ and !$workerdata->{$_} } keys %$workerdata;
for my $match (@data) {
my $prefix = substr $match, 0, -6;
for my $datum (grep { /^\Q$prefix\E/ } keys %$workerdata) {
printf $handle "%s:%s %s\n", $worker, $datum, $workerdata->{$datum};
munin_set_var_path($datafile_hash, $worker . ":". $datum, $workerdata->{$datum});
}
}
}
}
sub _print_old_service_configs_for_failed_workers {
my ($self, $handle, $datafile_hash) = @_;
for my $worker (@{$self->{failed_workers}}) {
# The empty set contains "undef" it seems
next if !defined($worker);
my $workerdata = $self->{old_service_configs}->look_up($worker);
# No data available on the failed worker
if (!defined($workerdata)) {
INFO "[INFO] No old data available for failed worker $worker. This node will disappear from the html web page hierarchy\n";
next;
}
for my $datum (keys %$workerdata) {
# Skip some book-keeping
next if ($datum eq 'group')
or ($datum eq 'host_name');
if (defined $workerdata->{$datum}) {
printf $handle "%s:%s %s\n", $worker, $datum, $workerdata->{$datum};
munin_set_var_path($datafile_hash, $worker . ":". $datum, $workerdata->{$datum});
} else {
WARN "[Warning] no data $worker -> $datum";
}
}
}
}
1;
__END__
=head1 NAME
Munin::Master::Update - Contacts Munin Nodes, gathers data from their
service data sources, and stores this information in RRD files.
=head1 SYNOPSIS
my $update = Munin::Master::Update->new();
$update->run();
=head1 METHODS
=over
=item B<new>
my $update = Munin::Master::Update->new();
Constructor.
=item B<run>
$update->run();
This is where all the work gets done.
=back
|