Class: Winevt::EventLog::Query

Inherits:
Object
  • Object
show all
Defined in:
ext/winevt/winevt_query.c,
lib/winevt/query.rb,
ext/winevt/winevt.c,
ext/winevt/winevt_query.c

Overview

Query Windows EventLog channel.

Examples:

require 'winevt'

@query = Winevt::EventLog::Query.new("Application", "*[System[(Level <= 3) and TimeCreated[timediff(@SystemTime) <= 86400000]]]")

@query.each do |eventlog, message, string_inserts|
  puts ({eventlog: eventlog, data: message})
end

Defined Under Namespace

Modules: Flag Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(channel, xpath, session = nil) ⇒ Query

Initalize Query class.

Parameters:

  • channel (String)

    Querying EventLog channel.

  • xpath (String)

    Querying XPath.

  • session (Session) (defaults to: nil)

    Session information for remoting access.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'ext/winevt/winevt_query.c', line 84

static VALUE
rb_winevt_query_initialize(VALUE argc, VALUE *argv, VALUE self)
{
  PWSTR evtChannel, evtXPath;
  VALUE channel, xpath, session;
  struct WinevtQuery* winevtQuery;
  struct WinevtSession* winevtSession;
  EVT_HANDLE hRemoteHandle = NULL;
  DWORD len;
  VALUE wchannelBuf, wpathBuf;
  DWORD err = ERROR_SUCCESS;

  rb_scan_args(argc, argv, "21", &channel, &xpath, &session);
  Check_Type(channel, T_STRING);
  Check_Type(xpath, T_STRING);

  if (rb_obj_is_kind_of(session, rb_cSession)) {
    winevtSession = EventSession(session);

    hRemoteHandle = connect_to_remote(winevtSession->server,
                                      winevtSession->domain,
                                      winevtSession->username,
                                      winevtSession->password,
                                      winevtSession->flags,
                                      &err);
    if (err != ERROR_SUCCESS) {
      raise_system_error(rb_eRuntimeError, err);
    }
  }

  // channel : To wide char
  len =
    MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(channel), RSTRING_LEN(channel), NULL, 0);
  evtChannel = ALLOCV_N(WCHAR, wchannelBuf, len + 1);
  MultiByteToWideChar(
    CP_UTF8, 0, RSTRING_PTR(channel), RSTRING_LEN(channel), evtChannel, len);
  evtChannel[len] = L'\0';

  // xpath : To wide char
  len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(xpath), RSTRING_LEN(xpath), NULL, 0);
  evtXPath = ALLOCV_N(WCHAR, wpathBuf, len + 1);
  MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(xpath), RSTRING_LEN(xpath), evtXPath, len);
  evtXPath[len] = L'\0';

  TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  winevtQuery->query = EvtQuery(
    hRemoteHandle, evtChannel, evtXPath, EvtQueryChannelPath | EvtQueryTolerateQueryErrors);
  err = GetLastError();
  if (err != ERROR_SUCCESS) {
    raise_system_error(rb_eRuntimeError, err);
  }
  winevtQuery->offset = 0L;
  winevtQuery->timeout = 0L;
  winevtQuery->renderAsXML = TRUE;
  winevtQuery->preserveQualifiers = FALSE;
  winevtQuery->localeInfo = &default_locale;
  winevtQuery->remoteHandle = hRemoteHandle;

  ALLOCV_END(wchannelBuf);
  ALLOCV_END(wpathBuf);

  return Qnil;
}

Instance Method Details

#cancelBoolean

This method cancels channel query.

Returns:

  • (Boolean)

Since:

  • 0.9.1



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'ext/winevt/winevt_query.c', line 530

static VALUE
rb_winevt_query_cancel(VALUE self)
{
  struct WinevtQuery* winevtQuery;
  BOOL result = FALSE;

  TypedData_Get_Struct(
    self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  if (winevtQuery->query) {
    result = EvtCancel(winevtQuery->query);
  }

  if (result) {
    return Qtrue;
  } else {
    return Qfalse;
  }
}

#closeObject

This method closes channel handles forcibly.

Since:

  • 0.9.1



555
556
557
558
559
560
561
562
563
564
565
566
# File 'ext/winevt/winevt_query.c', line 555

static VALUE
rb_winevt_query_close(VALUE self)
{
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(
    self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  close_handles(winevtQuery);

  return Qnil;
}

#each {|String, String, String| ... } ⇒ Object

Enumerate to obtain Windows EventLog contents.

This method yields the following: (Stringified EventLog, Stringified detail message, Stringified insert values)

Yields:

  • (String, String, String)


402
403
404
405
406
407
408
409
410
411
412
# File 'ext/winevt/winevt_query.c', line 402

static VALUE
rb_winevt_query_each(VALUE self)
{
  RETURN_ENUMERATOR(self, 0, 0);

  while (rb_winevt_query_next(self)) {
    rb_ensure(rb_winevt_query_each_yield, self, rb_winevt_query_close_handle, self);
  }

  return Qnil;
}

#localeObject

This method obtains specified locale with [String].

Since:

  • 0.8.0



509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'ext/winevt/winevt_query.c', line 509

static VALUE
rb_winevt_query_get_locale(VALUE self)
{
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(
    self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  if (winevtQuery->localeInfo->langCode) {
    return rb_str_new2(winevtQuery->localeInfo->langCode);
  } else {
    return rb_str_new2(default_locale.langCode);
  }
}

#locale=(rb_locale_str) ⇒ Object

This method specifies locale with [String].

Parameters:

  • rb_locale_str (String)

Since:

  • 0.8.0



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'ext/winevt/winevt_query.c', line 488

static VALUE
rb_winevt_query_set_locale(VALUE self, VALUE rb_locale_str)
{
  struct WinevtQuery* winevtQuery;
  LocaleInfo* locale_info = &default_locale;

  TypedData_Get_Struct(
    self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  locale_info = get_locale_info_from_rb_str(rb_locale_str);

  winevtQuery->localeInfo = locale_info;

  return Qnil;
}

#nextBoolean

Handle the next values. Since v0.6.0, this method is used for testing only. Please use #each instead.

Returns:

  • (Boolean)

See Also:



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'ext/winevt/winevt_query.c', line 221

static VALUE
rb_winevt_query_next(VALUE self)
{
  EVT_HANDLE hEvents[QUERY_ARRAY_SIZE];
  ULONG count;
  DWORD status = ERROR_SUCCESS;
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  if (!EvtNext(winevtQuery->query, QUERY_ARRAY_SIZE, hEvents, INFINITE, 0, &count)) {
    status = GetLastError();
    if (ERROR_CANCELLED == status) {
      return Qfalse;
    }
    if (ERROR_NO_MORE_ITEMS != status) {
      return Qfalse;
    }
  }

  if (status == ERROR_SUCCESS) {
    winevtQuery->count = count;
    for (int i = 0; i < count; i++) {
      winevtQuery->hEvents[i] = hEvents[i];
    }

    return Qtrue;
  }

  return Qfalse;
}

#offsetInteger

This method returns querying event offset.

Returns:

  • (Integer)


154
155
156
157
158
159
160
161
162
# File 'ext/winevt/winevt_query.c', line 154

static VALUE
rb_winevt_query_get_offset(VALUE self)
{
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  return LONG2NUM(winevtQuery->offset);
}

#offset=(offset) ⇒ Object

This method specifies querying event offset.

Parameters:

  • offset (Integer)

    offset value



169
170
171
172
173
174
175
176
177
178
179
# File 'ext/winevt/winevt_query.c', line 169

static VALUE
rb_winevt_query_set_offset(VALUE self, VALUE offset)
{
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  winevtQuery->offset = NUM2LONG(offset);

  return Qnil;
}

#preserve_qualifiers=(rb_preserve_qualifiers) ⇒ Object

This method specifies whether preserving qualifiers key or not.

Parameters:

  • rb_preserve_qualifiers (Boolean)

Since:

  • 0.7.3



452
453
454
455
456
457
458
459
460
461
462
463
# File 'ext/winevt/winevt_query.c', line 452

static VALUE
rb_winevt_query_set_preserve_qualifiers(VALUE self, VALUE rb_preserve_qualifiers)
{
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(
    self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  winevtQuery->preserveQualifiers = RTEST(rb_preserve_qualifiers);

  return Qnil;
}

#preserve_qualifiers?Integer

This method returns whether preserving qualifiers or not.

Returns:

  • (Integer)

Since:

  • 0.7.3



471
472
473
474
475
476
477
478
479
480
# File 'ext/winevt/winevt_query.c', line 471

static VALUE
rb_winevt_query_get_preserve_qualifiers_p(VALUE self)
{
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(
    self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  return winevtQuery->preserveQualifiers ? Qtrue : Qfalse;
}

#render_as_xml=(rb_render_as_xml) ⇒ Object

This method specifies whether render as xml or not.

Parameters:

  • rb_render_as_xml (Boolean)


434
435
436
437
438
439
440
441
442
443
444
# File 'ext/winevt/winevt_query.c', line 434

static VALUE
rb_winevt_query_set_render_as_xml(VALUE self, VALUE rb_render_as_xml)
{
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  winevtQuery->renderAsXML = RTEST(rb_render_as_xml);

  return Qnil;
}

#render_as_xml?Boolean

This method returns whether render as xml or not.

Returns:

  • (Boolean)


419
420
421
422
423
424
425
426
427
# File 'ext/winevt/winevt_query.c', line 419

static VALUE
rb_winevt_query_render_as_xml_p(VALUE self)
{
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  return winevtQuery->renderAsXML ? Qtrue : Qfalse;
}

#seek(bookmark_or_flag) ⇒ Boolean

This method specifies seek strategy.

Parameters:

Returns:

  • (Boolean)


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'ext/winevt/winevt_query.c', line 313

static VALUE
rb_winevt_query_seek(VALUE self, VALUE bookmark_or_flag)
{
  struct WinevtQuery* winevtQuery;
  struct WinevtBookmark* winevtBookmark = NULL;
  DWORD flag = 0;

  switch (TYPE(bookmark_or_flag)) {
    case T_SYMBOL:
      flag = get_evt_seek_flag_from_cstr(RSTRING_PTR(rb_sym2str(bookmark_or_flag)));
      break;
    case T_STRING:
      flag = get_evt_seek_flag_from_cstr(StringValueCStr(bookmark_or_flag));
      break;
    case T_FIXNUM:
      flag = NUM2LONG(bookmark_or_flag);
      break;
    default:
      if (!rb_obj_is_kind_of(bookmark_or_flag, rb_cBookmark))
        rb_raise(rb_eArgError, "Expected a String or a Symbol or a Bookmark instance");

      winevtBookmark = EventBookMark(bookmark_or_flag);
  }

  if (winevtBookmark) {
    TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);
    if (EvtSeek(winevtQuery->query,
                winevtQuery->offset,
                winevtBookmark->bookmark,
                winevtQuery->timeout,
                EvtSeekRelativeToBookmark))
      return Qtrue;
  } else {
    TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);
    if (EvtSeek(
          winevtQuery->query, winevtQuery->offset, NULL, winevtQuery->timeout, flag)) {
      return Qtrue;
    }
  }

  return Qfalse;
}

#timeoutInteger

This method returns timeout value.

Returns:

  • (Integer)


186
187
188
189
190
191
192
193
194
# File 'ext/winevt/winevt_query.c', line 186

static VALUE
rb_winevt_query_get_timeout(VALUE self)
{
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  return LONG2NUM(winevtQuery->timeout);
}

#timeout=(timeout) ⇒ Object

This method specifies timeout value.

Parameters:

  • timeout (Integer)

    timeout value



201
202
203
204
205
206
207
208
209
210
211
# File 'ext/winevt/winevt_query.c', line 201

static VALUE
rb_winevt_query_set_timeout(VALUE self, VALUE timeout)
{
  struct WinevtQuery* winevtQuery;

  TypedData_Get_Struct(self, struct WinevtQuery, &rb_winevt_query_type, winevtQuery);

  winevtQuery->timeout = NUM2LONG(timeout);

  return Qnil;
}