【saltstack学习笔记之五】salt的event接口
# Listen Salt Master Event Systemevent = 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
> 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
> 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
> 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
> 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)
页:
[1]