salt.utils.event - fire_event

294 views
Skip to first unread message

Łukasz Wróblewski

unread,
Apr 1, 2013, 12:30:55 PM4/1/13
to salt-...@googlegroups.com
Hi,

How to generate event on the minion with salt.utils.event so as to reach the master?

I would like to use the Salt Events, to transfer third-party application logs.


Receiving is clear:

Thomas S Hatch

unread,
Apr 1, 2013, 1:04:59 PM4/1/13
to salt-...@googlegroups.com
Take a look at event.fire_master in the event execution module, you can follow this code in your application, of call out to this function inside Salt:
https://github.com/saltstack/salt/blob/develop/salt/modules/event.py#L11

Thomas S. Hatch  |  Founder, CTO


5272 South College Drive, Suite 301 | Murray, UT 84123


--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Łukasz Wróblewski

unread,
Apr 1, 2013, 1:40:46 PM4/1/13
to salt-...@googlegroups.com
Thanks a lot.

Łukasz Wróblewski

unread,
Nov 18, 2013, 7:01:17 AM11/18/13
to salt-...@googlegroups.com
Hi,

Where can I get the dictionary __opts__?


>>> import salt.modules.event

>>> salt.modules.event.fire_master('test', 'test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/salt/modules/event.py", line 27, in fire_master
    auth = salt.crypt.SAuth(__opts__)
NameError: global name '__opts__' is not defined

Seth House

unread,
Nov 18, 2013, 9:03:09 AM11/18/13
to salt-...@googlegroups.com

import salt.config
__opts__ = salt.config.client_config('/etc/salt/master')

Łukasz Wróblewski

unread,
Nov 18, 2013, 9:09:09 AM11/18/13
to salt-...@googlegroups.com
Cool, but:
KeyError: 'master_uri'     in auth = salt.crypt.SAuth(__opts__)






2013/11/18 Seth House <se...@eseth.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Salt-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/salt-users/VsgCpmLL9Jc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to salt-users+...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Łukasz Wróblewski
http://www.nri.pl/ - Nowoczesne Rozwiązania Internetowe
http://www.hostowisko.pl/ - Profesjonalny i tani hosting
http://www.katalog-polskich-firm.pl/ - Najlepszy darmowy katalog firm

Seth House

unread,
Nov 18, 2013, 9:11:35 AM11/18/13
to salt-...@googlegroups.com

Ah, right. Sorry. Serves me right for trying to answer questions from my phone. :-P

John Spray

unread,
Nov 18, 2013, 9:37:21 AM11/18/13
to salt-...@googlegroups.com
Things like __opts__ and __salt__ are populated by the salt module loader: if you load the modules directly by just using "import" then things will not work.

Two ways that will work:
 * From another salt module, use __salt__['event.fire_master']("mydata", 'my/tag')
 * From the command line, use "salt-call event.fire_master mydata my/tag"

John Spray

unread,
Nov 18, 2013, 9:38:47 AM11/18/13
to salt-...@googlegroups.com
Oh, I just read the nested bits of your original email, you're calling from a third party application so my response probably isn't that helpful.

John

Joseph Hall

unread,
Nov 18, 2013, 9:54:58 AM11/18/13
to salt-...@googlegroups.com
You don't need __opts__ to fire an event; you just need to know the value that __opts__ would have been providing you. In this case, it just needs to know the sock_dir on the master.


[root@dufresne ~]# ipython2 
Python 2.7.5 (default, Sep  6 2013, 09:55:21) 
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import salt.utils.event

In [2]: event = salt.utils.event.SaltEvent('master', '/var/run/salt/master')

In [3]: event.fire_event({'key1': 'val1', 'key2': 'val2'}, 'mytag')
Out[3]: True

In [4]: 


Don't overthink the event system. It's easy to use, even from third party applications.
--
"In order to create, you have to have the willingness, the desire to be challenged, to be learning." -- Ferran Adria (speaking at Harvard, 2011)

Łukasz Wróblewski

unread,
Nov 18, 2013, 10:01:50 AM11/18/13
to salt-...@googlegroups.com

from salt.crypt import SAuth
from salt.payload import SREQ
from salt.config import minion_config
from salt.minion import resolve_dns

conf = minion_config('/etc/salt/minion')
conf.update(resolve_dns(conf))
auth = SAuth(conf)
sreq = SREQ(conf['master_uri'])
sreq.send('aes', auth.crypticle.dumps(payload))



Would be nice if this could get into some salt.utils...








2013/11/18 John Spray <john....@inktank.com>

Łukasz Wróblewski

unread,
Nov 18, 2013, 10:08:23 AM11/18/13
to salt-...@googlegroups.com
import salt.utils.event

event = salt.utils.event.SaltEvent('master', '/var/run/salt/master')
event.fire_event({'key1': 'val1', 'key2': 'val2'}, 'mytag')


It looks great, I would like for this to work.
My application is on minion and I want to send something to the master.
This code executes without errors (on minion), but does not transmit anything to the master.


# ll /var/run/salt
total 0
0 drwxr-xr-x  3 root root  60 Oct 24 05:55 ./
0 drwxr-xr-x 14 root root 560 Nov 18 10:02 ../
0 drwxr-xr-x  2 root root  80 Nov 18 06:26 minion/

Maybe lack of /var/run/salt/master is the problem.





2013/11/18 Joseph Hall <perl...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Salt-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/salt-users/VsgCpmLL9Jc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to salt-users+...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--

Markus Kramer

unread,
Mar 4, 2016, 5:27:02 PM3/4/16
to Salt-users

It is well documented but I found this thread first.

The below program has very good performance for Windows (I have no UNIX minion).
So far I use    salt-call event.send  but it has a very noticeable ramp-up on Windows.

event.fire_master   and   event.send   have about the same performance.

# Generate event on minion so as to reach the master
# On the master, use   `salt-run state.event pretty=True`  to watch the event
# https://docs.saltstack.com/en/latest/ref/clients/index.html#salt-s-loader-interface

import salt.config
import salt.loader
import platform

if platform.system() == 'Windows':
    __opts__ = salt.config.minion_config('/salt/conf/minion')
else:
    __opts__ = salt.config.minion_config('/etc/salt/minion') # WRONG for BSD
   
event_module = salt.loader.raw_mod(__opts__, 'event', None)
print event_module['event.send']('ccccccccccc', data={'aaaaaaaaa': 'bbbbbbbbbbb'})
#print event_module['event.fire_master']({'aaaaaaaaa': 'bbbbbbbbbbb'},'ccccccccccc_____fire_master')
Reply all
Reply to author
Forward
0 new messages