< Return to Video

Splunk Basic : Everything to know about macros

  • 0:00 - 0:03
    Okay. Today, we'll discuss Splunk
  • 0:03 - 0:06
    macros. Okay. So, Splunk macros--you can
  • 0:06 - 0:09
    think of them as reusable components in
  • 0:09 - 0:13
    your Splunk search where, suppose a
  • 0:13 - 0:16
    particular search portion is
  • 0:16 - 0:19
    if you used many times, then you
  • 0:19 - 0:20
    can put it in a macro so that you can
  • 0:20 - 0:24
    call that macro, and the macro will be
  • 0:24 - 0:26
    replaced at runtime with that search string.
  • 0:26 - 0:29
    Okay? So, macros are very, very useful when,
  • 0:29 - 0:32
    suppose you have many dashboards in your
  • 0:32 - 0:35
    application. And in all these dashboards, you
  • 0:35 - 0:39
    have some search which is running in all
  • 0:39 - 0:42
    the dashboards. Right? So, in this case, you
  • 0:42 - 0:43
    can implement that search portion in a
  • 0:43 - 0:46
    macro so that you can just call that macro
  • 0:46 - 0:48
    instead of using the whole search string in
  • 0:48 - 0:50
    your dashboards. So, in the future, if you
  • 0:50 - 0:52
    want to make any changes in that search
  • 0:52 - 0:55
    logic, we'll just change it in
  • 0:55 - 0:59
    one place--in that macro body. Okay?
  • 0:59 - 1:01
    So, by implementing a macro, basically, we
  • 1:01 - 1:04
    are achieving two things. One is the
  • 1:04 - 1:07
    modularity of your code--easy
  • 1:07 - 1:10
    maintenance of your code--as well as
  • 1:10 - 1:11
    your search string will be much
  • 1:11 - 1:16
    smaller. Okay? So, there are
  • 1:16 - 1:19
    two different types of macros we
  • 1:19 - 1:21
    generally deal with. One is an eval-based
  • 1:21 - 1:23
    macro, and another is a non-eval-based
  • 1:23 - 1:26
    macro. So, before we discuss those things,
  • 1:26 - 1:30
    let me show you the data I have here.
  • 1:30 - 1:33
    So, I have some people's names, their
  • 1:33 - 1:36
    basic salary, and variable percent. So,
  • 1:36 - 1:41
    I'll show you how to create an eval-based macro,
  • 1:41 - 1:42
    how to create a non-eval-based
  • 1:42 - 1:44
    macro, what the differences are
  • 1:44 - 1:48
    between them, and then how to call a
  • 1:48 - 1:50
    macro instead of the macro also. We'll try to
  • 1:50 - 1:52
    discuss that one. Because first, let us
  • 1:52 - 1:57
    see how we can create a non-eval macro.
  • 1:57 - 2:00
    So now, an eval macro and a non-eval macro--
  • 2:00 - 2:04
    if you see functionality-wise, both are the
  • 2:04 - 2:07
    same. So, when we define a macro--the macro
  • 2:07 - 2:10
    body--you are giving the search string. Right? So,
  • 2:10 - 2:13
    dynamically at runtime, that
  • 2:13 - 2:16
    macro will be replaced
  • 2:16 - 2:18
    by that macro body by Splunk search.
  • 2:18 - 2:23
    Okay? Now, when we talk about eval-based
  • 2:23 - 2:26
    macros, it is expected that the macro is
  • 2:26 - 2:31
    returning a string. When you use a non-eval-based macro,
  • 2:31 - 2:32
    you can directly put the
  • 2:32 - 2:34
    search string inside the macro. So, let me
  • 2:34 - 2:37
    show you. Suppose I want to calculate
  • 2:37 - 2:40
    total salary based on this formula:
  • 2:40 - 2:43
    called basic salary. Let me show you.
  • 2:43 - 2:50
    Basic salary plus basic into the
  • 2:50 - 2:54
    percentage--variable
  • 2:54 - 2:58
    percent. Okay? So, to do that--to create any
  • 2:58 - 3:01
    macro from the Splunk UI--this is how you
  • 3:01 - 3:04
    do it. You'll go to Settings. We go to
  • 3:04 - 3:08
    Advanced Search. Okay? From there, if you
  • 3:08 - 3:12
    see, there is an option called Search Macros.
  • 3:12 - 3:13
    You go there. You select your app
  • 3:13 - 3:16
    from there. So, I'll be creating this
  • 3:16 - 3:18
    macro in my TMDB app. So, I have selected
  • 3:18 - 3:21
    TMDB over here, then New Search Macro.
  • 3:21 - 3:24
    Okay? If you see the destination, I've
  • 3:24 - 3:26
    already selected it as TMDB. I'll give it a
  • 3:26 - 3:36
    name. Let's say, total salary. Okay? So now,
  • 3:36 - 3:39
    whether a macro is eval-based or not is
  • 3:39 - 3:41
    determined by this checkbox. If you
  • 3:41 - 3:43
    check this one, it will be an eval-based
  • 3:43 - 3:45
    macro. If you don't check this one,
  • 3:45 - 3:48
    it will be a non-eval-based macro. So
  • 3:48 - 3:51
    first, we'll discuss the non-eval-based macro.
  • 3:51 - 3:52
    So, what I am trying to say
  • 3:52 - 3:55
    is--whenever we
  • 3:55 - 3:57
    are creating a non-eval-based macro, we
  • 3:57 - 3:59
    can directly give a search string over
  • 3:59 - 4:01
    there. So, my search string will be
  • 4:01 - 4:03
    something like this. I'll be creating a
  • 4:03 - 4:06
    new field called total salary. So, that's
  • 4:06 - 4:14
    why eval total_salary =. Now,
  • 4:14 - 4:17
    I'll be passing this to my basic
  • 4:17 - 4:20
    salary and variable percent to this
  • 4:20 - 4:23
    macro. Right? To do that, what I'll do
  • 4:23 - 4:26
    is create arguments for this macro:
  • 4:26 - 4:28
    basic salary. So, in the argument
  • 4:28 - 4:33
    section, you'll be giving
  • 4:33 - 4:36
    these two things: basic salary and
  • 4:36 - 4:40
    variable percent. That's two arguments. As the
  • 4:40 - 4:43
    macro has two arguments, it is mandatory
  • 4:43 - 4:47
    that in this Name section, you give how
  • 4:47 - 4:49
    many number of arguments you have in
  • 4:49 - 4:51
    your macro. If it is one, you have to give
  • 4:51 - 4:56
    one here. Okay? Now the formula is: my
  • 4:56 - 4:59
    basic salary. Now, this basic salary is my
  • 4:59 - 5:03
    input variable. Right? So, to access input
  • 5:03 - 5:05
    variables, you access like this--just like
  • 5:05 - 5:12
    a token. Okay? Basic salary plus my basic
  • 5:12 - 5:21
    salary into that variable percent. Again,
  • 5:21 - 5:26
    as input, I am accessing. Right? Then
  • 5:26 - 5:30
    divided by 100. So, this will be my
  • 5:30 - 5:34
    macro body. Now, there are two options
  • 5:34 - 5:35
    for Validation Expression and Validation
  • 5:35 - 5:37
    Error Message. I will show you in the
  • 5:37 - 5:42
    next macro. So, if I save it--okay.
  • 5:42 - 5:45
    So, if you see, the macro has been created.
  • 5:45 - 5:50
    Now, if I call this macro over here--so to
  • 5:50 - 5:52
    call a macro, this is how you call: there
  • 5:52 - 5:54
    is a tick mark, if you see, okay, from your
  • 5:54 - 6:01
    keyboard, then the macro name--total
  • 6:01 - 6:05
    salary--and it has two arguments, right? My
  • 6:05 - 6:09
    basic salary and my variable percent,
  • 6:09 - 6:13
    then tick end. Now, this basic salary
  • 6:13 - 6:15
    corresponds to this basic salary, and
  • 6:15 - 6:17
    variable percent corresponds to this
  • 6:17 - 6:18
    variable percent field.
  • 6:18 - 6:21
    Now, when you define the macro, I have
  • 6:21 - 6:23
    given the same name as the macro input. You
  • 6:23 - 6:26
    can give any input variable name
  • 6:26 - 6:28
    over here. The same name you have to use
  • 6:28 - 6:31
    over here as well. But when you pass the
  • 6:31 - 6:34
    macro--pass this with basic salary and variable
  • 6:34 - 6:36
    percent to this macro--you'll be giving
  • 6:36 - 6:37
    the field names only. Okay?
  • 6:37 - 6:40
    So, let us run it and see what's going on.
  • 6:40 - 6:44
    If you see, it has created a total salary
  • 6:44 - 6:48
    field with this formula output--4,000 plus
  • 6:48 - 6:52
    4,000 into 15% is 4,600. Right? So, it is
  • 6:52 - 6:55
    basically the same as what is
  • 6:55 - 6:58
    happening at runtime. So, if
  • 6:58 - 7:00
    instead of total salary, I just gave
  • 7:00 - 7:04
    this one, and instead of this token
  • 7:04 - 7:08
    variable, I just gave my field name here,
  • 7:08 - 7:14
    the output will be the same--variable percent.
  • 7:14 - 7:19
    The output will be the same. So, this is also
  • 7:19 - 7:21
    what is happening as well. So, at runtime,
  • 7:21 - 7:25
    Splunk is replacing this macro body with
  • 7:25 - 7:28
    these variable inputs, and then it is
  • 7:28 - 7:30
    running the query. So ultimately, this
  • 7:30 - 7:33
    query is getting run. Okay? But the
  • 7:33 - 7:35
    thing is--this code, if you put it in
  • 7:35 - 7:37
    a macro--you can call that macro anywhere
  • 7:37 - 7:39
    in usage. So that means this code
  • 7:39 - 7:42
    portion will be reusable now. Okay? Now,
  • 7:42 - 7:46
    let's see an example where we can use
  • 7:46 - 7:48
    this validation expression and
  • 7:48 - 7:49
    validation error message. Now,
  • 7:49 - 7:52
    validation expression is used when you
  • 7:52 - 7:55
    want to do some kind of validation on
  • 7:55 - 8:00
    your macro inputs. Okay? And validation
  • 8:00 - 8:03
    error message means when this input is
  • 8:03 - 8:05
    getting--or this validation is getting--
  • 8:05 - 8:06
    failed, this error message will be
  • 8:06 - 8:09
    displayed over there. So, to do that, what
  • 8:09 - 8:15
    I'll do is create another macro.
  • 8:15 - 8:20
    Okay? So now, let's say, we have basic
  • 8:20 - 8:23
    salary and variable percent. Right? Let's
  • 8:23 - 8:25
    say we want to calculate a bonus
  • 8:25 - 8:28
    percentage with this formula: called
  • 8:28 - 8:32
    variable percent plus any other percent--
  • 8:32 - 8:36
    any number. Let's say, any number--
  • 8:36 - 8:38
    7% or 8%, any number
  • 8:38 - 8:41
    I’ll put for our inputs. It'll give me--it'll
  • 8:41 - 8:43
    add those two percentage values and
  • 8:43 - 8:46
    give me the bonus percent. Okay? So, to do
  • 8:46 - 8:48
    that, I'll just create that similar
  • 8:48 - 8:51
    macro named "bonus."
  • 8:52 - 8:57
    Okay? So, the formula will be--again--it
  • 8:57 - 9:10
    will be non-eval-based. Okay? eval bonus =.
  • 9:10 - 9:14
    So, my variable percent, right,
  • 9:14 - 9:16
    I have to provide as an input, so I'll be
  • 9:16 - 9:19
    giving input. So, let's say this time I
  • 9:19 - 9:22
    will rename this to something--VP. So,
  • 9:22 - 9:28
    I'll say $VP$, then plus
  • 9:28 - 9:31
    another input I'll be giving--let's say,
  • 9:31 - 9:37
    bonus input. Okay? So, this bonus, I'll be
  • 9:37 - 9:42
    adding here. Okay. Bonus. So, we have two
  • 9:42 - 9:44
    inputs. That means we have to give two
  • 9:44 - 9:47
    over here. Now I'll add an expression. Now,
  • 9:47 - 9:50
    I always want this bonus to be a number.
  • 9:50 - 9:54
    Okay? To do that--so, this validation
  • 9:54 - 9:56
    expression has to be a Boolean
  • 9:56 - 9:59
    expression or eval. Okay? So, if you saw my
  • 9:59 - 10:01
    previous video, we discussed the
  • 10:01 - 10:04
    various eval expressions. Right? And some
  • 10:04 - 10:06
    of the expressions return Boolean--like
  • 10:06 - 10:09
    the like operator, in operator, or
  • 10:09 - 10:12
    isnum operator, which basically checks
  • 10:12 - 10:15
    whether a particular input is a number or
  • 10:15 - 10:17
    not. So, we'll be using isnum over
  • 10:17 - 10:21
    here on this bonus. That means I am
  • 10:21 - 10:23
    checking whether whatever I am providing
  • 10:23 - 10:25
    as input to this bonus variable
  • 10:25 - 10:29
    parameter is a number or not. If
  • 10:29 - 10:32
    it is not a number, I'll give this
  • 10:32 - 10:37
    output error message: "Bonus must
  • 10:37 - 10:41
    be a number." Okay?
  • 10:41 - 10:50
    So, let's save this macro. Okay? Now I will
  • 10:50 - 10:57
    call this macro here. Let it be--my total
  • 10:57 - 11:02
    salary macro called as--is. If I just do
  • 11:02 - 11:04
    Ctrl+C--
  • 11:04 - 11:07
    okay. Here, I am calling my total salary. So
  • 11:07 - 11:09
    just tick the output. Now, after that, I'll
  • 11:09 - 11:15
    be calling the bonus macro. Right? So,
  • 11:15 - 11:18
    similar way: tick, macro name, macro
  • 11:18 - 11:20
    inputs. One of the inputs will be my
  • 11:20 - 11:23
    variable percent, and another
  • 11:23 - 11:26
    input maybe--let's say, 8% I want
  • 11:26 - 11:28
    to add to the variable percent. So, what
  • 11:28 - 11:31
    will be the output? In this case, it should
  • 11:31 - 11:34
    create a new field called "bonus" with 15
  • 11:34 - 11:36
    plus 8, 20 plus 8—something like this.
  • 11:36 - 11:39
    Okay? If you see, you can
  • 11:39 - 11:42
    access different macros in
  • 11:42 - 11:48
    your whole search. Okay? Now, let's say I
  • 11:48 - 11:53
    have provided a string here--it says test.
  • 11:53 - 11:55
    Okay. What is happening? So, if you provide
  • 11:55 - 11:58
    a string here, that validation is
  • 11:58 - 12:00
    getting failed. We have added over here
  • 12:00 - 12:02
    isnum. So, that's why it is giving you this
  • 12:02 - 12:06
    error: "Bonus must be a number." So, this is
  • 12:06 - 12:10
    how macro input validation works. Okay. Let
  • 12:10 - 12:15
    us continue. Now, we will discuss
  • 12:15 - 12:19
    how to create an eval macro. So, to do
  • 12:19 - 12:23
    that, what we'll do is--first, let me show
  • 12:23 - 12:25
    you two things. If you remember from my
  • 12:25 - 12:28
    previous video, we created a command
  • 12:28 - 12:32
    called getjournals. Right? And that
  • 12:32 - 12:34
    command takes an input with the journal
  • 12:34 - 12:36
    ID. And if we are not giving any journal
  • 12:36 - 12:39
    ID and we are giving *, it will give you all
  • 12:39 - 12:42
    the journal details. Right? And also, we
  • 12:42 - 12:48
    have... We have created a...
  • 12:48 - 12:51
    We have indexed our data into this main index.
  • 12:51 - 12:52
    Right? So, we have another dataset
  • 12:52 - 12:54
    called "main" where we are having
  • 12:54 - 12:56
    this salary and this information. Just
  • 12:56 - 12:59
    now we are working with this dataset. So,
  • 12:59 - 13:01
    suppose there is a requirement that,
  • 13:01 - 13:05
    based on a certain condition, either I need
  • 13:05 - 13:09
    to go with this dataset or I need to go
  • 13:09 - 13:11
    with that dataset. In this type of
  • 13:11 - 13:14
    scenario--where dynamically you have to
  • 13:14 - 13:16
    determine which dataset to work on, or
  • 13:16 - 13:17
    dynamically determine
  • 13:17 - 13:23
    which search string to return--
  • 13:23 - 13:25
    eval macros
  • 13:25 - 13:28
    come into the picture. Okay? Because the
  • 13:28 - 13:31
    main concept behind an eval macro is it has
  • 13:31 - 13:34
    to return a string. So, in that case, how
  • 13:34 - 13:36
    you will create it is like this. Let's say,
  • 13:36 - 13:40
    new search macro. I'll give a name called
  • 13:40 - 13:47
    getjournal_or_mainindex. Okay? Just
  • 13:47 - 13:49
    like a name to show you the use
  • 13:49 - 13:52
    case of it. So, here, what I will write--
  • 13:52 - 13:54
    I’ll write an if or case statement. Generally,
  • 13:54 - 13:55
    we mostly write an if or case
  • 13:55 - 13:59
    statement for eval-based macros,
  • 13:59 - 14:02
    so that we can have different
  • 14:02 - 14:03
    conditions, and based on the condition, we
  • 14:03 - 14:05
    return the search string. But
  • 14:05 - 14:07
    ultimately, if you see, we’re returning the
  • 14:07 - 14:09
    search string only at the end of the day--
  • 14:09 - 14:11
    so that Splunk will replace that macro
  • 14:11 - 14:13
    with that search string. So, I’m saying I’ll
  • 14:13 - 14:18
    provide the argument here. Let’s say,
  • 14:18 - 14:21
    same argument name. So,
  • 14:21 - 14:23
    either I’ll provide getjournal, or I’ll
  • 14:23 - 14:26
    provide main as my argument
  • 14:26 - 14:28
    value. Okay? I’ll show you how I
  • 14:28 - 14:29
    provide that one.
  • 14:29 - 14:42
    case($input$ == "getjournal", "getjournals *", $input$ == "main", "search index=main")
  • 14:42 - 14:47
    As it is within quotes, remember that this one
  • 14:47 - 14:50
    has to be in quotes as well. Or, when you are
  • 14:50 - 14:52
    calling it, you have to call with
  • 14:52 - 14:54
    quotes. I prefer it like this only so
  • 14:54 - 14:56
    that everything is inside your
  • 14:56 - 14:59
    macro. In that case, the string I
  • 14:59 - 15:05
    will return is this command: getjournals *.
  • 15:05 - 15:07
    Now, you have to be very careful over
  • 15:07 - 15:10
    here when you deal with a generating
  • 15:10 - 15:12
    command. If you see, getjournals is a
  • 15:12 - 15:14
    generating command. Right? That’s why this
  • 15:14 - 15:15
    has to be the first command in your
  • 15:15 - 15:20
    search string. Now, for generating commands,
  • 15:20 - 15:23
    when you run it--if you see--there is a
  • 15:23 - 15:25
    bar (|) over here in the search bar. Right? After
  • 15:25 - 15:27
    that only, if I run this command without
  • 15:27 - 15:30
    this bar, nothing will come up. So,
  • 15:30 - 15:32
    it requires this bar. But when you put
  • 15:32 - 15:35
    this getjournals *--this generating
  • 15:35 - 15:39
    command--inside a macro, you put it
  • 15:39 - 15:41
    without the bar, so that when you call
  • 15:41 - 15:44
    the macro, you’ll call with | then
  • 15:44 - 15:45
    macro name. I’ll show you that one as
  • 15:45 - 15:48
    well. That’s why I’m giving it without
  • 15:48 - 15:51
    the bar here. This is important--you have to be very
  • 15:51 - 15:53
    careful with generating commands.
  • 15:53 - 15:55
    Because if you put the bar inside, it will
  • 15:55 - 16:01
    not work. Now, when my input is "main"--okay--in
  • 16:01 - 16:05
    that case, I will return the string
  • 16:05 - 16:10
    called search index=main. Here
  • 16:10 - 16:13
    also, another good thing is when you run
  • 16:13 - 16:15
    index=main from your search
  • 16:15 - 16:17
    prompt, you do not need to mention
  • 16:17 - 16:19
    search then index=main,
  • 16:19 - 16:22
    because that comes by default. But
  • 16:22 - 16:24
    when you use it in a macro or somewhere
  • 16:24 - 16:24
    else,
  • 16:24 - 16:27
    you have to put it like this: search
  • 16:27 - 16:31
    index=main. Okay? So, this string I’ll
  • 16:31 - 16:33
    be returning. Since the macro has one
  • 16:33 - 16:37
    input, I’ll be giving 1 here. Okay? For now,
  • 16:37 - 16:39
    validation. We are not doing anything,
  • 16:39 - 16:44
    so let’s save it. Okay? This is our third
  • 16:44 - 16:49
    macro--getjournal_or_main. So, if I
  • 16:49 - 16:54
    run this macro now--okay--as I said, it
  • 16:54 - 16:56
    has a generating command, that’s why I’m
  • 16:56 - 17:00
    giving a bar over here, then my tick, and
  • 17:00 - 17:04
    then my input. Let’s say I’ll be giving
  • 17:04 - 17:10
    input as getjournal. I
  • 17:10 - 17:13
    want to work with the dataset getjournal. Okay?
  • 17:13 - 17:21
    Let’s see. Okay. I have a
  • 17:21 - 17:28
    bracket that’s missing over here. Save.
  • 17:28 - 17:30
    If you see, if I run this macro again, it
  • 17:30 - 17:32
    is giving me the same because sometimes it
  • 17:32 - 17:35
    doesn’t take the refreshed value.
  • 17:35 - 17:37
    So, what I’ll do in this case--just copy
  • 17:37 - 17:42
    this code, close it, run it again.
  • 17:42 - 17:44
    SearchFactory Unknown search command
  • 17:44 - 17:56
    'case'. Let’s see what’s going on. Okay. It
  • 17:56 - 17:58
    has to be an eval-based macro because it is
  • 17:58 - 18:01
    returning a string now. Right? So, let’s
  • 18:01 - 18:11
    save it, and let’s rerun it. Okay. It is not-- Okay. Now, if you
  • 18:11 - 18:14
    see, if I run this getjournal macro,
  • 18:14 - 18:15
    it is giving me this dataset where it
  • 18:15 - 18:17
    is running that getjournal *
  • 18:17 - 18:19
    generating command and giving me this
  • 18:19 - 18:24
    result. If I say main, in this case, it is
  • 18:24 - 18:26
    giving me this dataset where I have
  • 18:26 - 18:30
    indexed my data. So, this is how you can
  • 18:30 - 18:33
    create an eval-based macro. Okay? So now,
  • 18:33 - 18:37
    macros have different permissions as
  • 18:37 - 18:40
    well. If you see from this macro
  • 18:40 - 18:44
    list page, you can set the
  • 18:44 - 18:46
    permissions as well. So currently, I'll
  • 18:46 - 18:48
    show you. I'll set this permission as
  • 18:48 - 18:50
    "this app only" and read--I'll say
  • 18:50 - 18:53
    everyone, and write--let's say, admin. Let's
  • 18:53 - 18:57
    do it for all the macro permissions. So,
  • 18:57 - 18:59
    you have to be very careful with the
  • 18:59 - 19:02
    macro permissions as well. Otherwise, any
  • 19:02 - 19:05
    unprivileged user will not be able to
  • 19:05 - 19:07
    run this macro, and your dashboard will
  • 19:07 - 19:12
    not show up anything. So, read--right. Okay.
  • 19:12 - 19:14
    Now, we have set the permissions of the
  • 19:14 - 19:17
    macro as well. Now, let's see what's going
  • 19:17 - 19:19
    on in the background. So, I am in my
  • 19:19 - 19:25
    Splunk home. I'll go to etc/apps/tmdb_app/,
  • 19:25 - 19:27
    and I'll go to my local folder. If
  • 19:27 - 19:29
    you see, whenever you create a macro,
  • 19:29 - 19:31
    it creates another conf file called
  • 19:31 - 19:34
    macros.conf. I'll open this file. And
  • 19:34 - 19:37
    if you see all the different macros we have
  • 19:37 - 19:39
    created, it creates separate
  • 19:39 - 19:43
    stanzas for each macro name with all
  • 19:43 - 19:46
    the input numbers. Right? And args are the
  • 19:46 - 19:49
    inputs of these macros. Right? For bonus, if
  • 19:49 - 19:53
    you see here, my arguments are
  • 19:53 - 19:56
    VP and bonus. Right? Two arguments--those are
  • 19:56 - 19:57
    showing up here. This is
  • 19:57 - 19:59
    the macro definition. This is the error
  • 19:59 - 20:02
    message you set for this macro. This
  • 20:02 - 20:03
    is the validation you have set for
  • 20:03 - 20:06
    this macro. And iseval is telling
  • 20:06 - 20:08
    you whether this macro is eval-based or
  • 20:08 - 20:12
    not. As our last macro, getjournal_or_main,
  • 20:12 - 20:14
    is eval-based, that's why iseval is
  • 20:14 - 20:16
    1. For the rest of the two macros, iseval
  • 20:16 - 20:21
    is 0. Okay? Now, you can call a
  • 20:21 - 20:24
    macro inside another macro. So, to
  • 20:24 - 20:27
    do that, let's do this exercise where we
  • 20:27 - 20:28
    have created this totalsalary, right?
  • 20:28 - 20:30
    And also bonus.
  • 20:30 - 20:32
    So, inside totalsalary, we'll try to
  • 20:32 - 20:35
    call this bonus one. To do that, I'll
  • 20:35 - 20:39
    do one thing. I'll clone this totalsalary macro.
  • 20:39 - 20:40
    No--let's just
  • 20:40 - 20:51
    get a new one only. Let's say, totalsalary_v2 (version two).
  • 20:51 - 20:54
    Okay? So, what I will do--it will
  • 20:54 - 20:57
    not be an eval-based macro. Let's say my
  • 20:57 - 21:02
    previous one was eval, right? So,
  • 21:02 - 21:06
    it has two arguments: basic_salary and
  • 21:06 - 21:11
    variable_percent. Right? Now, I'll be
  • 21:11 - 21:14
    adding this bonus as well. So, bonus
  • 21:14 - 21:16
    requires another extra input called
  • 21:16 - 21:19
    bonus, right? So, I'll be adding this as
  • 21:19 - 21:23
    an extra input--comma, this. So, my total
  • 21:23 - 21:27
    macro inputs will be three. basic_salary
  • 21:27 - 21:30
    and variable_percent will be used
  • 21:30 - 21:33
    here, and variable_percent and bonus will be
  • 21:33 - 21:36
    used in another eval statement,
  • 21:36 - 21:38
    which you'll be calling in this
  • 21:38 - 21:41
    bonus macro. Right? So, in this case, I'll
  • 21:41 - 21:44
    be calling this bonus macro inside. So,
  • 21:44 - 21:49
    I'll set this one—pipe, then my bonus
  • 21:49 - 21:52
    macro. bonus macro takes two arguments.
  • 21:52 - 21:55
    One is variable_percent, and another is
  • 21:55 - 22:00
    bonus. So, variable_percent I'll pass, and
  • 22:00 - 22:04
    I'll pass this bonus as well. So, ideally,
  • 22:04 - 22:07
    the behavior should be the same as we have
  • 22:07 - 22:11
    seen before. Right? So,
  • 22:11 - 22:19
    done. Tick. Okay. Now, let's save it. Okay.
  • 22:19 - 22:22
    So, totalsalary_v2. So, before we do
  • 22:22 - 22:27
    that, let us run our macro. So, I'll
  • 22:27 - 22:32
    just copy-paste that code portion here.
  • 22:32 - 22:36
    Right. So, I'll just copy-paste this code
  • 22:36 - 22:38
    portion here.
  • 22:38 - 22:41
    It's mac_totalsalary_non--okay, I
  • 22:41 - 22:46
    think my macro is totalsalary. It
  • 22:54 - 23:00
    It has mismatched parenthesis. Okay. Okay. So,
  • 23:00 - 23:03
    it has created this one. In this new
  • 23:03 - 23:06
    field, in the new search field, what I'll
  • 23:06 - 23:10
    do is--after this one--
  • 23:10 - 23:14
    I'll be calling my totalsalary_v2 macro.
  • 23:14 - 23:16
    Okay? Where it is doing
  • 23:16 - 23:18
    the work of both of these two--bonus and
  • 23:18 - 23:22
    totalsalary. Right? So, totalsalary_v2.
  • 23:22 - 23:28
    Okay. So, my tick--v2. v2
  • 23:28 - 23:30
    requires three arguments. One
  • 23:30 - 23:32
    is my basic_salary, variable_percent, and
  • 23:32 - 23:35
    the similar percent value--I'll give 8
  • 23:35 - 23:37
    so that we can at least compare
  • 23:37 - 23:41
    the outputs. So, if I run this one--see--
  • 23:41 - 23:43
    it is working in the same way,
  • 23:43 - 23:45
    where it is getting this bonus field and
  • 23:45 - 23:47
    totalsalary. And if you see, the output
  • 23:47 - 23:50
    is same as well.
  • 23:50 - 23:54
    $15.22. We have given 7 here. Okay.
  • 23:54 - 23:58
    Let's run it with 7 only. So,
  • 23:58 - 24:02
    if you see--15.22, 20.22, 17.10, 17 like this.
  • 24:02 - 24:06
    Okay? So, this is how you can call a macro
  • 24:06 - 24:08
    inside another macro as well. So, if you
  • 24:08 - 24:12
    see here, by this structure, you
  • 24:12 - 24:15
    can achieve a very complex structure as
  • 24:15 - 24:18
    well--a complex structure which
  • 24:18 - 24:20
    is basically a reusable component in
  • 24:20 - 24:24
    Splunk search queries. Okay? So, this is what
  • 24:24 - 24:27
    you need to know, basically, to deal with
  • 24:27 - 24:30
    macros. And macros are very useful in
  • 24:30 - 24:33
    Splunk because not only do they give
  • 24:33 - 24:36
    you the modularity approach, but also
  • 24:36 - 24:39
    they give you a very short
  • 24:39 - 24:42
    search string and very readable code as well.
  • 24:42 - 24:44
    Okay? See you in the next video.
Title:
Splunk Basic : Everything to know about macros
Description:

more » « less
Video Language:
English
Duration:
24:44

English subtitles

Revisions Compare revisions