Debian Codegraph @

src/os/windows/wsman/mode/eventlog.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::eventlog;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use DateTime;
28
29 sub set_counters {
30 my ($self, %options) = @_;
31
32 $self->{maps_counters_type} = [
33 { name => 'global', type => 0, skipped_code => { -10 => 1 } }
34 ];
35
36 $self->{maps_counters}->{global} = [
37 { label => 'events', nlabel => 'events.total.count', set => {
38 key_values => [ { name => 'events' } ],
39 output_template => 'Number of event(s): %d',
40 perfdatas => [
41 { template => '%d', min => 0 }
42 ]
43 }
44 }
45 ];
46 }
47
48 sub new {
49 my ($class, %options) = @_;
50 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
51 bless $self, $class;
52
53 $options{options}->add_options(arguments => {
54 'filter-eventid:s' => { name => 'eventId' },
55 'filter-source:s' => { name => 'source' },
56 'filter-logfile:s' => { name => 'logfile' },
57 'filter-type:s' => { name => 'type' },
58 'timeframe:s' => { name => 'timeframe', default => 1 }
59 });
60
61 return $self;
62 }
63
64 sub check_options {
65 my ($self, %options) = @_;
66 $self->SUPER::check_options(%options);
67
68 if (defined($self->{option_results}->{type}) && $self->{option_results}->{type} ne '' &&
69 $self->{option_results}->{type} !~ /^(information|error|critical|warning|verbose)$/) {
70 $self->{output}->add_option_msg(short_msg => "Unsupported --type option.");
71 $self->{output}->option_exit();
72 }
73 }
74
75 sub wmi_to_seconds {
76 my ($self, %options) = @_;
77
78 # pass in a WMI Timestamp like 2021-11-04T21:24:11.871719Z
79 my $sec = '';
80 my $age_sec = '';
81 my $current_dt = '';
82 my $current_sec = '';
83 my $tz = '';
84 my $timezone_direction = '+';
85 my $timezone_offset = 0;
86
87 # 1 2 3 4 5 6 7 8 9
88 if ($options{ts} =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d*)(\S+)$/) {
89 my %ts_info= (
90 year => $1,
91 month => $2,
92 day => $3,
93 hour => $4,
94 minute => $5,
95 second => $6,
96 nanosecond => $7,
97 time_zone => $8 # set later
98 );
99
100 my $dt = DateTime->new(%ts_info);
101 $sec = $dt->epoch();
102 # force the current time into the same timezone as the queried system
103 $current_dt = DateTime->now(time_zone => $dt->time_zone());
104 $current_sec = $current_dt->epoch();
105 $age_sec = $current_sec - $sec;
106 } else {
107 $self->{output}->add_option_msg(short_msg => 'Wrong time format');
108 $self->{output}->option_exit();
109 }
110
111 return ($sec, $age_sec);
112 }
113
114 sub manage_selection {
115 my ($self, %options) = @_;
116
117 my $dt = DateTime->now;
118 $dt = $dt->subtract(hours => $self->{option_results}->{timeframe});
119 my $date = sprintf("%d%02d%02d%02d%02d%02d.000000-000", $dt->year, $dt->month, $dt->day, $dt->hour, $dt->minute, $dt->second);
120
121 my $wql = "SELECT EventCode, SourceName, Type, Logfile, TimeGenerated, Message FROM Win32_NTLogEvent where TimeGenerated > '$date'";
122
123 if (defined($self->{option_results}->{eventId}) && $self->{option_results}->{eventId} ne '') {
124 $wql .= " And EventCode = $self->{option_results}->{eventId}";
125 }
126 if (defined($self->{option_results}->{source}) && $self->{option_results}->{source} ne '') {
127 $wql .= " And SourceName = '$self->{option_results}->{source}'";
128 }
129 if (defined($self->{option_results}->{type}) && $self->{option_results}->{type} ne '') {
130 $wql .= " And Type = '$self->{option_results}->{type}'";
131 }
132 if (defined($self->{option_results}->{logfile}) && $self->{option_results}->{logfile} ne '') {
133 $wql .= " And Logfile = '$self->{option_results}->{logfile}'";
134 }
135 my $results = $options{wsman}->request(
136 uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*',
137 wql_filter => $wql,
138 result_type => 'array'
139 );
140
141 $self->{global} = { events => 0 };
142 foreach (@$results) {
143 my $time_generate = $_->{TimeGenerated};
144 my ($time, $diff_time) = $self->wmi_to_seconds(ts => $time_generate);
145
146 $self->{output}->output_add(long_msg =>
147 sprintf(
148 "[EventID: %d][Type: %s][Logfile: %s][Source: %s][Date: %s][Message: %s]",
149 $_->{EventCode},
150 $_->{Type},
151 $_->{Logfile},
152 $_->{SourceName},
153 scalar(localtime($time)),
154 $_->{Message}
155 )
156 );
157 $self->{global}->{events}++;
158 }
159 }
160
161 1;
162
163 __END__
164
165 =head1 MODE
166
167 Check Windoes event logs.
168
169 =over 8
170
171 =item B<--filter-eventid>
172
173 Filter on specific event id.
174
175 =item B<--filter-source>
176
177 Filter on event log source.
178
179 =item B<--filter-logfile>
180
181 Filter on specific logfile.
182 Example: Application, System.
183
184 =item B<--filter-type>
185
186 Filter on specific type.
187 Can be: 'information', 'critical', 'warning', 'error', 'verbose'.
188
189 =item B<--timeframe>
190
191 Time frame to filter events on hour (default: 1).
192
193 =item B<--warning-*> B<--critical-*>
194
195 Thresholds.
196 Can be: 'events'.
197
198 =back
199
200 =cut