所有的信息都是客户端minion直接写入服务端的mysql数据库的
在服务端和客户端都要安装MySQL-python包,
yum install MySQL-python -y
https://docs.saltstack.com/en/latest/ref/returners/all/salt.returners.mysql.html#module-salt.returners.mysql
++++++++++++++++++++++++++++++++++++++++++++++++++++
Return data to a mysql server
maintainer: DaveBoucha <dave@saltstack.com>, Seth House <shouse@saltstack.com>
maturity: mature
depends: python-mysqldb
platform: all
在服务端的master和客户端的minion最后一行加入以下语句:
vim /etc/salt/master
mysql.host: '172.16.1.110'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
[root@node110 salt]# /etc/init.d/salt-master restart
Stopping salt-master daemon: [ OK ]
Starting salt-master daemon: [ OK ]
在数据库中创建表结构;
Use the following mysql database schema:
CREATE DATABASE `salt`
DEFAULT CHARACTERSET utf8
DEFAULT COLLATEutf8_general_ci;
USE `salt`;
--
-- Table structure for table `jids`
--
DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
`jid`varchar(255) NOT NULL,
`load` mediumtextNOT 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@'%' identifiedby 'salt';
Query OK, 0 rows affected (0.09 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.09 sec)
使用方法一:
[root@node110 ~]# salt '*' cmd.run 'df -h ' --returnmysql #以下返回的数值会存入数据库中
minion.node2.saltstack.com:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_node1-lv_root
50G 9.0G 38G 20% /
tmpfs 244M 12K 244M 1% /dev/shm
/dev/sda1 477M 36M 416M 8% /boot
/dev/mapper/vg_node1-lv_home
47G 52M 45G 1% /home
minion.saltstack.com:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_node1-lv_root
50G 9.2G 38G 20% /
tmpfs 244M 12K 244M 1% /dev/shm
/dev/sda1 477M 36M 416M 8% /boot
/dev/mapper/vg_node1-lv_home
47G 52M 45G 1% /home
数据库内容如下:
|2016-12-13 10:34:14 |
| cmd.run |20161213103710517066 | "Filesystem Size Used Avail Use% Mountedon\n/dev/mapper/vg_node1-lv_root\n 50G 9.2G 38G 20% /\ntmpfs 244M 12K 244M 1% /dev/shm\n/dev/sda1 477M 36M 416M 8%/boot\n/dev/mapper/vg_node1-lv_home\n 47G 52M 45G 1% /home" | minion.saltstack.com | 1 | {"fun_args": ["df-h"], "jid": "20161213103710517066","return": "Filesystem Size Used Avail Use% Mountedon\n/dev/mapper/vg_node1-lv_root\n 50G 9.2G 38G 20% /\ntmpfs 244M 12K 244M 1% /dev/shm\n/dev/sda1 477M 36M 416M 8%/boot\n/dev/mapper/vg_node1-lv_home\n 47G 52M 45G 1% /home","retcode": 0, "success": true, "fun":"cmd.run", "id": "minion.saltstack.com"} |2016-12-13 10:37:11 |
方法二:master端接收minion的数据,master端再将数据写入数据库,不是minion端直接写入数据库
在master最后一行添加以下语句:
mysql.host: '172.16.1.110'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
master_job_cache: mysql #不需要在客户端安装MySQL-python包
现在可以直接执行语句,不要添加 --return mysql,也不需要在客户端安装MySQL-python包,所有的语句的返回结果将直接返回到数据库中
[root@node110 ~]# salt '*' cmd.run 'df -h ' #以下返回的数值会存入数据库中