events.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. .. _event-system:
  2. ============
  3. Event System
  4. ============
  5. The Salt Event System is used to fire off events enabling third party
  6. applications or external processes to react to behavior within Salt.
  7. The event system uses a publish-subscribe pattern, otherwise know as pub/sub.
  8. Event Bus
  9. =========
  10. The event system is comprised of a two primary components, which make up the
  11. concept of an Event Bus:
  12. - The event sockets, which publish events
  13. - The event library, which can listen to events and send events into the salt system
  14. Events are published onto the event bus and event bus subscribers listen for the
  15. published events.
  16. The event bus is used for both inter-process communication as well as network transport
  17. in Salt. Inter-process communication is provided through UNIX domain sockets (UDX).
  18. The Salt Master and each Salt Minion has their own event bus.
  19. Event types
  20. ===========
  21. .. toctree::
  22. :maxdepth: 2
  23. master_events
  24. Listening for Events
  25. ====================
  26. Salt's event system is used heavily within Salt and it is also written to
  27. integrate heavily with existing tooling and scripts. There is a variety of
  28. ways to consume it.
  29. From the CLI
  30. ------------
  31. The quickest way to watch the event bus is by calling the :py:func:`state.event
  32. runner <salt.runners.state.event>`:
  33. .. code-block:: bash
  34. salt-run state.event pretty=True
  35. That runner is designed to interact with the event bus from external tools and
  36. shell scripts. See the documentation for more examples.
  37. Remotely via the REST API
  38. -------------------------
  39. Salt's event bus can be consumed
  40. :py:class:`salt.netapi.rest_cherrypy.app.Events` as an HTTP stream from
  41. external tools or services.
  42. .. code-block:: bash
  43. curl -SsNk https://salt-api.example.com:8000/events?token=05A3
  44. From Python
  45. -----------
  46. Python scripts can access the event bus only as the same system user that Salt
  47. is running as.
  48. The event system is accessed via the event library and can only be accessed
  49. by the same system user that Salt is running as. To listen to events a
  50. SaltEvent object needs to be created and then the get_event function needs to
  51. be run. The SaltEvent object needs to know the location that the Salt Unix
  52. sockets are kept. In the configuration this is the ``sock_dir`` option. The
  53. ``sock_dir`` option defaults to "/var/run/salt/master" on most systems.
  54. The following code will check for a single event:
  55. .. code-block:: python
  56. import salt.config
  57. import salt.utils.event
  58. opts = salt.config.client_config('/etc/salt/master')
  59. event = salt.utils.event.get_event(
  60. 'master',
  61. sock_dir=opts['sock_dir'],
  62. transport=opts['transport'],
  63. opts=opts)
  64. data = event.get_event()
  65. Events will also use a "tag". Tags allow for events to be filtered by prefix.
  66. By default all events will be returned. If only authentication events are
  67. desired, then pass the tag "salt/auth".
  68. The ``get_event`` method has a default poll time assigned of 5 seconds. To
  69. change this time set the "wait" option.
  70. The following example will only listen for auth events and will wait for 10 seconds
  71. instead of the default 5.
  72. .. code-block:: python
  73. data = event.get_event(wait=10, tag='salt/auth')
  74. To retrieve the tag as well as the event data, pass ``full=True``:
  75. .. code-block:: python
  76. evdata = event.get_event(wait=10, tag='salt/job', full=True)
  77. tag, data = evdata['tag'], evdata['data']
  78. Instead of looking for a single event, the ``iter_events`` method can be used to
  79. make a generator which will continually yield salt events.
  80. The iter_events method also accepts a tag but not a wait time:
  81. .. code-block:: python
  82. for data in event.iter_events(tag='salt/auth'):
  83. print(data)
  84. And finally event tags can be globbed, such as they can be in the Reactor,
  85. using the fnmatch library.
  86. .. code-block:: python
  87. import fnmatch
  88. import salt.config
  89. import salt.utils.event
  90. opts = salt.config.client_config('/etc/salt/master')
  91. sevent = salt.utils.event.get_event(
  92. 'master',
  93. sock_dir=opts['sock_dir'],
  94. transport=opts['transport'],
  95. opts=opts)
  96. while True:
  97. ret = sevent.get_event(full=True)
  98. if ret is None:
  99. continue
  100. if fnmatch.fnmatch(ret['tag'], 'salt/job/*/ret/*'):
  101. do_something_with_job_return(ret['data'])
  102. Firing Events
  103. =============
  104. It is possible to fire events on either the minion's local bus or to fire
  105. events intended for the master.
  106. To fire a local event from the minion on the command line call the
  107. :py:func:`event.fire <salt.modules.event.fire>` execution function:
  108. .. code-block:: bash
  109. salt-call event.fire '{"data": "message to be sent in the event"}' 'tag'
  110. To fire an event to be sent up to the master from the minion call the
  111. :py:func:`event.send <salt.modules.event.send>` execution function. Remember
  112. YAML can be used at the CLI in function arguments:
  113. .. code-block:: bash
  114. salt-call event.send 'myco/mytag/success' '{success: True, message: "It works!"}'
  115. If a process is listening on the minion, it may be useful for a user on the
  116. master to fire an event to it:
  117. .. code-block:: python
  118. # Job on minion
  119. import salt.utils.event
  120. event = salt.utils.event.MinionEvent(**__opts__)
  121. for evdata in event.iter_events(tag='customtag/'):
  122. return evdata # do your processing here...
  123. .. code-block:: bash
  124. salt minionname event.fire '{"data": "message for the minion"}' 'customtag/african/unladen'
  125. Firing Events from Python
  126. =========================
  127. From Salt execution modules
  128. ---------------------------
  129. Events can be very useful when writing execution modules, in order to inform
  130. various processes on the master when a certain task has taken place. This is
  131. easily done using the normal cross-calling syntax:
  132. .. code-block:: python
  133. # /srv/salt/_modules/my_custom_module.py
  134. def do_something():
  135. '''
  136. Do something and fire an event to the master when finished
  137. CLI Example::
  138. salt '*' my_custom_module:do_something
  139. '''
  140. # do something!
  141. __salt__['event.send']('myco/my_custom_module/finished', {
  142. 'finished': True,
  143. 'message': "The something is finished!",
  144. })
  145. From Custom Python Scripts
  146. --------------------------
  147. Firing events from custom Python code is quite simple and mirrors how it is
  148. done at the CLI:
  149. .. code-block:: python
  150. import salt.client
  151. caller = salt.client.Caller()
  152. caller.sminion.functions['event.send'](
  153. 'myco/myevent/success',
  154. {
  155. 'success': True,
  156. 'message': "It works!",
  157. }
  158. )