How to include the ctx.payload.value in the email text for watcher alert?

Hi @Patr123

Going back to your very first post where you were so close why did you not put the same transform script that you have in the log action into the email action as well? Seems like it is missing since it is inside the action context I don't think those value atr available for the email action.

You can see in the output of your simulate that those values are there for the log action but not for the email action so try putting that transform script in the email action as well.

"transform": {
                "script": {
                    "source": "def last_period=ctx.payload.aggregations.periods.buckets.last_period.hosts.buckets.stream().map(e -> e.key).collect(Collectors.toList()); return ctx.payload.aggregations.periods.buckets.history.hosts.buckets.stream().map(e -> e.key).filter(p -> !last_period.contains(p)).collect(Collectors.toList());",
                    "lang": "painless"
                }
            },

If that works then perhaps you can pull it out side as a transform although that will overwrite the entire payload. Which is what I explained in the other thread.

If you put it at the bottom outside the actions it should be available for both actions see here. But be careful as it over writes the existing payload. The other thread showed how to do that.

After the change my action block looks like:

"actions": {
    "log": {
      "transform": {
        "script": {
          "source": "def last_period=ctx.payload.aggregations.periods.buckets.last_period.hosts.buckets.stream().map(e -> e.key).collect(Collectors.toList()); return ctx.payload.aggregations.periods.buckets.history.hosts.buckets.stream().map(e -> e.key).filter(p -> !last_period.contains(p)).collect(Collectors.toList());",
          "lang": "painless"
        }
      },
      "logging": {
        "level": "info",
        "text": "Systems not responding in the last {{ctx.metadata.last_period}} minutes:{{#ctx.payload._value}}{{.}}:{{/ctx.payload._value}}"
      }
    },
    "email_administrator": {
            "email": {
			"transform": {
                "script": {
                    "source": "def last_period=ctx.payload.aggregations.periods.buckets.last_period.hosts.buckets.stream().map(e -> e.key).collect(Collectors.toList()); return ctx.payload.aggregations.periods.buckets.history.hosts.buckets.stream().map(e -> e.key).filter(p -> !last_period.contains(p)).collect(Collectors.toList());",
                    "lang": "painless"
                }
            },
                "profile": "standard",
                "attachments": {
                    "attached_data": {
                        "data": {
                            "format": "json"
                        }
                    }
                },
                "from": "abc@xyx.com",
                "to": [
                    "test123@xyz.com"
                ],
                "subject": "Encountered {{ctx.payload.hits.total}} Metricbeat Agents not reporting in last 35 mins",
                "body": {
                    "text": "{{#ctx.payload._value}}{{.}}:{{/ctx.payload._value}}"
                }
            }
        }
    },

And I get cannot simulate watch error.

So I would try to take that script out of each of the the actions and put it at the bottom of the watcher right after the list of actions then it should be available for both the actions, which I think would be a better approach.

See how we did it here

Also it look like you put the transform in the wrong place in the email it should have gone right after this line I think

    "email_administrator": {
         <! --- Here 

That said I think doing what I said above (extract and make common) is a better approach in fact I am not even sure if you can transform the payload more than once.

Thank you so much. That did the trick. I added the transform script right after "email_administrator": { part and it worked.
I also tried the first approach which you mentioned and added the transform script at the end of the actions list and that worked too.
Thank you again.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.