Skip to content

Commit

Permalink
bp: add perfdata to service status aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Nov 22, 2024
1 parent 5e52d46 commit 2b0080b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
11 changes: 2 additions & 9 deletions examples/bp_filter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@ sub add_recursive_output_filter {
$args->{'extra'}->{'long_output'} = $text;
return;
}
sub _add_recursive_output_filter_clean_status {
my($text, $keeplongoutput) = @_;
chomp($text);
$text =~ s/\|.*$//gmx;
return($text // "") if $keeplongoutput;
return((split(/\n|\\+n/mx, $text, 2))[0] // "");
}
sub _add_recursive_output_filter_indent {
my($indent, $text) = @_;
my $prefix = (chr(8194) x ($indent*3));
Expand All @@ -143,7 +136,7 @@ sub _add_recursive_output_filter_recurse {
# add node itself
my @lines;
if($indent >= 0) {
@lines = split(/\n|\\+n/mx, '- ['.($node->{'label'} // '').'] '._add_recursive_output_filter_clean_status($node->{'status_text'} || $node->{'short_desc'}, 1));
@lines = split(/\n|\\+n/mx, '- ['.($node->{'label'} // '').'] '.Thruk::BP::Utils::clean_status_text($node->{'status_text'} || $node->{'short_desc'}, 1));
my $firstline = shift @lines;
$text .= _add_recursive_output_filter_indent($indent, $firstline)."\n";
if($node->{'function'} eq 'statusfilter') {
Expand Down Expand Up @@ -173,7 +166,7 @@ sub _add_recursive_output_filter_recurse {
$text = _add_recursive_output_filter_recurse($c, $text, $link_bp->[0], $first_node, $indent+1, $parents);
} else {
# remote bp
my @lines = split(/\n|\\+n/mx, _add_recursive_output_filter_clean_status($node->{'status_text'} || $node->{'short_desc'}, 1));
my @lines = split(/\n|\\+n/mx, Thruk::BP::Utils::clean_status_text($node->{'status_text'} || $node->{'short_desc'}, 1));
shift @lines;
for my $line (@lines) {
$text .= _add_recursive_output_filter_indent($indent+1, $line)."\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ sub worst {
}
return($state,
'worst of',
Thruk::Utils::Filter::state2text($state).' - Worst state is '.Thruk::Utils::Filter::state2text($state).': '.Thruk::BP::Utils::join_labels($nodes, $state),
Thruk::Utils::Filter::state2text($state)
.' - Worst state is '.Thruk::Utils::Filter::state2text($state)
.': '.Thruk::BP::Utils::join_labels($nodes, $state)
.'| '._nodes_perfdata($depends),
$extra,
);
}
Expand All @@ -272,7 +275,10 @@ sub best {
}
return($state,
'best of',
Thruk::Utils::Filter::state2text($state).' - Best state is '.Thruk::Utils::Filter::state2text($state).': '.Thruk::BP::Utils::join_labels($nodes, $state),
Thruk::Utils::Filter::state2text($state)
.' - Best state is '.Thruk::Utils::Filter::state2text($state)
.': '.Thruk::BP::Utils::join_labels($nodes, $state)
.'| '._nodes_perfdata($depends),
$extra,
);
}
Expand Down Expand Up @@ -526,6 +532,7 @@ sub statusfilter {
}
if($type eq 'services' || $type eq 'both') {
push @{$thresholdoutput}, sprintf("%d/%d services up", $good_services, $total_services);
$perfdata .= ' ' if $perfdata;
$perfdata .= 'services_up='.$good_services.' services_down='.$down_services;
}

Expand Down Expand Up @@ -758,6 +765,30 @@ sub _get_nodes_extra {
return($extra);
}

##########################################################
sub _nodes_perfdata {
my($nodes) = @_;
my($ok, $warning, $unknown, $critical, $pending) = (0, 0, 0, 0, 0);

for my $n (@{$nodes}) {
if( $n->{'status'} == 0) { $ok++; }
elsif($n->{'status'} == 1) { $warning++; }
elsif($n->{'status'} == 2) { $critical++; }
elsif($n->{'status'} == 3) { $unknown++; }
elsif($n->{'status'} == 4) { $pending++; }
}

my $res = sprintf(
"services_ok=%d services_warning=%d services_unknown=%d services_critical=%d services_pending=%d",
$ok,
$warning,
$unknown,
$critical,
$pending,
);
return($res);
}

##########################################################

1;
19 changes: 18 additions & 1 deletion plugins/plugins-available/business_process/lib/Thruk/BP/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ sub join_labels {
for my $n (@{$nodes}) {
my $firstline = "[".$n->{'label'}."] ".Thruk::Utils::Filter::state2text($state);
$firstline .= " - ".(split(/\n/mx, $n->{'status_text'}))[0] if $n->{'status_text'};
$long .= "\n- ".$firstline;
$long .= "\n- ".clean_status_text($firstline);
}
}
if($num == 1) {
Expand Down Expand Up @@ -891,4 +891,21 @@ sub _update_index {

##########################################################

=head2 clean_status_text
clean_status_text($text)
returns text and removes performance data
=cut
sub clean_status_text {
my($text, $keeplongoutput) = @_;
chomp($text);
$text =~ s/\|.*$//gmx;
return($text // "") if $keeplongoutput;
return((split(/\n|\\+n/mx, $text, 2))[0] // "");
}

##########################################################

1;

0 comments on commit 2b0080b

Please sign in to comment.