一、event接口介绍
event是一个本地的ZeroMQPUBInterface,event是一个开放的系统,用于
发送信息通知salt或其他的操作系统。每个event都有一个标签。事件标签允许
快速制定过滤事件。除了标签之外,每个事件都有一个数据结构。这个数据结构
是一个dict类型,其中包含关于事件的信息。在master上直接将返回结果写入mysql.
二、实验前测试event接口:
1、master上执行
[root@ip-192-168-1-55 salt]# vi 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
2、在开启一个会话执行命令
[root@ip-192-168-1-55 ~]# salt '*' test.ping
ip-192-168-1-52:
True
ip-192-168-1-55:
True
ip-192-168-1-53:
True
ip-192-168-1-56:
回到第一个会话
[root@ip-192-168-1-55 salt]# python salt-event.py
{'tag': 'salt/event/new_client', 'data': {'_stamp': '2017-06-14T16:17:10.463419'}}
------
{'tag': '20170615001710473901', 'data': {'_stamp': '2017-06-14T16:17:10.474253', 'minions': ['ip-192-168-1-53', 'ip-192-168-1-55', 'ip-192-168-1-52', 'ip-192-168-1-56']}}
------
{'tag': 'salt/job/20170615001710473901/new', 'data': {'tgt_type': 'glob', 'jid': '20170615001710473901', 'tgt': '*', '_stamp': '2017-06-14T16:17:10.474404', 'user': 'root', 'arg': [], 'fun': 'test.ping', 'minions': ['ip-192-168-1-53', 'ip-192-168-1-55', 'ip-192-168-1-52', 'ip-192-168-1-56']}}
------
{'tag': 'salt/job/20170615001710473901/ret/ip-192-168-1-52', 'data': {'fun_args': [], 'jid': '20170615001710473901', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2017-06-14T16:17:10.624316', 'fun': 'test.ping', 'id': 'ip-192-168-1-52'}}
------
{'tag': 'salt/job/20170615001710473901/ret/ip-192-168-1-55', 'data': {'fun_args': [], 'jid': '20170615001710473901', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2017-06-14T16:17:10.626311', 'fun': 'test.ping', 'id': 'ip-192-168-1-55'}}
------
{'tag': 'salt/job/20170615001710473901/ret/ip-192-168-1-53', 'data': {'fun_args': [], 'jid': '20170615001710473901', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2017-06-14T16:17:10.628304', 'fun': 'test.ping', 'id': 'ip-192-168-1-53'}}
------
{'tag': 'salt/job/20170615001710473901/ret/ip-192-168-1-56', 'data': {'fun_args': [], 'jid': '20170615001710473901', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2017-06-14T16:17:10.629863', 'fun': 'test.ping', 'id': 'ip-192-168-1-56'}}
至此event接口测试结束,测试证明event接口是能正常工作的。
三、编写自定义return脚本
http://www.iyunv.com/thread-68309-1-1.html
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 = conn = MySQLdb.connect(host='192.168.1.54',user='salt',passwd='salt123',db=salt,port='3306')
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
四、准备mysql
1、准备mysql服务器
yum -y mysql-server
2、安装python的mysql模块
yum -y install MySQL-python
3、创建数据库并授权
CREATE DATABASE `salt`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
USE `salt`;
--
-- Table structure for table `jids`
--
DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
`jid` varchar(255) NOT NULL,
`load` mediumtext NOT NULL,
UNIQUE KEY `jid` (`jid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX jid ON jids(jid) USING BTREE;
--
-- Table structure for table `salt_returns`
--
DROP TABLE IF EXISTS `salt_returns`;
CREATE TABLE `salt_returns` (
`fun` varchar(50) NOT NULL,
`jid` varchar(255) NOT NULL,
`return` mediumtext NOT NULL,
`id` varchar(255) NOT NULL,
`success` varchar(10) NOT NULL,
`full_ret` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
KEY `id` (`id`),
KEY `jid` (`jid`),
KEY `fun` (`fun`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Table structure for table `salt_events`
--
DROP TABLE IF EXISTS `salt_events`;
CREATE TABLE `salt_events` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`tag` varchar(255) NOT NULL,
`data` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`master_id` varchar(255) NOT NULL,PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql> grant all on salt.* to salt@'192.168.1.%' identified by "salt123";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
master主机
4、修改master的配置文件
return: mysql
mysql.host: '192.168.1.54'
mysql.user: 'salt'
mysql.pass: 'salt123'
mysql.db: 'salt'
mysql.port: 3306
五、登录mysql查看
python salt_event_to_mysql.py &
执行命令查看
salt '*' test.ping
mysql> use salt
Database changed
mysql> select * from salt_returns\G;
*************************** 1. row ***************************
fun: test.ping
jid: 20170615224945996417
return: true
id: ip-192-168-202-55
success: 1
full_ret: {"fun_args": [], "jid": "20170615224945996417", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-06-15T14:49:46.142671", "fun": "test.ping", "id": "ip-192-168-202-55"}
alter_time: 2017-06-15 22:49:46
*************************** 2. row ***************************
fun: test.ping
jid: 20170615224945996417
return: true
id: ip-192-168-202-52
success: 1
full_ret: {"fun_args": [], "jid": "20170615224945996417", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-06-15T14:49:46.145287", "fun": "test.ping", "id": "ip-192-168-202-52"}
alter_time: 2017-06-15 22:49:46
*************************** 3. row ***************************
fun: test.ping
jid: 20170615224945996417
return: true
id: ip-192-168-202-53
success: 1
full_ret: {"fun_args": [], "jid": "20170615224945996417", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-06-15T14:49:46.149664", "fun": "test.ping", "id": "ip-192-168-202-53"}
alter_time: 2017-06-15 22:49:46
*************************** 4. row ***************************
fun: test.ping
jid: 20170615224945996417
return: true
id: ip-192-168-202-56
success: 1
full_ret: {"fun_args": [], "jid": "20170615224945996417", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-06-15T14:49:46.151196", "fun": "test.ping", "id": "ip-192-168-202-56"}
alter_time: 2017-06-15 22:49:46
*************************** 5. row ***************************
fun: cmd.run
jid: 20170615225255651191
return: " total used free shared buffers cached\nMem: 3832 1082 2749 0 145 721\n-/+ buffers/cache: 215 3616\nSwap: 4095 0 4095"
id: ip-192-168-202-52
success: 1
full_ret: {"fun_args": ["free -m"], "jid": "20170615225255651191", "return": " total used free shared buffers cached\nMem: 3832 1082 2749 0 145 721\n-/+ buffers/cache: 215 3616\nSwap: 4095 0 4095", "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-06-15T14:52:55.807753", "fun": "cmd.run", "id": "ip-192-168-202-52"}
alter_time: 2017-06-15 22:52:55
5 rows in set (0.00 sec)
ERROR:
No query specified
event是一个本地的ZeroMQPUBInterface,event是一个开放的系统,用于
发送信息通知salt或其他的操作系统。每个event都有一个标签。事件标签允许
快速制定过滤事件。除了标签之外,每个事件都有一个数据结构。这个数据结构
是一个dict类型,其中包含关于事件的信息。在master上直接将返回结果写入mysql.
二、实验前测试event接口:
1、master上执行
[root@ip-192-168-1-55 salt]# vi 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
2、在开启一个会话执行命令
[root@ip-192-168-1-55 ~]# salt '*' test.ping
ip-192-168-1-52:
True
ip-192-168-1-55:
True
ip-192-168-1-53:
True
ip-192-168-1-56:
回到第一个会话
[root@ip-192-168-1-55 salt]# python salt-event.py
{'tag': 'salt/event/new_client', 'data': {'_stamp': '2017-06-14T16:17:10.463419'}}
------
{'tag': '20170615001710473901', 'data': {'_stamp': '2017-06-14T16:17:10.474253', 'minions': ['ip-192-168-1-53', 'ip-192-168-1-55', 'ip-192-168-1-52', 'ip-192-168-1-56']}}
------
{'tag': 'salt/job/20170615001710473901/new', 'data': {'tgt_type': 'glob', 'jid': '20170615001710473901', 'tgt': '*', '_stamp': '2017-06-14T16:17:10.474404', 'user': 'root', 'arg': [], 'fun': 'test.ping', 'minions': ['ip-192-168-1-53', 'ip-192-168-1-55', 'ip-192-168-1-52', 'ip-192-168-1-56']}}
------
{'tag': 'salt/job/20170615001710473901/ret/ip-192-168-1-52', 'data': {'fun_args': [], 'jid': '20170615001710473901', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2017-06-14T16:17:10.624316', 'fun': 'test.ping', 'id': 'ip-192-168-1-52'}}
------
{'tag': 'salt/job/20170615001710473901/ret/ip-192-168-1-55', 'data': {'fun_args': [], 'jid': '20170615001710473901', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2017-06-14T16:17:10.626311', 'fun': 'test.ping', 'id': 'ip-192-168-1-55'}}
------
{'tag': 'salt/job/20170615001710473901/ret/ip-192-168-1-53', 'data': {'fun_args': [], 'jid': '20170615001710473901', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2017-06-14T16:17:10.628304', 'fun': 'test.ping', 'id': 'ip-192-168-1-53'}}
------
{'tag': 'salt/job/20170615001710473901/ret/ip-192-168-1-56', 'data': {'fun_args': [], 'jid': '20170615001710473901', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2017-06-14T16:17:10.629863', 'fun': 'test.ping', 'id': 'ip-192-168-1-56'}}
至此event接口测试结束,测试证明event接口是能正常工作的。
三、编写自定义return脚本
http://www.iyunv.com/thread-68309-1-1.html
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 = conn = MySQLdb.connect(host='192.168.1.54',user='salt',passwd='salt123',db=salt,port='3306')
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
四、准备mysql
1、准备mysql服务器
yum -y mysql-server
2、安装python的mysql模块
yum -y install MySQL-python
3、创建数据库并授权
CREATE DATABASE `salt`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
USE `salt`;
--
-- Table structure for table `jids`
--
DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
`jid` varchar(255) NOT NULL,
`load` mediumtext NOT NULL,
UNIQUE KEY `jid` (`jid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX jid ON jids(jid) USING BTREE;
--
-- Table structure for table `salt_returns`
--
DROP TABLE IF EXISTS `salt_returns`;
CREATE TABLE `salt_returns` (
`fun` varchar(50) NOT NULL,
`jid` varchar(255) NOT NULL,
`return` mediumtext NOT NULL,
`id` varchar(255) NOT NULL,
`success` varchar(10) NOT NULL,
`full_ret` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
KEY `id` (`id`),
KEY `jid` (`jid`),
KEY `fun` (`fun`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Table structure for table `salt_events`
--
DROP TABLE IF EXISTS `salt_events`;
CREATE TABLE `salt_events` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`tag` varchar(255) NOT NULL,
`data` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`master_id` varchar(255) NOT NULL,PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql> grant all on salt.* to salt@'192.168.1.%' identified by "salt123";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
master主机
4、修改master的配置文件
return: mysql
mysql.host: '192.168.1.54'
mysql.user: 'salt'
mysql.pass: 'salt123'
mysql.db: 'salt'
mysql.port: 3306
五、登录mysql查看
python salt_event_to_mysql.py &
执行命令查看
salt '*' test.ping
mysql> use salt
Database changed
mysql> select * from salt_returns\G;
*************************** 1. row ***************************
fun: test.ping
jid: 20170615224945996417
return: true
id: ip-192-168-202-55
success: 1
full_ret: {"fun_args": [], "jid": "20170615224945996417", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-06-15T14:49:46.142671", "fun": "test.ping", "id": "ip-192-168-202-55"}
alter_time: 2017-06-15 22:49:46
*************************** 2. row ***************************
fun: test.ping
jid: 20170615224945996417
return: true
id: ip-192-168-202-52
success: 1
full_ret: {"fun_args": [], "jid": "20170615224945996417", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-06-15T14:49:46.145287", "fun": "test.ping", "id": "ip-192-168-202-52"}
alter_time: 2017-06-15 22:49:46
*************************** 3. row ***************************
fun: test.ping
jid: 20170615224945996417
return: true
id: ip-192-168-202-53
success: 1
full_ret: {"fun_args": [], "jid": "20170615224945996417", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-06-15T14:49:46.149664", "fun": "test.ping", "id": "ip-192-168-202-53"}
alter_time: 2017-06-15 22:49:46
*************************** 4. row ***************************
fun: test.ping
jid: 20170615224945996417
return: true
id: ip-192-168-202-56
success: 1
full_ret: {"fun_args": [], "jid": "20170615224945996417", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-06-15T14:49:46.151196", "fun": "test.ping", "id": "ip-192-168-202-56"}
alter_time: 2017-06-15 22:49:46
*************************** 5. row ***************************
fun: cmd.run
jid: 20170615225255651191
return: " total used free shared buffers cached\nMem: 3832 1082 2749 0 145 721\n-/+ buffers/cache: 215 3616\nSwap: 4095 0 4095"
id: ip-192-168-202-52
success: 1
full_ret: {"fun_args": ["free -m"], "jid": "20170615225255651191", "return": " total used free shared buffers cached\nMem: 3832 1082 2749 0 145 721\n-/+ buffers/cache: 215 3616\nSwap: 4095 0 4095", "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-06-15T14:52:55.807753", "fun": "cmd.run", "id": "ip-192-168-202-52"}
alter_time: 2017-06-15 22:52:55
5 rows in set (0.00 sec)
ERROR:
No query specified