How to pass event params from reactor to state file

326 views
Skip to first unread message

Rainer Krienke

unread,
Jul 20, 2018, 4:53:34 AM7/20/18
to Salt-users
Hello,

I have a beacon that watches disk space on minions, a reactor master config, a  reactor that manages the event and connects it to a state file:

# beacon.sls:
diskusage:
  - '/': 50%
  - interval: 30

# reactor.conf:
reactor:
  - 'salt/beacon/*/diskusage/':
    - /srv/reactor/base/filesystem.sls

# /srv/reactor/base/filesystem.sls
full_fs:
  local.state.apply:
    - tgt: 'saltmaster.mydomain.de'
    - args:
      - inform_full_filesystem

# /srv/salt/base/inform_full_filesystem.sls
email-on-failure:
  smtp.send_msg:
    - name: This is my message
    - profile: mySmtpServer # defined in default pillar
    - recipient: mym...@mydomain.de
    - subject: test beacons

This basically works, I get emails from minions for which the beacon is activated. 

My problem is how do I pass the events paramters that /srv/reactor/base/filesystem.sls gets to the state file /srv/salt/base/inform_full_filesystem, so that the email sent can tell me from which hosts  the event came i.e which host has a full filesystem. So I would like to write something in the subject of the sent email line like this:

email-on-failure:
    # ...
    - subject: Host {{ failed_host }}, filesystem {{ filesystem }} is full
 
I searched a lot, but did not find a working example for reactor with state files.

Any ideas?

Thanks a lot
Rainer

Daniel Wallace

unread,
Jul 20, 2018, 8:57:00 AM7/20/18
to Salt-users
all of the data that the beacon returns will be available in the `{{data}}` object, you can see that object by running `salt-run state.event pretty=True tagmatch="saslt/beacon/*/diskusage/` and then having the beacon fire an event.

Everything in the {} in the event is in the event output is in the {{data}} object.

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/fd84512f-a2af-47a2-823c-a0fdb8393005%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rainer Krienke

unread,
Jul 20, 2018, 11:28:08 AM7/20/18
to salt-...@googlegroups.com
Hello Daniel,

thanks you very much for your answer.  Your answer covers one part of
what I would like to to know but I still struggle with salt and so I did
not yet find a syntactically and semantically valid way to provide the
eventdata {{ data }}  from the reactor 
(/srv/reactor/base/filesystem.sls)  to the statefile I reference in the
reactor: inform_full_filesystem.sls.

So I would like to access the event data contained in {{ data  }} in the
state file in inform_full_filesystem.sls, eg to get the hostname that
triggered this event. I read that this can be done via pillars, but what
I tried up to now did not work. One not working example I found
somewhere in the net which I tried to adopt to my needs is this here :

full_fs:
  local.state.apply:
    - tgt: 'salt.uni-koblenz.de'
    - args:
      - inform_full_filesystem
      - pillar:
          myid: {{ data['id'] }}
          anotherValue: {{ data['anotherValue'] }}
          ...

This resulted in an error about a wrong number of arguments for
local.state.apply (one instead of two). And even if the number of
argumnets would have been correct how would access to the defined pillar
(since it has no name)  have been possible  in inform_full_filesystem.sls?
 
Can you please give an example of how this can be done correctly?

Thanks a lot
Rainer


Am 20.07.2018 um 14:56 schrieb Daniel Wallace:
> all of the data that the beacon returns will be available in the
> `{{data}}` object, you can see that object by running `salt-run
> state.event pretty=True tagmatch="saslt/beacon/*/diskusage/` and then
> having the beacon fire an event.
>
> Everything in the {} in the event is in the event output is in the
> {{data}} object.
>
> On Fri, Jul 20, 2018 at 3:53 AM 'Rainer Krienke' via Salt-users
> <salt-...@googlegroups.com <mailto:salt-...@googlegroups.com>> wrote:
>
> Hello,
>
> I have a beacon that watches disk space on minions, a reactor
> master config, a  reactor that manages the event and connects it
> to a state file:
>
> # beacon.sls:
> diskusage:
>   - '/': 50%
>   - interval: 30
>
> # reactor.conf:
> reactor:
>   - 'salt/beacon/*/diskusage/':
>     - /srv/reactor/base/filesystem.sls
>
> # /srv/reactor/base/filesystem.sls
> full_fs:
>   local.state.apply:
>     - tgt: 'saltmaster.mydomain.de <http://saltmaster.mydomain.de>'
>     - args:
>       - inform_full_filesystem
>
> # /srv/salt/base/inform_full_filesystem.sls
> email-on-failure:
>   smtp.send_msg:
>     - name: This is my message
>     - profile: mySmtpServer # defined in default pillar
>     - recipient: mym...@mydomain.de <mailto:mym...@mydomain.de>
>     - subject: test beacons
>
> This basically works, I get emails from minions for which the
> beacon is activated. 
>
> My problem is how do I pass the events paramters that
> /srv/reactor/base/filesystem.sls gets to the state file
> /srv/salt/base/inform_full_filesystem, so that the email sent can
> tell me from which hosts  the event came i.e which host has a full
> filesystem. So I would like to write something in the subject of
> the sent email line like this:
>
> email-on-failure:
>     # ...
>     - subject: Host {{ failed_host }}, filesystem {{ filesystem }}
> is full
>  
> I searched a lot, but did not find a working example for reactor
> with state files.
>
> Any ideas?
>
> Thanks a lot
> Rainer
> --
> You received this message because you are subscribed to the Google
> Groups "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to salt-users+...@googlegroups.com
> <mailto:salt-users+...@googlegroups.com>.
> <https://groups.google.com/d/msgid/salt-users/fd84512f-a2af-47a2-823c-a0fdb8393005%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to salt-users+...@googlegroups.com
> <mailto:salt-users+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/salt-users/CAA2%2B9hCAFLsGY5v-LvUxfZ9uTO%2BqC0HszayN%2BPxmdU9x4Hgj8A%40mail.gmail.com
> <https://groups.google.com/d/msgid/salt-users/CAA2%2B9hCAFLsGY5v-LvUxfZ9uTO%2BqC0HszayN%2BPxmdU9x4Hgj8A%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Rainer Krienke

unread,
Jul 24, 2018, 7:51:31 AM7/24/18
to Salt-users
Hello,

just in case someone else is having a similar problem. I was able to solve mine.

My fault in this case was that I exactly wanted to follow the tutorial for beacons and reactor. In this tutorial the reactor file under "arg:" has a state file that is used to set some desired state in case the reactor receives a diskusage beacon.

The reactor file gets all the event data in {{ data }} and my problem for my diskusage beacon  test was that I wanted to send an email alert not from the reactor but from the state file referenced in the reactor. Thus I needed a way to pass event data to this state file, something I was unable to do (if possible at all).

The solution was simply to drop the extra state file and instead using the reactor to do the job of sending an email in case a beacon for a disk over limit arrives. This reactor now looks like this:

full_fs:
  local.state.single:
    - tgt: {{ data['id'] }}
    - args:
      - fun: smtp.send_msg
      - subject: "Salt: Filesystem \"{{ data['mount'] }}\" nearly full on {{ data['id'] }}"
      - name: "Filesystem \"{{ data['mount'] }}\" nearly full on {{ data['id'] }}, current usage: {{ data['diskusage'] }}%"
      - profile: mySmtpServer
      - recipient: ad...@mydomain.de

This works great.

Thanks
Rainer
Reply all
Reply to author
Forward
0 new messages