今天对官网一个salt event的测试例子进行修改并总结,以更好的理解event这个怎么用法,下面的测试都基于官网的入门步骤来做
测试1:
先在master端写一个python脚本:
import salt.utils.event event = salt.utils.event.MasterEvent(‘/var/run/salt/master‘) for eachevent in event.iter_events(full=True): print eachevent print "------"
另外开一个终端,再执行命令:
salt ‘*‘ test.ping
结果如下:
{‘tag‘: ‘20150227111546144547‘, ‘data‘: {‘_stamp‘: ‘2015-02-27T11:15:46.145663‘, ‘minions‘: [‘gs-ssh‘]}} ------ {‘tag‘: ‘new_job‘, ‘data‘: {‘tgt_type‘: ‘glob‘, ‘jid‘: ‘20150227111546144547‘, ‘tgt‘: ‘*‘, ‘_stamp‘: ‘2015-02-27T11:15:46.147099‘, ‘user‘: ‘root‘, ‘arg‘: [], ‘fun‘: ‘test.ping‘, ‘minions‘: [‘gs-ssh‘]}} ------ {‘tag‘: ‘salt/job/20150227111546144547/new‘, ‘data‘: {‘tgt_type‘: ‘glob‘, ‘jid‘: ‘20150227111546144547‘, ‘tgt‘: ‘*‘, ‘_stamp‘: ‘2015-02-27T11:15:46.148181‘, ‘user‘: ‘root‘, ‘arg‘: [], ‘fun‘: ‘test.ping‘, ‘minions‘: [‘gs-ssh‘]}} ------ {‘tag‘: ‘20150227111546144547‘, ‘data‘: {‘fun_args‘: [], ‘jid‘: ‘20150227111546144547‘, ‘return‘: True, ‘retcode‘: 0, ‘success‘: True, ‘cmd‘: ‘_return‘, ‘_stamp‘: ‘2015-02-27T11:15:46.216332‘, ‘fun‘: ‘test.ping‘, ‘id‘: ‘gs-ssh‘}} ------ {‘tag‘: ‘salt/job/20150227111546144547/ret/gs-ssh‘, ‘data‘: {‘fun_args‘: [], ‘jid‘: ‘20150227111546144547‘, ‘return‘: True, ‘retcode‘: 0, ‘success‘: True, ‘cmd‘: ‘_return‘, ‘_stamp‘: ‘2015-02-27T11:15:46.218268‘, ‘fun‘: ‘test.ping‘, ‘id‘: ‘gs-ssh‘}} ------
从输出结果看, 对于tag只是jid的, 官方在源码中标记的注释是"old dup event", 推测是为了兼容旧的event系统(0.17.0版本event系统进行了更新), 本次对其不做处理. 下发任务对应的tag为 new_job, 并且下发任务时就master端就在event中注定了那些minions需要运行(对应的data字典中的minions). 如果tag中包含 salt/job/ 字样并且data字典中 return 为True, 则表示该Event是minion返回的结果.
测试2:
超过timeout设置(默认为5秒)的任务:
执行命令:
salt ‘*‘ cmd.run ‘sleep 6; echo hello world‘
结果如下:
{‘tag‘: ‘20150227113729719091‘, ‘data‘: {‘_stamp‘: ‘2015-02-27T11:37:29.720087‘, ‘minions‘: [‘gs-ssh‘]}} ------ {‘tag‘: ‘new_job‘, ‘data‘: {‘tgt_type‘: ‘glob‘, ‘jid‘: ‘20150227113729719091‘, ‘tgt‘: ‘gs*‘, ‘_stamp‘: ‘2015-02-27T11:37:29.722007‘, ‘user‘: ‘root‘, ‘arg‘: [‘sleep 6; echo hello world‘], ‘fun‘: ‘cmd.run‘, ‘minions‘: [‘gs-ssh‘]}} ------ {‘tag‘: ‘salt/job/20150227113729719091/new‘, ‘data‘: {‘tgt_type‘: ‘glob‘, ‘jid‘: ‘20150227113729719091‘, ‘tgt‘: ‘gs*‘, ‘_stamp‘: ‘2015-02-27T11:37:29.723067‘, ‘user‘: ‘root‘, ‘arg‘: [‘sleep 6; echo hello world‘], ‘fun‘: ‘cmd.run‘, ‘minions‘: [‘gs-ssh‘]}} ------ {‘tag‘: ‘20150227113734739021‘, ‘data‘: {‘_stamp‘: ‘2015-02-27T11:37:34.739470‘, ‘minions‘: [‘gs-ssh‘]}} ------ {‘tag‘: ‘new_job‘, ‘data‘: {‘tgt_type‘: ‘glob‘, ‘jid‘: ‘20150227113734739021‘, ‘tgt‘: ‘gs*‘, ‘_stamp‘: ‘2015-02-27T11:37:34.740245‘, ‘user‘: ‘root‘, ‘arg‘: [‘20150227113729719091‘], ‘fun‘: ‘saltutil.find_job‘, ‘minions‘: [‘gs-ssh‘]}} ------ {‘tag‘: ‘salt/job/20150227113734739021/new‘, ‘data‘: {‘tgt_type‘: ‘glob‘, ‘jid‘: ‘20150227113734739021‘, ‘tgt‘: ‘gs*‘, ‘_stamp‘: ‘2015-02-27T11:37:34.740498‘, ‘user‘: ‘root‘, ‘arg‘: [‘20150227113729719091‘], ‘fun‘: ‘saltutil.find_job‘, ‘minions‘: [‘gs-ssh‘]}} ------ {‘tag‘: ‘20150227113734739021‘, ‘data‘: {‘fun_args‘: [‘20150227113729719091‘], ‘jid‘: ‘20150227113734739021‘, ‘return‘: {‘tgt_type‘: ‘glob‘, ‘jid‘: ‘20150227113729719091‘, ‘tgt‘: ‘gs*‘, ‘pid‘: 24526, ‘ret‘: ‘‘, ‘user‘: ‘root‘, ‘arg‘: [‘sleep 6; echo hello world‘], ‘fun‘: ‘cmd.run‘}, ‘retcode‘: 0, ‘success‘: True, ‘cmd‘: ‘_return‘, ‘_stamp‘: ‘2015-02-27T11:37:34.806601‘, ‘fun‘: ‘saltutil.find_job‘, ‘id‘: ‘gs-ssh‘}} ------ {‘tag‘: ‘salt/job/20150227113734739021/ret/gs-ssh‘, ‘data‘: {‘fun_args‘: [‘20150227113729719091‘], ‘jid‘: ‘20150227113734739021‘, ‘return‘: {‘tgt_type‘: ‘glob‘, ‘jid‘: ‘20150227113729719091‘, ‘tgt‘: ‘gs*‘, ‘pid‘: 24526, ‘ret‘: ‘‘, ‘user‘: ‘root‘, ‘arg‘: [‘sleep 6; echo hello world‘], ‘fun‘: ‘cmd.run‘}, ‘retcode‘: 0, ‘success‘: True, ‘cmd‘: ‘_return‘, ‘_stamp‘: ‘2015-02-27T11:37:34.806801‘, ‘fun‘: ‘saltutil.find_job‘, ‘id‘: ‘gs-ssh‘}} ------ {‘tag‘: ‘20150227113729719091‘, ‘data‘: {‘fun_args‘: [‘sleep 6; echo hello world‘], ‘jid‘: ‘20150227113729719091‘, ‘return‘: ‘hello world‘, ‘retcode‘: 0, ‘success‘: True, ‘cmd‘: ‘_return‘, ‘_stamp‘: ‘2015-02-27T11:37:35.814281‘, ‘fun‘: ‘cmd.run‘, ‘id‘: ‘gs-ssh‘}} ------ {‘tag‘: ‘salt/job/20150227113729719091/ret/gs-ssh‘, ‘data‘: {‘fun_args‘: [‘sleep 6; echo hello world‘], ‘jid‘: ‘20150227113729719091‘, ‘return‘: ‘hello world‘, ‘retcode‘: 0, ‘success‘: True, ‘cmd‘: ‘_return‘, ‘_stamp‘: ‘2015-02-27T11:37:35.814502‘, ‘fun‘: ‘cmd.run‘, ‘id‘: ‘gs-ssh‘}} ------
除了之前test.ping测试类似的输出外, 可以看到tag为 new_job 的event产生后的5秒, 自动产生了一个fun值为 saltutil.find_job, 其arg为之前new_job的jid的event. 然后minion返回之前运行的fun值为 cmd.run 对应的进行运行信息(pid等信息, 已确保任务正在被执行).
salt对应的处理机制:
1.master在下发指令后,如果在设置的timeout时间内, 所有minion均返回了结果, 则直接退出.
2.如果达到timeout时间(默认是5s)后, 依然有minion没有返回结果, 则自动触发一个 saltutil.find_job 的任务, 去所有minion上查询该任务是否在执行.
3.如果minion返回任务当前正在执行中, 则等待一个新的timeout周期, 如果期间所有minion均返回了结果, 则退出;
依次类推, 一直等到直到所有minion均返回结果. 如果期间在触发saltutil.find_job 时minion并没有返回任务的执行状况, 且之前并没有返回结果, 则认为minion出现问题, 就会输出"Minion did not return" 字样(可以通过salt -v参数查询到).
从该机制中可以知道, 如果经常出现minion无法返回结果的情况, 对于某些场景如规模较大或minion高负载的情况下, 达到设置的timeout时间时, 自动触发 saltutil.find_job 任务, 而minion此时并没有开始运行之前下发的任务. 导致master直接认为"Minion did not return". 此时需要增大timeout的值(可以修改master的配置文件中的timeout选项)
同时由于master会自动触发 saltutil.find_job 任务, 而该任务也会记入Event系统, 所以对于如Halite等第三方系统, 执行长时间的任务时, 你会发现大量的 saltutil.find_job 操作, 此为正常现象, 无需处理。
总结:
Salt提供了强大的Event系统, 第三方程序可以轻松插入Event系统, 捕获当前Salt的运行状态, 易于扩展Salt功能.