saltstack returners 结果转存
returners 是saltstack对minion执行操作后,对返回的数据进行存储,可以存储到一个文件或者数据库当中。
支持的returners
http://docs.saltstack.cn/zh_CN/latest/ref/returners/all/index.html
官方例子:
https://github.com/saltstack/salt/tree/develop/salt/returners
参考:http://lixcto.blog.51cto.com/4834175/1430638
http://blog.cunss.com/?p=282
我们先来试下返回到本地(返回给master屏幕)
流程:
- 1.把自定义的returner传到minion端
- 2.master上执行操作,指定retuner的具体对象
- 3.minion执行命令,命令执行结果为ret表示,
- 4.这个结果一方面会按照通常的方式返回给master并在master的屏幕上面输出打印。
- 5.另一方面,ret这个结果会传给我们自定义的returner,然后在minion端执行returner,
- 6.执行returner产生的结果就是把最终的结果导入文件或者数据库
returner到minion控制台
[root@localhost ~]# cd /etc/salt/
[root@localhost salt]# mkdir _returners
[root@localhost salt]# cd _returners/
[root@localhost _returners]# cat local.py
在控制台输入执行的结果
from __future__ import absolute_import, print_function
def returner(ret):
'''
Print the return data to the terminal to verify functionality
'''
print(ret)
[root@localhost _returners]#
[root@localhost _returners]# salt '*' saltutil.sync_returners 状returner同步到minion端
[root@localhost salt]# salt-minion -l debug 在minion上启动debug模式
[root@localhost _returners]# salt '*' test.ping --return local 执行一条命令return到local的控制台
client1.roddypy.com:
True
client1.roddypy.com:
True
[root@localhost _returners]#
在minion的debug控制台中就有结果了
{'fun_args': [], 'jid': '20180306170253692413', 'return': True, 'retcode': 0, 'success': True, 'fun': 'test.ping', 'id': 'node76'}
returner到minion本地文件中
[root@localhost _returners]# vim local_file.py 自定义和脚本,其实就是把ret结果保留
import json
import time
def returner(ret):
now = time.localtime()
now = time.strftime("%Y-%m-%d %H:%M:%S",now)
result_file = '/tmp/returners_roddpy.log'
result = file(result_file,'a+')
result.write('At'+str(json.dumps(now))+'\n')
result.write(str(json.dumps(ret))+'\n')
result.close()
[root@localhost _returners]#
[root@localhost _returners]# salt '*' saltutil.sync_returners 分发到minion中
client1.roddypy.com:
- returners.local_file
[root@localhost _returners]#
[root@localhost _returners]# salt '*' cmd.run 'df -h ' --return local_file
client1.roddypy.com:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 1.4G 16G 9% /
tmpfs 495M 12K 495M 1% /dev/shm
/dev/sda1 485M 34M 426M 8% /boot
[root@localhost _returners]#
- 查看log
[root@localhost tmp]# cat returners_roddpy.log 嘿嘿结果已经保存到minion中的/tmp/目录下了
[DEBUG ] Re-using SAuth for ('/etc/salt/pki/minion', 'node76', 'tcp://192.168.104.76:4506')
[INFO ] User root Executing command cmd.run with jid 20180306184341492564
[DEBUG ] Command details {'tgt_type': 'glob', 'jid': '20180306184341492564', 'tgt': '*', 'ret': '', 'user': 'root', 'arg': ['uptime'], 'fun': 'cmd.run'}
[INFO ] Starting a new job with PID 23218
[DEBUG ] LazyLoaded cmd.run
[INFO ] Executing command 'uptime' in directory '/root'
[DEBUG ] output: 18:43:41 up 162 days, 7:48, 4 users, load average: 0.15, 0.10, 0.12
[DEBUG ] Minion return retry timer set to 8 seconds (randomized)
[INFO ] Returning information for job: 20180306184341492564
[DEBUG ] Re-using SAuth for ('/etc/salt/pki/minion', 'node76', 'tcp://192.168.104.76:4506')
returner到redis
- 1.安装redis server
[root@localhost _returners]# yum install redis -y
[root@localhost _returners]# vim /etc/redis.conf
# If you want you can bind a single interface, if the bind option is not
bind 0.0.0.0
[root@localhost _returners]#
[root@localhost _returners]# /etc/init.d/redis restart
停止 redis-server: [确定]
启动 : [确定]
[root@localhost _returners]#
检查启动端口:
[root@localhost _returners]# netstat -nlpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 12166/mysqld
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 14119/redis-server #已经启动
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1425/sshd
tcp 0 0 192.168.140.129:4505 0.0.0.0:* LISTEN 10378/python2.6
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1504/master
tcp 0 0 192.168.140.129:4506 0.0.0.0:* LISTEN 10400/python2.6
tcp 0 0 :::22 :::* LISTEN 1425/sshd
tcp 0 0 ::1:25
- 2.minion安装redis client
[root@localhost _returners]# salt '*' cmd.run_all 'yum install python-pip -y'
[root@localhost _returners]# salt '*' cmd.run_all 'pip install redis'
在minion上修改配置文件指定redis信息
[root@localhost tmp]# vim /etc/salt/minion
#return: mysql
redis.db: '0'
redis.host: 192.168.104.76
redis.port: 6379
重新启动minion
[root@localhost tmp]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [确定]
Starting salt-minion daemon: [确定]
[root@localhost tmp]#
在master上执行一条命令
[root@localhost _returners]# salt '*' cmd.run 'hostname' --return redis
client1.roddypy.com:
localhost.localdomain
[root@localhost _returners]#
在master上另起一个终端看看 redis的实时信息
[root@localhost ~]# redis-cli monitor
OK
1449284517.054169 "monitor"
1449284519.768636 "SELECT" "0"
1449284519.768868 "SET" "client1.roddypy.com:20181205110159739907" "{\"fun_args\": [\"hostname\"], \"jid\": \"20181205110159739907\", \"return\": \"localhost.localdomain\", \"retcode\": 0, \"success\": true, \"fun\": \"cmd.run\", \"id\": \"client1.roddypy.com\"}"
1449284519.769302 "LPUSH" "client1.roddypy.com:cmd.run" "20181205110159739907"
1449284519.769703 "SADD" "minions" "client1.roddypy.com"
1449284519.769963 "SADD" "jids" "20181205110159739907"
在master中查看redis keys
[root@localhost _returners]# redis-cli
redis 127.0.0.1:6379> ?
redis-cli 2.4.10
Type: "help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
redis 127.0.0.1:6379> get ?
(nil)
redis 127.0.0.1:6379> get "client1.roddypy.com:20181205110159739907"
"{\"fun_args\": [\"hostname\"], \"jid\": \"20181205110159739907\", \"return\": \"localhost.localdomain\", \"retcode\": 0, \"success\": true, \"fun\": \"cmd.run\", \"id\": \"client1.roddypy.com\"}"
redis 127.0.0.1:6379> keys * 查询所有的key
1) "client1.roddypy.com:20181205110159739907"
2) "client1.roddypy.com:20181205110142750466"
3) "minions"
4) "client1.roddypy.com:20181205110419271228"
5) "client1.roddypy.com:cmd.run"
6) "name"
7) "jids"
不错,,成功进入redis db里面
returner到mysql
[root@localhost salt]# yum install mysql mysql-server -y 安装mysql
已加载插件:fastestmirror, security
设置安装进程
Loading mirror speeds from cached hostfile
* base: centos.ustc.edu.cn
* epel: ftp.cuhk.edu.hk
* extras: mirrors.sina.cn
* updates: mirrors.sina.cn
* webtatic: sp.repo.webtatic.com
包 mysql-5.6.73-5.el7_6.x86_64 已安装并且是最新版本
包 mysql-server-5.6.73-5.el7_6.x86_64 已安装并且是最新版本
无须任何处理
[root@localhost salt]# /etc/init.d/mysqld start
正在启动 mysqld: [确定]
[root@localhost salt]# /etc/init.d/mysqld status
mysqld (pid 12166) 正在运行...
[root@localhost salt]#
[root@localhost _returners]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.73 Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
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 names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
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(255) NOT NULL,
`load` mediumtext NOT NULL,
UNIQUE KEY `jid` (`jid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- 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,
KEY `id` (`id`),
KEY `jid` (`jid`),
KEY `fun` (`fun`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql> grant all on salt.* to 'salt'@'%' identified by 'salt';
Query OK, 0 rows affected (0.03 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
参考官方github:https://github.com/saltstack/salt/blob/develop/salt/returners/mysql.py
[root@localhost _returners]# vim mysql.py 编辑returners mysql程序
from contextlib import contextmanager
import sys
import json
import logging
try:
import MySQLdb
HAS_MYSQL = True
except ImportError:
HAS_MYSQL = False
log = logging.getLogger(__name__)
def __virtual__():
if not HAS_MYSQL:
return False
return 'mysql'
def _get_options():
'''
Returns options used for the MySQL connection.
'''
defaults = {'host': '192.168.140.129',
'user': 'salt',
'pass': 'salt',
'db': 'salt',
'port': 3306}
_options = {}
for attr in defaults:
_attr = __salt__['config.option']('mysql.{0}'.format(attr))
if not _attr:
log.debug('Using default for MySQL {0}'.format(attr))
_options[attr] = defaults[attr]
continue
_options[attr] = _attr
return _options
@contextmanager
def _get_serv(commit=False):
'''
Return a mysql cursor
'''
_options = _get_options()
conn = MySQLdb.connect(host=_options['host'], user=_options['user'], passwd=_options['pass'], db=_options['db'], port=_options['port'])
cursor = conn.cursor()
try:
yield cursor
except MySQLdb.DatabaseError as err:
error, = err.args
sys.stderr.write(error.message)
cursor.execute("ROLLBACK")
raise err
else:
if commit:
cursor.execute("COMMIT")
else:
cursor.execute("ROLLBACK")
finally:
conn.close()
def returner(ret):
'''
Return data to a mysql server
'''
with _get_serv(commit=True) as cur:
sql = '''INSERT INTO `salt_returns`
(`fun`, `jid`, `return`, `id`, `success`, `full_ret` )
VALUES (%s, %s, %s, %s, %s, %s)'''
cur.execute(sql, (ret['fun'], ret['jid'],
str(ret['return']), ret['id'],
ret['success'], json.dumps(ret)))
[root@localhost _returners]#
- 操作
[root@localhost _returners]# salt '*' saltutil.sync_returners 同步到minion
[root@localhost _returners]# salt '*' cmd.run_all 'yum install MySQL-python* -y' 客户端需要安装Mysqldb模块
[root@localhost _returners]# salt '*' cmd.run 'uptime' --return mysql 执行一条命令
client1.roddypy.com:
19:20:26 up 1 day, 1:35, 2 users, load average: 0.07, 0.02, 0.00
- 检查return过来的数据
[root@localhost _returners]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.73 Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
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 names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids |
| salt_returns |
+----------------+
2 rows in set (0.00 sec)
mysql> select * from salt_returns;
+---------+----------------------+----------------------------------------------------------------------+---------------------+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| fun | jid | return | id | success | full_ret |
+---------+----------------------+----------------------------------------------------------------------+---------------------+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| cmd.run | 20151206045940088527 | 19:20:26 up 1 day, 1:35, 2 users, load average: 0.07, 0.02, 0.00 | client1.roddypy.com | 1 | {"fun_args": ["uptime"], "jid": "20151206045940088527", "return": " 19:20:26 up 1 day, 1:35, 2 users, load average: 0.07, 0.02, 0.00", "retcode": 0, "success": true, "fun": "cmd.run", "id": "client1.roddypy.com"} |
+---------+----------------------+----------------------------------------------------------------------+---------------------+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
在高并发业务情况下,如果Minion非常多,minion同时写到数据的话,会造成mysql的压力
saltstack returners 结果转存的更多相关文章
- saltstack returners
大意就是将saltstack 执行的结果,不在终端显示,写到文件或者数据库,功能类似于ansible的callback 参考 https://github.com/saltstack/salt/tre ...
- 【SaltStack官方版】—— returners——返回器
ETURNERS 返回器 By default the return values of the commands sent to the Salt minions are returned to t ...
- 菜鸟玩云计算之二十:saltstack入门初步
菜鸟玩云计算之二十 SaltStack 入门初步 0. saltstack 是什么 参考下面的文章: http://docs.saltstack.com/en/latest/topics/tutori ...
- saltstack(七)返回值
一.自定义创建模块 在base目录下创建_modules目录,你自己编写的模块都可以存放在该目录下,当前目录结构下: 1 2 3 4 5 6 7 8 [root@localhost:]# tree - ...
- saltstack结合Elasticsearch来做salt运行结果展现
salt尽管好用可是机器管理的越来越多,通过cli的结果输出方式查看运行结果越来越多不能满足我的需求.并且作为一个推动运维自己主动化的攻城狮,使用这样的人眼查看运行结果的方式简直土到掉渣.尽管别人看起 ...
- 4、saltstack的使用
官方文档地址:http://repo.saltstack.com/#rhel 4.1.saltstatck介绍: 用户要一致,这里使用的是root用户: 用于批量管理成百上千的服务器: 并行的分发,使 ...
- 学习saltstack (六)
Slatstack 介绍 官网:https://saltstack.com/ 官方源:http://repo.saltstack.com/ (介绍各操作系统安装方法) centos 6源 ? 1 y ...
- Saltstack
一.Satlstack的概述 Saltstack是什么? Salt是一种和以往不同的基础设施管理方法,它是建立在大规模系统高速通讯能力可以大幅提升的想法上.这种方法使得Salt成为一个强大的能够解决基 ...
- Saltstack之SSH(十一)
Saltstack之SSH 安装 yum install -y salt-ssh 官方文档 https://docs.saltstack.com/en/latest/topics/ssh/index ...
随机推荐
- Eclipse创建Dynamic Web部署
Eclipse创建Dynamic Web部署 http://blog.csdn.net/sweblish/article/details/6686046 Eclipse3.x中热部署项目,启动错误问题 ...
- [转]ajQuery的deferred对象详解
来自:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html 作者: ...
- 安卓逆向学习---初始APK、Dalvik字节码以及Smali
参考链接:https://www.52pojie.cn/thread-395689-1-1.html res目录下资源文件在编译时会自动生成索引文件(R.java ), asset目录下的资源文件无需 ...
- win/mac平台搭建ionic开发环境教程(转)
出处:http://www.ionic-china.com/doc/ionic-winmac.html#preface 前言 ionic中文网为大家准备了绿色版的nodejs和androidSDK以及 ...
- Codeforces Round #543 (Div. 2) D 双指针 + 模拟
https://codeforces.com/contest/1121/problem/D 题意 给你一个m(<=5e5)个数的序列,选择删除某些数,使得剩下的数按每组k个数以此分成n组(n*k ...
- SpringBoot2.0.2 不使用parent作为maven单继承方式操作 : org.springframework.boot : spring-boot-dependencies : 2.0.2.RELEASE
1.pom配置方式 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- Eclipse错误: 找不到或无法加载主类或项目无法编译10种解决大法
1.在src文件夹上点右键-Build Path-Use as Source Folder,重新进行编译,一切正常了.2.在Eclipse工程文件夹上点右键-Refresh,重新编译,一功OK(这个方 ...
- python读取文件操作.CSV
#-*- encoding:utf-8 -*- import numpy as np import pandas as pd def test(): # header=0,表示文件第0行为列索引 # ...
- 手把手教Electron+vue的使用
.现如今前端框架数不胜数,尤其是angular.vue吸引一大批前端开发者,在这个高新技术快速崛起的时代,自然少不了各种框架的结合使用.接下来是介绍electron+vue的结合使用. 2.Elect ...
- python一键升级所有第三方库
import pip from subprocess import call for dist in pip.get_installed_distributions(): call("pip ...