File: munin-asyncd

package info (click to toggle)
munin 2.999.14-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 10,036 kB
  • sloc: perl: 31,052; sh: 12,473; java: 1,924; python: 852; makefile: 336; ruby: 232; awk: 169; xml: 45; sql: 11
file content (398 lines) | stat: -rwxr-xr-x 10,492 bytes parent folder | download
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
#!/usr/bin/perl
#
# Copyright (C) 2010 Steve Schnepp
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

use strict;
use warnings;

use English qw(-no_match_vars);
use IO::Socket;
use IO::File;
use File::Path qw(mkpath);
use Getopt::Long;
use Pod::Usage;

use List::Util qw(min max);

use Munin::Node::SpoolWriter;
use Munin::Common::Defaults;
use Munin::Common::Logger;

my $host = "localhost:4949";
my $metahostname;
my $SPOOLDIR = $Munin::Common::Defaults::MUNIN_SPOOLDIR . "/async";
my $intervalsize = 86400;
my $timeout = 3600;
my $minrate;
my $retaincount = 7;
my $nocleanup;
my $do_fork;
my $verbose;
my $debug;
my $screen;
my $help;
my $update_rate = 300;
my @nodes;
my %spoolwriter;

GetOptions(
	"host=s" => \$host,
	"spooldir|s=s" => \$SPOOLDIR,
	"interval|i=i" => \$intervalsize,
	"timeout=i" => \$timeout,
	"minrate=i" => \$minrate,
	"retain|r=i" => \$retaincount,
	"update-rate=i" => \$update_rate,

	"fork" => \$do_fork,

	"help|h" => \$help,
	"verbose|v" => \$verbose,
	"nocleanup|n" => \$nocleanup,
	"debug" => \$debug,
        "screen" => \$screen,
) or pod2usage(1);
if ($help) {
	pod2usage(1);
}

if ( $verbose || $debug || $screen ) {
    my %log;
    $log{output} = 'screen' if $screen;
    $log{level}  = 'info'   if $verbose;
    $log{level}  = 'debug'  if $debug;
    Munin::Common::Logger::configure(%log);
}

# $minrate defaults to $update_rate.
$minrate = $update_rate if ! defined $minrate;

# Debug implies Verbose
$verbose = 1 if $debug;

unless (-d $SPOOLDIR) {
	mkpath($SPOOLDIR, { verbose => $verbose, } )
		or LOGCROAK("Cannot create '$SPOOLDIR': $!");
}

my $process_name = "main";

my $sock = new IO::Socket::INET(
	PeerAddr        => "$host",
	Proto   => 'tcp'
);
if (!$sock) {
	print STDERR "[$$][$process_name] Failed to connect to munin-node - trying again in a few seconds ...\n" if $verbose;
	sleep 20;
	$sock = new IO::Socket::INET(
		PeerAddr => "$host",
		Proto => 'tcp'
	) || LOGCROAK("Error connecting to munin node ($host): $!");
}
my $nodeheader = <$sock>;
print STDERR "[sock][>] nodes\n" if $debug;
print $sock "nodes\n";

# get all virtual node names exposed by one physical node
while(my $line = <$sock>) {
	chomp($line);
	print STDERR "[sock][<] $line\n" if $debug;
	last if $line =~ /^\./;

	# un-taint nodename TODO: too strict?
	$line =~ /^([\w.]+)$/x;
	$line = $1;

	push(@nodes, $line);
}

print $sock "quit\n";
close ($sock);
( $metahostname ) = ( $nodeheader =~ /munin node at (\S+)\n/);
$metahostname = "unknown" unless $metahostname;

foreach my $node (@nodes) {
	unless (-d "$SPOOLDIR/$node") {
		mkpath("$SPOOLDIR/$node", { verbose => $verbose, } )
			or die ("Cannot create '$SPOOLDIR/$node': $!");
	}
	$spoolwriter{$node} = Munin::Node::SpoolWriter->new(
	spooldir => "$SPOOLDIR/$node",
	interval_size => $intervalsize,
	interval_keep => $retaincount,
	hostname  => $metahostname,
	);
}

$0 = "munin-asyncd [$metahostname] [idle]";

my $plugins = {};
{
	INFO("[$$][$process_name] Reading config from $host");
	my $sock = new IO::Socket::INET(
		PeerAddr	=> "$host",
		Proto	=> 'tcp'
	) || die "Error creating socket: $!";

	local $0 = "munin-asyncd [$metahostname] [list]";
	DEBUG("[sock][>] cap multigraph dirtyconfig");
	print $sock "cap multigraph dirtyconfig\n";
	<$sock>; # Read the first header comment line
	<$sock>; # Read the multigraph response line
	foreach my $node (@nodes)
	{
		print STDERR "[sock][>] list $node\n" if $debug;
		print $sock "list $node\n";
		my $plugins_line = <$sock>;
		chomp($plugins_line);
		print STDERR "[sock][<] $plugins_line\n" if $debug;
		{
			my $fh_list = IO::File->new(
				"$SPOOLDIR/$node/munin-daemon.list",
				"w",
			);
			my $sanitised_plugins_line = $plugins_line;
			$sanitised_plugins_line =~ s/[^_A-Za-z0-9 ]/_/g;
			print $fh_list $sanitised_plugins_line;
			print $fh_list "\n";
		}

		#un-taint plugin_line TODO: more strict?
		$plugins_line =~ /^(.*)$/x;
		$plugins_line = $1;

		$plugins->{$node} = [ split(/ /, $plugins_line) ];
	}
}

my $keepgoing = 1;

sub termhandler {
	$keepgoing = 0;
}

# Q&D child collection
$SIG{CHLD} = 'IGNORE';
$SIG{HUP} = 'IGNORE';
$SIG{INT} = 'termhandler';
$SIG{TERM} = 'termhandler';

# now, update regularly...
# ... but each plugin in its own process to avoid delay-leaking
my %last_updated;
my $last_cleanup=0;
MAIN: while($keepgoing) {
	my $when = time;

	my $process_name_orig = $process_name;

	# start the next run close to the end of a munin-node update operation
	# (i.e. try to avoid overlapping activities)
	my $when_next = int((int($when / $minrate) + 0.75) * $minrate);
	while ($when_next <= $when) {
		$when_next = $when_next + $minrate;
	}

	foreach my $node (@nodes)
	{
		my $sock;
		PLUGIN: foreach my $plugin ( @{$plugins->{$node}} ) {
			# See if this plugin should be updated
			my $plugin_rate = $spoolwriter{$node}->get_metadata("plugin_rates/$plugin") || 300;
			my $plugin_next = ($last_updated{$plugin} || 0) + $plugin_rate;
			if ($when < $plugin_next) {
				# not yet, wake me up later, and see next plugin
				if ($plugin_next < $when_next) {
					$when_next = $plugin_next;
				}

				next;
			}

			# Should update it - based on wall clock time
			my $should_have_been = $when - ($when % $plugin_rate);
			my $should_be_next = $should_have_been + $plugin_rate;

			$last_updated{$plugin} = $should_have_been;
			if ($should_be_next < $when_next) {
				$when_next = $should_be_next;
			}

			DEBUG("[$$][$process_name] $plugin: should_have_been $should_have_been (" .
				localtime($should_have_been) .
				"), should_be_next: $should_be_next (" .
				localtime($should_be_next) .
				"), when: $when (" .
				localtime($when) .
				"), when_next: $when_next (" .
				localtime($when_next) . ")" );

			if ($do_fork && fork()) {
				# parent, return directly
				next PLUGIN;
			}

			unless ($sock) {
				$sock = new IO::Socket::INET(
					PeerAddr	=> "$host",
					Proto	=> 'tcp'
				);
				unless ($sock) {
					warn "Error creating socket: $!, moving to next plugin to try again";
					next;
				}
				<$sock>; # skip header
			}

			# Setting the command name for a useful top information
			$process_name = "plugin:$plugin";
			local $0 = "munin-asyncd [$metahostname] [$process_name]";

			fetch_data($node, $plugin, $when, $sock);

			# We end here if we forked
			last MAIN if $do_fork;
		}
		$spoolwriter{$node}->set_metadata("lastruntime", $when);
	}
	$process_name = $process_name_orig;

	$process_name = "main";
	if ($sock)
	{
		if ( $sock->connected ) {
			print STDERR "[$$][$process_name][>] quit\n" if $verbose;
			print $sock "quit\n" ;
		}
		print STDERR "[$$][$process_name] closing sock\n" if $verbose;
		$sock = undef;
	}

	# Clean spool dir
	if (!$nocleanup && $last_cleanup<(time - 600)) {
		$last_cleanup = time;
		foreach ( @nodes ) { $spoolwriter{$_}->cleanup() };
	}

	# Sleep until next plugin exec.
	my $sleep_sec = $when_next - time;

	if ($sleep_sec > 0) {
		INFO("[$$][$process_name] Sleeping $sleep_sec sec");
		sleep $sleep_sec;
	} else {
		INFO("[$$][$process_name] Already late : should sleep $sleep_sec sec\n");
	}
}

print STDERR "[$$][$process_name] Exiting\n" if $verbose;

sub fetch_data
{
	my $node = shift;
	my $plugin = shift;
	my $when = shift;
	my $sock = shift;


		print STDERR "[$$][$process_name][>][$plugin] asking for config\n" if $verbose;

		print STDERR "[$$][$process_name][>][$plugin][sock] config $plugin\n" if $debug;
		print $sock "config $plugin\n";

		my $is_dirtyconfig;

		my $output_rows = [];

		while(my $line = <$sock>) {
			chomp($line);
			print STDERR "[$$][$process_name][<][$plugin][sock] $line\n" if $debug;

			if ($line =~ m/^\./) {
				# Starting with . => end
				last;
			}

			push @$output_rows, $line;
			if ($line =~ m/^update_rate (\d+)/) {
				# The plugin has a special update_rate: overriding it
				# XXX - Doesn't take into account a per field update_rate

				# This has to be sent back to the master
				$spoolwriter{$node}->set_metadata("plugin_rates/$plugin", $1);
			}

			$is_dirtyconfig = 1 if ($line =~ m/^[^.]+\.value /);
		}

		# handle the special case of cli update_rate
		if ($update_rate && ! grep { $_ =~ m/^update_rate (\d+)/ } @$output_rows ) {
			push @$output_rows, "update_rate $update_rate";
		}

		# if dirty config, bypass the "fetch" part
		goto WRITE_SPOOL if $is_dirtyconfig;

		INFO("[$$][$process_name][>][$plugin] asking for data\n");
                DEBUG("[sock][>][$plugin] fetch $plugin\n");
		print $sock "fetch $plugin\n";

		while(my $line = <$sock>) {
			chomp($line);
			DEBUG("[sock][<][$plugin] $line");

			if ($line =~ m/^\./) {
				# Starting with . => end
				last;
			}

			# Save the line
			push @$output_rows, $line;
		}

WRITE_SPOOL:
		# Write the whole load into the spool
		$spoolwriter{$node}->write($when, $plugin, $output_rows);
}

__END__

=head1 NAME

munin-asyncd - A program to spool munin-node calls

=head1 SYNOPSIS

munin-asyncd [options]

 Options:
        --host <hostname:port>  Connect to this munin-node [localhost:4949]
     -s --spooldir <spooldir>   Store the spooled data in this dir [@@SPOOLDIR@@/async]
     -i --interval <seconds>    Override default interval size of one day [86400]
        --update-rate <seconds> Override default update_rate [300]
        --timeout <seconds>     Wake up at least this number of seconds. [3600]
        --minrate <seconds>     This is the minimal rate you want to poll a node [$update_rate]
                                Note that having $update_rate < $minrate leads to unexpected results.
     -r --retain <count>        Specify number of interval files to retain [7]
     -n --nocleanup             Disable automated spool dir cleanup

        --fork                  Do fork
        --screen                Log to screen instead of syslog
        --debug                 Log debug messages
     -v --verbose               Be verbose
     -h --help                  View this message