Skip to main content
Version: 4.3

Macros

Description

Search macros are reusable parts of SAF Language queries that can be used in other search queries.

Structure

info

Only letters, numbers, hyphens, and underscores can be used in the macro name.

Macros contain the following parameters:

Field NameDescriptionRequired
Macros nameThe name of the macroYes
Search query textThe text of the search query that will replace this macro. If arguments are used, they should be enclosed in dollar signs, e.g., $arg1$Yes
ArgumentsA string with argument names separated by commasNo
Macro Name with Arguments

If the macro has arguments, the number of arguments should be indicated at the end of the name in parentheses. For example: my_macro(2)

Examples of Usage

Querying Data from the sysmon_operational Index for the Last Week

ParameterValue
Namesource_sysmon(1)
Search Query textsource sysmon_operational-* earliest="now-7d" latest="now" qsize=$qsize$
Argumentsqsize
Calling a Macro in a Query

To call a macro in a query, its name must be enclosed in backticks ` `. If the macro has arguments, they must be specified in parentheses, separated by commas. If you need to pass a comma in an argument, it must be escaped using a backslash.

Example query:

`source_sysmon(1000)`
| stats count by host.hostname

After processing, the query will be transformed into the following form:

source sysmon_operational-* earliest="now-7d" latest="now" qsize=1000
| stats count by host.hostname

Calling the stats Command with a Function and by Fields

ParameterValue
Namestats_rename_macros(3)
Definitionstats $func$ by $field$ | rename '$func$' as $newfield$
Argumentsfunc,field,newfield

In this macro, the first parameter is the function of the stats command, the second parameter is the by fields, and the third parameter is the name of the new field.

Escaping the Comma

By escaping the comma, you can pass multiple by fields to this macro.

Example query:

`source_sysmon(1000)`
| `stats_rename_macros(values(event.code), host.hostname \, winlog.task, vals)`

After processing, the query will be transformed into the following form:

source sysmon_operational-* earliest="now-7d" latest="now" qsize=1000
| stats values(event.code) by host.hostname , winlog.task | rename 'values(event.code)' as vals