Repository navigation
Conversation
Read all registered event log channels through wevtapi.dll. This makes Applications and Services Logs visible without WMI. Keep the backend in check_eventlog_windows.go and remove pkg/eventlog, together with the WQL filter translation. Keep the existing check arguments and filters. Event messages are rendered only for matching events.
Use the event batch for the EvtNext array and check ERROR_NO_MORE_ITEMS before treating a zero return value as an error. The inlined call previously passed the event slice and panicked on the first empty batch.
|
This change from WMI to wevtapi is required if we want to get logs that are found in "Applications and services logs" . I tried to minimize the total line changes, so most of the DLL calls are inlined into the two main functions: getEventLog and getEventLogChannels. GetEventLog is long, but it cant be any shorter without increasing the total line count. Publisher cache and render context could have been initialized/kept elsewhere but that would spread the logic around and increase LOC . I explicitly asked AI to rename args/attributes "file" -> "channel" and "source" -> "provider", turn "file" and "source" into aliases of "channel" and "provider" . But that was the last commit, and I can revert it if you do not want it @sni |
|
Failing CI tests are due to Github, there is an acknowledged outage. Unrelated to the code changes. |
| | event-codes | Event codes to query. SNClient combines comma-separated values with OR. Valid range: 0-65535. You can repeat this option. | | ||
| | file | File to read. Repeat this option to check multiple files. | | ||
| | log | Alias for file | | ||
| | channel | Event log channel to read. Repeat this option to check multiple channels. | |
There was a problem hiding this comment.
like i said in the comment, the additions of new args/attributes are done in a single comment and can be reverted
| procEvtClose = wevtapiDll.NewProc("EvtClose") | ||
| ) | ||
|
|
||
| // getEventLogChannels returns all event log channels registered on the system. |
There was a problem hiding this comment.
The term in the new api is channels. Includes the defualt windows log:
HKLM\SYSTEM\CurrentControlSet\Services\EventLog<LogName>
and whatever other application logs registered themselves under:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Publishers{GUID}
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Channels<ChannelName>
Thats why I renamed the "file" to channels . One provider can provide multiple channels, which can point to different files:
Microsoft-Windows-CloudStore/Initialization
Microsoft-Windows-CloudStore/Operational
| // If matchers is empty, all messages are rendered. | ||
| // | ||
| //nolint:funlen // the query setup is inlined into the event loop | ||
| func getEventLog(ctx context.Context, channel string, newerThan time.Time, matchers []func(*eventLogEvent) bool) ([]eventLogEvent, error) { |
There was a problem hiding this comment.
This is the main function of the PR.
I deliberately had it argument based on a channel. So that typical user who might only want one channel/file can use it without querying every channel.
Inside the channel, we only filter using xpath based time filter. Only with the time filter, the queries are fast enough. Way faster than the WMI it is replacing anyway.
PR description has some numbers for it.
| return nil, fmt.Errorf("eventlog query, file: '%s' , got an error: %w", file, res.err) | ||
| } | ||
| // eventLogEvent is a single entry read from a Windows event log channel. | ||
| type eventLogEvent struct { |
There was a problem hiding this comment.
there are way more details we can get using the wevtapi.dll , but I did not add them.
Especially the "file". In my view, it should point to full path of the backing .evtx logfile, but that would be confused by the "channel" here.
Warning
AI-generated code. This change was implemented with AI assistance. Please review the implementation, tests, and behavior carefully before merging.
Summary
check_eventlogno longer uses WMI. It enumerates every registered Windows event log channel throughwevtapi.dlland reads events with the native Windows Event Log API.This makes Applications and Services Logs visible to the check. For example,
Microsoft-Windows-Fault-Tolerant-Heap/Operationalis now found; the WMI backend could not see that channel at all.Motivation
WMI
Win32_NTLogEventonly exposes classic, registry-backed logs such asApplication,System, andSecurity. Modern channels defined by provider manifests are not part of that store. A check targetingMicrosoft-Windows-Fault-Tolerant-Heap/Operationaltherefore returnedOK - No entries found, even though Event Viewer showed the event.The native API also removes the WMI/RPC overhead and the WQL translation layer.
What changed
pkg/snclient/check_eventlog_windows.go.pkg/eventlogis removed.pkg/snclient/check_eventlog_query_windows.goand the WQL filter translation are removed.channel=to limit the check to selected channels.message.event-codeskeeps the old AND-of-ORs behavior.wevtapi functions
The DLL is loaded with
windows.NewLazySystemDLL("wevtapi.dll").EvtOpenChannelEnumEvtNextChannelPathEvtQueryEvtNextEvtCreateRenderContextSystemvalue paths once per query.EvtRenderEvtRenderEventValuesintoEVT_VARIANTvalues. No XML parsing.EvtOpenPublisherMetadataEvtFormatMessageEvtCloseEvtQueryis called withEvtQueryChannelPathandEvtQueryForwardDirection. The time limit is part of the XPath:Nis the scan range in milliseconds. Forward direction keeps the previous oldest-first order.EvtRenderusesEvtRenderEventValues; the value paths are:The values are read through a 16-byte
evtVariantstructure and small accessors for strings, unsigned integers, andFILETIMEvalues.Event flow
EvtOpenChannelEnumandEvtNextChannelPath.EvtQuery.EvtNext.EvtRender.EvtFormatMessage.Channel errors are skipped. If no channel can be read at all, the first error is returned.
Channel and provider aliases
The old names are kept, and new preferred names are added:
channelfile,logprovidersourcechannelworks both as an argument and as a filter attribute:All old checks keep working:
The defaults now use the preferred names:
The rendered output and unique-index grouping are unchanged, because
channelandproviderexpand to the same values asfileandsource.Internally the event fields are also named
ChannelandProvider.Compatibility
file,log, andsourceremain supported aliases.event-codeskeeps the previous AND-of-ORs semantics.scan-range,truncate-message,unique-index, andshow-allare unchanged.levelkeeps the WMI-compatible mapping: levels 1 and 2 areerror, 3 iswarning, 4 isinformation, 5 isverbose. Audit success/failure is derived from the Windows keywords.eventidentifiercontinues to expose the event ID.Performance
EvtFormatMessage; messages are only rendered for matching events unless the filter referencesmessage.event-codesis applied before message rendering, even when the generic filter needs the message.Limitations
EvtQuery; their errors are skipped.EvtFormatMessagehas nothing to resolve in that case.Testing
go build ./pkg/snclientgo test -run 'TestCheckEventlog|TestEventlog|TestEvtVariant' ./pkg/snclientsource like 'Fault-Tolerant'command.channel,file,log,source, andprovider.golangci-lintreports no findings in the changed files;go vetis clean.New tests cover channel enumeration, event reading, unknown channels, level mapping, event-codes parsing, the channel alias, and
evtVariantvalue extraction.