change session windows mode

This commit is contained in:
qgarnier 2017-09-14 11:03:48 +02:00
parent b57675c48b
commit 88446f9e2b
4 changed files with 317 additions and 207 deletions

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<qwinsta language="en">
<created>Total sessions created</created>
<disconnected>Total sessions disconnected</disconnected>
<reconnected>Total sessions reconnected</reconnected>
<activestate>Active</activestate>
<header_sessionname>SESSIONNAME</header_sessionname>
<header_state>STATE</header_state>
</qwinsta>
<qwinsta language="fr">
<created>Nombre total de sessions c.*?s</created>
<disconnected>Nombre total de sessions d.*?connect.*?es</disconnected>
<reconnected>Nombre total de sessions reconnect.*?es</reconnected>
<activestate>Actif</activestate>
<header_sessionname>SESSION</header_sessionname>
<header_state>^.*?TAT</header_state>
</qwinsta>
</root>

View File

@ -1,206 +0,0 @@
#
# Copyright 2017 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application 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 os::windows::local::mode::rdpsessions;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
use Win32::OLE;
use DateTime;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"warning-inactive:s" => { name => 'warning_inactive' },
"critical-inactive:s" => { name => 'critical_inactive' },
"warning-active:s" => { name => 'warning_active' },
"critical-active:s" => { name => 'critical_active' },
"warning-time:s" => { name => 'warning_time' },
"critical-time:s" => { name => 'critical_time' },
});
$self->{os_is2003} = 0;
$self->{os_is2008} = 0;
$self->{os_is2012} = 0;
$self->{result_sessions} = {};
$self->{result_detailed} = {};
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
foreach my $label (('warning_inactive', 'critical_inactive', 'warning_active', 'critical_active', 'warning_time', 'critical_time')) {
if (($self->{perfdata}->threshold_validate(label => $label, value => $self->{option_results}->{$label})) == 0) {
my ($label_opt) = $label;
$label_opt =~ tr/_/-/;
$self->{output}->add_option_msg(short_msg => "Wrong " . $label_opt . " threshold '" . $self->{option_results}->{$label} . "'.");
$self->{output}->option_exit();
}
}
}
sub check_version {
my ($self, %options) = @_;
my ($ver_string, $ver_major, $ver_minor, $ver_build, $ver_id) = Win32::GetOSVersion();
#"Operating system is " . "$ver_string - ($ver_id.$ver_major.$ver_minor.$ver_build)\n";
# 5.1, 5.2 => XP/2003
# 6.0, 6.1 => Vista/7/2008
# 6.2, 6.3 => 2012
if ($ver_major == 5 && ($ver_minor == 1 || $ver_minor == 2)) {
$self->{os_is2003} = 1;
} elsif ($ver_major == 6 && ($ver_minor == 0 || $ver_minor == 1)) {
$self->{os_is2008} = 1;
} elsif ($ver_major == 6 && ($ver_minor == 2 || $ver_minor == 3)) {
$self->{os_is2012} = 1;
} else {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'OS version ' . $ver_major . '.' . $ver_minor . ' not managed.');
return 1;
}
return 0;
}
sub get_sessions {
my ($self, %options) = @_;
$self->{wmi} = Win32::OLE->GetObject('winmgmts:root\CIMV2');
if (!defined($self->{wmi})) {
$self->{output}->add_option_msg(short_msg => "Cant create server object:" . Win32::OLE->LastError());
$self->{output}->option_exit();
}
my $query = 'SELECT ActiveSessions, InactiveSessions FROM Win32_PerfRawData_LocalSessionManager_TerminalServices';
if ($self->{os_is2003} == 1) {
$query = 'SELECT ActiveSessions, InactiveSessions FROM Win32_PerfRawData_TermService_TerminalServices ';
}
my $resultset = $self->{wmi}->ExecQuery($query);
foreach my $obj (in $resultset) {
$self->{result_sessions}->{active_sessions} = $obj->{ActiveSessions};
if ($self->{os_is2003} == 1) {
$self->{result_sessions}->{inactive_sessions} = $obj->{InactiveSessions} - 1; # Console session
} else {
$self->{result_sessions}->{inactive_sessions} = $obj->{InactiveSessions} - 2; # Service and Console sessions
}
}
}
sub get_detailed_informations {
my ($self, %options) = @_;
my $query = 'SELECT LogonId, StartTime FROM Win32_LogonSession WHERE LogonType = 10';
my $resultset = $self->{wmi}->ExecQuery($query);
foreach my $obj (in $resultset) {
my $resultset2 = $self->{wmi}->ExecQuery('Associators of {Win32_LogonSession.LogonId=' .
$obj->{LogonId} . '} Where AssocClass=Win32_LoggedOnUser Role=Dependent');
foreach my $obj2 (in $resultset2) {
$obj->{StartTime} =~ /^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})\.[0-9]+([+\-])(.*)/;
my ($year, $month, $day, $hour, $min, $sec, $tz, $tz_min) = ($1, $2, $3, $4, $5, $6, $7, $8);
my $tz_time = sprintf("%02d%02d", $tz_min / 60, $tz_min % 60);
my $dt = DateTime->new(
year => $year,
month => $month,
day => $day,
hour => $hour,
minute => $min,
second => $sec,
time_zone => $tz . $tz_time,
);
$self->{result_detailed}->{$obj2->{Domain} . '\\' . $obj2->{Name}} = $dt->epoch;
}
}
}
sub manage {
my ($self, %options) = @_;
my $exit = $self->{perfdata}->threshold_check(value => $self->{result_sessions}->{active_sessions}, threshold => [ { label => 'critical_active', exit_litteral => 'critical' }, { label => 'warning_active', exit_litteral => 'warning' } ]);
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("%d active session(s)", $self->{result_sessions}->{active_sessions}));
$exit = $self->{perfdata}->threshold_check(value => $self->{result_sessions}->{inactive_sessions}, threshold => [ { label => 'critical_inactive', exit_litteral => 'critical' }, { label => 'warning_inactive', exit_litteral => 'warning' } ]);
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("%d inactive session(s)", $self->{result_sessions}->{inactive_sessions}));
foreach my $user (sort keys %{$self->{result_detailed}}) {
$exit = $self->{perfdata}->threshold_check(value => time() - $self->{result_detailed}->{$user}, threshold => [ { label => 'critical_time', exit_litteral => 'critical' }, { label => 'warning_time', exit_litteral => 'warning' } ]);
$self->{output}->output_add(long_msg => sprintf("User %s session opened since %s", $user, scalar(localtime($self->{result_detailed}->{$user}))));
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("User %s session opened since %s", $user, scalar(localtime($self->{result_detailed}->{$user}))));
}
}
$self->{output}->perfdata_add(label => 'active_sessions',
value => $self->{result_sessions}->{active_sessions},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning_active'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical_active'),
min => 0, max => $self->{result_sessions}->{active_sessions} + $self->{result_sessions}->{inactive_sessions});
$self->{output}->perfdata_add(label => 'inactive_sessions',
value => $self->{result_sessions}->{inactive_sessions},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning_inactive'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical_inactive'),
min => 0, max => $self->{result_sessions}->{active_sessions} + $self->{result_sessions}->{inactive_sessions});
}
sub run {
my ($self, %options) = @_;
if ($self->check_version() == 0) {
$self->get_sessions();
$self->get_detailed_informations();
$self->manage();
}
$self->{output}->display();
$self->{output}->exit();
}
1;
__END__
=head1 MODE
Check rdp sessions.
=over 8
=item B<--warning-*>
Threshold warning.
Can be: 'inactive', 'active', 'time' (in seconds since the session starts).
=item B<--critical-*>
Threshold critical.
Can be: 'inactive', 'active', 'time' (in seconds since the session starts).
=back
=cut

View File

@ -0,0 +1,297 @@
#
# Copyright 2017 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application 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 os::windows::local::mode::sessions;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use Digest::MD5 qw(md5_hex);
use XML::Simple;
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', },
];
$self->{maps_counters}->{global} = [
{ label => 'sessions-created', set => {
key_values => [ { name => 'sessions_created', diff => 1 } ],
output_template => 'Created : %s',
perfdatas => [
{ label => 'sessions_created', value => 'sessions_created_absolute', template => '%s', min => 0 },
],
}
},
{ label => 'sessions-disconnected', set => {
key_values => [ { name => 'sessions_disconnected', diff => 1 } ],
output_template => 'Disconnected : %s',
perfdatas => [
{ label => 'sessions_disconnected', value => 'sessions_disconnected_absolute', template => '%s', min => 0 },
],
}
},
{ label => 'sessions-reconnected', set => {
key_values => [ { name => 'sessions_reconnected', diff => 1 } ],
output_template => 'Reconnected : %s',
perfdatas => [
{ label => 'sessions_reconnected', value => 'sessions_reconnected_absolute', template => '%s', min => 0 },
],
}
},
{ label => 'sessions-active', set => {
key_values => [ { name => 'sessions_active' } ],
output_template => 'Active : %s',
perfdatas => [
{ label => 'sessions_active', value => 'sessions_active_absolute', template => '%s', min => 0 },
],
}
},
];
}
sub prefix_global_output {
my ($self, %options) = @_;
return "Sessions ";
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"command:s" => { name => 'command', default => 'qwinsta' },
"command-path:s" => { name => 'command_path' },
"command-options:s" => { name => 'command_options', default => '/COUNTER' },
"timeout:s" => { name => 'timeout', default => 30 },
"filter-sessionname:s" => { name => 'filter_sessionname' },
"config:s" => { name => 'config' },
"language:s" => { name => 'language', default => 'en' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if (defined($self->{option_results}->{config})) {
$self->{config_file} = $self->{option_results}->{config};
} else {
$self->{output}->add_option_msg(short_msg => "Need to specify config file option.");
$self->{output}->option_exit();;
}
}
sub read_config {
my ($self, %options) = @_;
my $content_file = do {
local $/ = undef;
if (!open my $fh, "<", $self->{option_results}->{config}) {
$self->{output}->add_option_msg(short_msg => "Could not open file $self->{option_results}->{config} : $!");
$self->{output}->option_exit();
}
<$fh>;
};
my $content;
eval {
$content = XMLin($content_file, ForceArray => ['qwinsta'], KeyAttr => ['language']);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@");
$self->{output}->option_exit();
}
if (!defined($content->{qwinsta}->{$self->{option_results}->{language}})) {
$self->{output}->add_option_msg(short_msg => "Cannot find language '$self->{option_results}->{language}' in config file");
$self->{output}->option_exit();
}
return $content->{qwinsta}->{$self->{option_results}->{language}};
}
sub read_qwinsta {
my ($self, %options) = @_;
$self->{output}->output_add(long_msg => $options{stdout}, debug => 1);
if ($options{stdout} !~ /^(.*?)$options{config}->{created}/si) {
$self->{output}->add_option_msg(short_msg => "Cannot find information in command output");
$self->{output}->option_exit();
}
my $sessions = $1;
my @lines = split /\n/, $sessions;
my $header = shift @lines;
my @position_wrap = ();
while ($header =~ /(\s+(\S+))/g) {
push @position_wrap, { begin => $-[1], word_begin => $-[2], end => $+[1], label => $2 };
}
my $session_data = [];
foreach my $line (@lines) {
my $data = {};
for (my $pos = 0; $pos <= $#position_wrap; $pos++) {
my $area;
if (length($line) < $position_wrap[$pos]->{begin}) {
$area = '';
} else {
if ($pos + 1 <= $#position_wrap) {
$area = substr($line, $position_wrap[$pos]->{begin}, ($position_wrap[$pos]->{end} - $position_wrap[$pos]->{begin}) + ($position_wrap[$pos + 1]->{word_begin} - $position_wrap[$pos]->{end}));
} else {
$area = substr($line, $position_wrap[$pos]->{begin});
}
}
$data->{$position_wrap[$pos]->{label}} = '-';
while ($area =~ /([^\s]+)/g) {
if (($-[1] >= $position_wrap[$pos]->{word_begin} - $position_wrap[$pos]->{begin} && $-[1] <= $position_wrap[$pos]->{end} - $position_wrap[$pos]->{begin})
||
($+[1] >= $position_wrap[$pos]->{word_begin} - $position_wrap[$pos]->{begin} && $+[1] <= $position_wrap[$pos]->{end} - $position_wrap[$pos]->{begin})) {
$data->{$position_wrap[$pos]->{label}} = $1;
last;
}
}
}
push @$session_data, $data;
}
return $session_data;
}
sub read_qwinsta_counters {
my ($self, %options) = @_;
my $counters = {};
$counters->{sessions_created} = $1
if ($options{stdout} =~ /$options{config}->{created}.*?(\d+)/si);
$counters->{sessions_disconnected} = $1
if ($options{stdout} =~ /$options{config}->{disconnected}.*?(\d+)/si);
$counters->{sessions_reconnected} = $1
if ($options{stdout} =~ /$options{config}->{reconnected}.*?(\d+)/si);
return $counters;
}
sub manage_selection {
my ($self, %options) = @_;
my $config = $self->read_config();
my ($stdout) = centreon::plugins::misc::execute(output => $self->{output},
options => $self->{option_results},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options});
my $datas = $self->read_qwinsta(stdout => $stdout, config => $config);
my $counters = $self->read_qwinsta_counters(stdout => $stdout, config => $config);
my $active = 0;
foreach my $session (@$datas) {
if (defined($self->{option_results}->{filter_sessionname}) && $self->{option_results}->{filter_sessionname} ne '' &&
$session->{$config->{header_sessionname}} !~ /$self->{option_results}->{filter_sessionname}/) {
$self->{output}->output_add(long_msg => "skipping '" . $session->{$config->{header_sessionname}} . "': no matching filter.", debug => 1);
next;
}
my $matching = 0;
foreach my $label (keys %$session) {
$matching = 1 if ($label =~ /$config->{header_state}/ &&
$session->{$label} =~ /$config->{activestate}/);
}
if ($matching == 1) {
$active++;
my $output = '';
$output .= " [$_ => $session->{$_}]" for (sort keys %$session);
$self->{output}->output_add(long_msg => $output);
}
}
$self->{global} = { %$counters, sessions_active => $active };
$self->{cache_name} = "windows_" . $self->{mode} . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
}
1;
__END__
=head1 MODE
Check sessions.
=over 8
=item B<--config>
command can be localized by using a configuration file.
This parameter can be used to specify an alternative location for the configuration file
=item B<--language>
Set the language used in config file (default: 'en').
=item B<--command>
Command to get information (Default: 'qwinsta').
Can be changed if you have output in a file.
=item B<--command-path>
Command path (Default: none).
=item B<--command-options>
Command options (Default: '/COUNTER').
=item B<--timeout>
Timeout in seconds for the command (Default: 30).
=item B<--filter-sessionname>
Filter session name (can be a regexp).
=item B<--warning-*>
Threshold warning.
Can be: 'inactive', 'active'.
=item B<--critical-*>
Threshold critical.
Can be: 'inactive', 'active', 'time' (in seconds since the session starts).
=back
=cut

View File

@ -31,7 +31,7 @@ sub new {
$self->{version} = '0.1';
%{$self->{modes}} = (
'rdp-sessions' => 'os::windows::local::mode::rdpsessions',
'sessions' => 'os::windows::local::mode::sessions',
'time' => 'os::windows::local::mode::ntp',
);