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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 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, rb_flags;
  struct WinevtQuery* winevtQuery;
  struct WinevtSession* winevtSession;
  EVT_HANDLE hRemoteHandle = NULL;
  DWORD len, flags = 0;
  VALUE wchannelBuf, wpathBuf;
  DWORD err = ERROR_SUCCESS;

  rb_scan_args(argc, argv, "22", &channel, &xpath, &session, &rb_flags);
  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);
    }
  }

  switch (TYPE(rb_flags)) {
  case T_FIXNUM:
    flags = NUM2LONG(rb_flags);
    break;
  case T_NIL:
    flags = EvtQueryChannelPath | EvtQueryTolerateQueryErrors;
    break;
  default:
    if (hRemoteHandle != NULL) {
      EvtClose(hRemoteHandle);
    }
    rb_raise(rb_eArgError, "Expected a String, a Symbol, a Fixnum, or a NilClass instance");
  }

  // 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, flags);
  err = GetLastError();
  if (err != ERROR_SUCCESS) {
    if (hRemoteHandle != NULL) {
      EvtClose(hRemoteHandle);
    }
    if (err == ERROR_EVT_CHANNEL_NOT_FOUND) {
      raise_channel_not_found_error(channel);
    }
    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;
  winevtQuery->preserveSID = TRUE;

  ALLOCV_END(wchannelBuf);
  ALLOCV_END(wpathBuf);

  return Qnil;
}

Instance Method Details

#cancelBoolean

This method cancels channel query.

Returns:

  • (Boolean)

Since:

  • 0.9.1



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'ext/winevt/winevt_query.c', line 586

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



611
612
613
614
615
616
617
618
619
620
621
622
# File 'ext/winevt/winevt_query.c', line 611

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)


424
425
426
427
428
429
430
431
432
433
434
# File 'ext/winevt/winevt_query.c', line 424

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



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

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



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

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:



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'ext/winevt/winevt_query.c', line 242

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)


175
176
177
178
179
180
181
182
183
# File 'ext/winevt/winevt_query.c', line 175

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



190
191
192
193
194
195
196
197
198
199
200
# File 'ext/winevt/winevt_query.c', line 190

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



474
475
476
477
478
479
480
481
482
483
484
485
# File 'ext/winevt/winevt_query.c', line 474

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



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

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;
}

#preserve_sid=(rb_preserve_sid_p) ⇒ Object

This method specifies whether preserving SID or not.

Parameters:

  • rb_preserve_sid_p (Boolean)


551
552
553
554
555
556
557
558
559
560
561
562
# File 'ext/winevt/winevt_query.c', line 551

static VALUE
rb_winevt_query_set_preserve_sid(VALUE self, VALUE rb_preserve_sid_p)
{
  struct WinevtQuery* winevtQuery;

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

  winevtQuery->preserveSID = RTEST(rb_preserve_sid_p);

  return Qnil;
}

#preserve_sid?Boolean

This method returns whether preserving SID or not.

Returns:

  • (Boolean)


569
570
571
572
573
574
575
576
577
578
# File 'ext/winevt/winevt_query.c', line 569

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

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

  return winevtQuery->preserveSID ? 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)


456
457
458
459
460
461
462
463
464
465
466
# File 'ext/winevt/winevt_query.c', line 456

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)


441
442
443
444
445
446
447
448
449
# File 'ext/winevt/winevt_query.c', line 441

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)


335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'ext/winevt/winevt_query.c', line 335

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)


207
208
209
210
211
212
213
214
215
# File 'ext/winevt/winevt_query.c', line 207

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



222
223
224
225
226
227
228
229
230
231
232
# File 'ext/winevt/winevt_query.c', line 222

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;
}