Nagios-Plugin-OverHTTP
view release on metacpan or search on metacpan
lib/Nagios/Plugin/OverHTTP/Formatter/Nagios/Auto.pm view on Meta::CPAN
package Nagios::Plugin::OverHTTP::Formatter::Nagios::Auto;
use 5.008001;
use strict;
use warnings 'all';
###########################################################################
# METADATA
our $AUTHORITY = 'cpan:DOUGDUDE';
our $VERSION = '0.16';
###########################################################################
# MODULE IMPORTS
use Carp qw(croak);
use Const::Fast qw(const);
use English qw(-no_match_vars);
use Env::Path 0.04;
use IPC::System::Simple 0.13;
use Regexp::Common 2.119;
use Try::Tiny;
###########################################################################
# ALL IMPORTS BEFORE THIS WILL BE ERASED
use namespace::clean 0.04 -except => [qw(meta)];
###########################################################################
# PRIVATE CONSTANTS
const my $NAGIOS_FORMATTER_PRE => 'Nagios::Plugin::OverHTTP::Formatter::Nagios';
const my $DEFAULT_FORMATTER => join q{::}, $NAGIOS_FORMATTER_PRE, q{Version3};
const my $NAGIOS_EXECUTABLE => 'nagios';
const my $VERSION_RE => $RE{num}{int}{-sep => q{.}}{-group => q{1,3}};
###########################################################################
# CONSTRUCTOR
sub new {
my ($class, @args) = @_;
# Find all Nagios executables
my @nagios_executables = Env::Path->PATH->Whence($NAGIOS_EXECUTABLE);
# Get a list of version numbers
my @nagios_versions =
grep { defined $_ }
map { _get_nagios_version($_) }
@nagios_executables;
# The module to use
my $formatter = $DEFAULT_FORMATTER;
# Look for the formatter
VERSION:
foreach my $version (@nagios_versions) {
# Split the version into the different dot parts
my @parts = split m{\.}msx, $version;
VERSION_PART:
while (@parts) {
# Construct version to check for
my $check_for_version = join q{.}, @parts;
my $module_name = join q{::Version},
$NAGIOS_FORMATTER_PRE,
$check_for_version;
if (eval "require $module_name; 1;") {
# The module exists, so use this
$formatter = $module_name;
last VERSION;
}
# Remove last version part for next check
pop @parts;
}
}
# Make sure the formatter is loaded
if (!eval "require $formatter; 1;") {
# Module failed to load
croak $EVAL_ERROR;
}
# Return a new formatter instance
return $formatter->new(@args);
}
###########################################################################
# PRIVATE FUNCTIONS
sub _get_nagios_version {
my ($nagios_executable) = @_;
# Get the output from using the -v switch
my $version_info = try {
# Capture output
IPC::System::Simple::capturex($nagios_executable, q{-v});
}
catch {
# If error thrown, return empty string
q{};
};
# Parse out the version number
my ($version) = $version_info =~ m{^nagios \s+ (?:core \s+)? ($VERSION_RE)}imsx;
# Return the version (or undef)
return $version;
}
1;
__END__
=head1 NAME
Nagios::Plugin::OverHTTP::Formatter::Nagios::Auto - Detect installed Nagios
version and format accordingly
( run in 1.153 second using v1.01-cache-2.11-cpan-71847e10f99 )