salt-stack-piggy-back-encrypted-zmq-event-bus

JSON

rev 34  |  foxhop  |  1394744287000  |  JSON

rev 33
rev 34
260On the master we could have a 3rd party script subscribed to the special channel260On the master we could have a 3rd party script subscribed to the special channel
> tag and perform further manipulation or persist the output somewhere.> tag and perform further manipulation or persist the output somewhere.
261261
tt262UPDATE: I wrote a <returner that tagged data back to masters zermq bus here <htt
 >p://russell.ballestrini.net/replace-the-nagios-scheduler-and-nrpe-with-salt-stac
 >k/>`_.
263 
262264
263references265references
rev 33  |  foxhop  |  1394744144000  |  JSON

rev 32
rev 33
267267
268268
nn269related work
270=================
269271
nn272https://github.com/felskrone/salt-eventsd
270273
tt274 
275 
276 
277 
278 
rev 32  |  foxhop  |  1393883178000  |  JSON

rev 31
rev 32
179 salt-call event.fire_master '{"data": "message for the master"}' 'mytag'179 salt-call event.fire_master '{"data": "message for the master"}' 'mytag'
180180
n181From My understanding the minion to master communication could work like this:n181From my understanding the minion to master communication could work like this:
182182
183#. A 3rd party script publishes to minion bus183#. A 3rd party script publishes to minion bus
n184#. salt-minion daemon sees the properly formatted "package" event and runs _firen184#. salt-minion daemon sees the properly formatted event tagged 'fire_master' and
>_master> runs _fire_master() method
185#. the "package" event  ends up on master bus185#. the event ends up on master bus
186#. A 3rd party script subscribes to the tag and gains access to the data186#. A 3rd party script listens to the master bus and subscribes (filters) on the 
 >nested tag and gains access to the message
187187
n188The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-n188The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-
>encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see>encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see
>ms my "package" format is incorrect so it never gets forwarded to the master ...>ms my event "package" format is incorrect so it never gets forwarded to the mast
 >er ...
189189
n190This is the area in the minion daemon loop that tests if the package/event shouln190This area in the salt-minion daemon loop has conditionals which determine if the
>d be forwarded ('fire_master'):> package/event should be forwarded.
191It seems to look for the 'fire_master' tag:
191192
192* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_ma193* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_ma
>ster' tag>ster' tag
193* https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_ma194* https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_ma
>ster' event>ster' event
tt195 
196 
194197
195emit_to_minion_bus_forward_to_master_bus.py198emit_to_minion_bus_forward_to_master_bus.py
rev 31  |  foxhop  |  1393882502000  |  JSON

rev 30
rev 31
224 ]224 ]
225225
t226# multi event example, supports a list of event ret objectst226 # multi event example, supports a list of event ret objects
227payload = {227 payload = {
228  'id': opts['id'],228   'id': opts['id'],
229  'events': events,229   'events': events,
230  'tag': None,230   'tag': None,
231  'pretag': None,231   'pretag': None,
232  'data': None232   'data': None
233}233 }
234 
235234
236 print (payload)235 print (payload)
rev 30  |  foxhop  |  1393882461000  |  JSON

rev 29
rev 30
188The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-188The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-
>encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see>encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see
>ms my "package" format is incorrect so it never gets forwarded to the master ...>ms my "package" format is incorrect so it never gets forwarded to the master ...
189189
n190This is the area in the minion daemon loop that tests if the package/event shouln190This is the area in the minion daemon loop that tests if the package/event shoul
>d be forwarded:>d be forwarded ('fire_master'):
191191
t192* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337t192* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_ma
 >ster' tag
193 193* https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_ma
 >ster' event
194194
195emit_to_minion_bus_forward_to_master_bus.py195emit_to_minion_bus_forward_to_master_bus.py
rev 29  |  foxhop  |  1393882355000  |  JSON

rev 28
rev 29
214214
215 tag = 'mytag'215 tag = 'mytag'
nn216 
217 # a list of event ret objects
218 events = [
219   {'message':'a drap in the bucket', 'tag': tag },
220   {'message':'a drep in the bucket', 'tag': tag },
221   {'message':'a drip in the bucket', 'tag': tag },
216 data = {'message':'a drop in the bucket', 'tag': tag }222   {'message':'a drop in the bucket', 'tag': tag },
223   {'message':'a drup in the bucket', 'tag': tag },
224 ]
217225
t218 # multi event example, supports a list of event ret objectst226# multi event example, supports a list of event ret objects
219 payload = {227payload = {
220   'id': opts['id'],228  'id': opts['id'],
221   'events': [ data  ],229  'events': events,
222   'tag': None,230  'tag': None,
223   'pretag': None,231  'pretag': None,
224   'data': None232  'data': None
225 }233}
234 
226235
227 print (payload)236 print (payload)
rev 28  |  foxhop  |  1393847549000  |  JSON

rev 27
rev 28
8Requires:8Requires:
99
t10* the salt-minion alive and running on the minion hostt10* the salt-minion daemon alive and running on the minion host
11* the salt-master alive and running on the master host11* the salt-master daemon alive and running on the master host
12* the 3rd party scripts must be run as the same user as the salt-minion or salt-12* the 3rd party scripts must execute as the same user who runs the salt-minion o
>master>r salt-master daemon
13  so the script has the ability to bind to the ZMQ sockets13  because the 3rd party script needs to bind to the ZMQ sockets managed by the d
 >aemons.
1414
15.. contents::15.. contents::
rev 27  |  foxhop  |  1393847419000  |  JSON

rev 26
rev 27
99
10* the salt-minion alive and running on the minion host10* the salt-minion alive and running on the minion host
t11* the salt-master alive and running on the master host t11* the salt-master alive and running on the master host
12* the 3rd party scripts must be run as the same user as the salt-minion or salt-
 >master
13  so the script has the ability to bind to the ZMQ sockets
1214
13.. contents::15.. contents::
rev 26  |  foxhop  |  1393846121000  |  JSON

rev 25
rev 26
191191
192192
tt193emit_to_minion_bus_forward_to_master_bus.py
194-------------------------------------------------------
195 
196.. code-block:: python
197 
198 # needed for config to opts processing
199 import os
200 import salt.syspaths as syspaths
201 import salt.config
202 
203 # get opts from minion config file, this function also looks in drop dir!
204 opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))
205 
206 # debug to STDOUT
207 import salt.log
208 salt.log.setup_console_logger('all')
209 
210 # event libary for events over ZMQ
211 import salt.utils.event
212 
213 tag = 'mytag'
214 data = {'message':'a drop in the bucket', 'tag': tag }
215 
216 # multi event example, supports a list of event ret objects
217 payload = {
218   'id': opts['id'],
219   'events': [ data  ],
220   'tag': None,
221   'pretag': None,
222   'data': None
223 }
224 
225 print (payload)
226 
227 # opts must valid minion ID else it binds to invalid socket
228 event = salt.utils.event.SaltEvent('minion', **opts)
229 
230 # Fire event payload with 'fire_master' tag which the
231 # salt-minion daemon will forward to the master event bus!
232 event.fire_event(payload, 'fire_master')
233 
193ideas234ideas
194============235============
rev 25  |  foxhop  |  1393782677000  |  JSON

rev 24
rev 25
194============194============
195195
n196I think it could be neat to build a returner which accepted a ZMQ "channel" tag.n196I think it could be neat to build a returner which accepted a ZMQ "channel" tag.
>  Then after a remote execution the and ret data could be passed from minion to >  Then after a remote execution the ret data could be passed from minion to mast
>master on a special channel.  Then we could have a 3rd party script on the salt >er on a special channel.  Then we could have a 3rd party script on the salt mast
>master that subscribes to this special channel and performs further processing o>er that subscribes to this special channel and performs further processing or pr
>r presents the data in a neat way.>esents the data in a neat way.
197197
198for example:198for example:
202 salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plug202 salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plug
>ins/check_procs -w 150 -c 200'>ins/check_procs -w 150 -c 200'
203203
n204The minion would see the remote execution command, run the job, get data back, an204The minion would see the remote execution command, run the job, get data back, a
>nd return to master on a special tagged channel.>nd return to master on a special channel tag.
205205
t206On the master we could have a 3rd party script subscribed to the special tagged t206On the master we could have a 3rd party script subscribed to the special channel
>channel and perform further manipulation or persist the output somewhere.> tag and perform further manipulation or persist the output somewhere.
207207
208208
rev 24  |  foxhop  |  1393772005000  |  JSON

rev 23
rev 24
186The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-186The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-
>encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see>encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see
>ms my "package" format is incorrect so it never gets forwarded to the master ...>ms my "package" format is incorrect so it never gets forwarded to the master ...
187187
tt188This is the area in the minion daemon loop that tests if the package/event shoul
 >d be forwarded:
189 
190* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337
191 
188192
189ideas193ideas
rev 23  |  foxhop  |  1393771817000  |  JSON

rev 22
rev 23
179From My understanding the minion to master communication could work like this:179From My understanding the minion to master communication could work like this:
180180
t181.. code-block:: textt
182 
183 A 3rd party script publishes to minion bus ->181#. A 3rd party script publishes to minion bus
184 salt-minion daemon sees the properly formatted "package" event and runs _fire_m182#. salt-minion daemon sees the properly formatted "package" event and runs _fire
>aster ->>_master
185 the "package" event  ends up on master bus -> 183#. the "package" event  ends up on master bus
186 A 3rd party script subscribes to the tag and gains access to the data.184#. A 3rd party script subscribes to the tag and gains access to the data
187185
188The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-186The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-
>encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see>encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see
>ms my "package" format is incorrect so it never gets forwarded to the master ...>ms my "package" format is incorrect so it never gets forwarded to the master ...
rev 22  |  foxhop  |  1393771708000  |  JSON

rev 21
rev 22
f1Salt Stack Piggy Back Encrypted ZMQ Event Busf1Salt Stack Piggy Back Encrypted ZMQ Event Bus
2##################################################2##################################################
nn3 
3Some code examples that piggy back on Salt Stack's encrypted ZMQ event bus.4Some code examples that piggy back on Salt Stack's encrypted ZMQ event bus.
4These code examples show how 3rd party scripts on each side may communicate over5These code examples show how 3rd party scripts on each side may communicate over
> > 
186187
187The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-188The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-
>encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see>encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see
>ms my "package" format is incorrect so it never gets forwarded to the master ...>ms my "package" format is incorrect so it never gets forwarded to the master ...
tt189 
190 
188ideas191ideas
189============192============
rev 21  |  foxhop  |  1393771656000  |  JSON

rev 20
rev 21
176 salt-call event.fire_master '{"data": "message for the master"}' 'mytag'176 salt-call event.fire_master '{"data": "message for the master"}' 'mytag'
177177
nn178From My understanding the minion to master communication could work like this:
178179
tt180.. code-block:: text
181 
182 A 3rd party script publishes to minion bus ->
183 salt-minion daemon sees the properly formatted "package" event and runs _fire_m
 >aster ->
184 the "package" event  ends up on master bus -> 
185 A 3rd party script subscribes to the tag and gains access to the data.
186 
187The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back-
 >encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see
 >ms my "package" format is incorrect so it never gets forwarded to the master ...
179ideas188ideas
180============189============
rev 20  |  foxhop  |  1393769681000  |  JSON

rev 19
rev 20
180============180============
181181
t182I think it could be neat to build a returner which accepted a tag and returned dt182I think it could be neat to build a returner which accepted a ZMQ "channel" tag.
>ata (events) from minion to master on a special channel.  then we could have a 3>  Then after a remote execution the and ret data could be passed from minion to 
>rd party script on the salt master that subscribes to this special channel and p>master on a special channel.  Then we could have a 3rd party script on the salt 
>erforms further processing on the and data presents it in a neat way.>master that subscribes to this special channel and performs further processing o
 >r presents the data in a neat way.
183183
184for example:184for example:
rev 19  |  foxhop  |  1393769097000  |  JSON

rev 18
rev 19
176 salt-call event.fire_master '{"data": "message for the master"}' 'mytag'176 salt-call event.fire_master '{"data": "message for the master"}' 'mytag'
177177
tt178 
179ideas
180============
181 
182I think it could be neat to build a returner which accepted a tag and returned d
 >ata (events) from minion to master on a special channel.  then we could have a 3
 >rd party script on the salt master that subscribes to this special channel and p
 >erforms further processing on the and data presents it in a neat way.
183 
184for example:
185 
186.. code-block:: text
187 
188 salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plug
 >ins/check_procs -w 150 -c 200'
189 
190The minion would see the remote execution command, run the job, get data back, a
 >nd return to master on a special tagged channel.
191 
192On the master we could have a 3rd party script subscribed to the special tagged 
 >channel and perform further manipulation or persist the output somewhere.
193 
194 
178references195references
179===============196===============
rev 18  |  foxhop  |  1393766692000  |  JSON

rev 17
rev 18
165165
166166
tt167Fire events between minion and master
168====================================================
169 
170There is where I get stuck ...  I want to allow minions to fire events which end
 > up on the master bus ...
171 
172I'm able to do this with `salt-call`, for example:
173 
174.. code-block:: python
175 
176 salt-call event.fire_master '{"data": "message for the master"}' 'mytag'
177 
178references
179===============
180 
181http://docs.saltstack.com/topics/event/index.html
182 
183 
184 
185 
rev 17  |  foxhop  |  1393766458000  |  JSON

rev 16
rev 17
16====================================================16====================================================
17In this example we write a listener/subscriber script that connects to the minio17In this example we write a listener/subscriber script that connects to the minio
>n bus.>n bus.
n18We then write a emitter/publisher script that connects  to the minion bus and pon18We then write a emitter/publisher script that connects to the minion bus and pos
>sts events.>ts events.
1919
20listen_to_minion_bus.py20listen_to_minion_bus.py
114Fire event to master bus and listen to master bus114Fire event to master bus and listen to master bus
115====================================================115====================================================
t116In this example we write a listener/subscriber script that connects to the miniot116In this example we write a listener/subscriber script that connects to the maste
>n bus.>r bus.
117We then write a emitter/publisher script that connects  to the minion bus and po117We then write a emitter/publisher script that connects to the master bus and pos
>sts events.>ts events.
118118
119listen_to_master_bus.py119listen_to_master_bus.py
rev 16  |  foxhop  |  1393766421000  |  JSON

rev 15
rev 16
138 # generator iterator yields events forever, we filter on tag138 # generator iterator yields events forever, we filter on tag
139 for data in event.iter_events(tag=tag):139 for data in event.iter_events(tag=tag):
t140     print(data)emit_to_master_bus.pyt140     print(data)
141 
142 
143emit_to_master_bus.py
141---------------------------------144---------------------------------
142145
rev 15  |  foxhop  |  1393766399000  |  JSON

rev 14
rev 15
117We then write a emitter/publisher script that connects  to the minion bus and po117We then write a emitter/publisher script that connects  to the minion bus and po
>sts events.>sts events.
118118
n119 n
120 
121listen_to_master_bus.py119listen_to_master_bus.py
122--------------------------------120--------------------------------
140 # generator iterator yields events forever, we filter on tag138 # generator iterator yields events forever, we filter on tag
141 for data in event.iter_events(tag=tag):139 for data in event.iter_events(tag=tag):
tt140     print(data)emit_to_master_bus.py
141---------------------------------
142 
143.. code-block:: python
144 
145 # debug logging to STDOUT
146 import salt.log
147 salt.log.setup_console_logger('all')
148 
149 # event libary for events over ZMQ
150 import salt.utils.event
151 
152 payload = {'sample-msg': 'this is a test',
153            'example': 'this is the same test'}
154 
155 sock_dir = '/var/run/salt/master'
156 
157 # create event object, attach to master socket ...
158 event = salt.utils.event.SaltEvent('master', sock_dir)
159 
160 # post the event
161 event.fire_event(payload, 'mytag')
162 
163 
rev 14  |  foxhop  |  1393766360000  |  JSON

rev 13
rev 14
117We then write a emitter/publisher script that connects  to the minion bus and po117We then write a emitter/publisher script that connects  to the minion bus and po
>sts events.>sts events.
118118
nn119 
120 
119listen_to_master_bus.py121listen_to_master_bus.py
120--------------------------------122--------------------------------
138 # generator iterator yields events forever, we filter on tag140 # generator iterator yields events forever, we filter on tag
139 for data in event.iter_events(tag=tag):141 for data in event.iter_events(tag=tag):
t140     print(data)emit_to_master_bus.pyt
141---------------------------------
142 
143.. code-block:: python
144 
145 # debug logging to STDOUT
146 import salt.log
147 salt.log.setup_console_logger('all')
148 
149 # event libary for events over ZMQ
150 import salt.utils.event
151 
152 payload = {'sample-msg': 'this is a test',
153            'example': 'this is the same test'}
154 
155 sock_dir = '/var/run/salt/master'
156 
157 # create event object, attach to master socket ...
158 event = salt.utils.event.SaltEvent('master', sock_dir)
159 
160 # post the event
161 event.fire_event(payload, 'mytag')
162 
163 
rev 13  |  foxhop  |  1393766331000  |  JSON

rev 12
rev 13
f1Salt Stack Piggy Back Encrypted ZMQ Event Busf1Salt Stack Piggy Back Encrypted ZMQ Event Bus
2##################################################2##################################################
n3 n
4Some code examples that piggy back on Salt Stack's encrypted ZMQ event bus.3Some code examples that piggy back on Salt Stack's encrypted ZMQ event bus.
5These code examples show how 3rd party scripts on each side may communicate over4These code examples show how 3rd party scripts on each side may communicate over
> > 
16Fire event to minion bus and listen to minion bus15Fire event to minion bus and listen to minion bus
17====================================================16====================================================
n18 n
19In this example we write a listener/subscriber script that connects to the minio17In this example we write a listener/subscriber script that connects to the minio
>n bus.>n bus.
20We then write a emitter/publisher script that connects  to the minion bus and po18We then write a emitter/publisher script that connects  to the minion bus and po
>sts events.>sts events.
22listen_to_minion_bus.py20listen_to_minion_bus.py
23------------------------------21------------------------------
n24 n
253rd Party Subscriber / Listener:223rd Party Subscriber / Listener:
2623
55emit_to_minion_bus.py52emit_to_minion_bus.py
56-------------------------------53-------------------------------
n57 n
583rd Party Publisher / Emitter:543rd Party Publisher / Emitter:
5955
94Notes on the implementation90Notes on the implementation
95----------------------------------91----------------------------------
n96 n
97Notice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitte92Notice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitte
>r? >r? 
98The `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent93The `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent
>` so you can use>` so you can use
119Fire event to master bus and listen to master bus114Fire event to master bus and listen to master bus
120====================================================115====================================================
n121 n
122In this example we write a listener/subscriber script that connects to the minio116In this example we write a listener/subscriber script that connects to the minio
>n bus.>n bus.
123We then write a emitter/publisher script that connects  to the minion bus and po117We then write a emitter/publisher script that connects  to the minion bus and po
>sts events.>sts events.
126--------------------------------120--------------------------------
127121
t128emit_to_master_bus.pyt122.. code-block:: python
123 
124 # debug logging to STDOUT
125 import salt.log
126 salt.log.setup_console_logger('all')
127 
128 # event libary for events over ZMQ
129 import salt.utils.event
130 
131 # create event object, attach to master socket ...
132 event = salt.utils.event.MasterEvent('/var/run/salt/master')
133 
134 tag = 'mytag'
135 
136 print('Listening for events tagged \'{}\' on Salt Master bus.'.format(tag))
137 
138 # generator iterator yields events forever, we filter on tag
139 for data in event.iter_events(tag=tag):
140     print(data)emit_to_master_bus.py
129---------------------------------141---------------------------------
130142
rev 12  |  foxhop  |  1393766064000  |  JSON

rev 11
rev 12
123We then write a emitter/publisher script that connects  to the minion bus and po123We then write a emitter/publisher script that connects  to the minion bus and po
>sts events.>sts events.
124124
tt125listen_to_master_bus.py
126--------------------------------
127 
128emit_to_master_bus.py
129---------------------------------
130 
131.. code-block:: python
132 
133 # debug logging to STDOUT
134 import salt.log
135 salt.log.setup_console_logger('all')
136 
137 # event libary for events over ZMQ
138 import salt.utils.event
139 
140 payload = {'sample-msg': 'this is a test',
141            'example': 'this is the same test'}
142 
143 sock_dir = '/var/run/salt/master'
144 
145 # create event object, attach to master socket ...
146 event = salt.utils.event.SaltEvent('master', sock_dir)
147 
148 # post the event
149 event.fire_event(payload, 'mytag')
150 
151 
rev 11  |  foxhop  |  1393765844000  |  JSON

rev 10
rev 11
117 opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}117 opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}
118118
tt119Fire event to master bus and listen to master bus
120====================================================
121 
122In this example we write a listener/subscriber script that connects to the minio
 >n bus.
123We then write a emitter/publisher script that connects  to the minion bus and po
 >sts events.
124 
rev 10  |  foxhop  |  1393765443000  |  JSON

rev 9
rev 10
10* the salt-minion alive and running on the minion host10* the salt-minion alive and running on the minion host
11* the salt-master alive and running on the master host 11* the salt-master alive and running on the master host 
tt12 
13.. contents::
1214
1315
rev 9  |  foxhop  |  1393765385000  |  JSON

rev 8
rev 9
2##################################################2##################################################
33
n4Salt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each sidn4Some code examples that piggy back on Salt Stack's encrypted ZMQ event bus.
>e. 
5These code examples show how 3rd party scripts on each side may communicate over
 > 
6secure and fast transport!
57
6Requires:8Requires:
8* the salt-minion alive and running on the minion host10* the salt-minion alive and running on the minion host
9* the salt-master alive and running on the master host 11* the salt-master alive and running on the master host 
tt12 
1013
11Fire event to minion bus and listen to minion bus14Fire event to minion bus and listen to minion bus
rev 8  |  foxhop  |  1393765256000  |  JSON

rev 7
rev 8
11Fire event to minion bus and listen to minion bus11Fire event to minion bus and listen to minion bus
12====================================================12====================================================
tt13 
14In this example we write a listener/subscriber script that connects to the minio
 >n bus.
15We then write a emitter/publisher script that connects  to the minion bus and po
 >sts events.
1316
14listen_to_minion_bus.py17listen_to_minion_bus.py
rev 7  |  foxhop  |  1393765159000  |  JSON

rev 6
rev 7
12====================================================12====================================================
1313
nn14listen_to_minion_bus.py
15------------------------------
16 
143rd Party Subscriber / Listener:173rd Party Subscriber / Listener:
n15 n
16**listen_to_minion_bus.py**
1718
18.. code-block:: python19.. code-block:: python
44     print(data)45     print(data)
4546
n46**emit_to_minion_bus.py**n47emit_to_minion_bus.py
48-------------------------------
49 
503rd Party Publisher / Emitter:
4751
48.. code-block:: python52.. code-block:: python
80 event.fire_event(payload, tag)84 event.fire_event(payload, tag)
8185
n82Notice how I use event.MinionEvent in listener and event.SaltEvent in emitter?  n86Notes on the implementation
>event.MinionEvent is just a very light weight subclass of event.SaltEvent, you c 
>an use either or... 
87----------------------------------
8388
nn89Notice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitte
 >r? 
90The `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent
 >` so you can use
91either or...
92 
84Also the configuration code could be reduced to static varibles, for instance re93The configuration code could be reduced to static variables, for instance replac
>place ->e -
8594
86.. code-block:: python95.. code-block:: python
96with this 105with this 
97106
t98 t
99.. code-block:: python107.. code-block:: python
100108
rev 6  |  foxhop  |  1393764906000  |  JSON

rev 5
rev 6
80 event.fire_event(payload, tag)80 event.fire_event(payload, tag)
8181
tt82Notice how I use event.MinionEvent in listener and event.SaltEvent in emitter?  
 >event.MinionEvent is just a very light weight subclass of event.SaltEvent, you c
 >an use either or...
83 
84Also the configuration code could be reduced to static varibles, for instance re
 >place -
85 
86.. code-block:: python
87 
88 # needed for config to opts processing
89 import os
90 import salt.syspaths as syspaths
91 import salt.config
92 
93 # get opts from minion config file, this function also looks in drop dir!
94 opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))
95 
96with this 
97 
98 
99.. code-block:: python
100 
101 opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}
102 
rev 5  |  foxhop  |  1393764595000  |  JSON

rev 4
rev 5
48.. code-block:: python48.. code-block:: python
4949
n50 print(1)n50 # needed for config to opts processing
51 import os
52 import salt.syspaths as syspaths
53 import salt.config
5154
nn55 # get opts from minion config file, this function also looks in drop dir!
56 opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))
5257
tt58 # debug to STDOUT
59 import salt.log
60 salt.log.setup_console_logger('all')
61 
62 # event libary for events over ZMQ
63 import salt.utils.event
64 
65 tag = 'mytag'
66 data = {'message':'a drop in the bucket'}
67 
68 payload = {
69   'id': opts['id'],
70   'tag': tag,
71   'data': data,
72 }
73 
74 print (payload)
75 
76 # opts must valid minion ID else it binds to invalid socket
77 event = salt.utils.event.SaltEvent('minion', **opts)
78 
79 # Fire event payload with tag
80 event.fire_event(payload, tag)
81 
rev 4  |  foxhop  |  1393764001000  |  JSON

rev 3
rev 4
9* the salt-master alive and running on the master host 9* the salt-master alive and running on the master host 
1010
n11Fire event from minion to minion busn11Fire event to minion bus and listen to minion bus
12======================================12====================================================
1313
143rd Party Subscriber / Listener:143rd Party Subscriber / Listener:
44     print(data)44     print(data)
4545
tt46**emit_to_minion_bus.py**
47 
48.. code-block:: python
49 
50 print(1)
4651
4752
rev 3  |  foxhop  |  1393763893000  |  JSON

rev 2
rev 3
33 import salt.utils.event33 import salt.utils.event
3434
tt35 # opts must have the valid minion ID else it binds to invalid socket
35 event = salt.utils.event.MinionEvent(**opts)36 event = salt.utils.event.MinionEvent(**opts)
3637
rev 2  |  foxhop  |  1393763810000  |  JSON

rev 1
rev 2
39 print('Listening for events tagged \'{}\' on Salt Minion bus.'.format(tag))39 print('Listening for events tagged \'{}\' on Salt Minion bus.'.format(tag))
4040
t41 #for data in event.iter_events(tag='nagios'):t41 # generator iterator yields events forever, we filter on tag
42 for data in event.iter_events(tag=tag):42 for data in event.iter_events(tag=tag):
43     print(data)43     print(data)
rev 1  |  foxhop  |  1393763722000  |  JSON

empty
rev 1
tt1Salt Stack Piggy Back Encrypted ZMQ Event Bus
2##################################################
3 
4Salt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each sid
 >e.
5 
6Requires:
7 
8* the salt-minion alive and running on the minion host
9* the salt-master alive and running on the master host 
10 
11Fire event from minion to minion bus
12======================================
13 
143rd Party Subscriber / Listener:
15 
16**listen_to_minion_bus.py**
17 
18.. code-block:: python
19 
20 # needed for config to opts processing
21 import os
22 import salt.syspaths as syspaths
23 import salt.config
24 
25 # get opts from minion config file, this function also looks in drop dir!
26 opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))
27 
28 # debug to STDOUT
29 import salt.log
30 salt.log.setup_console_logger('all')
31 
32 # event libary for events over ZMQ
33 import salt.utils.event
34 
35 event = salt.utils.event.MinionEvent(**opts)
36 
37 tag = 'mytag'
38 
39 print('Listening for events tagged \'{}\' on Salt Minion bus.'.format(tag))
40 
41 #for data in event.iter_events(tag='nagios'):
42 for data in event.iter_events(tag=tag):
43     print(data)
44 
45 
46