1
0

index.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. .. _beacons:
  2. =======
  3. Beacons
  4. =======
  5. Beacons let you use the Salt event system to monitor non-Salt processes. The
  6. beacon system allows the minion to hook into a variety of system processes and
  7. continually monitor these processes. When monitored activity occurs in a system
  8. process, an event is sent on the Salt event bus that can be used to trigger a
  9. :ref:`reactor <reactor>`.
  10. Salt beacons can currently monitor and send Salt events for many system
  11. activities, including:
  12. - file system changes
  13. - system load
  14. - service status
  15. - shell activity, such as user login
  16. - network and disk usage
  17. See :ref:`beacon modules <all-salt.beacons>` for a current list.
  18. .. note::
  19. Salt beacons are an event generation mechanism. Beacons leverage the Salt
  20. :ref:`reactor <reactor>` system to make changes when beacon events occur.
  21. Configuring Beacons
  22. ===================
  23. Salt beacons do not require any changes to the system components that are being
  24. monitored, everything is configured using Salt.
  25. Beacons are typically enabled by placing a ``beacons:`` top level block in
  26. ``/etc/salt/minion`` or any file in ``/etc/salt/minion.d/`` such as
  27. ``/etc/salt/minion.d/beacons.conf`` or add it to pillars for that minion:
  28. .. code-block:: yaml
  29. beacons:
  30. inotify:
  31. - files:
  32. /etc/important_file: {}
  33. /opt: {}
  34. The beacon system, like many others in Salt, can also be configured via the
  35. minion pillar, grains, or local config file.
  36. .. note::
  37. The `inotify` beacon only works on OSes that have `inotify` kernel support.
  38. Currently this excludes FreeBSD, macOS, and Windows.
  39. All beacon configuration is done using list based configuration.
  40. Beacon Monitoring Interval
  41. --------------------------
  42. Beacons monitor on a 1-second interval by default. To set a different interval,
  43. provide an ``interval`` argument to a beacon. The following beacons run on 5-
  44. and 10-second intervals:
  45. .. code-block:: yaml
  46. beacons:
  47. inotify:
  48. - files:
  49. /etc/important_file: {}
  50. /opt: {}
  51. - interval: 5
  52. - disable_during_state_run: True
  53. load:
  54. - averages:
  55. 1m:
  56. - 0.0
  57. - 2.0
  58. 5m:
  59. - 0.0
  60. - 1.5
  61. 15m:
  62. - 0.1
  63. - 1.0
  64. - interval: 10
  65. .. _avoid-beacon-event-loops:
  66. Avoiding Event Loops
  67. --------------------
  68. It is important to carefully consider the possibility of creating a loop
  69. between a reactor and a beacon. For example, one might set up a beacon which
  70. monitors whether a file is read which in turn fires a reactor to run a state
  71. which in turn reads the file and re-fires the beacon.
  72. To avoid these types of scenarios, the ``disable_during_state_run`` argument
  73. may be set. If a state run is in progress, the beacon will not be run on its
  74. regular interval until the minion detects that the state run has completed, at
  75. which point the normal beacon interval will resume.
  76. .. code-block:: yaml
  77. beacons:
  78. inotify:
  79. - files:
  80. /etc/important_file: {}
  81. - disable_during_state_run: True
  82. .. _beacon-example:
  83. .. note::
  84. For beacon writers: If you need extra stuff to happen, like closing file
  85. handles for the ``disable_during_state_run`` to actually work, you can add
  86. a `close()` function to the beacon to run those extra things. See the
  87. `inotify` beacon.
  88. Beacon Example
  89. ==============
  90. This example demonstrates configuring the :py:mod:`~salt.beacons.inotify`
  91. beacon to monitor a file for changes, and then restores the file to its
  92. original contents if a change was made.
  93. .. note::
  94. The inotify beacon requires Pyinotify on the minion, install it using
  95. ``salt myminion pkg.install python-inotify``.
  96. Create Watched File
  97. -------------------
  98. Create the file named ``/etc/important_file`` and add some simple content:
  99. .. code-block:: yaml
  100. important_config: True
  101. Add Beacon Configs to Minion
  102. ----------------------------
  103. On the Salt minion, add the following configuration to
  104. ``/etc/salt/minion.d/beacons.conf``:
  105. .. code-block:: yaml
  106. beacons:
  107. inotify:
  108. - files:
  109. /etc/important_file:
  110. mask:
  111. - modify
  112. - disable_during_state_run: True
  113. Save the configuration file and restart the minion service. The beacon is now
  114. set up to notify salt upon modifications made to the file.
  115. .. note::
  116. The ``disable_during_state_run: True`` parameter :ref:`prevents
  117. <avoid-beacon-event-loops>` the inotify beacon from generating reactor
  118. events due to salt itself modifying the file.
  119. .. _beacon-event-bus:
  120. View Events on the Master
  121. -------------------------
  122. On your Salt master, start the event runner using the following command:
  123. .. code-block:: bash
  124. salt-run state.event pretty=true
  125. This runner displays events as they are received by the master on the Salt
  126. event bus. To test the beacon you set up in the previous section, make and save
  127. a modification to ``/etc/important_file``. You'll see an event similar to the
  128. following on the event bus:
  129. .. code-block:: json
  130. {
  131. "_stamp": "2015-09-09T15:59:37.972753",
  132. "data": {
  133. "change": "IN_IGNORED",
  134. "id": "larry",
  135. "path": "/etc/important_file"
  136. },
  137. "tag": "salt/beacon/larry/inotify//etc/important_file"
  138. }
  139. This indicates that the event is being captured and sent correctly. Now you can
  140. create a reactor to take action when this event occurs.
  141. Create a Reactor
  142. ----------------
  143. This reactor reverts the file named ``/etc/important_file`` to the contents
  144. provided by salt each time it is modified.
  145. Reactor SLS
  146. ```````````
  147. On your Salt master, create a file named ``/srv/reactor/revert.sls``.
  148. .. note::
  149. If the ``/srv/reactor`` directory doesn't exist, create it.
  150. .. code-block:: bash
  151. mkdir -p /srv/reactor
  152. Add the following to ``/srv/reactor/revert.sls``:
  153. .. code-block:: yaml
  154. revert-file:
  155. local.state.apply:
  156. - tgt: {{ data['data']['id'] }}
  157. - arg:
  158. - maintain_important_file
  159. .. note::
  160. In addition to :ref:`setting <avoid-beacon-event-loops>`
  161. ``disable_during_state_run: True`` for an inotify beacon whose reaction is
  162. to modify the watched file, it is important to ensure the state applied is
  163. also :term:`idempotent`.
  164. .. note::
  165. The expression ``{{ data['data']['id'] }}`` :ref:`is correct
  166. <beacons-and-reactors>` as it matches the event structure :ref:`shown above
  167. <beacon-event-bus>`.
  168. State SLS
  169. `````````
  170. Create the state sls file referenced by the reactor sls file. This state file
  171. will be located at ``/srv/salt/maintain_important_file.sls``.
  172. .. code-block:: yaml
  173. important_file:
  174. file.managed:
  175. - name: /etc/important_file
  176. - contents: |
  177. important_config: True
  178. Master Config
  179. `````````````
  180. Configure the master to map the inotify beacon event to the ``revert`` reaction
  181. in ``/etc/salt/master.d/reactor.conf``:
  182. .. code-block:: yaml
  183. reactor:
  184. - salt/beacon/*/inotify//etc/important_file:
  185. - /srv/reactor/revert.sls
  186. .. note::
  187. You can have only one top level ``reactor`` section, so if one already
  188. exists, add this code to the existing section. See :ref:`here
  189. <reactor-sls>` to learn more about reactor SLS syntax.
  190. Start the Salt Master in Debug Mode
  191. -----------------------------------
  192. To help with troubleshooting, start the Salt master in debug mode:
  193. .. code-block:: bash
  194. service salt-master stop
  195. salt-master -l debug
  196. When debug logging is enabled, event and reactor data are displayed so you can
  197. discover syntax and other issues.
  198. Trigger the Reactor
  199. -------------------
  200. On your minion, make and save another change to ``/etc/important_file``. On the
  201. Salt master, you'll see debug messages that indicate the event was received and
  202. the ``state.apply`` job was sent. When you inspect the file on the minion,
  203. you'll see that the file contents have been restored to ``important_config:
  204. True``.
  205. All beacons are configured using a similar process of enabling the beacon,
  206. writing a reactor SLS (and state SLS if needed), and mapping a beacon event to
  207. the reactor SLS.
  208. .. _writing-beacons:
  209. Writing Beacon Plugins
  210. ======================
  211. Beacon plugins use the standard Salt loader system, meaning that many of the
  212. constructs from other plugin systems holds true, such as the ``__virtual__``
  213. function.
  214. The important function in the Beacon Plugin is the ``beacon`` function. When
  215. the beacon is configured to run, this function will be executed repeatedly by
  216. the minion. The ``beacon`` function therefore cannot block and should be as
  217. lightweight as possible. The ``beacon`` also must return a list of dicts, each
  218. dict in the list will be translated into an event on the master.
  219. Beacons may also choose to implement a ``validate`` function which
  220. takes the beacon configuration as an argument and ensures that it
  221. is valid prior to continuing. This function is called automatically
  222. by the Salt loader when a beacon is loaded.
  223. Please see the :py:mod:`~salt.beacons.inotify` beacon as an example.
  224. The `beacon` Function
  225. ---------------------
  226. The beacons system will look for a function named `beacon` in the module. If
  227. this function is not present then the beacon will not be fired. This function
  228. is called on a regular basis and defaults to being called on every iteration of
  229. the minion, which can be tens to hundreds of times a second. This means that
  230. the `beacon` function cannot block and should not be CPU or IO intensive.
  231. The beacon function will be passed in the configuration for the executed
  232. beacon. This makes it easy to establish a flexible configuration for each
  233. called beacon. This is also the preferred way to ingest the beacon's
  234. configuration as it allows for the configuration to be dynamically updated
  235. while the minion is running by configuring the beacon in the minion's pillar.
  236. The Beacon Return
  237. -----------------
  238. The information returned from the beacon is expected to follow a predefined
  239. structure. The returned value needs to be a list of dictionaries (standard
  240. python dictionaries are preferred, no ordered dicts are needed).
  241. The dictionaries represent individual events to be fired on the minion and
  242. master event buses. Each dict is a single event. The dict can contain any
  243. arbitrary keys but the 'tag' key will be extracted and added to the tag of the
  244. fired event.
  245. The return data structure would look something like this:
  246. .. code-block:: python
  247. [{'changes': ['/foo/bar'], 'tag': 'foo'},
  248. {'changes': ['/foo/baz'], 'tag': 'bar'}]
  249. Calling Execution Modules
  250. -------------------------
  251. Execution modules are still the preferred location for all work and system
  252. interaction to happen in Salt. For this reason the `__salt__` variable is
  253. available inside the beacon.
  254. Please be careful when calling functions in `__salt__`, while this is the
  255. preferred means of executing complicated routines in Salt not all of the
  256. execution modules have been written with beacons in mind. Watch out for
  257. execution modules that may be CPU intense or IO bound. Please feel free to add
  258. new execution modules and functions to back specific beacons.
  259. Distributing Custom Beacons
  260. ---------------------------
  261. Custom beacons can be distributed to minions via the standard methods, see
  262. :ref:`Modular Systems <modular-systems>`.