events.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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", sock_dir=opts["sock_dir"], transport=opts["transport"], opts=opts
  61. )
  62. data = event.get_event()
  63. Events will also use a "tag". Tags allow for events to be filtered by prefix.
  64. By default all events will be returned. If only authentication events are
  65. desired, then pass the tag "salt/auth".
  66. The ``get_event`` method has a default poll time assigned of 5 seconds. To
  67. change this time set the "wait" option.
  68. The following example will only listen for auth events and will wait for 10 seconds
  69. instead of the default 5.
  70. .. code-block:: python
  71. data = event.get_event(wait=10, tag="salt/auth")
  72. To retrieve the tag as well as the event data, pass ``full=True``:
  73. .. code-block:: python
  74. evdata = event.get_event(wait=10, tag="salt/job", full=True)
  75. tag, data = evdata["tag"], evdata["data"]
  76. Instead of looking for a single event, the ``iter_events`` method can be used to
  77. make a generator which will continually yield salt events.
  78. The iter_events method also accepts a tag but not a wait time:
  79. .. code-block:: python
  80. for data in event.iter_events(tag="salt/auth"):
  81. print(data)
  82. And finally event tags can be globbed, such as they can be in the Reactor,
  83. using the fnmatch library.
  84. .. code-block:: python
  85. import fnmatch
  86. import salt.config
  87. import salt.utils.event
  88. opts = salt.config.client_config("/etc/salt/master")
  89. sevent = salt.utils.event.get_event(
  90. "master", sock_dir=opts["sock_dir"], transport=opts["transport"], opts=opts
  91. )
  92. while True:
  93. ret = sevent.get_event(full=True)
  94. if ret is None:
  95. continue
  96. if fnmatch.fnmatch(ret["tag"], "salt/job/*/ret/*"):
  97. do_something_with_job_return(ret["data"])
  98. Firing Events
  99. =============
  100. It is possible to fire events on either the minion's local bus or to fire
  101. events intended for the master.
  102. To fire a local event from the minion on the command line call the
  103. :py:func:`event.fire <salt.modules.event.fire>` execution function:
  104. .. code-block:: bash
  105. salt-call event.fire '{"data": "message to be sent in the event"}' 'tag'
  106. To fire an event to be sent up to the master from the minion call the
  107. :py:func:`event.send <salt.modules.event.send>` execution function. Remember
  108. YAML can be used at the CLI in function arguments:
  109. .. code-block:: bash
  110. salt-call event.send 'myco/mytag/success' '{success: True, message: "It works!"}'
  111. If a process is listening on the minion, it may be useful for a user on the
  112. master to fire an event to it. An example of listening local events on
  113. a minion on a non-Windows system:
  114. .. code-block:: python
  115. # Job on minion
  116. import salt.utils.event
  117. opts = salt.config.minion_config("/etc/salt/minion")
  118. event = salt.utils.event.MinionEvent(opts)
  119. for evdata in event.iter_events(match_type="regex", tag="custom/.*"):
  120. # do your processing here...
  121. ...
  122. And an example of listening local events on a Windows system:
  123. .. code-block:: python
  124. # Job on minion
  125. import salt.utils.event
  126. opts = salt.config.minion_config(salt.minion.DEFAULT_MINION_OPTS)
  127. event = salt.utils.event.MinionEvent(opts)
  128. for evdata in event.iter_events(match_type="regex", tag="custom/.*"):
  129. # do your processing here...
  130. ...
  131. .. code-block:: bash
  132. salt minionname event.fire '{"data": "message for the minion"}' 'customtag/african/unladen'
  133. Firing Events from Python
  134. =========================
  135. From Salt execution modules
  136. ---------------------------
  137. Events can be very useful when writing execution modules, in order to inform
  138. various processes on the master when a certain task has taken place. This is
  139. easily done using the normal cross-calling syntax:
  140. .. code-block:: python
  141. # /srv/salt/_modules/my_custom_module.py
  142. def do_something():
  143. """
  144. Do something and fire an event to the master when finished
  145. CLI Example::
  146. salt '*' my_custom_module:do_something
  147. """
  148. # do something!
  149. __salt__["event.send"](
  150. "myco/my_custom_module/finished",
  151. {"finished": True, "message": "The something is finished!",},
  152. )
  153. From Custom Python Scripts
  154. --------------------------
  155. Firing events from custom Python code is quite simple and mirrors how it is
  156. done at the CLI:
  157. .. code-block:: python
  158. import salt.client
  159. caller = salt.client.Caller()
  160. ret = caller.cmd(
  161. "event.send", "myco/event/success", {"success": True, "message": "It works!"}
  162. )
  163. if not ret:
  164. # the event could not be sent, process the error here
  165. ...