Monito...status(3pm)
π NAME π
Monitoring::Livestatus β the Perl API for check_mk livestatus, so you can pull runtime data straight out of Nagios and Icinga. It's giving "I have access to everything happening in your monitoring stack." β¨
(psst, this page is Monitoring::Livestatus, section 3pm β that's Perl module docs, bestie β from the User Contributed Perl Documentation girlies. π
)
πΈ SYNOPSIS πΈ
use Monitoring::Livestatus;
my $ml = Monitoring::Livestatus->new(
socket => '/var/lib/livestatus/livestatus.sock'
);
my $hosts = $ml->selectall_arrayref("GET hosts");
okay literally three lines and you're already querying. main character energy. π¦
β¨ DESCRIPTION β¨
So this module connects via socket/tcp to the livestatus addon for Naemon, Nagios, Icinga and Shinken. π«Ά
Heads up bestie: you have to install and activate the livestatus addon in your monitoring installation first. No addon, no vibes. π
π CONSTRUCTOR π
new ( [ARGS] )
Creates a Monitoring::Livestatus object. new takes at least the socketpath. Arguments come in key-value pairs, like a little dictionary of love. π
socketβ path to the UNIX socket of check_mk livestatus π§serverβ server address, for when you're doing a TCP connection πpeerβ an alternative way to set socket or server: if the value contains':', then server gets used, otherwise socket. sneaky but iconic.nameβ a human readable name for this connection, defaults to the the socket/server address βverboseβ verbose mode πline_separatorβ ascii code of the line separator, defaults to10, (newline) πΈcolumn_separatorβ ascii code of the column separator, defaults to0(null byte) π«§list_separatorβ ascii code of the list separator, defaults to44(comma) πhost_service_separatorβ ascii code of the host/service separator, defaults to124(pipe) πkeepaliveβ enable keepalive. Default is off πerrors_are_fatalβ errors will die with an error message. Default: on π€warningsβ show warnings; currently only querys withoutColumns:Header will result in a warning πtimeoutβ set a general timeout. Used for connect and querys, no default βquery_timeoutβ set a query timeout. Used for retrieving querys, Default 60sec β±οΈconnect_timeoutβ set a connect timeout. Used for initial connections, default 5sec β‘
If the constructor is only passed a single argument, it is assumed to be a the peer specification. Use either socket OR server, bestie, not both. Pick a lane. π¦
π METHODS π
do
do($statement)
do($statement, %opts)
Send a single statement without fetching the result. Always returns true. no notes, it just does the thing. β
selectall_arrayref
selectall_arrayref($statement)
selectall_arrayref($statement, %opts)
selectall_arrayref($statement, %opts, $limit )
Sends a query and returns an array reference of arrays π
my $arr_refs = $ml->selectall_arrayref("GET hosts");
to get an array of hash references do something like
my $hash_refs = $ml->selectall_arrayref(
"GET hosts", { Slice => {} }
);
to get an array of hash references from the first 2 returned rows only
my $hash_refs = $ml->selectall_arrayref(
"GET hosts", { Slice => {} }, 2
);
you may use limit to limit the result to this number of rows π
column aliases can be defined with a rename hash
my $hash_refs = $ml->selectall_arrayref(
"GET hosts", {
Slice => {},
rename => {
'name' => 'host_name'
}
}
);
selectall_hashref
selectall_hashref($statement, $key_field)
selectall_hashref($statement, $key_field, %opts)
Sends a query and returns a hashref with the given key π
my $hashrefs = $ml->selectall_hashref("GET hosts", "name");
selectcol_arrayref
selectcol_arrayref($statement)
selectcol_arrayref($statement, %opt )
Sends a query an returns an arrayref for the first columns π
my $array_ref = $ml->selectcol_arrayref("GET hosts\nColumns: name");
$VAR1 = [
'localhost',
'gateway',
];
returns an empty array if nothing was found π³οΈ
to get a different column use this
my $array_ref = $ml->selectcol_arrayref(
"GET hosts\nColumns: name contacts",
{ Columns => [2] }
);
you can link 2 columns in a hash result set
my %hash = @{
$ml->selectcol_arrayref(
"GET hosts\nColumns: name contacts",
{ Columns => [1,2] }
)
};
produces a hash with host the contact assosiation π
$VAR1 = {
'localhost' => 'user1',
'gateway' => 'user2'
};
selectrow_array
selectrow_array($statement)
selectrow_array($statement, %opts)
Sends a query and returns an array for the first row π₯
my @array = $ml->selectrow_array("GET hosts");
returns undef if nothing was found πΆπ«οΈ
selectrow_arrayref
selectrow_arrayref($statement)
selectrow_arrayref($statement, %opts)
Sends a query and returns an array reference for the first row π―
my $arrayref = $ml->selectrow_arrayref("GET hosts");
returns undef if nothing was found πΆπ«οΈ
selectrow_hashref
selectrow_hashref($statement)
selectrow_hashref($statement, %opt)
Sends a query and returns a hash reference for the first row π
my $hashref = $ml->selectrow_hashref("GET hosts");
returns undef if nothing was found πΆπ«οΈ
selectscalar_value
selectscalar_value($statement)
selectscalar_value($statement, %opt)
Sends a query and returns a single scalar 1οΈβ£
my $count = $ml->selectscalar_value("GET hosts\nStats: state = 0");
returns undef if nothing was found πΆπ«οΈ
errors_are_fatal
errors_are_fatal()
errors_are_fatal($value)
Enable or disable fatal errors. When enabled the module will confess on any error. π
returns the current setting if called without new value π
warnings
warnings()
warnings($value)
Enable or disable warnings. When enabled the module will carp on warnings. π
returns the current setting if called without new value π
verbose
verbose()
verbose($values)
Enable or disable verbose output. When enabled the module will dump out debug output π£οΈ
returns the current setting if called without new value π
peer_addr
$ml->peer_addr()
returns the current peer address π
when using multiple backends, a list of all addresses is returned in list context π
peer_name
$ml->peer_name()
$ml->peer_name($string)
if new value is set, name is set to this value βοΈ
always returns the current peer name π
when using multiple backends, a list of all names is returned in list context π
peer_key
$ml->peer_key()
returns a uniq key for this peer π
post_processing
$ml->post_processing($result, $options, $keys)
returns postprocessed result. β¨
Useful when using select based io. π§
π¦ QUERY OPTIONS π¦
In addition to the normal query syntax from the livestatus addon, it is possible to set column aliases in various ways. so many ways, pick your fave.
AddPeer
adds the peers name, addr and key to the result set: π·οΈ
my $hosts = $ml->selectall_hashref(
"GET hosts\nColumns: name alias state",
"name",
{ AddPeer => 1 }
);
Backend
send the query only to some specific backends. Only useful when using multiple backends. ποΈ
my $hosts = $ml->selectall_arrayref(
"GET hosts\nColumns: name alias state",
{ Backends => [ 'key1', 'key4' ] }
);
Columns
only return the given column indexes π’
my $array_ref = $ml->selectcol_arrayref(
"GET hosts\nColumns: name contacts",
{ Columns => [2] }
);
see selectcol_arrayref for more examples π
Deepcopy
deep copy/clone the result set. π§¬
Only effective when using multiple backends and threads. This can be safely turned off if you don't change the result set. If you get an error like "Invalid value for shared scalar" error" this should be turned on.
my $array_ref = $ml->selectcol_arrayref(
"GET hosts\nColumns: name contacts",
{ Deepcopy => 1 }
);
Limit
Just like the Limit: <nr> option from livestatus itself. In addition you can add a start,length limit. βοΈ
my $array_ref = $ml->selectcol_arrayref(
"GET hosts\nColumns: name contacts",
{ Limit => "10,20" }
);
This example will return 20 rows starting at row 10. You will get row 10-30. π
Cannot be combined with aLimitinside the query because aLimitwill be added automatically. π«
Adding a limit this way will greatly increase performance and reduce memory usage. π
This option is multibackend safe contrary to the Limit: part of a statement. Sending a statement like GET...Limit: 10 with 3 backends will result in 30 rows. Using this options, you will receive only the first 10 rows. π
Rename
see COLUMN ALIAS for detailed explainaton π·οΈ
Slice
see selectall_arrayref for detailed explainaton π°
Sum
The Sum option only applies when using multiple backends. The values from all backends with be summed up to a total. β
my $stats = $ml->selectrow_hashref(
"GET hosts\nStats: state = 0\nStats: state = 1",
{ Sum => 1 }
);
π COLUMN ALIAS π
In addition to the normal query syntax from the livestatus addon, it is possible to set column aliases in various ways. (yes, this is the same intro as above, they really wanted you to know π )
A valid Columns: Header could look like this:
my $hosts = $ml->selectall_arrayref(
"GET hosts\nColumns: state as status"
);
Stats queries could be aliased too:
my $stats = $ml->selectall_arrayref(
"GET hosts\nStats: state = 0 as up"
);
This syntax is available for: Stats, StatsAnd, StatsOr and StatsGroupBy π
An alternative way to set column aliases is to define rename option key/value pairs:
my $hosts = $ml->selectall_arrayref(
"GET hosts\nColumns: name", {
rename => { 'name' => 'hostname' }
}
);
extract_keys_from_stats_statement
extract_keys_from_stats_statement($statement)
Extract column keys from statement. π
π ERROR HANDLING π
Errorhandling can be done like this:
use Monitoring::Livestatus;
my $ml = Monitoring::Livestatus->new(
socket => '/var/lib/livestatus/livestatus.sock'
);
$ml->errors_are_fatal(0);
my $hosts = $ml->selectall_arrayref("GET hosts");
if($Monitoring::Livestatus::ErrorCode) {
confess($Monitoring::Livestatus::ErrorMessage);
}
turn off the fatal errors, check the error code, confess about it. the full drama arc. π
π SEE ALSO π
For more information about the query syntax and the livestatus plugin installation see the Livestatus page: http://mathias-kettner.de/checkmk_livestatus.html π
π AUTHOR π
Sven Nierlein, 2009-present, <sven@nierlein.org> π
π COPYRIGHT AND LICENSE π
Copyright (C) by Sven Nierlein
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. π«Ά
and that's the whole page bestie, no cap. perl v5.40.1 Β· 2025-05-15 β now go query something iconic. β¨ππ¦
π¦ where this page comes from
- debian trixie: libmonitoring-livestatus-perl 0.86-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmonitoring-livestatus-perl 0.86-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmonitoring-livestatus-perl 0.86-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms