首页 > 代码库 > saltstack 把数据返回到mysql服务器

saltstack 把数据返回到mysql服务器

master端需要安装MySQL-python和mysql-server

mysql-server用于存储minion数据,MySQL-python用来收集数据  

              master端

安装mysql-server和MySQL-python

[root@salt-master /]# yum -y install mysql-server MySQL-python

启动数据库

[root@salt-master /]# /etc/init.d/mysqld start[root@salt-master /]# mysqlWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.1.73 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type help; or \h for help. Type \c to clear the current input statement.mysql> 
CREATE DATABASE  `salt`  DEFAULT CHARACTER SET utf8  DEFAULT COLLATE utf8_general_ci;USE `salt`;
如下:
mysql> CREATE DATABASE  `salt`    ->   DEFAULT CHARACTER SET utf8    ->   DEFAULT COLLATE utf8_general_ci;Query OK, 1 row affected (0.00 sec)mysql> USE `salt`;Database changed

表结构表的jid

CREATE TABLE `jids` (  `jid` varchar(255) NOT NULL,  `load` mediumtext NOT NULL,  UNIQUE KEY `jid` (`jid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql> CREATE TABLE `jids` (    ->   `jid` varchar(255) NOT NULL,    ->   `load` mediumtext NOT NULL,    ->   UNIQUE KEY `jid` (`jid`)    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;Query OK, 0 rows affected (0.02 sec)

表结构表“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;
mysql> 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;Query OK, 0 rows affected (0.01 sec)

表结构表“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> 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;Query OK, 0 rows affected (0.00 sec)

授权

mysql> grant all on salt.* to salt@‘192.168.161.131‘ identified by ‘salt‘;
Query OK, 0 rows affected (0.00 sec)

测试

[root@salt-master /]# mysql -u salt -p -h 192.168.161.131Enter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 5Server version: 5.1.73 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type help; or \h for help. Type \c to clear the current input statement.mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || salt               || test               |+--------------------+3 rows in set (0.00 sec)

修改/etc/salt/master

[root@salt-master /]# vim /etc/salt/mastermysql.host: 192.168.161.131mysql.user: saltmysql.pass: saltmysql.db: saltmysql.port: 3306master_job_cache: mysql      #每次执行不加--return mysql由master端将返回的数据写入数据库例:salt * test.ping --return mysql

重启master

[root@salt-master /]# /etc/init.d/salt-master restart
Stopping salt-master daemon: [ OK ]
Starting salt-master daemon: [ OK ]

 

minion端

[root@salt-minion /]# yum -y install MySQL-python

修改/etc/salt/minion 添加如下内容

[root@salt-minion /]# vim /etc/salt/minion
mysql.host: 192.168.161.131mysql.user: saltmysql.pass: saltmysql.db: saltmysql.port: 3306

重启minion

[root@salt-minion /]# /etc/init.d/salt-minion restartStopping salt-minion daemon:                               [  OK  ]Starting salt-minion daemon:                               [  OK  ]

测试

[root@salt-master /]# salt * test.ping --return mysqlsalt-minion:    True[root@salt-master /]# salt salt-minion test.pingsalt-minion:    True[root@salt-master /]# salt salt-minion cmd.run df -hsalt-minion:    Filesystem      Size  Used Avail Use% Mounted on    /dev/sda3        18G  935M   16G   6% /    tmpfs           495M   12K  495M   1% /dev/shm    /dev/sda1       194M   27M  158M  15% /bootroot@salt-master /]# mysql -u salt -p -h 192.168.161.131Enter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 8Server version: 5.1.73 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type help; or \h for help. Type \c to clear the current input statement.mysql> use salt;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from salt_returns;+-----------+----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+| fun       | jid                  | return                                                                                                                                                                                   | id          | success | full_ret                                                                                                                                                                                                                                                                                                                                                                                   | alter_time          |+-----------+----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+| test.ping | 20160826200517605155 | true                                                                                                                                                                                     | salt-minion | 1       | {"fun_args": [], "jid": "20160826200517605155", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "salt-minion"}                                                                                                                                                                                                                                                    | 2016-08-26 20:05:17 || test.ping | 20160826202029989457 | true                                                                                                                                                                                     | salt-minion | 1       | {"fun_args": [], "jid": "20160826202029989457", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2016-08-26T12:20:30.138166", "fun": "test.ping", "id": "salt-minion"}                                                                                                                                                                                          | 2016-08-26 20:20:30 || cmd.run   | 20160826202045948708 | "Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda3        18G  935M   16G   6% /\ntmpfs           495M   12K  495M   1% /dev/shm\n/dev/sda1       194M   27M  158M  15% /boot" | salt-minion | 1       | {"fun_args": ["df -h"], "jid": "20160826202045948708", "return": "Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda3        18G  935M   16G   6% /\ntmpfs           495M   12K  495M   1% /dev/shm\n/dev/sda1       194M   27M  158M  15% /boot", "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2016-08-26T12:20:45.984974", "fun": "cmd.run", "id": "salt-minion"} | 2016-08-26 20:20:46 |+-----------+----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+3 rows in set (0.00 sec)

 

saltstack 把数据返回到mysql服务器