Pre-reading
Environment Description
Commencement
Pre-Configuration
Write returner
Test
The returner of saltstack is the storage of execution results by the minion client actively connected to returner, which cannot meet the requirements in some scenarios. because an event system has been built at the bottom layer of salt, all operations will generate events. therefore, it is possible to build master-side returner Based on the salt Event System.
We have completed the test of listening to events in the saltstack Event System. This article will build a master-side returner Based on the salt Event System.
Pre-reading
Saltstack Event System: http://docs.saltstack.com/en/latest/topics/event/index.html
Saltstack Event System listening events test: http://pengyao.org/saltstack_event_system_listen_events.html
Environment Description
Test Structure: Master/minions structure, a total of Minion, corresponding ID: salt-minion-01.example.com
Salt version: 2014.1.1
The test results will be stored in MySQL. To facilitate the test, the MySQL server has been deployed locally on the master.
Commencement
Pre-Configuration
Install mysqldb dependency
Yum-y install mysql-Python
Configure the databases and users required for this test
# Create a salt Database
Mysql-e 'create database salt'
# Create a user to connect to the salt Database
Mysql-e '"grant all on salt. * to [email protected] identified by" salt_pass ';
# Add the database configuration to the master configuration file
Echo-e "\ n # MySQL \ nmysql. HOST: 'localhost' \ nmysql. user: 'salt' \ nmysql. pass: 'sale _ pass' \ nmysql. DB: 'salt' \ nmysql. port: 3306 ">/etc/salt/Master
To be compatible with the MySQL returner provided by salt, the database table structure corresponding to MySQL retuner is used directly this time:
Use 'salt ';
--
-- Table structure for table 'jids'
--
Drop table if exists 'jids ';
Create Table 'jkids '(
'Jid' varchar (255) not null,
'Load' mediumtext not null,
Unique key 'jid' ('jid ')
) Engine = InnoDB default charset = utf8;
--
-- Table structure for table 'sale _ returns'
--
Drop table if exists 'sale _ returns ';
Create Table 'sale _ 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;
Write returner
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 party 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 '):
# Igonre 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 )'''
Cursor.exe cute (SQL, (Ret ['fun '], RET ['jid'],
JSON. dumps (Ret ['Return ']), RET ['id'],
RET ['success'], JSON. dumps (RET )))
Cursor.exe cute ("commit ")
# Other event
Else:
Pass
Run the returner:
Python salt_event_to_mysql.py
Test
Start a new terminal and run the salt command:
Salt '*' test. Ping
Output:
Salt-minion-01.example.com:
True
Check the MySQL database and query the salt_returns table data:
MySQL salt-e "select * From salt_returns \ G"
Output:
* *************************** 1. row ***************************
Fun: test. Ping
Jid: 20140417161103569310.
Return: True
Salt-minion-01.example.com
Success: 1
Full_ret: {"fun_args": [], "jid": "20140417161103569310", "Return": True, "retcode": 0, "success": True, "cmd ": "_ return", "_ stamp": "2014-04-17t16: 11: 03.584859", "fun": "test. ping "," ID ":" salt-minion-01.example.com "}
Alter_time: 2014-04-17 16:11:03
Warehouse receiving successful
This article is from the "davideylee" blog and will not be reposted!
Build master returner Based on Salt Event System