File: munin-node-configure.in

package info (click to toggle)
munin 1.2.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,940 kB
  • ctags: 98
  • sloc: sh: 4,215; makefile: 452; perl: 135
file content (726 lines) | stat: -rwxr-xr-x 17,033 bytes parent folder | download | duplicates (2)
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!@@PERL@@ -w
#
# Copyright (C) 2004 Jimmy Olsen
#
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Program to suggest configurations and configuration changes.
#
# $Log$
# Revision 1.11.2.1  2005/03/09 21:49:57  jimmyo
# munin-node-configure now properly respect user plugins.
#
# Revision 1.11  2004/12/10 10:40:12  jimmyo
# Fix bug with underscore in wildcard plugins.
#
# Revision 1.10  2004/11/16 20:00:42  jimmyo
# License cleanups.
#
# Revision 1.9  2004/09/07 19:33:32  jimmyo
# Documented the SNMP options a bit better.
#
# Revision 1.8  2004/09/05 12:00:18  jimmyo
# Set family and capabilities.
#
# Revision 1.7  2004/09/03 23:25:16  jimmyo
# Brushed up the SNMP probe functionality.
#
# Revision 1.6  2004/09/03 22:59:03  jimmyo
# Support comma-formatted snmp-options.
#
# Revision 1.5  2004/09/03 22:56:51  jimmyo
# Added support for SNMP probing.
#
# Revision 1.4  2004/01/29 19:25:52  jimmyo
# Fixed bad debug output (forgotten linebreaks) in munin-node-configure (SF#882385).
#
# Revision 1.3  2004/01/29 17:36:19  jimmyo
# Updated copyright information
#
# Revision 1.2  2004/01/15 15:20:00  jimmyo
# Making things workable after name change. Upping for test verwion.
#
# Revision 1.1  2004/01/02 18:50:00  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:06  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.11  2003/12/18 16:46:59  jimmyo
# now prints the reason for suggesting to not use a plugin, as long as the plugins gives one.
#
# Revision 1.10  2003/12/17 21:29:26  jimmyo
# Don\'t try to change uid/gid if not running as root. (Deb#224300)
#
# Revision 1.9  2003/11/10 22:05:42  toreanderson
# bugfux
#
# Revision 1.8  2003/11/10 17:52:33  jimmyo
# Small briainer-typo
#
# Revision 1.7  2003/11/10 17:32:18  jimmyo
# Check what to do with new plugins when upgrading.
#
# Revision 1.6  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#

$| = 1; # Flush after every write to stdout

use strict;
use Getopt::Long;

my $version    = "@@VERSION@@";
my $config     = "@@CONFDIR@@/munin-node.conf";
my $servicedir = "@@CONFDIR@@/plugins";
my $libdir     = "@@LIBDIR@@/plugins";
my $bindir     = "@@SBINDIR@@";
my $installed  = undef;
my $plugins    = undef;
my $debug      = 0;
my $suggest    = 0;
my $shell      = 0;
my @afamilies  = ("auto", "manual", "contrib");
my @dfamilies  = ("auto");
my @families   = ();
my @snmp       = ();
my $snmpver    = undef;
my $snmpcomm   = undef;
my $removes    = 0;
my $do_usage   = 0;
my $do_error   = 0;
my $do_version = 0;
my $newer      = undef;

$do_error = 1 unless GetOptions (
	"help"            => \$do_usage,
	"shell!"          => \$shell,
	"debug!"          => \$debug,
	"suggest!"        => \$suggest,
	"config=s"        => \$config,
	"servicedir=s"    => \$servicedir,
	"remove-also!"    => \$removes,
	"libdir=s"        => \$libdir,
	"families=s"      => \@families,
	"version!"        => \$do_version,
	"snmp=s"          => \@snmp,
	"snmpversion=s"   => \$snmpver,
	"snmpcommunity=s" => \$snmpcomm,
	"newer=s"         => \$newer
);

if ($do_error or $do_usage)
{
	print "Usage: $0 [options]
	
Options:
	--help			View this help page
	--version		Show version information
	--debug			View debug information (very verbose)
	--config <file>		Override configuration file 
	                        [$config]
	--servicedir <dir>	Override plugin dir [$servicedir]
	--libdir <dir>		Override plugin lib [$libdir]
	--families <family,...>	Override families (", join(',',@afamilies),") [",join(',',@dfamilies),"]
	--suggest		Show suggestions instead of status
	--shell			Show shell commands (implies --suggest)
	--remove-also		Also show rm-commands when doing --shell
	--newer <version>	Only show suggestions related to plugins newer
	                        than version <version>. [0.0.0]
	--snmp <host|cidr>      Do SNMP probing on the host or CIDR network.
	--snmpversion <ver>     Set SNMP version (1, 2c or 3) [2c]
	--snmpcommunity <comm>  Set SNMP community [public]

";

	exit ($do_error); # 1 if error, 0 if --help
}

if ($do_version)
{
    print "munin-node-configure (munin-node) version $version.\n";
    print "Written by Jimmy Olsen\n";
    print "\n";
    print "Copyright (C) 2003-2004 Jimmy Olsen\n";
    print "This is free software released under the GNU Public License. There is NO\n";
    print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
    exit 0;
}

if (defined $newer and (!$shell or $removes))
{
    print "Fatal: --newer only supported along with --shell.\n";
    exit 2;
}

if (!@families)
{
    if (@snmp)
    {
	@families = ();
	$shell=1;
    }
    elsif ($suggest or $shell)
    {
	@families = @dfamilies;
    }
    else
    {
	@families = @afamilies;
    }
}

@families = split (/,/, join (',', @families));
@snmp = split (/,/, join (',', @snmp));

if (@snmp and !$shell)
{
    print "Fatal: --snmp only supported along with --shell.\n";
    exit 3;
}

if (@families)
{
    $plugins   = &get_plugins   ($plugins  , $libdir);
    $installed = &get_installed ($installed, $servicedir);

    if (! $shell and $suggest)
    {
	&normal ("Plugin", "Used", "Suggestions");
	&normal ("------", "----", "-----------");
    }
    elsif (! $shell)
    {
	&normal ("Plugin", "Used", "Extra information");
	&normal ("------", "----", "-----------------");
    }

    foreach my $plug (sort keys %{$plugins})
    {
	next unless defined $plugins->{$plug}->{'family'};
	next unless (grep (/^$plugins->{$plug}->{'family'}$/, @families));

	my $now   = (exists $installed->{$plug}?"yes":"no");
	my $want  = (exists $plugins->{$plug}->{'default'}?$plugins->{$plug}->{'default'}:"no");
	my $snow  = (exists $installed->{$plug}->{'suggest'}?$installed->{$plug}->{'suggest'}:undef);
	my $swant = (exists $plugins->{$plug}->{'suggest'}?$plugins->{$plug}->{'suggest'}:undef);
	my $sugg  = "";


	if ($shell)
	{
	    if ($plug =~ /_$/) # Wildcard plugin...
	    {
		if ($now eq "yes" and $want eq "no")
		{
		    if ($snow and @{$snow})
		    {
			foreach my $wild (sort @{$snow})
			{
			    &link_remove ("$plug$wild");
			}
		    }
		}
		else
		{
		    if ($snow and @{$snow})
		    {
			foreach my $wild (sort @{$snow})
			{
			    if (! grep (/^$wild$/, @{$swant}))
			    {
				&link_remove ("$plug$wild");
			    }
			}
		    }
		    if ($swant and @{$swant})
		    {
			foreach my $wild (sort @{$swant})
			{
			    next if -e "$servicedir/$plug"."_".$wild; # None of our business...
			    if (! grep (/^$wild$/, @{$snow}))
			    {
				&link_add ($plug, $wild);
			    }
			}
		    }
		}
	    }
	    else
	    {
		if ($now eq "yes" and $want eq "no")
		{
		    &link_remove ($plug);
		}
		elsif ($now eq "no" and $want eq "yes")
		{
		    next if -e "$servicedir/$plug"; # None of our business...
		    &link_add ($plug);
		}
	    }
	}
	elsif ($suggest)
	{
	    if ($plug =~ /_$/) # Wildcard plugin...
	    {
		if ($now eq "yes" && $want eq "no" or
			$now eq "no" && $want eq "yes" && $swant && @{$swant})
		{
		    $sugg = "$want ";
		}
		if ($snow and @{$snow})
		{
		    foreach my $wild (sort @{$snow})
		    {
			if (! grep (/^$wild$/, @{$swant}))
			{
			    $sugg .= "-$wild ";
			}
		    }
		}
		if ($swant and @{$swant})
		{
		    foreach my $wild (sort @{$swant})
		    {
			next if -e "$servicedir/$plug"."_".$wild; # None of our business...
			if (! grep (/^$wild$/, @{$snow}))
			{
			    $sugg .= "+$wild ";
			}
		    }
		}
		&normal ($plug, $now, $sugg);
	    }
	    else
	    {
		next if -e "$servicedir/$plug"; # None of our business...
		if ($now ne $want)
		{
		    $sugg = $want;
		}
		elsif (defined $plugins->{$plug}->{'defaultreason'} and
			$plugins->{$plug}->{'defaultreason'} =~ /^[^\(]*\((.+)\)\s*$/)
		{
		    $sugg = "[$1]";
		}

		&normal ($plug, $now, $sugg);
	    }
	}
	else
	{
	    if ($plug =~ /_$/) # Wildcard plugin...
	    {
		if ($snow and @{$snow})
		{
		    $sugg = join ' ', @{$snow};
		}
		&normal ($plug, $now, $sugg);
	    }
	    else
	    {
		&normal ($plug, $now, "");
	    }
	    
	}
    }
}

if (@snmp and $shell)
{
    if (-x "$bindir/munin-node-configure-snmp")
    {
	if (eval "require Net::SNMP;")
	{
	    my @params;
	    push (@params, "$bindir/munin-node-configure-snmp");
	    push (@params, "--snmpversion", $snmpver) if defined $snmpver;
	    push (@params, "--community", $snmpcomm) if defined $snmpcomm;
	    push (@params, "--debug") if $debug;
	    push (@params, @snmp);
	    exec (@params);
	    # NOTREACHED
	}
	else
	{
	    print STDERR "# ERROR: Cannot perform SNMP probe. SNMP operations require the perl module\n";
	    print STDERR "# Net::SNMP, which is currently not installed (or not in one of the following\n";
	    print STDERR "# directories: @INC\n";
	}
    }
    else
    {
	print STDERR "# ERROR: Cannot perform SNMP probe. SNMP operations require the SNMP part\n";
	print STDERR "# of Munin to be installed, which is currently not installed, or not in the \n";
	print STDERR "# rigt directory: $bindir\n";
    }
}

sub link_remove
{
    my $plug = shift;

    return unless $removes;
    return unless (-l "$servicedir/$plug"); # Strange...

    print "rm -f $servicedir/$plug\n";
}

sub link_add
{
    my $plug = shift;
    my $wild = shift;

    if (defined $wild)
    {
	print "ln -s $libdir/$plug $servicedir/$plug$wild\n";
    }
    else
    {
	print "ln -s $libdir/$plug $servicedir/$plug\n";
    }
}

sub normal
{
    printf ("%-26s | %-4s | %-39s\n", @_);
}

sub get_installed
{
	(my $ps, my $dir) = @_;

	print "DEBUG: Opening \"$dir\" for reading...\n" if $debug;
	opendir (DIR, $dir) or die "Could not open \"$dir\" for reading: $!";
	my @installed = readdir (DIR);
	close (DIR);

	foreach my $inst (@installed)
	{
		my $realfile = undef;

		next unless -l "$dir/$inst"; # None of our business...
		next unless ($realfile = readlink ("$dir/$inst"));
		next unless ($realfile =~ /^$libdir\//); # None of our business...
		$realfile =~ s/^.+\///;

		print "DEBUG: Checking file: $inst..." if $debug;
		if ($realfile =~ /_$/) # Wildcard plugin...
		{
		    print "$realfile..." if $debug;
		    (my $wild = $inst) =~ s/^$realfile//;
		    print "$wild..." if $debug;
		    push @{$ps->{$realfile}->{'suggest'}}, $wild;
		}
		$ps->{$realfile}->{'installed'} = "yes";

		print "\n" if $debug;
	}

	return $ps;
}

sub get_plugins
{
	(my $ps, my $dir) = @_;
	my %versions = ();

	if (defined $newer and -f "$dir/plugins.history")
	{
	    if (open (HIST, "$dir/plugins.history"))
	    {
		my @slurp = <HIST>;
		my $reached_version = 0;
		my $ver = "0.0.0";
		close (HIST);
		my $uname = lc `uname`;
		chomp $uname;

		foreach my $line (@slurp)
		{
		    chomp $line;
		    $line =~ s/#.*//g;
		    $line =~ s/^\s+//g;
		    $line =~ s/\s+$//g;
		    next unless $line =~ /\S/;

		    if ($line =~ /^\[([^\]]+)\]$/)
		    {
			$ver = $1;
			print "Setting version to \"$ver\".\n" if $debug;
			if ($ver eq $newer)
			{
			    $reached_version = 1;
			}
			elsif ($reached_version)
			{
			    $reached_version++;
			}
		    }
		    elsif ($reached_version < 2)
		    {
			next;
		    }
		    elsif ($line =~ /^([^\/]+)\/(.+)$/)
		    {
			if ($uname eq $1)
			{
			    $ps->{$2}->{'version'} = $ver;
			    print "-- Adding plugin \"$2\" to version tree ($ver)...\n" if $debug;
			}
		    }
		    elsif ($line =~ /^(.+)$/)
		    {
			$ps->{$1}->{'version'} = $ver;
			print "-- Adding plugin \"$1\" to version tree ($ver)...\n" if $debug;
		    }
		}
	    }
	    else
	    {
		warn "Warning: Could not open \"$dir/plugins.history\": $!";
	    }
	}

	print "DEBUG: Opening \"$dir\" for reading...\n" if $debug;
	opendir (DIR, $dir) or die "Could not open \"$dir\" for reading: $!";
	my @plugins = readdir (DIR);
	close (DIR);

	foreach my $plug (@plugins)
	{
		my $path = "$dir/$plug";
		$path = readlink($path) and $path = $path =~ /^\// ? $path : "$dir/$path" while -l $path;
		next unless -f $path;
		next unless -x _;

		next if $plug =~ /^\./;

		$ps->{$plug}->{'family'} = "contrib"; # Set default family...

		print "DEBUG: Checking plugin: $plug..." if $debug;
		if (! open (FILE, "$dir/$plug"))
		{
			warn "WARNING: Could not open file \"$dir/$plug\" for reading ($!). Skipping.";
			next;
		}
		while (<FILE>)
		{
			chomp;
			if (/#%#\s+family\s*=\s*(\S+)\s*$/)
			{
				$ps->{$plug}->{'family'} = $1;
				print "$1..." if $debug;
			}
			elsif (/#%#\s+capabilities\s*=\s*(.+)$/)
			{
				foreach my $cap (split (/\s+/, $1))
				{
					$ps->{$plug}->{'capability'}->{$cap} = 1;
					print "$cap..." if $debug;
				}
			}
		}
		close (FILE);

		unless (grep (/^$ps->{$plug}->{'family'}$/, @families)) {
			print "\n" if $debug;
			next;
		}
		if ($ps->{$plug}->{'family'} eq "auto" and defined $newer and
			!defined $ps->{$plug}->{'version'}) {
			print "\n" if $debug;
			next;
		}

		if ($ps->{$plug}->{'capability'}->{'autoconf'})
		{
			my $fork = open (PLUG, "-|");

			if ($fork == -1)
			{
				die "ERROR: Unable to fork: $!"
			}
			elsif ($fork == 0) # Child
			{
				close (STDERR);
				open (STDERR, ">&STDOUT");
				exec ("$bindir/munin-run", "--servicedir", $libdir, $plug, "autoconf");
			}
			else
			{
				$ps->{$plug}->{'defaultreason'} = join (' ', map {chomp; $_} <PLUG>);
			}
			close (PLUG);
			if ($?)
			{
				$ps->{$plug}->{'default'} = "no";
			}
			else
			{
				$ps->{$plug}->{'default'} = "yes";
			}
			print $ps->{$plug}->{'default'}, ":", $ps->{$plug}->{'defaultreason'}, "..." if $debug;
		}

		if ($ps->{$plug}->{'capability'}->{'suggest'})
		{
			if (exists $ps->{$plug}->{'default'} and
					$ps->{$plug}->{'default'} eq "yes")
			{
				my $fork = open (PLUG, "-|");

				if ($fork == -1)
				{
					die "ERROR: Unable to fork: $!"
				}
				elsif ($fork == 0) # Child
				{
					exec ("$bindir/munin-run", "--servicedir", $libdir, $plug, "suggest");
				}
				else
				{
					push (@{$ps->{$plug}->{'suggest'}}, split (/\s+/, join (' ', map {chomp; $_} <PLUG>)));
				}
				close (PLUG);
				if ($?)
				{
					$ps->{$plug}->{'suggest'} = "";
				}
				print join (' ', @{$ps->{$plug}->{'suggest'}}) if $debug;

			}
		}

		print "\n" if $debug;
	}
	return $ps;
}

1;

=head1 NAME

munin-node-configure - A program to view configurations for munin-node

=head1 SYNOPSIS

munin-node-configure [options]

=head1 OPTIONS

=over 5

=item B<< --help >>

View this help page

=item B<< --version >>

Show version information

=item B<< --debug >>

View debug information (very verbose)

=item B<< --config <file> >>

Override configuration file [@@CONFDIR@@/client.conf]

=item B<< --servicedir <dir> >>

Override plugin dir [@@CONFDIR@@/plugins/]

=item B<< --libdir <dir> >>

Override plugin lib [@@LIBDIR@@/plugins]

=item B<< --families <family,...> >>

Override families [auto]

=item B<< --suggest >>

Show suggestions instead of status

=item B<< --shell >>

Show shell commands (implies --suggest)

=item B<< --remove-also >>

Also show rm-commands when doing --shell

=item B<< --snmp <host|cidr,...> >>

Do SNMP probing on the host or CIDR network (e.g. "192.168.1.0/24"). This
may take some time, especially if the probe includes many hosts. This option
can be specified multiple times, or once with a comma-separated list, to
include more than one host/CIDR.

=item B<< --snmpversion <ver> >>

Set the SNMP version (1, 2c or 3) to use when probing. Default is "2c".

=item B<< --snmpcommunity <comm> >>

Set SNMP community to use when probing. Default is "public".

=back

=head1 DESCRIPTION

Munin's node is a daemon that Munin connects to fetch data. This data is
stored in .rrd-files, and later graphed and htmlified. It's designed to
let it be very easy to graph new datasources.

Munin-node-configure is a script to show the current configuration of which
plugins the host is running, as well as suggestions on what changes to make
to this configuration. Munin-node-configure does this by using munin-run(1) to
ask the plugins themselves of wether they should be run or not.

=head1 FILES

	@@CONFDIR@@/munin-node.conf
	@@CONFDIR@@/plugin-conf.d/*
	@@CONFDIR@@/plugins/*
	@@LIBDIR@@/plugins/*

=head1 VERSION

This is munin-node-configure v@@VERSION@@.

=head1 AUTHORS

Jimmy Olsen.

=head1 BUGS

None known.

=head1 COPYRIGHT

Copyright  2003 Jimmy Olsen

This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

This program is released under the GNU General Public License

=cut

# vim:syntax=perl