| 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 cloud::microsoft::office365::teams::mode::usersactivity; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use Time::Local; |
| 28 |
|
| 29 |
sub custom_active_perfdata { |
| 30 |
my ($self, %options) = @_; |
| 31 |
|
| 32 |
my %total_options = (); |
| 33 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 34 |
$total_options{total} = $self->{result_values}->{total}; |
| 35 |
$total_options{cast_int} = 1; |
| 36 |
} |
| 37 |
|
| 38 |
if ($self->{result_values}->{report_date} =~ /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/) { |
| 39 |
$self->{output}->perfdata_add(label => 'perfdate', value => timelocal(0,0,12,$3,$2-1,$1-1900)); |
| 40 |
} |
| 41 |
|
| 42 |
$self->{output}->perfdata_add( |
| 43 |
label => 'active_users', |
| 44 |
nlabel => 'teams.users.active.count', |
| 45 |
value => $self->{result_values}->{active}, |
| 46 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options), |
| 47 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options), |
| 48 |
unit => 'users', min => 0, max => $self->{result_values}->{total} |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
sub custom_active_threshold { |
| 53 |
my ($self, %options) = @_; |
| 54 |
|
| 55 |
my $threshold_value = $self->{result_values}->{active}; |
| 56 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 57 |
$threshold_value = $self->{result_values}->{prct_active}; |
| 58 |
} |
| 59 |
my $exit = $self->{perfdata}->threshold_check( |
| 60 |
value => $threshold_value, |
| 61 |
threshold => [ |
| 62 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 63 |
{ label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } |
| 64 |
] |
| 65 |
); |
| 66 |
return $exit; |
| 67 |
} |
| 68 |
|
| 69 |
sub custom_active_output { |
| 70 |
my ($self, %options) = @_; |
| 71 |
|
| 72 |
return sprintf( |
| 73 |
"Active users on %s : %d/%d (%.2f%%)", |
| 74 |
$self->{result_values}->{report_date}, |
| 75 |
$self->{result_values}->{active}, |
| 76 |
$self->{result_values}->{total}, |
| 77 |
$self->{result_values}->{prct_active} |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
sub custom_active_calc { |
| 82 |
my ($self, %options) = @_; |
| 83 |
|
| 84 |
$self->{result_values}->{active} = $options{new_datas}->{$self->{instance} . '_active'}; |
| 85 |
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; |
| 86 |
$self->{result_values}->{report_date} = $options{new_datas}->{$self->{instance} . '_report_date'}; |
| 87 |
$self->{result_values}->{prct_active} = ($self->{result_values}->{total} != 0) ? $self->{result_values}->{active} * 100 / $self->{result_values}->{total} : 0; |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
sub prefix_global_output { |
| 93 |
my ($self, %options) = @_; |
| 94 |
|
| 95 |
return "Total "; |
| 96 |
} |
| 97 |
|
| 98 |
sub prefix_user_output { |
| 99 |
my ($self, %options) = @_; |
| 100 |
|
| 101 |
return "User '" . $options{instance_value}->{name} . "' "; |
| 102 |
} |
| 103 |
|
| 104 |
sub set_counters { |
| 105 |
my ($self, %options) = @_; |
| 106 |
|
| 107 |
$self->{maps_counters_type} = [ |
| 108 |
{ name => 'active', type => 0 }, |
| 109 |
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' }, |
| 110 |
{ name => 'users', type => 1, cb_prefix_output => 'prefix_user_output', message_multiple => 'All users activity are ok' } |
| 111 |
]; |
| 112 |
|
| 113 |
$self->{maps_counters}->{active} = [ |
| 114 |
{ label => 'active-users', set => { |
| 115 |
key_values => [ { name => 'active' }, { name => 'total' }, { name => 'report_date' } ], |
| 116 |
closure_custom_calc => $self->can('custom_active_calc'), |
| 117 |
closure_custom_output => $self->can('custom_active_output'), |
| 118 |
closure_custom_threshold_check => $self->can('custom_active_threshold'), |
| 119 |
closure_custom_perfdata => $self->can('custom_active_perfdata') |
| 120 |
} |
| 121 |
} |
| 122 |
]; |
| 123 |
|
| 124 |
$self->{maps_counters}->{global} = [ |
| 125 |
{ label => 'total-team-chat', nlabel => 'teams.users.messages.team.total.count', set => { |
| 126 |
key_values => [ { name => 'team_chat' } ], |
| 127 |
output_template => 'Team Chat Message Count: %d', |
| 128 |
perfdatas => [ |
| 129 |
{ label => 'total_team_chat', template => '%d', |
| 130 |
min => 0 } |
| 131 |
] |
| 132 |
} |
| 133 |
}, |
| 134 |
{ label => 'total-private-chat', nlabel => 'teams.users.messages.private.total.count', set => { |
| 135 |
key_values => [ { name => 'private_chat' } ], |
| 136 |
output_template => 'Private Chat Message Count: %d', |
| 137 |
perfdatas => [ |
| 138 |
{ label => 'total_private_chat', template => '%d', |
| 139 |
min => 0 } |
| 140 |
] |
| 141 |
} |
| 142 |
}, |
| 143 |
{ label => 'total-call', nlabel => 'teams.users.call.total.count', set => { |
| 144 |
key_values => [ { name => 'call' } ], |
| 145 |
output_template => 'Call Count: %d', |
| 146 |
perfdatas => [ |
| 147 |
{ label => 'total_call', template => '%d', |
| 148 |
min => 0 } |
| 149 |
] |
| 150 |
} |
| 151 |
}, |
| 152 |
{ label => 'total-meeting', nlabel => 'teams.users.meeting.total.count', set => { |
| 153 |
key_values => [ { name => 'meeting' } ], |
| 154 |
output_template => 'Meeting Count: %d', |
| 155 |
perfdatas => [ |
| 156 |
{ label => 'total_meeting', template => '%d', |
| 157 |
min => 0 } |
| 158 |
] |
| 159 |
} |
| 160 |
} |
| 161 |
]; |
| 162 |
|
| 163 |
$self->{maps_counters}->{users} = [ |
| 164 |
{ label => 'team-chat', nlabel => 'teams.users.messages.team.count', set => { |
| 165 |
key_values => [ { name => 'team_chat' }, { name => 'name' } ], |
| 166 |
output_template => 'Team Chat Message Count: %d', |
| 167 |
perfdatas => [ |
| 168 |
{ label => 'team_chat', template => '%d', |
| 169 |
min => 0, label_extra_instance => 1, instance_use => 'name' } |
| 170 |
] |
| 171 |
} |
| 172 |
}, |
| 173 |
{ label => 'private-chat', nlabel => 'teams.users.messages.private.count', set => { |
| 174 |
key_values => [ { name => 'private_chat' }, { name => 'name' } ], |
| 175 |
output_template => 'Private Chat Message Count: %d', |
| 176 |
perfdatas => [ |
| 177 |
{ label => 'private_chat', template => '%d', |
| 178 |
min => 0, label_extra_instance => 1, instance_use => 'name' } |
| 179 |
] |
| 180 |
} |
| 181 |
}, |
| 182 |
{ label => 'call', nlabel => 'teams.users.call.count', set => { |
| 183 |
key_values => [ { name => 'call' }, { name => 'name' } ], |
| 184 |
output_template => 'Call Count: %d', |
| 185 |
perfdatas => [ |
| 186 |
{ label => 'call', template => '%d', |
| 187 |
min => 0, label_extra_instance => 1, instance_use => 'name' } |
| 188 |
] |
| 189 |
} |
| 190 |
}, |
| 191 |
{ label => 'meeting', nlabel => 'teams.users.meeting.count', set => { |
| 192 |
key_values => [ { name => 'meeting' }, { name => 'name' } ], |
| 193 |
output_template => 'Meeting Count: %d', |
| 194 |
perfdatas => [ |
| 195 |
{ label => 'meeting', template => '%d', |
| 196 |
min => 0, label_extra_instance => 1, instance_use => 'name' } |
| 197 |
] |
| 198 |
} |
| 199 |
} |
| 200 |
]; |
| 201 |
} |
| 202 |
|
| 203 |
sub new { |
| 204 |
my ($class, %options) = @_; |
| 205 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 206 |
bless $self, $class; |
| 207 |
|
| 208 |
$options{options}->add_options(arguments => { |
| 209 |
"filter-user:s" => { name => 'filter_user' }, |
| 210 |
"units:s" => { name => 'units', default => '%' }, |
| 211 |
"filter-counters:s" => { name => 'filter_counters', default => 'active|total' } |
| 212 |
}); |
| 213 |
|
| 214 |
return $self; |
| 215 |
} |
| 216 |
|
| 217 |
sub manage_selection { |
| 218 |
my ($self, %options) = @_; |
| 219 |
|
| 220 |
$self->{active} = { active => 0, total => 0, report_date => '' }; |
| 221 |
$self->{global} = { team_chat => 0, private_chat => 0, call => 0, meeting => 0 }; |
| 222 |
$self->{users} = {}; |
| 223 |
|
| 224 |
my $results = $options{custom}->office_get_teams_activity(param => "period='D7'"); |
| 225 |
my $results_daily = []; |
| 226 |
if (scalar(@{$results})) { |
| 227 |
$self->{active}->{report_date} = @{$results}[0]->{'Report Refresh Date'}; |
| 228 |
$results_daily = $options{custom}->office_get_teams_activity(param => "date=" . $self->{active}->{report_date}); |
| 229 |
} |
| 230 |
|
| 231 |
foreach my $user (@{$results}, @{$results_daily}) { |
| 232 |
if (defined($self->{option_results}->{filter_user}) && $self->{option_results}->{filter_user} ne '' && |
| 233 |
$user->{'User Principal Name'} !~ /$self->{option_results}->{filter_user}/) { |
| 234 |
$self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no matching filter name.", debug => 1); |
| 235 |
next; |
| 236 |
} |
| 237 |
|
| 238 |
if ($user->{'Report Period'} != 1) { |
| 239 |
if (!defined($user->{'Last Activity Date'}) || ($user->{'Last Activity Date'} ne $self->{active}->{report_date})) { |
| 240 |
$self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no activity.", debug => 1); |
| 241 |
} |
| 242 |
$self->{active}->{total}++; |
| 243 |
next; |
| 244 |
} |
| 245 |
|
| 246 |
$self->{active}->{active}++; |
| 247 |
|
| 248 |
$self->{global}->{team_chat} += $user->{'Team Chat Message Count'}; |
| 249 |
$self->{global}->{private_chat} += $user->{'Private Chat Message Count'}; |
| 250 |
$self->{global}->{call} += $user->{'Call Count'}; |
| 251 |
$self->{global}->{meeting} += $user->{'Meeting Count'}; |
| 252 |
|
| 253 |
$self->{users}->{$user->{'User Principal Name'}}->{name} = $user->{'User Principal Name'}; |
| 254 |
$self->{users}->{$user->{'User Principal Name'}}->{team_chat} = $user->{'Team Chat Message Count'}; |
| 255 |
$self->{users}->{$user->{'User Principal Name'}}->{private_chat} = $user->{'Private Chat Message Count'}; |
| 256 |
$self->{users}->{$user->{'User Principal Name'}}->{call} = $user->{'Call Count'}; |
| 257 |
$self->{users}->{$user->{'User Principal Name'}}->{meeting} = $user->{'Meeting Count'}; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
1; |
| 262 |
|
| 263 |
__END__ |
| 264 |
|
| 265 |
=head1 MODE |
| 266 |
|
| 267 |
Check users activity (reporting period over the last refreshed day). |
| 268 |
|
| 269 |
(see link for details about metrics : |
| 270 |
https://docs.microsoft.com/en-us/office365/admin/activity-reports/microsoft-teams-user-activity?view=o365-worldwide) |
| 271 |
|
| 272 |
=over 8 |
| 273 |
|
| 274 |
=item B<--filter-user> |
| 275 |
|
| 276 |
Filter users. |
| 277 |
|
| 278 |
=item B<--warning-*> |
| 279 |
|
| 280 |
Warning threshold. |
| 281 |
Can be: 'active-users', 'total-team-chat' (count), 'total-private-chat' (count), |
| 282 |
'total-call' (count), 'total-meeting' (count), 'team-chat' (count), |
| 283 |
'private-chat' (count), 'call' (count), 'meeting' (count). |
| 284 |
|
| 285 |
=item B<--critical-*> |
| 286 |
|
| 287 |
Critical threshold. |
| 288 |
Can be: 'active-users', 'total-team-chat' (count), 'total-private-chat' (count), |
| 289 |
'total-call' (count), 'total-meeting' (count), 'team-chat' (count), |
| 290 |
'private-chat' (count), 'call' (count), 'meeting' (count). |
| 291 |
|
| 292 |
=item B<--filter-counters> |
| 293 |
|
| 294 |
Only display some counters (regexp can be used). |
| 295 |
Example to hide per user counters: --filter-counters='total' |
| 296 |
(default: 'active|total') |
| 297 |
|
| 298 |
=item B<--units> |
| 299 |
|
| 300 |
Unit of thresholds (default: '%') ('%', 'count'). |
| 301 |
|
| 302 |
=back |
| 303 |
|
| 304 |
=cut |