centreon-plugins
centreon-plugins copied to clipboard
new(monitoring/logs): add common code and plugin to parse logs
Adds a plugin to parse a log file locally, through SSH or on a CIFS/Samba share, or a web page. This plugin is really simple and awaits contributions to give it more features. It is not made to replace check_logfiles though (or is it?). The more important thing is the common code that can be used by anyone to build their own modes that will parse exotic logs. It moves the cifs lib binding, hence the plugin and packaging modifications.
Example of plugin usage:
perl centreon_plugins.pl --plugin=apps::monitoring::logs::plugin --mode=parse --custommode=file --file='/var/log/centreon-broker/central-broker-master.log' --parse-regexp='\[(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).*\]\s\[.*\]\s\[.*\]\s(.*)' --parse-mapping='date=$1' --parse-mapping='message=$2' --date-regexp='(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})' --date-mapping='year=$1' --date-mapping='month=$2' --date-mapping='day=$3' --date-mapping='hour=$4' --date-mapping='minute=$5' --date-mapping='second=$6' --memory --critical-status='%{message} =~ /Duplicate entry/'
CRITICAL: 4 problem(s) detected | 'logs.total.count'=39;;;0; 'logs.problem.count'=4;;;0;
Example of plugin and mode using the common code:
package apps::myapp::logs::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ( $class, %options ) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{modes} = {
'sessions' => 'apps::myapp::logs::mode::sessions'
};
$self->{custom_modes}->{file} = 'centreon::common::monitoring::logs::custom::file';
$self->{custom_modes}->{cifs} = 'centreon::common::monitoring::logs::custom::cifs';
return $self;
}
1;
package apps::myapp::logs::mode::sessions;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::common::monitoring::logs::read;
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0 }
];
$self->{maps_counters}->{global} = [
{ label => 'sessions', nlabel => 'sessions.current.count', set => {
key_values => [ { name => 'sessions' } ],
output_template => 'Current sessions : %d',
perfdatas => [
{ template => '%d', min => 0 }
]
}
},
{ label => 'removed', nlabel => 'sessions.removed.count', set => {
key_values => [ { name => 'removed' } ],
output_template => 'Removed sessions : %d',
perfdatas => [
{ template => '%d', min => 0 }
]
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $data = centreon::common::monitoring::logs::read::parse(
%options,
parse_regexp => '\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2},\d{3}\s\S{6}\s\{\}\s+\S+\s+\S+\s+(.*)',
parse_matching => ['message=$1']
);
foreach my $entry (reverse @{$data}) {
next if (defined($entry->{message}) && $entry->{message} ne '' && $entry->{message} !~ /SessionController: checked expiration on (\d+) sessions \(removed (\d+)\)/);
$self->{global}->{sessions} = $1 - $2;
$self->{global}->{removed} = $2;
last;
}
}
1;
__END__
=head1 MODE
Check current sessions.
=over 8
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'current', 'removed'.
=back
=cut