实验目标

1.salt执行的状态然后结果写入MySQL可以方便查询执行salt执行的历史记录

实现方法

1.使用salt的return功能,是minion直接写入MySQL(相对比较麻烦)

2.使用master的jobcache写入到MySQL

环境

node1  192.168.56.11   角色 salt-master  salt-minon  DB

node2  192.168.56.12   角色  salt-minon

步骤

一,在master的配置文件添加以下内容
[root@linux-node1 salt]# tail - /etc/salt/master
#return: mysql
master_job_cache: mysql
mysql.host: '192.168.56.11'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port:
二,安装数据库,这里用MariaDB
[root@linux-node1 salt]# yum install -y mariadb mariadb-server
[root@linux-node1 salt]# systemctl start mariadb
[root@linux-node1 salt]#mysql_secure_installation #删除anonymous 删除test库 设置root密码 刷新权限表 关闭root远程登录
三,登陆到数据库创建salt数据和表
[root@linux-node1 salt]# mysql -uroot -p123456

#下面是创建数据库和表语句
CREATE DATABASE `salt`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci; USE `salt`; --
-- Table structure for table `jids`
-- DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
`jid` varchar() NOT NULL,
`load` mediumtext NOT 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() NOT NULL,
`jid` varchar() NOT NULL,
`return` mediumtext NOT NULL,
`id` varchar() NOT NULL,
`success` varchar() 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() NOT NULL,
`data` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`master_id` varchar() NOT NULL,
PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
四,授权用户

如果是授权的新用户就不需要刷新用户表

MariaDB [salt]> grant all on salt.* to salt@192.168.56.11 identified by 'salt';
Query OK, rows affected (0.03 sec)
五,测试用户是否能正常登陆
[root@linux-node1 salt]# mysql -h 192.168.56.11 -usalt -psalt
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is
Server version: 5.5.-MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
六,重启salt-master

因为修改了master的配置文件

[root@linux-node1 ~]# systemctl restart salt-master
七,先看下salt_returns表信息,是空的
MariaDB [(none)]> 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
MariaDB [salt]> select * from salt_returns\G;
Empty set (0.00 sec)
八,salt执行远程命令
[root@linux-node1 salt]# salt "*" test.ping
linux-node2.example.com:
True
linux-node1.example.com:
True [root@linux-node1 salt]# salt "*" cmd.run "w"
linux-node1.example.com:
:: up :, user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/ 192.168.56.1 : .00s .55s .37s /usr/bin/python /usr/bin/salt * cmd.run w
linux-node2.example.com:
:: up :, user, load average: 0.01, 0.02, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/ 192.168.56.1 : : .00s .00s -bash
九,再次查看salt库的salt_returns表信息,执行的数据已经记录到MariaDB中
MariaDB [salt]> use salt
Database changed
MariaDB [salt]> select * from salt_returns\G;
*************************** . row ***************************
fun: test.ping
jid:
return: true
id: linux-node2.example.com
success:
full_ret: {"fun_args": [], "jid": "", "return": true, "retcode": , "success": true, "cmd": "_return", "_stamp": "2017-10-22T20:23:59.736379", "fun": "test.ping", "id": "linux-node2.example.com"}
alter_time: -- ::
*************************** . row ***************************
fun: test.ping
jid:
return: true
id: linux-node1.example.com
success:
full_ret: {"fun_args": [], "jid": "", "return": true, "retcode": , "success": true, "cmd": "_return", "_stamp": "2017-10-22T20:23:59.757709", "fun": "test.ping", "id": "linux-node1.example.com"}
alter_time: -- ::
*************************** . row ***************************
fun: cmd.run
jid:
return: " 16:25:01 up 10:48, 1 user, load average: 0.00, 0.01, 0.05\nUSER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT\nroot pts/0 192.168.56.1 16:01 5.00s 0.55s 0.37s /usr/bin/python /usr/bin/salt * cmd.run w"
id: linux-node1.example.com
success:
full_ret: {"fun_args": ["w"], "jid": "", "return": " 16:25:01 up 10:48, 1 user, load average: 0.00, 0.01, 0.05\nUSER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT\nroot pts/0 192.168.56.1 16:01 5.00s 0.55s 0.37s /usr/bin/python /usr/bin/salt * cmd.run w", "retcode": , "success": true, "cmd": "_return", "_stamp": "2017-10-22T20:25:01.867563", "fun": "cmd.run", "id": "linux-node1.example.com"}
alter_time: -- ::
*************************** . row ***************************
fun: cmd.run
jid:
return: " 16:25:02 up 10:48, 1 user, load average: 0.01, 0.02, 0.05\nUSER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT\nroot pts/0 192.168.56.1 16:01 23:13 0.00s 0.00s -bash"
id: linux-node2.example.com
success:
full_ret: {"fun_args": ["w"], "jid": "", "return": " 16:25:02 up 10:48, 1 user, load average: 0.01, 0.02, 0.05\nUSER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT\nroot pts/0 192.168.56.1 16:01 23:13 0.00s 0.00s -bash", "retcode": , "success": true, "cmd": "_return", "_stamp": "2017-10-22T20:25:01.882218", "fun": "cmd.run", "id": "linux-node2.example.com"}
alter_time: -- ::
rows in set (0.01 sec)
十,在salt执行命令时可用加上-v 显示作业ID
[root@linux-node1 salt]#  salt '*' cmd.run 'w' -v
Executing job with jid
------------------------------------------- linux-node1.example.com:
:: up :, user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/ 192.168.56.1 : .00s .57s .35s /usr/bin/python /usr/bin/salt * cmd.run w -v
linux-node2.example.com:
:: up :, user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/ 192.168.56.1 : : .00s .00s -bash

总结

可以这样对SaltStack 做二次开发 :

1.Master Job cache将所有的job输出保存到MySQL

2.如果做管理平台,可以将User id和Jid做关联

3.使用List做目标选择

附:英文参考文档

https://www.unixhot.com/docs/saltstack/ref/returners/all/salt.returners.mysql.html#module-salt.returners.mysql

SaltStack执行状态收集入库-第五篇的更多相关文章

  1. 【Python五篇慢慢弹(4)】模块异常谈python

    模块异常谈python 作者:白宁超 2016年10月10日12:08:31 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondo ...

  2. 【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸

    类的继承案例解析,python相关知识延伸 作者:白宁超 2016年10月10日22:36:57 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给 ...

  3. 第十五篇 Integration Services:SSIS参数

    本篇文章是Integration Services系列的第十五篇,详细内容请参考原文. 简介在前一篇,我们使用SSDT-BI将第一个SSIS项目My_First_SSIS_Project升级/转换到S ...

  4. 【译】第十五篇 Integration Services:SSIS参数

    本篇文章是Integration Services系列的第十五篇,详细内容请参考原文. 简介在前一篇,我们使用SSDT-BI将第一个SSIS项目My_First_SSIS_Project升级/转换到S ...

  5. 【Python五篇慢慢弹】快速上手学python

    快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...

  6. 【Python五篇慢慢弹】数据结构看python

    数据结构看python 作者:白宁超 2016年10月9日14:04:47 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc ...

  7. 【Python五篇慢慢弹(3)】函数修行知python

    函数修行知python 作者:白宁超 2016年10月9日21:51:52 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc ...

  8. 解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译)

    解剖SQLSERVER 第十五篇  SQLSERVER存储过程的源文本存放在哪里?(译) http://improve.dk/where-does-sql-server-store-the-sourc ...

  9. 解剖SQLSERVER 第五篇 OrcaMDF里读取Bits类型数据(译)

    解剖SQLSERVER 第五篇  OrcaMDF里读取Bits类型数据(译) http://improve.dk/reading-bits-in-orcamdf/ Bits类型的存储跟SQLSERVE ...

随机推荐

  1. 使用Ansible自动配置JDK环境

    1.首先安装好Ansible环境,具体步骤请见Ansible安装 2.先创建hosts文件(为后面编写脚本安装JDK做铺垫) [root@localhost /]# vi hosts [jdktest ...

  2. http协议----->请求头和响应头

    http实用头字段-----Range 如果请求里有这个range头,那么响应里也有 1.首先在webroot下放好a.txt 内容如下: 2.然后在本地有个下载未完成的a.txt 本地a.txt内容 ...

  3. green rgb(255, 102, 0) #FF6600

    w通过元素背景色定位元素,改变其属性. style="background-color: #FF6600" <script> var w = document.quer ...

  4. 剑指Offer——二叉树的深度

    题目描述: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 分析: 二叉树的深度等于其左子树的深度和右子树的深度两个中最大的深 ...

  5. C++应该被看成是个语言集合——四种语言(C语言,OO语言,泛型语言,STL)

    至少有三种语言: 一,C++ is C 二,C++ is an OO language 三,C++ is a genetic programming language 有的童鞋觉得难,可能是没有看清楚 ...

  6. mac配置python自然语言处理环境

    一.nltk安装 Ⅰ.工具安装步骤 1.根据python版本从 https://pypi.python.org/pypi/setuptools 下载对应版本的setuptools.然后,在终端下运行, ...

  7. iOS版微信6.5.21发布 适配iPhone X

    昨日,iOS版微信迎来v6.5.21正式版发布,本次升级主要适配iPhone X,在聊天中查找聊天内容时,可以查找交易消息.可以给聊天中的消息设置日期提醒.上一个正式版v6.5.16发布于9月13日, ...

  8. Python 调用 Shell脚本的方法

    Python 调用 Shell脚本的方法 1.os模块的popen方法 通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出. > ...

  9. JPG PNG GIF 的优缺点

    pg/jpeg:优点:体积比png小,打开速度比png快缺点:属于有损压缩,每次保存都会产生数据损失(jpeg artifacts), 故不适合用于多次编辑保存.压缩率过高图像会失真PS:网络上最最常 ...

  10. 如何确定LDA的主题个数

    本文参考自:https://www.zhihu.com/question/32286630 LDA中topic个数的确定是一个困难的问题. 当各个topic之间的相似度的最小的时候,就可以算是找到了合 ...