saltstack返回信息写入数据库

返回
将返回结果写入到mysql中

master端需要安装MySQL-python和mysql-server
mysql-server用来存储minion数据,MySQL-python用来收集数据
master端:
1.安装mysql-server和MySQL-python

yum install MySQL-python mysql-server

2.创建表结构

service mysqld start

CREATE DATABASE `salt`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;

如下:
创建salt,在salt下添加jids和salt_returns

mysql> CREATE DATABASE `salt`
    -> DEFAULT CHARACTER SET utf8
    -> DEFAULT COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> 

3,在USE salt下创建js表

USE salt;

CREATE TABLE jids (
jid varchar(255) NOT NULL,
load mediumtext NOT NULL,
UNIQUE KEY jid (jid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

如下:

mysql> use salt;
Database changed
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.03 sec)

mysql> 

在USE salt下创建salt_returns表salt_events
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.02 sec)

创建salt_events

mysql> CREATE TABLE `salt_events` (
    -> `id` BIGINT NOT NULL AUTO_INCREMENT,
    -> `tag` varchar(255) NOT NULL,
    -> `data` varchar(1024) NOT NULL,
    -> `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    -> PRIMARY KEY (`id`),
    -> KEY `tag` (`tag`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.03 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

授权

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

测试,这里没有授权localhost,使用-h主机ip登录测试,查看

[root@master ~]# mysql -u salt -p -h 10.0.0.7
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| salt               |
| test               |
+--------------------+
3 rows in set (0.00 sec)

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_events    |
| salt_returns   |
+----------------+
3 rows in set (0.00 sec)

minion端:

yum -y install MySQL-python

修改minion配置文件

[root@master ~]# vim /etc/salt/minion
mysql.host: '10.0.0.7'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
[root@master ~]# /etc/init.d/salt-minion restart

测试:
在master执行:

salt '*' test.ping --return mysql

查看数据库是否写入值
如下所示:

mysql> select * from salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid: 20151212003833932291
    return: true
        id: minion-node11.10.0.0.11.com
   success: 1
  full_ret: {"fun_args": [], "jid": "20151212003833932291", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "minion-node11.10.0.0.11.com"}
alter_time: 2015-12-12 00:38:34
*************************** 2. row ***************************
       fun: test.ping
       jid: 20151212004045681725
    return: true
        id: minion-node20.10.0.0.20.com
   success: 1
  full_ret: {"fun_args": [], "jid": "20151212004045681725", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "minion-node20.10.0.0.20.com"}
alter_time: 2015-12-12 00:40:45
*************************** 3. row ***************************
       fun: test.ping
       jid: 20151212004045681725
    return: true
        id: minios.10.0.0.8.com

2 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2015-12-13分类: saltstack

标签: saltstack

发表评论