Debian Codegraph @

src/os/windows/wsman/mode/sessions.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 os::windows::wsman::mode::sessions;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5 qw(md5_hex);
28 use XML::Simple;
29
30 sub set_counters {
31 my ($self, %options) = @_;
32
33 $self->{maps_counters_type} = [
34 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', },
35 ];
36
37 $self->{maps_counters}->{global} = [
38 { label => 'sessions-created', nlabel => 'sessions.created.total.count', set => {
39 key_values => [ { name => 'sessions_created', diff => 1 } ],
40 output_template => 'created: %s',
41 perfdatas => [
42 { label => 'sessions_created', template => '%s', min => 0 }
43 ]
44 }
45 },
46 { label => 'sessions-disconnected', nlabel => 'sessions.disconnected.total.count', set => {
47 key_values => [ { name => 'sessions_disconnected', diff => 1 } ],
48 output_template => 'disconnected: %s',
49 perfdatas => [
50 { label => 'sessions_disconnected', template => '%s', min => 0 }
51 ]
52 }
53 },
54 { label => 'sessions-reconnected', nlabel => 'sessions.reconnected.total.count', set => {
55 key_values => [ { name => 'sessions_reconnected', diff => 1 } ],
56 output_template => 'reconnected : %s',
57 perfdatas => [
58 { label => 'sessions_reconnected', template => '%s', min => 0 }
59 ]
60 }
61 },
62 { label => 'sessions-active', nlabel => 'sessions.active.current.count', set => {
63 key_values => [ { name => 'sessions_active' } ],
64 output_template => 'current active : %s',
65 perfdatas => [
66 { label => 'sessions_active', template => '%s', min => 0 }
67 ]
68 }
69 },
70 { label => 'sessions-disconnected-current', nlabel => 'sessions.disconnected.current.count', set => {
71 key_values => [ { name => 'sessions_disconnected_current' } ],
72 output_template => 'current disconnected : %s',
73 perfdatas => [
74 { label => 'sessions_disconnected_current', template => '%s', min => 0 }
75 ]
76 }
77 }
78 ];
79 }
80
81 sub prefix_global_output {
82 my ($self, %options) = @_;
83
84 return "Sessions ";
85 }
86
87 sub new {
88 my ($class, %options) = @_;
89 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
90 bless $self, $class;
91
92 $options{options}->add_options(arguments => {
93 'command:s' => { name => 'command', default => 'qwinsta' },
94 'command-path:s' => { name => 'command_path' },
95 'command-options:s' => { name => 'command_options', default => '/COUNTER' },
96 'timeout:s' => { name => 'timeout', default => 30 },
97 'filter-sessionname:s' => { name => 'filter_sessionname' },
98 'config:s' => { name => 'config' },
99 'language:s' => { name => 'language', default => 'en' }
100 });
101
102 return $self;
103 }
104
105 sub read_config {
106 my ($self, %options) = @_;
107
108 my $content_file = <<'END_FILE';
109 <?xml version="1.0" encoding="UTF-8"?>
110 <root>
111 <qwinsta language="en">
112 <created>Total sessions created</created>
113 <disconnected>Total sessions disconnected</disconnected>
114 <reconnected>Total sessions reconnected</reconnected>
115 <activestate>Active</activestate>
116 <disconnectedstate>Disc</disconnectedstate>
117 <header_sessionname>SESSIONNAME</header_sessionname>
118 <header_state>STATE</header_state>
119 </qwinsta>
120 <qwinsta language="fr">
121 <created>Nombre total de sessions c.*?s</created>
122 <disconnected>Nombre total de sessions d.*?connect.*?es</disconnected>
123 <reconnected>Nombre total de sessions reconnect.*?es</reconnected>
124 <activestate>Actif</activestate>
125 <disconnectedstate>D.*?co</disconnectedstate>
126 <header_sessionname>SESSION</header_sessionname>
127 <header_state>^.*?TAT</header_state>
128 </qwinsta>
129 <qwinsta language="it">
130 <created>Totale sessioni create</created>
131 <disconnected>Totale sessioni disconnesse</disconnected>
132 <reconnected>Totale sessioni riconnesse</reconnected>
133 <activestate>Attivo</activestate>
134 <disconnectedstate>Disc</disconnectedstate>
135 <header_sessionname>NOMESESSIONE</header_sessionname>
136 <header_state>STATO</header_state>
137 </qwinsta>
138 <qwinsta language="de">
139 <created>Erstellte Sitzungen gesamt</created>
140 <disconnected>Getrennte Sitzungen gesamt</disconnected>
141 <reconnected>Erneut verbundene Sitzungen gesamt</reconnected>
142 <activestate>Aktiv</activestate>
143 <disconnectedstate>Getr\.</disconnectedstate>
144 <header_sessionname>SITZUNGSNAME</header_sessionname>
145 <header_state>STATUS</header_state>
146 </qwinsta>
147 </root>
148 END_FILE
149
150 if (defined($self->{option_results}->{config}) && $self->{option_results}->{config} ne '') {
151 $content_file = do {
152 local $/ = undef;
153 if (!open my $fh, "<", $self->{option_results}->{config}) {
154 $self->{output}->add_option_msg(short_msg => "Could not open file $self->{option_results}->{config} : $!");
155 $self->{output}->option_exit();
156 }
157 <$fh>;
158 };
159 }
160
161 my $content;
162 eval {
163 $content = XMLin($content_file, ForceArray => ['qwinsta'], KeyAttr => ['language']);
164 };
165 if ($@) {
166 $self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@");
167 $self->{output}->option_exit();
168 }
169
170 if (!defined($content->{qwinsta}->{$self->{option_results}->{language}})) {
171 $self->{output}->add_option_msg(short_msg => "Cannot find language '$self->{option_results}->{language}' in config file");
172 $self->{output}->option_exit();
173 }
174
175 return $content->{qwinsta}->{$self->{option_results}->{language}};
176 }
177
178 sub read_qwinsta {
179 my ($self, %options) = @_;
180
181 $self->{output}->output_add(long_msg => $options{stdout}, debug => 1);
182 if ($options{stdout} !~ /^(.*?)$options{config}->{created}/si) {
183 $self->{output}->add_option_msg(short_msg => "Cannot find information in command output");
184 $self->{output}->option_exit();
185 }
186 my $sessions = $1;
187
188 my @lines = split /\n/, $sessions;
189 my $header = shift @lines;
190
191 my @position_wrap = ();
192 while ($header =~ /(\s+(\S+))/g) {
193 push @position_wrap, { begin => $-[1], word_begin => $-[2], end => $+[1], label => $2 };
194 }
195 my $session_data = [];
196 foreach my $line (@lines) {
197 $line =~ s/^>/ /;
198 my $data = {};
199 for (my $pos = 0; $pos <= $#position_wrap; $pos++) {
200 my $area;
201
202 if (length($line) < $position_wrap[$pos]->{begin}) {
203 $area = '';
204 } else {
205 if ($pos + 1 <= $#position_wrap) {
206 $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}));
207 } else {
208 $area = substr($line, $position_wrap[$pos]->{begin});
209 }
210 }
211
212 $data->{$position_wrap[$pos]->{label}} = '-';
213 while ($area =~ /([^\s]+)/g) {
214 if (($-[1] >= $position_wrap[$pos]->{word_begin} - $position_wrap[$pos]->{begin} && $-[1] <= $position_wrap[$pos]->{end} - $position_wrap[$pos]->{begin})
215 ||
216 ($+[1] >= $position_wrap[$pos]->{word_begin} - $position_wrap[$pos]->{begin} && $+[1] <= $position_wrap[$pos]->{end} - $position_wrap[$pos]->{begin})) {
217 $data->{$position_wrap[$pos]->{label}} = $1;
218 last;
219 }
220 }
221 }
222 push @$session_data, $data;
223 }
224
225 return $session_data;
226 }
227
228 sub read_qwinsta_counters {
229 my ($self, %options) = @_;
230
231 my $counters = {};
232 $counters->{sessions_created} = $1
233 if ($options{stdout} =~ /$options{config}->{created}.*?(\d+)/si);
234 $counters->{sessions_disconnected} = $1
235 if ($options{stdout} =~ /$options{config}->{disconnected}.*?(\d+)/si);
236 $counters->{sessions_reconnected} = $1
237 if ($options{stdout} =~ /$options{config}->{reconnected}.*?(\d+)/si);
238
239 return $counters;
240 }
241
242 sub manage_selection {
243 my ($self, %options) = @_;
244 $self->{wsman} = $options{wsman};
245
246 my $qwinsta_cmd;
247 my $config = $self->read_config();
248 if (!defined($self->{option_results}->{command_path}) || $self->{option_results}->{command_path} eq '') {
249 $qwinsta_cmd = $self->{option_results}->{command} . ' ' . $self->{option_results}->{command_options};
250 } else {
251 $qwinsta_cmd = $self->{option_results}->{command_path} . $self->{option_results}->{command} . $self->{option_results}->{command_options};
252 }
253 $self->{result} = $self->{wsman}->execute_winshell_commands(commands => [ { label => 'qwinsta', value => $qwinsta_cmd } ]);
254
255 my $datas = $self->read_qwinsta(stdout => $self->{result}->{qwinsta}->{stdout}, config => $config);
256 my $counters = $self->read_qwinsta_counters(stdout => $self->{result}->{qwinsta}->{stdout}, config => $config);
257
258 my ($active, $disconnected) = (0, 0);
259 foreach my $session (@$datas) {
260 if (defined($self->{option_results}->{filter_sessionname}) && $self->{option_results}->{filter_sessionname} ne '' &&
261 $session->{$config->{header_sessionname}} !~ /$self->{option_results}->{filter_sessionname}/) {
262 $self->{output}->output_add(long_msg => "skipping '" . $session->{$config->{header_sessionname}} . "': no matching filter.", debug => 1);
263 next;
264 }
265
266 my ($matching_active, $matching_discon) = (0, 0);
267 foreach my $label (keys %$session) {
268 $matching_active = 1 if ($label =~ /$config->{header_state}/ &&
269 $session->{$label} =~ /$config->{activestate}/);
270 $matching_discon = 1 if ($label =~ /$config->{header_state}/ &&
271 $session->{$label} =~ /$config->{disconnectedstate}/);
272 }
273
274 if ($matching_active == 1 || $matching_discon == 1) {
275 $active++ if ($matching_active == 1);
276 $disconnected++ if ($matching_discon == 1);
277 my $output = '';
278 $output .= " [$_ => $session->{$_}]" for (sort keys %$session);
279 $self->{output}->output_add(long_msg => $output);
280 }
281 }
282
283 $self->{global} = { %$counters, sessions_active => $active, sessions_disconnected_current => $disconnected };
284
285 $self->{cache_name} = 'windows_' . $self->{mode} . '_' .
286 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
287 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
288 }
289
290 1;
291
292 __END__
293
294 =head1 MODE
295
296 Check sessions.
297
298 =over 8
299
300 =item B<--config>
301
302 The command can be localized by using a configuration file.
303 This parameter can be used to specify an alternative location for the configuration file.
304
305 =item B<--language>
306
307 Set the language used in config file (default: 'en').
308
309 =item B<--command>
310
311 Command to get information (default: 'qwinsta').
312 Can be changed if you have output in a file.
313
314 =item B<--command-path>
315
316 Command path (default: none).
317
318 =item B<--command-options>
319
320 Command options (default: '/COUNTER').
321
322 =item B<--timeout>
323
324 Timeout in seconds for the command (default: 30).
325
326 =item B<--filter-sessionname>
327
328 Filter session name (can be a regexp).
329
330 =item B<--warning-*> B<--critical-*>
331
332 Thresholds.
333 Can be: 'sessions-created', 'sessions-disconnected',
334 'sessions-reconnected', 'sessions-active', 'sessions-disconnected-current'.
335
336 =back
337
338 =cut