{"node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "revisions": [{"id": "f3dea637-2f95-11f1-96e9-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion daemon alive and running on the minion host\r\n* the salt-master daemon alive and running on the master host\r\n* the 3rd party scripts must execute as the same user who runs the salt-minion or salt-master daemon\r\n  because the 3rd party script needs to bind to the ZMQ sockets managed by the daemons.\r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom my understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted event tagged 'fire_master' and runs _fire_master() method\r\n#. the event ends up on master bus\r\n#. A 3rd party script listens to the master bus and subscribes (filters) on the nested tag and gains access to the message\r\n\r\nThe 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 seems my event \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis area in the salt-minion daemon loop has conditionals which determine if the package/event should be forwarded.\r\nIt seems to look for the 'fire_master' tag:\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_master' tag\r\n* https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_master' event\r\n\r\n\r\n\r\nemit_to_minion_bus_forward_to_master_bus.py\r\n-------------------------------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n\r\n # a list of event ret objects\r\n events = [\r\n   {'message':'a drap in the bucket', 'tag': tag },\r\n   {'message':'a drep in the bucket', 'tag': tag },\r\n   {'message':'a drip in the bucket', 'tag': tag },\r\n   {'message':'a drop in the bucket', 'tag': tag },\r\n   {'message':'a drup in the bucket', 'tag': tag },\r\n ]\r\n\r\n # multi event example, supports a list of event ret objects\r\n payload = {\r\n   'id': opts['id'],\r\n   'events': events,\r\n   'tag': None,\r\n   'pretag': None,\r\n   'data': None\r\n }\r\n\r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with 'fire_master' tag which the\r\n # salt-minion daemon will forward to the master event bus!\r\n event.fire_event(payload, 'fire_master')\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a ZMQ \"channel\" tag.  Then after a remote execution the ret data could be passed from minion to master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special channel tag.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special channel tag and perform further manipulation or persist the output somewhere.\r\n\r\nUPDATE: I wrote a <returner that tagged data back to masters zermq bus here <http://russell.ballestrini.net/replace-the-nagios-scheduler-and-nrpe-with-salt-stack/>`_.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\nrelated work\r\n=================\r\n\r\nhttps://github.com/felskrone/salt-eventsd\r\n\r\n\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 34, "created": 1394744287000}, {"id": "f3dea246-2f95-11f1-916a-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion daemon alive and running on the minion host\r\n* the salt-master daemon alive and running on the master host\r\n* the 3rd party scripts must execute as the same user who runs the salt-minion or salt-master daemon\r\n  because the 3rd party script needs to bind to the ZMQ sockets managed by the daemons.\r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom my understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted event tagged 'fire_master' and runs _fire_master() method\r\n#. the event ends up on master bus\r\n#. A 3rd party script listens to the master bus and subscribes (filters) on the nested tag and gains access to the message\r\n\r\nThe 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 seems my event \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis area in the salt-minion daemon loop has conditionals which determine if the package/event should be forwarded.\r\nIt seems to look for the 'fire_master' tag:\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_master' tag\r\n* https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_master' event\r\n\r\n\r\n\r\nemit_to_minion_bus_forward_to_master_bus.py\r\n-------------------------------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n\r\n # a list of event ret objects\r\n events = [\r\n   {'message':'a drap in the bucket', 'tag': tag },\r\n   {'message':'a drep in the bucket', 'tag': tag },\r\n   {'message':'a drip in the bucket', 'tag': tag },\r\n   {'message':'a drop in the bucket', 'tag': tag },\r\n   {'message':'a drup in the bucket', 'tag': tag },\r\n ]\r\n\r\n # multi event example, supports a list of event ret objects\r\n payload = {\r\n   'id': opts['id'],\r\n   'events': events,\r\n   'tag': None,\r\n   'pretag': None,\r\n   'data': None\r\n }\r\n\r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with 'fire_master' tag which the\r\n # salt-minion daemon will forward to the master event bus!\r\n event.fire_event(payload, 'fire_master')\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a ZMQ \"channel\" tag.  Then after a remote execution the ret data could be passed from minion to master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special channel tag.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special channel tag and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\nrelated work\r\n=================\r\n\r\nhttps://github.com/felskrone/salt-eventsd\r\n\r\n\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 33, "created": 1394744144000}, {"id": "f3de9dfc-2f95-11f1-9b06-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion daemon alive and running on the minion host\r\n* the salt-master daemon alive and running on the master host\r\n* the 3rd party scripts must execute as the same user who runs the salt-minion or salt-master daemon\r\n  because the 3rd party script needs to bind to the ZMQ sockets managed by the daemons.\r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom my understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted event tagged 'fire_master' and runs _fire_master() method\r\n#. the event ends up on master bus\r\n#. A 3rd party script listens to the master bus and subscribes (filters) on the nested tag and gains access to the message\r\n\r\nThe 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 seems my event \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis area in the salt-minion daemon loop has conditionals which determine if the package/event should be forwarded.\r\nIt seems to look for the 'fire_master' tag:\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_master' tag\r\n* https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_master' event\r\n\r\n\r\n\r\nemit_to_minion_bus_forward_to_master_bus.py\r\n-------------------------------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n\r\n # a list of event ret objects\r\n events = [\r\n   {'message':'a drap in the bucket', 'tag': tag },\r\n   {'message':'a drep in the bucket', 'tag': tag },\r\n   {'message':'a drip in the bucket', 'tag': tag },\r\n   {'message':'a drop in the bucket', 'tag': tag },\r\n   {'message':'a drup in the bucket', 'tag': tag },\r\n ]\r\n\r\n # multi event example, supports a list of event ret objects\r\n payload = {\r\n   'id': opts['id'],\r\n   'events': events,\r\n   'tag': None,\r\n   'pretag': None,\r\n   'data': None\r\n }\r\n\r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with 'fire_master' tag which the\r\n # salt-minion daemon will forward to the master event bus!\r\n event.fire_event(payload, 'fire_master')\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a ZMQ \"channel\" tag.  Then after a remote execution the ret data could be passed from minion to master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special channel tag.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special channel tag and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 32, "created": 1393883178000}, {"id": "f3de99e2-2f95-11f1-aa51-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion daemon alive and running on the minion host\r\n* the salt-master daemon alive and running on the master host\r\n* the 3rd party scripts must execute as the same user who runs the salt-minion or salt-master daemon\r\n  because the 3rd party script needs to bind to the ZMQ sockets managed by the daemons.\r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master\r\n#. the \"package\" event  ends up on master bus\r\n#. A 3rd party script subscribes to the tag and gains access to the data\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis is the area in the minion daemon loop that tests if the package/event should be forwarded ('fire_master'):\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_master' tag\r\n* https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_master' event\r\n\r\nemit_to_minion_bus_forward_to_master_bus.py\r\n-------------------------------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n\r\n # a list of event ret objects\r\n events = [\r\n   {'message':'a drap in the bucket', 'tag': tag },\r\n   {'message':'a drep in the bucket', 'tag': tag },\r\n   {'message':'a drip in the bucket', 'tag': tag },\r\n   {'message':'a drop in the bucket', 'tag': tag },\r\n   {'message':'a drup in the bucket', 'tag': tag },\r\n ]\r\n\r\n # multi event example, supports a list of event ret objects\r\n payload = {\r\n   'id': opts['id'],\r\n   'events': events,\r\n   'tag': None,\r\n   'pretag': None,\r\n   'data': None\r\n }\r\n\r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with 'fire_master' tag which the\r\n # salt-minion daemon will forward to the master event bus!\r\n event.fire_event(payload, 'fire_master')\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a ZMQ \"channel\" tag.  Then after a remote execution the ret data could be passed from minion to master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special channel tag.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special channel tag and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 31, "created": 1393882502000}, {"id": "f3de8f8d-2f95-11f1-9aae-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion daemon alive and running on the minion host\r\n* the salt-master daemon alive and running on the master host\r\n* the 3rd party scripts must execute as the same user who runs the salt-minion or salt-master daemon\r\n  because the 3rd party script needs to bind to the ZMQ sockets managed by the daemons.\r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master\r\n#. the \"package\" event  ends up on master bus\r\n#. A 3rd party script subscribes to the tag and gains access to the data\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis is the area in the minion daemon loop that tests if the package/event should be forwarded ('fire_master'):\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_master' tag\r\n* https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_master' event\r\n\r\nemit_to_minion_bus_forward_to_master_bus.py\r\n-------------------------------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n\r\n # a list of event ret objects\r\n events = [\r\n   {'message':'a drap in the bucket', 'tag': tag },\r\n   {'message':'a drep in the bucket', 'tag': tag },\r\n   {'message':'a drip in the bucket', 'tag': tag },\r\n   {'message':'a drop in the bucket', 'tag': tag },\r\n   {'message':'a drup in the bucket', 'tag': tag },\r\n ]\r\n\r\n# multi event example, supports a list of event ret objects\r\npayload = {\r\n  'id': opts['id'],\r\n  'events': events,\r\n  'tag': None,\r\n  'pretag': None,\r\n  'data': None\r\n}\r\n\r\n\r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with 'fire_master' tag which the\r\n # salt-minion daemon will forward to the master event bus!\r\n event.fire_event(payload, 'fire_master')\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a ZMQ \"channel\" tag.  Then after a remote execution the ret data could be passed from minion to master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special channel tag.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special channel tag and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 30, "created": 1393882461000}, {"id": "f3de8b90-2f95-11f1-b82d-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion daemon alive and running on the minion host\r\n* the salt-master daemon alive and running on the master host\r\n* the 3rd party scripts must execute as the same user who runs the salt-minion or salt-master daemon\r\n  because the 3rd party script needs to bind to the ZMQ sockets managed by the daemons.\r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master\r\n#. the \"package\" event  ends up on master bus\r\n#. A 3rd party script subscribes to the tag and gains access to the data\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis is the area in the minion daemon loop that tests if the package/event should be forwarded:\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337\r\n\r\n\r\nemit_to_minion_bus_forward_to_master_bus.py\r\n-------------------------------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n\r\n # a list of event ret objects\r\n events = [\r\n   {'message':'a drap in the bucket', 'tag': tag },\r\n   {'message':'a drep in the bucket', 'tag': tag },\r\n   {'message':'a drip in the bucket', 'tag': tag },\r\n   {'message':'a drop in the bucket', 'tag': tag },\r\n   {'message':'a drup in the bucket', 'tag': tag },\r\n ]\r\n\r\n# multi event example, supports a list of event ret objects\r\npayload = {\r\n  'id': opts['id'],\r\n  'events': events,\r\n  'tag': None,\r\n  'pretag': None,\r\n  'data': None\r\n}\r\n\r\n\r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with 'fire_master' tag which the\r\n # salt-minion daemon will forward to the master event bus!\r\n event.fire_event(payload, 'fire_master')\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a ZMQ \"channel\" tag.  Then after a remote execution the ret data could be passed from minion to master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special channel tag.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special channel tag and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 29, "created": 1393882355000}, {"id": "f3de8701-2f95-11f1-8c65-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion daemon alive and running on the minion host\r\n* the salt-master daemon alive and running on the master host\r\n* the 3rd party scripts must execute as the same user who runs the salt-minion or salt-master daemon\r\n  because the 3rd party script needs to bind to the ZMQ sockets managed by the daemons.\r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master\r\n#. the \"package\" event  ends up on master bus\r\n#. A 3rd party script subscribes to the tag and gains access to the data\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis is the area in the minion daemon loop that tests if the package/event should be forwarded:\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337\r\n\r\n\r\nemit_to_minion_bus_forward_to_master_bus.py\r\n-------------------------------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket', 'tag': tag }\r\n\r\n # multi event example, supports a list of event ret objects\r\n payload = {\r\n   'id': opts['id'],\r\n   'events': [ data  ],\r\n   'tag': None,\r\n   'pretag': None,\r\n   'data': None\r\n }\r\n\r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with 'fire_master' tag which the\r\n # salt-minion daemon will forward to the master event bus!\r\n event.fire_event(payload, 'fire_master')\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a ZMQ \"channel\" tag.  Then after a remote execution the ret data could be passed from minion to master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special channel tag.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special channel tag and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 28, "created": 1393847549000}, {"id": "f3de82f5-2f95-11f1-9d7a-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host\r\n* the 3rd party scripts must be run as the same user as the salt-minion or salt-master\r\n  so the script has the ability to bind to the ZMQ sockets\r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master\r\n#. the \"package\" event  ends up on master bus\r\n#. A 3rd party script subscribes to the tag and gains access to the data\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis is the area in the minion daemon loop that tests if the package/event should be forwarded:\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337\r\n\r\n\r\nemit_to_minion_bus_forward_to_master_bus.py\r\n-------------------------------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket', 'tag': tag }\r\n\r\n # multi event example, supports a list of event ret objects\r\n payload = {\r\n   'id': opts['id'],\r\n   'events': [ data  ],\r\n   'tag': None,\r\n   'pretag': None,\r\n   'data': None\r\n }\r\n\r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with 'fire_master' tag which the\r\n # salt-minion daemon will forward to the master event bus!\r\n event.fire_event(payload, 'fire_master')\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a ZMQ \"channel\" tag.  Then after a remote execution the ret data could be passed from minion to master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special channel tag.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special channel tag and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 27, "created": 1393847419000}, {"id": "f3de7f0b-2f95-11f1-a00e-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master\r\n#. the \"package\" event  ends up on master bus\r\n#. A 3rd party script subscribes to the tag and gains access to the data\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis is the area in the minion daemon loop that tests if the package/event should be forwarded:\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337\r\n\r\n\r\nemit_to_minion_bus_forward_to_master_bus.py\r\n-------------------------------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket', 'tag': tag }\r\n\r\n # multi event example, supports a list of event ret objects\r\n payload = {\r\n   'id': opts['id'],\r\n   'events': [ data  ],\r\n   'tag': None,\r\n   'pretag': None,\r\n   'data': None\r\n }\r\n\r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with 'fire_master' tag which the\r\n # salt-minion daemon will forward to the master event bus!\r\n event.fire_event(payload, 'fire_master')\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a ZMQ \"channel\" tag.  Then after a remote execution the ret data could be passed from minion to master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special channel tag.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special channel tag and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 26, "created": 1393846121000}, {"id": "f3de7b19-2f95-11f1-938f-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master\r\n#. the \"package\" event  ends up on master bus\r\n#. A 3rd party script subscribes to the tag and gains access to the data\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis is the area in the minion daemon loop that tests if the package/event should be forwarded:\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337\r\n\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a ZMQ \"channel\" tag.  Then after a remote execution the ret data could be passed from minion to master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special channel tag.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special channel tag and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 25, "created": 1393782677000}, {"id": "f3de771e-2f95-11f1-aa0c-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master\r\n#. the \"package\" event  ends up on master bus\r\n#. A 3rd party script subscribes to the tag and gains access to the data\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\nThis is the area in the minion daemon loop that tests if the package/event should be forwarded:\r\n\r\n* https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337\r\n\r\n\r\nideas\r\n============\r\n\r\nI 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 master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special tagged channel.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special tagged channel and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 24, "created": 1393772005000}, {"id": "f3de72a2-2f95-11f1-82a1-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n#. A 3rd party script publishes to minion bus\r\n#. salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master\r\n#. the \"package\" event  ends up on master bus\r\n#. A 3rd party script subscribes to the tag and gains access to the data\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\n\r\nideas\r\n============\r\n\r\nI 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 master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special tagged channel.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special tagged channel and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 23, "created": 1393771817000}, {"id": "f3de6cd1-2f95-11f1-b7c7-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n.. code-block:: text\r\n\r\n A 3rd party script publishes to minion bus ->\r\n salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master ->\r\n the \"package\" event  ends up on master bus -> \r\n A 3rd party script subscribes to the tag and gains access to the data.\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\n\r\n\r\nideas\r\n============\r\n\r\nI 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 master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special tagged channel.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special tagged channel and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 22, "created": 1393771708000}, {"id": "f3de68ea-2f95-11f1-a842-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nFrom My understanding the minion to master communication could work like this:\r\n\r\n.. code-block:: text\r\n\r\n A 3rd party script publishes to minion bus ->\r\n salt-minion daemon sees the properly formatted \"package\" event and runs _fire_master ->\r\n the \"package\" event  ends up on master bus -> \r\n A 3rd party script subscribes to the tag and gains access to the data.\r\n\r\nThe 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 seems my \"package\" format is incorrect so it never gets forwarded to the master ...\r\nideas\r\n============\r\n\r\nI 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 master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special tagged channel.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special tagged channel and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 21, "created": 1393771656000}, {"id": "f3de64f2-2f95-11f1-bb18-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\n\r\nideas\r\n============\r\n\r\nI 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 master on a special channel.  Then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing or presents the data in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special tagged channel.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special tagged channel and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 20, "created": 1393769681000}, {"id": "f3de608e-2f95-11f1-beb9-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\n\r\nideas\r\n============\r\n\r\nI think it could be neat to build a returner which accepted a tag and returned data (events) from minion to master on a special channel.  then we could have a 3rd party script on the salt master that subscribes to this special channel and performs further processing on the and data presents it in a neat way.\r\n\r\nfor example:\r\n\r\n.. code-block:: text\r\n\r\n salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plugins/check_procs -w 150 -c 200'\r\n\r\nThe minion would see the remote execution command, run the job, get data back, and return to master on a special tagged channel.\r\n\r\nOn the master we could have a 3rd party script subscribed to the special tagged channel and perform further manipulation or persist the output somewhere.\r\n\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 19, "created": 1393769097000}, {"id": "f3de5903-2f95-11f1-96ca-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\nFire events between minion and master\r\n====================================================\r\n\r\nThere is where I get stuck ...  I want to allow minions to fire events which end up on the master bus ...\r\n\r\nI'm able to do this with `salt-call`, for example:\r\n\r\n.. code-block:: python\r\n\r\n salt-call event.fire_master '{\"data\": \"message for the master\"}' 'mytag'\r\n\r\nreferences\r\n===============\r\n\r\nhttp://docs.saltstack.com/topics/event/index.html\r\n\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 18, "created": 1393766692000}, {"id": "f3de5232-2f95-11f1-a1bd-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the master bus.\r\nWe then write a emitter/publisher script that connects to the master bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\n", "source_format": "rst", "revision_number": 17, "created": 1393766458000}, {"id": "f3de4bc7-2f95-11f1-a143-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\n", "source_format": "rst", "revision_number": 16, "created": 1393766421000}, {"id": "f3de45af-2f95-11f1-b1d4-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)emit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\n", "source_format": "rst", "revision_number": 15, "created": 1393766399000}, {"id": "f3de41dd-2f95-11f1-9de6-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\n\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n", "source_format": "rst", "revision_number": 14, "created": 1393766360000}, {"id": "f3de3d60-2f95-11f1-ad4a-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.MasterEvent('/var/run/salt/master')\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Master bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)emit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\n", "source_format": "rst", "revision_number": 13, "created": 1393766331000}, {"id": "f3de3950-2f95-11f1-9318-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\n\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\n\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\n\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_master_bus.py\r\n--------------------------------\r\n\r\nemit_to_master_bus.py\r\n---------------------------------\r\n\r\n.. code-block:: python\r\n\r\n # debug logging to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n payload = {'sample-msg': 'this is a test',\r\n            'example': 'this is the same test'}\r\n\r\n sock_dir = '/var/run/salt/master'\r\n\r\n # create event object, attach to master socket ...\r\n event = salt.utils.event.SaltEvent('master', sock_dir)\r\n\r\n # post the event\r\n event.fire_event(payload, 'mytag')\r\n\r\n\r\n", "source_format": "rst", "revision_number": 12, "created": 1393766064000}, {"id": "f3de3511-2f95-11f1-8ced-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\n\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\n\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\nFire event to master bus and listen to master bus\r\n====================================================\r\n\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\n", "source_format": "rst", "revision_number": 11, "created": 1393765844000}, {"id": "f3de3071-2f95-11f1-8110-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n.. contents::\r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\n\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\n\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\n", "source_format": "rst", "revision_number": 10, "created": 1393765443000}, {"id": "f3de2c1b-2f95-11f1-bde9-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSome code examples that piggy back on Salt Stack's encrypted ZMQ event bus.\r\nThese code examples show how 3rd party scripts on each side may communicate over \r\nsecure and fast transport!\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\n\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\n\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\n", "source_format": "rst", "revision_number": 9, "created": 1393765385000}, {"id": "f3de270d-2f95-11f1-b496-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSalt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each side.\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\n\r\nIn this example we write a listener/subscriber script that connects to the minion bus.\r\nWe then write a emitter/publisher script that connects  to the minion bus and posts events.\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\n\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\n", "source_format": "rst", "revision_number": 8, "created": 1393765256000}, {"id": "f3de22a8-2f95-11f1-9333-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSalt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each side.\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\n\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\n", "source_format": "rst", "revision_number": 7, "created": 1393765159000}, {"id": "f3de1d71-2f95-11f1-b8e2-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSalt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each side.\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n**listen_to_minion_bus.py**\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n**emit_to_minion_bus.py**\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotice 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 can use either or...\r\n\r\nAlso the configuration code could be reduced to static varibles, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\n", "source_format": "rst", "revision_number": 6, "created": 1393764906000}, {"id": "f3de1868-2f95-11f1-b3af-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSalt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each side.\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n**listen_to_minion_bus.py**\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n**emit_to_minion_bus.py**\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\n", "source_format": "rst", "revision_number": 5, "created": 1393764595000}, {"id": "f3de13f7-2f95-11f1-a4a4-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSalt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each side.\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n**listen_to_minion_bus.py**\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n**emit_to_minion_bus.py**\r\n\r\n.. code-block:: python\r\n\r\n print(1)\r\n\r\n\r\n", "source_format": "rst", "revision_number": 4, "created": 1393764001000}, {"id": "f3de0e70-2f95-11f1-b67c-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSalt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each side.\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\nFire event from minion to minion bus\r\n======================================\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n**listen_to_minion_bus.py**\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 3, "created": 1393763893000}, {"id": "f3de0666-2f95-11f1-993d-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSalt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each side.\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\nFire event from minion to minion bus\r\n======================================\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n**listen_to_minion_bus.py**\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 2, "created": 1393763810000}, {"id": "f3ddfc78-2f95-11f1-8f92-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSalt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each side.\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\nFire event from minion to minion bus\r\n======================================\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n**listen_to_minion_bus.py**\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n #for data in event.iter_events(tag='nagios'):\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\n\r\n\r\n", "source_format": "rst", "revision_number": 1, "created": 1393763722000}], "count": 34}