标签:saltstack
event是一个本地的ZeroMQ PUB Interface,event是一个开放的系统,用于发送信息通知salt或其他的操作系统。每个event都有一个标签。事件标签允许快速制定过滤事件。除了标签之外,每个事件都有一个数据结构。这个数据结构是一个dict类型,其中包含关于事件的信息。
在master上直接将返回结果写入mysql
1.event接口测试
测试脚本salt-event.py内容如下:
import salt.utils.event
event = salt.utils.event.MasterEvent(‘/var/run/salt/master‘)
for data in event.iter_events(full=True):
print data
print ‘------‘
python salt-event.py //执行这个脚本
然后再开一个新的master终端进行salt操作:
salt ‘*‘ test.ping
就可以看到返回结果了。结果正常应该如下:
{‘tag‘: ‘salt/event/new_client‘, ‘data‘: {‘_stamp‘: ‘2015-05-18T15:59:56.010670‘}}
------
{‘tag‘: ‘20150518235956026979‘, ‘data‘: {‘_stamp‘: ‘2015-05-18T15:59:56.027668‘, ‘minions‘: [‘Admin2-saltstack.littlebee.com‘, ‘Admin3-saltstack.littlebee.com‘]}}
------
{‘tag‘: ‘salt/job/20150518235956026979/new‘, ‘data‘: {‘tgt_type‘: ‘glob‘, ‘jid‘: ‘20150518235956026979‘, ‘tgt‘: ‘*‘, ‘_stamp‘: ‘2015-05-18T15:59:56.029385‘, ‘user‘: ‘root‘, ‘arg‘: [], ‘fun‘: ‘test.ping‘, ‘minions‘: [‘Admin2-saltstack.littlebee.com‘, ‘Admin3-saltstack.littlebee.com‘]}}
------
{‘tag‘: ‘salt/job/20150518235956026979/ret/Admin2-saltstack.littlebee.com‘, ‘data‘: {‘fun_args‘: [], ‘jid‘: ‘20150518235956026979‘, ‘return‘: True, ‘retcode‘: 0, ‘success‘: True, ‘cmd‘: ‘_return‘, ‘_stamp‘: ‘2015-05-18T15:59:56.086006‘, ‘fun‘: ‘test.ping‘, ‘id‘: ‘Admin2-saltstack.littlebee.com‘}}
------
{‘tag‘: ‘salt/job/20150518235956026979/ret/Admin3-saltstack.littlebee.com‘, ‘data‘: {‘fun_args‘: [], ‘jid‘: ‘20150518235956026979‘, ‘return‘: True, ‘retcode‘: 0, ‘success‘: True, ‘cmd‘: ‘_return‘, ‘_stamp‘: ‘2015-05-18T15:59:56.088761‘, ‘fun‘: ‘test.ping‘, ‘id‘: ‘Admin3-saltstack.littlebee.com‘}}
------
至此event接口测试结束,测试证明event接口是能正常工作的。
2.编写自定义return脚本
vim salt_event_to_mysql.py
#!/bin/env python
#coding=utf8
# Import python libs
import json
# Import salt modules
import salt.config
import salt.utils.event
# Import third part libs
import MySQLdb
__opts__ = salt.config.client_config(‘/etc/salt/master‘)
#create MySQL connect
conn = MySQLdb.connect(host=__opts__[‘mysql.host‘],user=__opts__[‘mysql.user‘],passwd=__opts__[‘mysql.pass‘],db=__opts__[‘mysql.db‘],port=__opts__[‘mysql.port‘])
cursor = conn.cursor()
# Listen Salt Master Event System
event = salt.utils.event.MasterEvent(__opts__[‘sock_dir‘])
for eachevent in event.iter_events(full=True):
ret = eachevent[‘data‘]
if "salt/job/" in eachevent[‘tag‘]:
#Return Event
if ret.has_key(‘id‘) and ret.has_key(‘return‘):
#Ignore saltutil.find_job event
if ret[‘fun‘] == "saltutil.find_job":
continue
sql = ‘‘‘INSERT INTO `salt_returns`
(`fun`,`jid`,`return`,`id`,`success`,`full_ret` )
VALUES (%s,%s,%s,%s,%s,%s)‘‘‘
cursor.execute(sql,(ret[‘fun‘],ret[‘jid‘],
json.dumps(ret[‘return‘]),ret[‘id‘],
ret[‘success‘],json.dumps(ret)))
cursor.execute("COMMIT")
# Other Event
else:
pass
保存退出
3.修改master的配置文件
vim /etc/salt/master
mysql.host: ‘192.168.1.105‘ //mysql服务器的IP地址
mysql.user: ‘salt‘ //mysql数据库的用户名,需要跟后面授权的用户名一致
mysql.pass: ‘salt‘ //mysql数据库的密码,需要跟后面授权的密码一致
mysql.db: ‘salt‘ //mysql数据库的名称
mysql.port: 3306 //使用端口为3306
保存退出
4.在master上安装MySQL-python
yum -y install MySQL-python
5.在master的后台执行自定义return脚本
python salt_event_to_mysql.py &
6.开一个新的master终端进行测试
salt ‘*‘ test.ping
7.在mysql上看是否已经将数据写入数据库
mysql -uroot -p
输入密码之后进入mysql数据库
use salt
show tables;
select * from salt_returns \G
如果出现如下结果表示插入成功:
*************************** 11. row ***************************
fun: test.ping
jid: 20150519004647663439
return: true
id: Admin2-saltstack.littlebee.com
success: 1
full_ret: {"fun_args": [], "jid": "20150519004647663439", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-05-18T16:46:47.729339", "fun": "test.ping", "id": "Admin2-saltstack.littlebee.com"}
*************************** 12. row ***************************
fun: test.ping
jid: 20150519004647663439
return: true
id: Admin2-saltstack.littlebee.com
success: 1
full_ret: {"fun_args": [], "jid": "20150519004647663439", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-05-18T16:46:47.729339", "fun": "test.ping", "id": "Admin2-saltstack.littlebee.com"}
*************************** 13. row ***************************
fun: test.ping
jid: 20150519004647663439
return: true
id: Admin3-saltstack.littlebee.com
success: 1
full_ret: {"fun_args": [], "jid": "20150519004647663439", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-05-18T16:46:47.732864", "fun": "test.ping", "id": "Admin3-saltstack.littlebee.com"}
*************************** 14. row ***************************
fun: test.ping
jid: 20150519004647663439
return: true
id: Admin3-saltstack.littlebee.com
success: 1
full_ret: {"fun_args": [], "jid": "20150519004647663439", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2015-05-18T16:46:47.732864", "fun": "test.ping", "id": "Admin3-saltstack.littlebee.com"}
14 rows in set (0.00 sec)
本文出自 “求阙斋” 博客,谢绝转载!
标签:saltstack
原文地址:http://ryomajia.blog.51cto.com/240953/1652626