saltstack event和websocket实时监控触发事件

前沿:

        那么以前都是调取一个url的接口,把结果post过去,web服务器接收到请求,会给所有链接到websocket server的客户端send数据。那么问题来了,这总是频繁的调用url,然后再send,觉得有些多此一举,还不如用python websocket client直接链接到websocket server上来,那么既然都连接在一块了,互相交互也就方便的多。

虽然爬虫爬了我的网站,但是大家别忘了我的原站   ,  blog.xiaorui.cc

2014版本的saltstack出来了一个叫做 event 事件的东西,那么问题来了,event是什么?  

我也不长篇大论了,他是为saltstack提供了一个事件机制,咱们通过saltstack做的一些实例操作,在event可以体现出来。比如,调用test.ping 、 cmd.run的时候,会发现在event上可以看到我们在操作的动作。 


最明显的log是,创建任务,然后找jid的结果,没有的话,再循环的查训,知道你配置了timeout为止。

In [1]: import salt.utils.event


In [2]: event = salt.utils.event.MasterEvent('/var/run/salt/master')


In [3]: for eachevent in event.iter_events(full=True):
   ...:         print eachevent
   ...:         print "------"
   ...:    

跑一下,结果如下


[root@devops-ruifengyun bj_lvs2 ]$ python event.py 
{'tag': '20140528090741247990', 'data': {'_stamp': '2014-05-28_09:07:41.248175', 'minions': ['10.150.145.51']}}
------
{'tag': 'new_job', 'data': {'tgt_type': 'glob', 'jid': '20140528090741247990', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:07:41.248224', 'user': 'root', 'arg': ['ip a'], 'fun': 'cmd.run', 'minions': ['10.150.145.51']}}
------
{'tag': 'salt/job/20140528090741247990/new', 'data': {'tgt_type': 'glob', 'jid': '20140528090741247990', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:07:41.248250', 'user': 'root', 'arg': ['ip a'], 'fun': 'cmd.run', 'minions': ['10.150.145.51']}}
------
{'tag': '20140528090741247990', 'data': {'fun_args': ['ip a'], 'jid': '20140528090741247990', 'return': '1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN \n    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n    inet 127.0.0.1/8 scope host lo\n    inet6 ::1/128 scope host \n       valid_lft forever preferred_lft forever\n2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000\n    link/ether 00:0c:29:fe:a4:a1 brd ff:ff:ff:ff:ff:ff\n    inet 10.150.145.51/16 brd 10.150.255.255 scope global eth0\n    inet 10.150.145.55/32 scope global eth0\n    inet 10.150.145.100/32 scope global eth0\n    inet6 fe80::20c:29ff:fefe:a4a1/64 scope link \n       valid_lft forever preferred_lft forever', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:07:41.393589', 'fun': 'cmd.run', 'id': '10.150.145.51'}}
------
{'tag': 'salt/job/20140528090741247990/ret/10.150.145.51', 'data': {'fun_args': ['ip a'], 'jid': '20140528090741247990', 'return': '1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN \n    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n    inet 127.0.0.1/8 scope host lo\n    inet6 ::1/128 scope host \n       valid_lft forever preferred_lft forever\n2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000\n    link/ether 00:0c:29:fe:a4:a1 brd ff:ff:ff:ff:ff:ff\n    inet 10.150.145.51/16 brd 10.150.255.255 scope global eth0\n    inet 10.150.145.55/32 scope global eth0\n    inet 10.150.145.100/32 scope global eth0\n    inet6 fe80::20c:29ff:fefe:a4a1/64 scope link \n       valid_lft forever preferred_lft forever', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:07:41.393740', 'fun': 'cmd.run', 'id': '10.150.145.51'}}
------
{'tag': '20140528090742530725', 'data': {'_stamp': '2014-05-28_09:07:42.530881', 'minions': ['10.150.145.51']}}
------
{'tag': 'new_job', 'data': {'tgt_type': 'glob', 'jid': '20140528090742530725', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:07:42.530922', 'user': 'root', 'arg': ['ip a'], 'fun': 'cmd.run', 'minions': ['10.150.145.51']}}
------
{'tag': 'salt/job/20140528090742530725/new', 'data': {'tgt_type': 'glob', 'jid': '20140528090742530725', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:07:42.530946', 'user': 'root', 'arg': ['ip a'], 'fun': 'cmd.run', 'minions': ['10.150.145.51']}}
------
{'tag': '20140528090742530725', 'data': {'fun_args': ['ip a'], 'jid': '20140528090742530725', 'return': '1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN \n    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n    inet 127.0.0.1/8 scope host lo\n    inet6 ::1/128 scope host \n       valid_lft forever preferred_lft forever\n2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000\n    link/ether 00:0c:29:fe:a4:a1 brd ff:ff:ff:ff:ff:ff\n    inet 10.150.145.51/16 brd 10.150.255.255 scope global eth0\n    inet 10.150.145.55/32 scope global eth0\n    inet 10.150.145.100/32 scope global eth0\n    inet6 fe80::20c:29ff:fefe:a4a1/64 scope link \n       valid_lft forever preferred_lft forever', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:07:42.655764', 'fun': 'cmd.run', 'id': '10.150.145.51'}}
------
{'tag': 'salt/job/20140528090742530725/ret/10.150.145.51', 'data': {'fun_args': ['ip a'], 'jid': '20140528090742530725', 'return': '1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN \n    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n    inet 127.0.0.1/8 scope host lo\n    inet6 ::1/128 scope host \n       valid_lft forever preferred_lft forever\n2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000\n    link/ether 00:0c:29:fe:a4:a1 brd ff:ff:ff:ff:ff:ff\n    inet 10.150.145.51/16 brd 10.150.255.255 scope global eth0\n    inet 10.150.145.55/32 scope global eth0\n    inet 10.150.145.100/32 scope global eth0\n    inet6 fe80::20c:29ff:fefe:a4a1/64 scope link \n       valid_lft forever preferred_lft forever', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:07:42.655909', 'fun': 'cmd.run', 'id': '10.150.145.51'}}
------
{'tag': '20140528090829833342', 'data': {'_stamp': '2014-05-28_09:08:29.833516', 'minions': ['10.150.145.51']}}
------
{'tag': 'new_job', 'data': {'tgt_type': 'glob', 'jid': '20140528090829833342', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:08:29.833566', 'user': 'root', 'arg': ['sleep 3;ip a'], 'fun': 'cmd.run', 'minions': ['10.150.145.51']}}
------

在这里也可以琢磨出来他获取结果的思路:

创建一个任务及jid,然后每次去看看刚才(sleep 30;ip a ) 的结果,每隔五秒轮寻一次结果。每次去saltutil.find_job的jid是相同的,不同的是saltutil.find_job他本身的任务号。

------
 
{'tag': 'new_job', 'data': {'tgt_type': 'glob', 'jid': '20140528092216402264', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:22:16.402889', 'user': 'root', 'arg': ['20140528092151158538', {'__kwarg__': True}], 'fun': 'saltutil.find_job', 'minions': ['10.150.145.51']}}
------
{'tag': 'salt/job/20140528092216402264/new', 'data': {'tgt_type': 'glob', 'jid': '20140528092216402264', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:22:16.402975', 'user': 'root', 'arg': ['20140528092151158538', {'__kwarg__': True}], 'fun': 'saltutil.find_job', 'minions': ['10.150.145.51']}}
------
{'tag': '20140528092216402264', 'data': {'fun_args': ['20140528092151158538', {'__kwarg__': True}], 'jid': '20140528092216402264', 'return': {'tgt_type': 'glob', 'jid': '20140528092151158538', 'tgt': '10.150.145.51', 'pid': 30325, 'ret': '', 'user': 'root', 'arg': ['sleep 30;ip a'], 'fun': 'cmd.run'}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:22:16.470647', 'fun': 'saltutil.find_job', 'id': '10.150.145.51'}}
------
{'tag': 'salt/job/20140528092216402264/ret/10.150.145.51', 'data': {'fun_args': ['20140528092151158538', {'__kwarg__': True}], 'jid': '20140528092216402264', 'return': {'tgt_type': 'glob', 'jid': '20140528092151158538', 'tgt': '10.150.145.51', 'pid': 30325, 'ret': '', 'user': 'root', 'arg': ['sleep 30;ip a'], 'fun': 'cmd.run'}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:22:16.470789', 'fun': 'saltutil.find_job', 'id': '10.150.145.51'}}
------
{'tag': '20140528092151158538', 'data': {'fun_args': ['sleep 30;ip a'], 'jid': '20140528092151158538', 'return': '1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN \n    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n    inet 127.0.0.1/8 scope host lo\n    inet6 ::1/128 scope host \n       valid_lft forever preferred_lft forever\n2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000\n    link/ether 00:0c:29:fe:a4:a1 brd ff:ff:ff:ff:ff:ff\n    inet 10.150.145.51/16 brd 10.150.255.255 scope global eth0\n    inet 10.150.145.55/32 scope global eth0\n    inet 10.150.145.100/32 scope global eth0\n    inet6 fe80::20c:29ff:fefe:a4a1/64 scope link \n       valid_lft forever preferred_lft forever', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:22:21.281017', 'fun': 'cmd.run', 'id': '10.150.145.51'}}


对于websocket把结果打到前端,咱们就直接用websocket客户端搞~

websocket模块的地址在,https://github.com/liris/websocket-client.git

pip install https://github.com/liris/websocket-client.git
cd webso*
python setup.py install

下面是用python实现的websocket的客户端。

from websocket import create_connection
ws = create_connection("ws://localhost:8888/ws")
print "Sending 'Hello, World'..."
ws.send("Hello, World")
print "Sent"
print "Reeiving..."
result =  ws.recv()
print "Received '%s'" % result
ws.close()

话说event和websocket的逻辑有些乱,可以预先把任务的jid放到list里面,然后每次去遍历数据,send到前端。具体的实现代码我就不详细写了,我上面已经说了event的用法,相信大家也明白。 


讲的不多,同样写的也很是泛泛,大家图个Happy就好了 ~


大家觉得文章对你有些作用! 如果想赏钱,可以用微信扫描下面的二维码,感谢!
另外再次标注博客原地址  xiaorui.cc

7 Responses

  1. 玉开Sir 2015年1月7日 / 下午2:31

    在你自己的网站上图片显示都提示“被爬虫了”?

    • 峰云 2015年1月7日 / 下午5:05

      先放着,过段时间再干掉这个图片。

  2. noops 2014年11月19日 / 上午8:23

    果然是你在51cto写的内容,应该早点移到这里

    • 峰云 2014年11月19日 / 上午11:03

      呵呵,是呀。

发表评论

电子邮件地址不会被公开。 必填项已用*标注