/usr/share/perl5/Munin/Node/ProxySpooler.pm is in munin-node 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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | package Munin::Node::ProxySpooler;
# $Id$
use strict;
use warnings;
use Net::Server::Daemonize qw( daemonize safe_fork unlink_pid_file );
use IO::Socket;
use List::MoreUtils qw( any );
use Time::HiRes qw( ualarm gettimeofday );
use Carp;
use Munin::Common::Defaults;
use Munin::Node::Logger;
use Munin::Node::SpoolWriter;
use Munin::Node::Config;
my $config = Munin::Node::Config->instance;
sub new
{
my ($class, %args) = @_;
$args{spooldir} ||= $Munin::Common::Defaults::MUNIN_SPOOLDIR;
$args{spool} = Munin::Node::SpoolWriter->new(spooldir => $args{spooldir});
# don't want to run as root unless absolutely necessary. but only root can
# change user
$args{user} = $< || $Munin::Common::Defaults::MUNIN_PLUGINUSER;
$args{group} = $( || $Munin::Common::Defaults::MUNIN_GROUP;
$args{host} ||= 'localhost';
$args{port} ||= '4949';
return bless \%args, $class;
}
sub run
{
my ($class, %args) = @_;
my $self = __PACKAGE__->new(%args);
croak "No pidfile specified" unless $args{pid_file};
# Daemonzises, and runs for cover.
daemonize($self->{user}, $self->{group}, $self->{pid_file});
$self->{have_pid_file}++;
open STDERR, '>>', "$Munin::Common::Defaults::MUNIN_LOGDIR/munin-sched.log";
STDERR->autoflush(1);
# FIXME: reopen logfile on SIGHUP
logger('Spooler starting up');
# Indiscriminately kill every process in the group with SIGTERM when asked
# to quit. this is just the list of signals the Perl Cookbook suggests
# trapping.
#
# FIXME: might be better if this was implemented with sigtrap pragma.
#
# !!!NOTE!!! this should always be the same list as the one down there in
# _launch_single_poller()
$SIG{INT} = $SIG{TERM} = $SIG{HUP} = sub {
logger("Spooler caught SIG$_[0]. Shutting down");
kill -15 => $$;
if ($self->{have_pid_file}) {
logger('Removing pidfile') if $config->{DEBUG};
unlink_pid_file($self->{pid_file});
}
exit 0;
};
$self->_launch_pollers();
logger('Spooler going to sleep');
# Reap any dead pollers
while (my $deceased = wait) {
if ($deceased < 0) {
last if $!{ECHILD}; # all the children are dead!
logger("wait() error: $!");
next;
}
$self->_restart_poller($deceased);
}
logger('Spooler shutting down');
exit 0;
}
### SETUP ######################################################################
# queries the node for a list of services, and works out how often each one
# should be checked
sub _get_intervals
{
my ($self) = @_;
my %intervals;
$self->_open_node_connection;
my @services = $self->_get_service_list() or die "No services\n";
foreach my $service (@services) {
if (my $interval = $config->{sconf}{$service}{update_rate}) {
logger("Setting interval for service '$service' from config")
if $config->{DEBUG};
$intervals{$service} = $interval;
next;
}
else {
logger("Fetching interval for service '$service' from node")
if $config->{DEBUG};
$intervals{$service} = $self->_service_interval(
$self->_talk_to_node("config $service")
);
}
logger("Interval is $intervals{$service} seconds") if $config->{DEBUG};
}
$self->_close_node_connection;
$self->{intervals} = \%intervals;
return \%intervals;
}
# gets a list of all the nodes served by that node
sub _get_node_list { return (shift)->_talk_to_node('nodes'); }
# gets a list of every service on every node
sub _get_service_list
{
my ($self) = @_;
my @services;
my @nodes = $self->_get_node_list() or die "No nodes\n";
foreach my $node (@nodes) {
logger("Fetching services for node $node") if $config->{DEBUG};
my $service_list = $self->_talk_to_node("list $node");
if ($service_list) {
logger("Got services $service_list") if $config->{DEBUG};
push @services, split / /, $service_list;
}
else {
logger("No services for $node") if $config->{DEBUG};
}
}
return @services;
}
# takes the config response for the service, and returns the correct interval
sub _service_interval { /^update_rate (\d+)/ && return $1 foreach @_; return 300; }
#### GATHER DATA ###############################################################
# forks off a child for each process on the node, and sets them to work.
sub _launch_pollers
{
my ($self) = @_;
my %pollers;
my $intervals = $self->_get_intervals();
while (my ($service, $interval) = each %$intervals) {
my $poller_pid = $self->_launch_single_poller($service, $interval);
}
return;
}
sub _launch_single_poller
{
my ($self, $service, $interval) = @_;
logger("Launching poller for '$service' with an interval of ${interval}s")
if $config->{DEBUG};
if (my $poller_pid = safe_fork()) {
logger("Poller for '$service' running with pid $poller_pid")
if $config->{DEBUG};
$self->{pollers}{$poller_pid} = $service;
return;
}
# don't want the pollers to have the kill-all-the-process-group handler
# installed. !!!NOTE!!! this should always be the same list as the one up
# there in run()
delete @SIG{qw( INT TERM HUP )};
$0 .= " [$service]";
# Fetch data
_poller_loop($interval, sub {
logger(sprintf "%s: %d %d", $service, gettimeofday); # FIXME: for testing timing accuracy
my @result = $self->_fetch_service($service);
logger("Read " . scalar @result . " lines from $service")
if $config->{DEBUG};
$self->{spool}->write(time, $service, \@result);
});
exit 0;
}
# calls coderef $code every $interval seconds.
sub _poller_loop
{
my ($interval, $code) = @_;
$interval *= 1e6; # it uses microseconds.
# sleep a random amount. should help spread the load up a bit.
# then run $code every $interval seconds.
#
# FIXME: this will interact really really badly with any code that uses
# sleep().
$SIG{ALRM} = $code;
ualarm(rand($interval), $interval);
while (1) { sleep; }
ualarm(0);
return;
}
# connect to the node, fetch data, write it out to the spooldir.
sub _fetch_service
{
my ($self, $service) = @_;
$self->_open_node_connection;
my @config = $self->_talk_to_node("config $service");
push @config, $self->_talk_to_node("fetch $service")
unless any {/\.value /} @config;
$self->_close_node_connection;
if (any { m{# (?:Timed out|Unknown service|Bad exit)} } @config) {
return ();
}
return @config;
}
# takes the PID of a dead poller, and respawns it.
sub _restart_poller
{
my ($self, $pid) = @_;
my $service = delete $self->{pollers}{$pid};
my $exit = ($? >> 8);
my $signal = ($? & 127);
logger("Poller $pid ($service) exited with $exit/$signal");
# avoid restarting the poller if it was last restarted too recently.
if (time - ($self->{poller_restarted}{$service} || 0) < 10) {
logger("Poller for '$service' last restarted at $self->{poller_restarted}{$service}. Giving up.");
return;
}
# Respawn the poller
logger("Respawning poller for '$service'");
$self->_launch_single_poller($service, $self->{intervals}{$service});
$self->{poller_restarted}{$service} = time;
return;
}
### NODE INTERACTION ###########################################################
# returns an open IO::Socket to the node, ready for reading.
sub _open_node_connection
{
my ($self) = @_;
logger("Opening connection to $self->{host}:$self->{port}")
if $config->{DEBUG};
$self->{socket} = IO::Socket::INET->new(
PeerAddr => $self->{host},
PeerPort => $self->{port},
Proto => 'tcp',
) or die "Failed to connect to node on $self->{host}:$self->{port}: $!\n";
my $line = $self->_read_line or die "Failed to read banner\n";
die "Service is not a Munin node (responded with '$line')\n"
unless ($line =~ /^# munin node at /);
# report capabilities to unlock all the special services
$line = $self->_talk_to_node('cap multigraph dirtyconfig')
or die "Failed to read node capabilities\n";
return;
}
# closes the socket, and deletes it from the instance hash.
sub _close_node_connection { (delete $_[0]->{socket})->close; }
# prints $command to the node on $socket, and returns the response.
sub _talk_to_node
{
my ($self, $command) = @_;
my $multiline = ($command =~ m{^(?:nodes|config|fetch)});
croak "multiline means scalar context" if $multiline and not wantarray;
my $socket = $self->{socket};
$self->_write_line($command);
my @response = ($multiline) ? $self->_read_multiline() : $self->_read_line();
return wantarray ? @response : shift @response;
}
# write a single line to the node
sub _write_line
{
my ($self, $command) = @_;
logger("DEBUG: > $command") if $config->{DEBUG};
$self->{socket}->print($command, "\n") or die "Write error to socket: $!\n";
return;
}
# read a single line from the node
sub _read_line
{
my ($self) = @_;
my $line = $self->{socket}->getline;
defined($line) or die "Read error from socket: $!\n";
chomp $line;
logger("DEBUG: < $line") if $config->{DEBUG};
return $line;
}
# read a multiline response from the node (ie. up to but not including the
# '.' line at the end.
sub _read_multiline
{
my ($self) = @_;
my ($line, @response);
push @response, $line until ($line = $self->_read_line) eq '.';
return @response;
}
1;
__END__
=head1 NAME
Munin::Node::ProxySpooler - Daemon to gather spool information by querying a
munin-node instance.
=head1 SYNOPSIS
Munin::Node::ProxySpooler->run(spooldir => '/var/spool/munin');
# never returns.
# meanwhile, in another process
my $spoolreader = Munin::Node::Spoolreader->new(
spooldir => '/var/spool/munin',
);
print $spoolreader->fetch(123456789);
=head1 METHODS
=over 4
=item B<new>
Munin::Node::ProxySpooler->new(%args);
Constructor. It is called automatically by the C<run> method, so probably
isn't of much use otherwise.
=item B<run>
Munin::Node::ProxySpooler->run(%args);
Daemonises the current process, and starts fetching data from a Munin node.
Never returns. The process will clean up and exit(0) upon receipt of SIGINT,
SIGTERM or SIGHUP.
=over 8
=item C<spooldir>
The directory to write results to. Optional.
=item C<host>, C<port>
The host and port the spooler will gather results from. Defaults to
C<localhost> and C<4949> respectively, which should be acceptable for most
purposes.
=item C<pid_file>
The pidfile to use. Required.
=back
=back
=cut
# vim: sw=4 : ts=4 : et
|