/usr/share/perl5/Munin/Node/Configure/Plugin.pm is in munin-node 2.0.37-1.
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 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 | package Munin::Node::Configure::Plugin;
# $Id$
use strict;
use warnings;
use Munin::Node::Utils qw(set_intersection set_difference);
use Munin::Node::Configure::Debug;
sub new
{
my ($class, %opts) = @_;
my $name = delete $opts{name} or die "Must provide name\n";
my $path = delete $opts{path} or die "Must provide path\n";
my %plugin = (
name => $name, # the (base)name of the plugin
path => $path, # the full path to the plugin
default => 'no', # whether this plugin thinks it should be installed
installed => [], # list of installed services (as link names)
suggestions => [], # list of suggestions (as wildcards)
family => 'contrib', # the family it belongs to
capabilities => {}, # what capabilities it supports
errors => [], # list of errors reported against this plugin
%opts,
);
return bless \%plugin, $class;
}
################################################################################
sub is_wildcard { return ((shift)->{path} =~ /_$/); }
sub is_snmp { return ((shift)->{name} =~ /^snmp(?:v3)?__/); }
sub in_family { $_[0]->{family} eq $_ && return 1 foreach @_; return 0; }
sub is_installed { return @{(shift)->{installed}} ? 'yes' : 'no'; }
# report which services (link or wildcard) should be added, removed,
# or left as they are.
# (remove) = (installed) \ (suggested)
# (add) = (suggested) \ (installed)
# (same) = (installed) ⋂ (suggested)
sub _remove { set_difference(@_); }
sub _add { set_difference(reverse @_); }
sub _same { set_intersection(@_); }
sub suggestion_string
{
my ($self) = @_;
my $msg = '';
if ($self->{default} eq 'yes') {
my @suggestions = _same($self->_installed_wild, $self->_suggested_wild);
push @suggestions,
map { "+$_" } _add($self->_installed_wild, $self->_suggested_wild);
push @suggestions,
map { "-$_" } _remove($self->_installed_wild, $self->_suggested_wild);
$msg = ' (' . join(' ', @suggestions) . ')' if @suggestions;
}
elsif ($self->{defaultreason}) {
# Report why it's not being used
$msg = " [$self->{defaultreason}]";
}
return $self->{default} . $msg;
}
sub installed_services_string { return join ' ', @{(shift)->_installed_wild}; }
### Service name <-> wildcard conversion ###############################################
# NOTE that these functions do not round-trip!
# Extracts the wildcards from a service name and formats them in a user-friendly way.
sub _reduce_wildcard
{
my ($self, $link_name) = @_;
my $name = $self->{name};
my $wild;
if ($name =~ /^snmp(?:v3)?_(_\w+)/) {
$link_name =~ /^snmp(?:v3)?_(.+)$1(.*)/;
$wild = $1 . (length($2)? "/$2" : ''); # FIXME hack :-(
}
else {
($wild = $link_name) =~ s/^$name//;
}
return length($wild)? $wild : (); # FIXME more hack
}
# converts a wildcard to the appropriate service name
sub _expand_wildcard
{
my ($self, $suggestion) = @_;
if ($self->{name} =~ /^snmp__(\w+)/) {
my ($host, $wild) = @$suggestion;
$wild ||= '';
return 'snmp_' . $host . '_' . $1 . $wild;
}
else {
return $self->{name} . $suggestion;
}
}
# Converts a wildcard into a human-readable form
sub _flatten_wildcard { return ref($_[0]) ? join('/', @{$_[0]}) : $_[0]; }
################################################################################
# return an arrayref of the installed and suggested service names (eg. 'memory'
# or 'if_eth0')
sub _installed_links { return (shift)->{installed}; }
sub _suggested_links
{
my ($self) = @_;
# no suggestions if the plugin shouldn't be installed
return [] if $self->{default} ne 'yes';
if ($self->is_wildcard or $self->is_snmp) {
return [ map { $self->_expand_wildcard($_) } @{$self->{suggestions}} ];
}
else {
return [ $self->{name} ];
}
}
# return an arrayref of the installed or suggested wildcards (eg. 'eth0' or
# 'switch.example.com/1'). nothing is returned if the plugin contains no wildcards.
sub _installed_wild { return [ map { $_[0]->_reduce_wildcard($_) } @{$_[0]->{installed}} ]; }
sub _suggested_wild { return [ map { _flatten_wildcard($_) } @{(shift)->{suggestions}} ]; }
sub services_to_add
{
my ($self) = @_;
return _add($self->_installed_links, $self->_suggested_links);
}
sub services_to_remove
{
my ($self) = @_;
return _remove($self->_installed_links, $self->_suggested_links);
}
sub add_instance { push @{(shift)->{installed}}, shift; }
sub add_suggestions { push @{(shift)->{suggestions}}, @_; }
sub read_magic_markers
{
my ($self) = @_;
my $PLUGIN;
DEBUG("\tReading magic markers.");
unless (open ($PLUGIN, '<', $self->{path})) {
DEBUG("Could not open plugin '$self->{path}' for reading: $!");
return;
}
while (<$PLUGIN>) {
if (/#%#\s+family\s*=\s*(\S+)\s*/) {
$self->{family} = $1;
DEBUG("\tSet family to '$1'." );
}
elsif (/#%#\s+capabilities\s*=\s*(.+)/) {
my @caps = split(/\s+/, $1);
@{$self->{capabilities}}{@caps} = (1) x scalar @caps;
DEBUG("\tCapabilities are: $1");
}
}
close ($PLUGIN);
# Some sanity-checks
$self->log_error(q{In family 'auto' but doesn't have 'autoconf' capability})
if ($self->{family} eq 'auto' and not $self->{capabilities}{autoconf});
$self->log_error(q{In family 'auto' but doesn't have 'autoconf' capability})
if ($self->{family} eq 'snmpauto' and not $self->{capabilities}{snmpconf});
$self->log_error(q{Has 'suggest' capability, but isn't a wildcard plugin})
if ($self->{capabilities}{suggest} and not $self->is_wildcard);
return;
}
### Parsing plugin responses ###################################################
sub parse_autoconf_response
{
my ($self, @response) = @_;
unless (scalar(@response) == 1) {
# FIXME: not a good message
$self->log_error('Wrong amount of autoconf');
return;
}
my $line = shift @response;
unless ($line =~ /^(yes)$/
or $line =~ /^(no)(?:\s+\((.*)\))?\s*$/)
{
$self->log_error("Junk printed to stdout");
return;
}
DEBUG("\tGot yes/no: $line");
$self->{default} = $1;
$self->{defaultreason} = $2;
return;
}
sub parse_suggest_response
{
my ($self, @suggested) = @_;
foreach my $line (@suggested) {
if ($line =~ /^[-\w.:]+$/) {
DEBUG("\tAdded suggestion: $line");
$self->add_suggestions($line);
}
else {
$self->log_error("\tBad suggestion: $line");
}
}
unless (@{ $self->{suggestions} }) {
$self->log_error("No valid suggestions");
return;
}
return;
}
my $oid_pattern = qr/^[0-9.]+[0-9]+$/;
my $oid_root_pattern = qr/^[0-9.]+\.$/;
sub parse_snmpconf_response
{
my ($self, @response) = @_;
foreach my $line (@response) {
my ($key, $value) = $line =~ /(\w+)\s+(.+\S)/;
next unless $key and defined $value;
DEBUG("\tAnalysing line: $line");
if ($key eq 'require') {
my ($oid, $regex) = split /\s+/, $value, 2;
if ($oid =~ /$oid_root_pattern/) {
$oid =~ s/\.$//;
push @{ $self->{table} }, [$oid, $regex];
DEBUG("\tRegistered 'require': $oid");
DEBUG("\t\tFiltering on /$regex/") if $regex;
}
elsif ($oid =~ /$oid_pattern/) {
push @{ $self->{require_oid} }, [$oid, $regex];
DEBUG("\tRegistered 'require': $oid");
DEBUG("\t\tFiltering on /$regex/") if $regex;
}
else {
$self->log_error("Invalid format for 'require': $value");
}
}
elsif ($key eq 'index') {
if ($self->{index}) {
$self->log_error(q{'index' is already defined});
next;
}
unless ($value =~ /$oid_root_pattern/) {
$self->log_error(q{'index' must be an OID root});
next;
}
unless ($self->is_wildcard) {
$self->log_error(q{'index' only applies to double-wildcard SNMP plugins (ie. with a trailing '_'). Use 'require' instead.});
# it's valid, just suggest the author does s/index/require/
}
$value =~ s/\.$//;
# two copies. one for checking requirements, the other for
# retrieving the indices
push @{ $self->{table} }, [ $value ];
$self->{index} = $value;
DEBUG("\tRegistered 'index' : $value");
}
elsif ($key eq 'number') {
$self->log_error(q{'number' is no longer used.});
}
else {
$self->log_error("Couldn't parse line: $line");
}
}
if ($self->is_wildcard and !$self->{index}) {
$self->log_error(q{SNMP plugins with a trailing '_' need an index});
# FIXME: this should be fatal!
}
return;
}
### Debugging and error reporting ##############################################
# Logs an error due to this plugin, and prints it out if debugging is on
sub log_error
{
my ($self, $msg) = @_;
chomp $msg;
push @{$self->{errors}}, $msg;
DEBUG($msg);
return;
}
1;
__END__
=head1 NAME
Munin::Node::Configure::Plugin - Class representing a plugin, along with its
installed and suggested services.
=head1 SYNOPSIS
my $plugin = Munin::Node::Configure::Plugin->new();
=head1 METHODS
=over
=item B<new(%args)>
Constructor.
Required arguments are 'name' and 'path', which should be the
basename and full path of the plugin, respectively.
=item B<is_wildcard()>
Returns true if the plugin is a wildcard. In the case of SNMP plugins,
only double-wild plugins will return true (ie. 'snmp__memory' would
return false, but 'snmp__if_' would return true).
=item B<is_snmp()>
Returns true if the plugin is an SNMP plugin.
=item B<in_family(@families)>
Returns true if plugin's family is in @families, false otherwise.
=item B<is_installed()>
Returns 'yes' if one or more links to this plugin exist in the service
directory, 'no' otherwise.
=item B<suggestion_string()>
Returns a string detailing whether or not autoconf considers that the plugin
should be installed. The string may also report the reason why the plugin
declined to be installed, or the list of suggestions it provided, if this
information is available.
=item B<installed_services_string()>
Returns a string detailing which wildcards are installed for this plugin.
=item B<services_to_add()>
=item B<services_to_remove()>
Return a list of service names that should be added or removed for this
plugin.
=item B<add_instance($name)>
Associates a link from the service directory with this plugin.
=item B<add_suggestions(@suggestions)>
Adds @suggestions to the list of suggested wildcards for this plugin. They
are not validated.
=item B<read_magic_markers()>
Sets the family and capabilities from the magic markers embedded in the plugin's
executable, as specified by
L<http://munin-monitoring.org/wiki/ConcisePlugins#Magicmarkers>
=item B<parse_autoconf_response(@response)>
Parses and validates the autoconf response from the plugin, in the format
specified by L<http://munin-monitoring.org/wiki/ConcisePlugins#autoconf>
Invalid input will cause an error to be logged against the plugin.
=item B<parse_suggest_response(@response)>
Validates the suggestions from the plugin.
Invalid suggestions will cause an error to be logged against the plugin.
=item B<parse_snmpconf_response(@response)>
Parses and validates the snmpconf response from the plugin, in the format
specified by L<http://munin-monitoring.org/wiki/ConcisePlugins#suggest>
Invalid or inconsistent input will cause an error to be logged against the
plugin.
=item B<log_error($message)>
Logs an error for later retrieval. The error will also be displayed if
debugging output is enabled.
=back
=cut
# vim: sw=4 : ts=4 : expandtab
|