saltstack中的event为我们提供了跟踪salt执行的的机制。 下面是一个简单实用salt event的例子:
import salt.utils.event event = salt.utils.event.MasterEvent('/var/run/salt/master') for eachevent in event.iter_events(full=True): print eachevent print "------"
上面一段程序会简单的打印saltstack执行的salt情况,当然,我们可以把执行日志保存到文件或者数据库。使用方式是首先执行上面的脚本:
python monitor_event.py
然后在另外一个终端中执行一个salt命令,例如: salt '*' cmd.run ifconfig
这样前面的event监控脚本就会打印出执行任务的情况。 输出类似:
{'tag': 'salt/job/20150107144335112361/ret/YTJ159-93', 'data': {'_stamp': '2015-01-07_14:43:35.187762', 'return': 'eth0 Link encap:Ethernet HWaddr 00:50:56:87:21:EB \n inet addr:192.168.159.93 Bcast:192.168.159.255 Mask:255.255.255.0\n inet6 addr: fe80::250:56ff:fe87:21eb/64 Scope:Link\n UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\n RX packets:6433862 errors:0 dropped:90 overruns:0 frame:0\n TX packets:3675637 errors:0 dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:1000 \n RX bytes:1500465047 (1.3 GiB) TX bytes:414845375 (395.6 MiB)\n\nlo Link encap:Local Loopback \n inet addr:127.0.0.1 Mask:255.0.0.0\n inet6 addr: ::1/128 Scope:Host\n UP LOOPBACK RUNNING MTU:16436 Metric:1\n RX packets:656 errors:0 dropped:0 overruns:0 frame:0\n TX packets:656 errors:0 dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:0 \n RX bytes:57678 (56.3 KiB) TX bytes:57678 (56.3 KiB)', 'retcode': 0, 'success': True, 'cmd': '_return', 'jid': '20150107144335112361', 'fun': 'cmd.run', 'id': 'YTJ159-93'}} ------ {'tag': '20150107144335112361', 'data': {'_stamp': '2015-01-07_14:43:35.188341', 'return': 'eth0 Link encap:Ethernet HWaddr 00:50:56:87:24:45 \n inet addr:192.168.159.91 Bcast:192.168.159.255 Mask:255.255.255.0\n inet6 addr: fe80::250:56ff:fe87:2445/64 Scope:Link\n UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\n RX packets:6685123 errors:0 dropped:480 overruns:0 frame:0\n TX packets:4521306 errors:0 dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:1000 \n RX bytes:2803978390 (2.6 GiB) TX bytes:820875087 (782.8 MiB)\n\nlo Link encap:Local Loopback \n inet addr:127.0.0.1 Mask:255.0.0.0\n inet6 addr: ::1/128 Scope:Host\n UP LOOPBACK RUNNING MTU:16436 Metric:1\n RX packets:115 errors:0 dropped:0 overruns:0 frame:0\n TX packets:115 errors:0 dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:0 \n RX bytes:12222 (11.9 KiB) TX bytes:12222 (11.9 KiB)', 'retcode': 0, 'success': True, 'cmd': '_return', 'jid': '20150107144335112361', 'fun': 'cmd.run', 'id': 'YTJ159-91'}} ------ {'tag': 'salt/job/20150107144335112361/ret/YTJ159-91', 'data': {'_stamp': '2015-01-07_14:43:35.197173', 'return': 'eth0 Link encap:Ethernet HWaddr 00:50:56:87:24:45 \n inet addr:192.168.159.91 Bcast:192.168.159.255 Mask:255.255.255.0\n inet6 addr: fe80::250:56ff:fe87:2445/64 Scope:Link\n UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\n RX packets:6685123 errors:0 dropped:480 overruns:0 frame:0\n TX packets:4521306 errors:0 dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:1000 \n RX bytes:2803978390 (2.6 GiB) TX bytes:820875087 (782.8 MiB)\n\nlo Link encap:Local Loopback \n inet addr:127.0.0.1 Mask:255.0.0.0\n inet6 addr: ::1/128 Scope:Host\n UP LOOPBACK RUNNING MTU:16436 Metric:1\n RX packets:115 errors:0 dropped:0 overruns:0 frame:0\n TX packets:115 errors:0 dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:0 \n RX bytes:12222 (11.9 KiB) TX bytes:12222 (11.9 KiB)', 'retcode': 0, 'success': True, 'cmd': '_return', 'jid': '20150107144335112361', 'fun': 'cmd.run', 'id': 'YTJ159-91'}} ------
输出内容中包含了salt执行任务的详细情况。
原文链接:http://outofmemory.cn/saltstack/monitor-salt-with-event