saltstack把执行的结果保存到mysql中,以便进行命令安全审计

  mysql负责存储数据,mysql-python负责收集数据

  master需要安装mysql和MySQL-python,minion端安装MySQL-python

系统环境

 CentOS Linux release 7.4

   master    10.0.0.111

   minion    10.0.0.112

1、master 端操作:

参考网址:https://docs.saltstack.com/en/latest/ref/returners/all/salt.returners.mysql.html

参考博客:https://www.cnblogs.com/zzzhfo/p/5867771.html

  1.1、安装mysql

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-server MySQL-python #启动mysql
systemctl start mysqld.service
systemctl enable mysqld.service
systemctl status mysqld.service #修改mysql密码
[root@salt-server ~]# grep "password" /var/log/mysqld.log

set global validate_password_policy=0;
set global validate_password_length=1;
ALTER USER 'root'@'localhost' IDENTIFIED BY '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;
CREATE INDEX jid ON jids(jid) USING BTREE;
############################################################
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;
############################################################
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;
#############################################################
grant all on salt.* to salt@'%' identified by '123456'; #以下是上边操作的截图

  

  

  

 

  1.2、测试mysql是否设置完成

  1.3、修改master主配置文件

[root@salt-server ~]# vim /etc/salt/minion
mysql.host: '10.0.0.111'
mysql.user: 'salt'
mysql.pass: '123456'
mysql.db: 'salt'
mysql.port: 3306

#重启配置文件

systemctl restart salt-minion  

    

2、minion端修改内容如下:

yum -y install MySQL-python
[root@minion02 ~]# vim /etc/salt/minion
mysql.host: '10.0.0.111'
mysql.user: 'salt'
mysql.pass: '123456'
mysql.db: 'salt'
mysql.port: 3306 #重启minion服务
systemctl restart salt-minion

  

总测试:

[root@salt-server ~]# salt '*' test.ping --return mysql

[root@salt-server ~]# salt '*' cmd.run 'df -h' --return mysql


 在master的mysql服务查看:

[root@salt-server ~]# mysql -uroot -p
Enter password:
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> mysql> select * from salt_returns\G;

方法2

master端安装MySQL-python和mysql-server
minion端不需要安装MySQL-python包
操作步骤略(与发一相同)

[root@salt-master /]# vim /etc/salt/master   追加如下内容
master_job_cache: mysql #每次执行不加--return mysql由master端将返回的数据写入数据库 不需要minion 重启服务
[root@salt-master /]# /etc/init.d/salt-master restart 测试:
[root@salt-master /]# salt 'salt-minion' test.ping
salt-minion:
True
[root@salt-master /]# salt 'salt-minion' cmd.run 'df -h'
salt-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% /boot

root@salt-master /]# mysql -u salt -p -h 192.168.161.131
Enter password:
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 | 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)

  

 

最新文章

  1. 入园记------我的DBA之路
  2. [原创]在Docker上部署mongodb分片副本集群。
  3. cufflinks install
  4. CentOS6.5 python 2.6升级到2.7
  5. location.hash && location.href
  6. js 不可变的原始值和可变的对象引用
  7. C#(Net)软件开发常用工具汇总,提高你的开发效率
  8. IIC 概述之24c系列存储器内存分析
  9. 【转】FAE及其发展前景
  10. 【C语言的日常实践(十四)】constkeyword详细解释
  11. webapi中的自定义路由约束
  12. ubuntu上安装nginx+mysql+php5-fpm(PHP5 - FastCGI Process Manager)
  13. Python:笔记1_字符串处理【转载】
  14. angular2 学习笔记 (Typescript - Attribute & reflection & decorator)
  15. Entity Framework 同一个上下文中,如何进行对同一个实体进行指定字段更新
  16. FastAdmin 开发环境详细设置
  17. 再学Java 之 private、protected、public和default的作用域
  18. bzoj 1212: [HNOI2004]L语言
  19. 获取作为 URL 部署清单的位置。
  20. 学习笔记之SQL / MySQL

热门文章

  1. Python3中使用urllib的方法详解(header,代理,超时,认证,异常处理)
  2. Android Studio 打包生成 APK
  3. 【转】CSS浮动(float,clear)通俗讲解
  4. Xshell基础入门
  5. 添加右键使用 SublimeText 打开
  6. winform绑定多张图片
  7. IDEA下的第一个springBoot
  8. h5笔记
  9. CentOS7系列--1.5CentOS7配置vim
  10. SwipeRefreshLayout的高度测量