一,event介绍
event是一个本地的ZeroMQ PUB Interace,event是一个开放的系统,用于发送消息通知salt或其它操作系统。
每个event都有一个标签。时间标签允许快速至顶过滤事件。除了标签之外,每个时间都有一个数据结构。这个数据结构是一个dict类型,其中包含关于事件的信息。
salt-master发送命令到salt-minion,minion将命令结果发送给event,master从event读取返回结果,然后将结果写入mysql。
二,系统结构图
三,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操作:
[root@saltmaster ~]# salt '*' cmd.run 'hostname' dns02: dns02 dns01: dns01
结果正常返回如下:
{'tag': 'salt/event/new_client', 'data': {'_stamp': '2017-12-10T02:31:52.985835'}} ------ {'tag': '20171210103152996055', 'data': {'_stamp': '2017-12-10T02:31:52.996321', 'minions': ['dns01', 'dns02']}} ------ {'tag': 'salt/job/20171210103152996055/new', 'data': {'tgt_type': 'glob', 'jid': '20171210103152996055', 'tgt': '*', '_stamp': '2017-12-10T02:31:52.996572', 'user': 'root', 'arg': ['hostname'], 'fun': 'cmd.run', 'minions': ['dns01', 'dns02']}} ------ {'tag': 'salt/job/20171210103152996055/ret/dns02', 'data': {'fun_args': ['hostname'], 'jid': '20171210103152996055', 'return': 'dns02', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2017-12-10T02:31:53.129170', 'fun': 'cmd.run', 'id': 'dns02'}} ------ {'tag': 'salt/job/20171210103152996055/ret/dns01', 'data': {'fun_args': ['hostname'], 'jid': '20171210103152996055', 'return': 'dns01', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2017-12-10T02:31:53.129705', 'fun': 'cmd.run', 'id': 'dns01'}} ------
四,安装MySQL
1,安装MySQL软件
[root@saltmaster ~]# yum install -y mysql [root@saltmaster ~]# yum install -y mysql-server
2,启动MySQL
[root@saltmaster tmp]# service mysqld start Starting MySQL........ SUCCESS!
[root@saltmaster tmp]# mysqladmin -u root password '123456'
--创建数据库 CREATE DATABASE `salt`DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; USE `salt`; 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; 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,KEY `id` (`id`),KEY `jid` (`jid`),KEY `fun` (`fun`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; --授权 GRANT ALL PRIVILEGES ON salt.* to 'salt'@'%' identified by 'salt'; flush privileges;
五,event-to-mysql测试
1,在master安装MySQL-python模块
[root@saltmaster ~]# yum install -y MySQL-python
2,在master配置文件结尾添加如下配置:
[root@saltmaster ~]# vi /etc/salt/master
mysql.host: '10.80.0.162' mysql.user: 'salt' mysql.pass: 'salt' mysql.db: 'salt' mysql.port: 3306
3,编写自定义returnn脚本
[root@saltmaster ~]#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']) conn = MySQLdb.connect(host='192.168.3.87',user='salt',passwd='salt',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
- 注意:
MySQLdb.connect(host=__opts__['mysql.host'],user=__opts__['mysql.user'],passwd=__opts__['mysql.pass'],db=__opts__['mysql.db'],port=__opts__['mysql.port']),要换成自己的实际数据库地址、数据库用户、密码,如:
conn = MySQLdb.connect(host='10.80.0.162',user='salt',passwd='salt',db='salt',port=3306) - python语言是根据缩进来识别语法的,上面脚本不要随意修改缩进,否则可能会出现各种各样的问题。
4,在master的后台执行自定义return脚本
[root@saltmaster scripts]# python salt_event_to_mysql.py & [1] 20988
注意:常见报错处理“_mysql_exceptions.OperationalError: (1045, "Access denied for user 'salt'@'saltmaster' (using password: YES)")”
#使用mysql-client登录mysql测试 [root@saltmaster scripts]# mysql -usalt -psalt -h10.80.0.162 ERROR 1045 (28000): Access denied for user 'salt'@'saltmaster' (using password: YES)
#28000一般为mysql存在空用户导致,需要先删除空用户 mysql> use mysql mysql> select user,host,password from user; [root@saltmaster scripts]# mysql -usalt -psalt -h10.80.0.162 Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 2 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>
5,开一个新的master终端进行测试
salt '*' test.ping
6,在mysql上看是否已经将数据写入数据库
mysql -uroot –p
输入密码之后进入mysql数据库
mysql> use salt Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from salt_returns; +-----------+----------------------+--------+-------+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | fun | jid | return | id | success | full_ret | +-----------+----------------------+--------+-------+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | test.ping | 20171210232456533428 | true | dns01 | 1 | {"fun_args": [], "jid": "20171210232456533428", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-12-10T15:24:56.692614", "fun": "test.ping", "id": "dns01"} | | test.ping | 20171210232456533428 | true | dns02 | 1 | {"fun_args": [], "jid": "20171210232456533428", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-12-10T15:24:56.699512", "fun": "test.ping", "id": "dns02"} | | test.ping | 20171210232602079640 | true | dns02 | 1 | {"fun_args": [], "jid": "20171210232602079640", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-12-10T15:26:02.237228", "fun": "test.ping", "id": "dns02"} | | test.ping | 20171210232602079640 | true | dns01 | 1 | {"fun_args": [], "jid": "20171210232602079640", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2017-12-10T15:26:02.244711", "fun": "test.ping", "id": "dns01"} | +-----------+----------------------+--------+-------+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec)
六,总结:
数据库服务器:
- 安装mysql
- 创建数据库,授权
master
- 修改master配置文件
- 准备监听event的py脚本
- 安装python的mysql模块(MySQL-python)