Debian Codegraph @

src/network/paloalto/ssh/mode/licenses.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package network::paloalto::ssh::mode::licenses;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use DateTime;
28 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 my $msg = sprintf("expired status is '%s'", $self->{result_values}->{expired});
34 if ($self->{result_values}->{expiry_date} eq 'never') {
35 $msg .= ', never expires';
36 } else {
37 $msg .= sprintf(
38 ", expires in %d days [%s]",
39 $self->{result_values}->{expiry_days},
40 $self->{result_values}->{expiry_date}
41 );
42 }
43
44 return $msg;
45 }
46
47 sub custom_status_calc {
48 my ($self, %options) = @_;
49
50 $self->{result_values}->{feature} = $options{new_datas}->{$self->{instance} . '_feature'};
51 $self->{result_values}->{expired} = $options{new_datas}->{$self->{instance} . '_expired'};
52 $self->{result_values}->{expiry_date} = $options{new_datas}->{$self->{instance} . '_expiry_date'};
53 $self->{result_values}->{expiry_seconds} = $options{new_datas}->{$self->{instance} . '_expiry_seconds'};
54 $self->{result_values}->{expiry_days} = ($self->{result_values}->{expiry_seconds} ne '') ? $self->{result_values}->{expiry_seconds} / 86400 : -1;
55 return 0;
56 }
57
58 sub set_counters {
59 my ($self, %options) = @_;
60
61 $self->{maps_counters_type} = [
62 { name => 'features', type => 1, cb_prefix_output => 'prefix_feature_output', message_multiple => 'All features licensing are ok' },
63 ];
64
65 $self->{maps_counters}->{features} = [
66 { label => 'status', type => 2, critical_default => '%{expired} eq "yes"', set => {
67 key_values => [ { name => 'feature' }, { name => 'expired' }, { name => 'expiry_date' }, { name => 'expiry_seconds' } ],
68 closure_custom_calc => $self->can('custom_status_calc'),
69 closure_custom_output => $self->can('custom_status_output'),
70 closure_custom_perfdata => sub { return 0; },
71 closure_custom_threshold_check => \&catalog_status_threshold_ng
72 }
73 },
74 ];
75 }
76
77 sub prefix_feature_output {
78 my ($self, %options) = @_;
79
80 return "Feature '" . $options{instance_value}->{feature} . "' ";
81 }
82
83 sub new {
84 my ($class, %options) = @_;
85 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
86 bless $self, $class;
87
88 $options{options}->add_options(arguments => {
89 'filter-feature:s' => { name => 'filter_feature' }
90 });
91
92 return $self;
93 }
94
95 sub manage_selection {
96 my ($self, %options) = @_;
97
98 my $result = $options{custom}->execute_command(command => 'request license info', ForceArray => ['entry']);
99
100 my $months = {
101 january => 1, february => 2, march => 3, april => 4, may => 5, june => 6,
102 july => 7, august => 8, september => 9, october => 10, november => 11, december => 12
103 };
104
105 $self->{features} = {};
106 foreach my $feature (@{$result->{licenses}->{entry}}) {
107 $feature->{expires} = lc($feature->{expires});
108
109 if (defined($self->{option_results}->{filter_feature}) && $self->{option_results}->{filter_feature} ne '' &&
110 $feature->{feature} !~ /$self->{option_results}->{filter_feature}/) {
111 $self->{output}->output_add(long_msg => "skipping '" . $feature->{feature} . "': no matching feature.", debug => 1);
112 next;
113 }
114
115 # January 30, 2022
116 my $dt;
117 if ($feature->{expires} =~ /^(\w+)\s+(\d+).*?(\d+)$/) {
118 $dt = DateTime->new(year => $3, month => $months->{$1}, day => $2);
119 }
120
121 $self->{features}->{ $feature->{feature} } = {
122 feature => $feature->{feature},
123 expired => $feature->{expired},
124 expiry_date => $feature->{expires},
125 expiry_seconds => $feature->{expires} ne 'never' ? $dt->epoch - time() : ''
126 };
127 }
128
129 if (scalar(keys %{$self->{features}}) <= 0) {
130 $self->{output}->add_option_msg(short_msg => "No features found.");
131 $self->{output}->option_exit();
132 }
133 }
134
135 1;
136
137 __END__
138
139 =head1 MODE
140
141 Check features licensing.
142
143 =over 8
144
145 =item B<--filter-feature>
146
147 Filter license by feature (can be a regexp).
148
149 =item B<--warning-status>
150
151 Define the conditions to match for the status to be WARNING.
152 Can use special variables like: %{expired}, %{expiry_days}, %{feature}
153
154 =item B<--critical-status>
155
156 Define the conditions to match for the status to be CRITICAL. (default: '%{expired} eq "yes"').
157 Can use special variables like: %{expired}, %{expiry_days}, %{feature}
158
159 =back
160
161 =cut