File: getchart.pm

package info (click to toggle)
centreon-plugins 0.0~20221017-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 30,580 kB
  • sloc: perl: 353,497; makefile: 130; xml: 49; python: 35; ansic: 24; sh: 10
file content (193 lines) | stat: -rw-r--r-- 6,015 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#
# Copyright 2022 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and alarm monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

package apps::monitoring::netdata::restapi::mode::getchart;

use base qw(centreon::plugins::templates::counter);

use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);

sub custom_metric_perfdata {
    my ($self, %options) = @_;

    $self->{output}->perfdata_add(
        label => $self->{result_values}->{perf_label},
        value => $self->{result_values}->{value},
        unit  => $self->{result_values}->{unit},
        warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-metric'),
        critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-metric')
    );
}

sub custom_metric_threshold {
    my ($self, %options) = @_;

    my $exit = $self->{perfdata}->threshold_check(
        value => $self->{result_values}->{value},
        threshold => [
            { label => 'critical-metric', exit_litteral => 'critical' },
            { label => 'warning-metric', exit_litteral => 'warning' }
        ]
    );
    return $exit;
}

sub custom_metric_output {
    my ($self, %options) = @_;

    return sprintf(
        "Metric '%s' value is %.2f %s",
        $self->{result_values}->{display}, $self->{result_values}->{value}, $self->{result_values}->{unit}
    );
}

sub set_counters {
    my ($self, %options) = @_;

    $self->{maps_counters_type} = [
        { name => 'metric', type => 1, message_multiple => 'All metrics are ok' }
    ];

    $self->{maps_counters}->{metric} = [
        { label => 'metric', set => {
                key_values => [ { name => 'value' }, { name => 'perf_label' }, { name => 'display' }, { name => 'unit' } ],
                closure_custom_output => $self->can('custom_metric_output'),
                closure_custom_perfdata => $self->can('custom_metric_perfdata'),
                closure_custom_threshold_check => $self->can('custom_metric_threshold')
            }
        }
    ];
}

sub new {
    my ($class, %options) = @_;
    my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
    bless $self, $class;

    $options{options}->add_options(arguments => {
          'chart-name:s'       => { name => 'chart_name' },
          'chart-period:s'     => { name => 'chart_period', default => '300' },
          'chart-statistics:s' => { name => 'chart_statistics', default => 'average' },
          'filter-metric:s'    => { name => 'filter_metric' }
    });

    return $self;
}

sub check_options {
    my ($self, %options) = @_;
    $self->SUPER::check_options(%options);

    if (!defined($self->{option_results}->{chart_name}) || $self->{option_results}->{chart_name} eq '') {
       $self->{output}->add_option_msg(short_msg => "Missing --chart-name option or value.");
       $self->{output}->option_exit();
    }
}

sub manage_selection {
    my ($self, %options) = @_;
    my $dimensions = '';

    if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne '') {
        $dimensions = $self->{option_results}->{filter_metric}
    };

    my $unit = $options{custom}->get_chart_properties(chart => $self->{option_results}->{chart_name}, filter_info => 'units');
    my $result = $options{custom}->get_data(
        chart => $self->{option_results}->{chart_name},
        points => $self->{option_results}->{chart_point},
        after_period => $self->{option_results}->{chart_period},
        group => $self->{option_results}->{chart_statistics},
        dimensions => $dimensions
    );

    my $chart_name = $self->{option_results}->{chart_name};
    my $stat = $self->{option_results}->{chart_statistics};

    $self->{metrics}->{$chart_name} = {};
    # we remove the first time value
    shift @{$result->{labels}};
    foreach my $chart_value (@{$result->{data}}) {
        shift(@$chart_value);
        foreach my $chart_label (@{$result->{labels}}) {
            $self->{metrics}->{$chart_name}->{$chart_label} = shift(@$chart_value);
        }
    }

    foreach my $metric (keys %{$self->{metrics}->{$chart_name}}) {
        $self->{metric}->{$metric . '_' . $stat} = {
            display => $metric . '_' . $stat,
            value => $self->{metrics}->{$chart_name}->{$metric},
            unit => $unit,
            perf_label => $metric . '_' . $stat
        };
    }
};

1;

__END__

=head1 MODE

Get data for charts available on the Netdata RestAPI.

Example:
perl centreon_plugins.pl --plugin=apps::monitoring::netdata::restapi::plugin
--mode=get-chart --hostname=10.0.0.1 --chart-name='system.cpu' --filter-metric=iowait

More information on'https://learn.netdata.cloud/docs/agent/web/api'.

=over 8

=item B<--chart-name>

The Netdata chart name to query
This option is mandatory

=item B<--chart-period>

The period in seconds on which the values are calculated
Default: 300

=item B<--chart-statistics>

The statistic calculation method used to parse the collected data.
Can be : average, sum, min, max
Default: average

=item B<--filter-metric>

Filter on specific chart metric.
By default, all the metrics will be displayed

=item B<--warning-metric>

Warning threshold (global to all the collected metrics)

=item B<--critical-metric>

Critical threshold (global to all the collected metrics)

=back

=cut