Nagios::Object(3pm)
β¨ NAME β¨
Nagios::Object β the module that makes perl objects for all ur Nagios objects. it's giving object-oriented Nagios, bestie π
(this is from the User Contributed Perl Documentation, the Nagios::Object(3pm) page, and yes i'm about to make it make sense π«Ά)
πΈ DESCRIPTION πΈ
okay so. this module is the whole reason you get to poke at Nagios stuff with perl objects. it holds the code that builds perl objects for any of the Nagios objects. and the wild part? every single perl class is auto-generated at compile-time, so adding a new attribute β or even a whole new object β is lowkey trivial. no cap. β¨
here's the list of classes we currently support, and yes they're all icons:
Nagios::TimePeriod
Nagios::Command
Nagios::Contact
Nagios::ContactGroup
Nagios::Host
Nagios::Service
Nagios::HostGroup
Nagios::ServiceEscalation
Nagios::HostDependency
Nagios::HostEscalation
Nagios::HostGroupEscalation
Nagios::ServiceDependency
-- next two are for status.dat in Nagios 2.x
Nagios::Info
Nagios::Program
(those last two? they're for status.dat in Nagios 2.x, just so u know π)
π¦ EXAMPLE π¦
here's the vibe in action, copy it, run it, slay:
use Nagios::Object;
my $generic_host = Nagios::Host->new(
register => 0,
parents => undef,
check_command => $some_command,
max_check_attempts => 3,
checks_enabled => 1,
event_handler => $some_command,
event_handler_enabled => 0,
low_flap_threshold => 0,
high_flap_threshold => 0,
flap_detection_enabled => 0,
process_perf_data => 1,
retain_status_information => 1,
retain_nonstatus_information => 1,
notification_interval => $timeperiod,
notification_options => [qw(d u r)],
notifications_enabled => 1,
stalking_options => [qw(o d u)]
);
# this will automatically 'use' $generic_host
my $localhost = $generic_host->new(
host_name => "localhost",
alias => "Loopback",
address => "127.0.0.1"
);
my $hostname = $localhost->host_name();
printf "max check attempts for $hostname is %s.\n",
$localhost->max_check_attempts;
$localhost->set_event_handler(
Nagios::Command->new(
command_name => "new_event_handler",
command_line => "/bin/true"
)
);
see how $generic_host->new(...) automatically 'use's the template? main character energy, honestly π
π METHODS π
new()
makes u a brand new object of one of the types from that list above. easy.
and here's the cute part: calling new() on an object u already have will use the LHS object as a template for the new one. mainly useful for creating objects without dragging Nagios::Object::Config into it (like in the test suite, iykyk).
Nagios::Host->new( ... );
dump()
spits out a Nagios define { } block from an object. still EXPERIMENTAL, but it might eventually be robust enough to actually drive a configuration GUI. pass in a single true argument and it'll flatten the object inheritance when it dumps.
print $object->dump();
print $object->dump(1); # flatten
name()
this one lives in every class this module makes. it should always hand u back the textual name of an object, and the Nagios::Object modules use it internally so they can do polymorphism β which is literally the thing that makes this module so compact. π
it's also the only way to get the name of a template, since templates are identified by their "name" field.
my $svc_desc = $service->name;
my $hostname = $host->name;
which is just short for:
my $svc_desc = $service->service_description;
my $hostname = $service->host_name;
register()
returns true/undef so u know whether the calling object is registerable or not. π
if ( $object->register ) { print $object->name, " is registerable." }
has_attribute()
returns true/undef depending on whether the calling object has the attribute u passed as the one and only argument.
# check to see if $object has attribute "command_line"
die if ( !$object->has_attribute("command_line") );
list_attributes()
hands u back a list of valid attributes for the calling object. that's it, that's the whole vibe.
my @host_attributes = $host->list_attributes();
attribute_type()
tells u the type of data the object's set_ method is expecting for the attribute u asked about. for some fields β like notification_options β it might return "char_flag."
for "name" attributes, it'll just return whatever %setup_data contains. simple.
honestly this method needs some TLC ... π
my $type = $host->attribute_type("notification_period");
attribute_is_list()
returns true if the attribute is supposed to be a list (an ARRAYREF). so u know whether to wrap it or not:
if ( $object->attribute_is_list("members") ) {
$object->set_members( [$member] );
} else {
$object->set_members( $member );
}
π AUTHOR π
Al Tobey <tobeya@cpan.org> β king behavior π
and a huge thank u to the fine people of #perl on freenode.net for helping out with some hairy code and silly optimizations. y'all ate. π
β WARNINGS β
see AUTHOR. that's it. that's the warning. π
perl v5.34.0 Β· 2022-06-16 β ok that's a wrap bestie, that's the whole Nagios::Object(3pm) page from back in june 2022, go build some objects and be iconic π«Άβ¨πΈ
π¦ where this page comes from
- debian bookworm: libnagios-object-perl 0.21.20-3 Β· GPL-2.0-or-later
- debian trixie: libnagios-object-perl 0.21.20-3 Β· GPL-2.0-or-later
- ubuntu noble: libnagios-object-perl 0.21.20-3 Β· GPL-2.0-or-later
- ubuntu resolute: libnagios-object-perl 0.21.20-3 Β· GPL-2.0-or-later
- ubuntu stonking: libnagios-object-perl 0.21.20-3 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms