saltstack mysql returner

https://github.com/saltstack/salt/blob/develop/salt/returners/mysql.py

http://docs.saltstack.com/en/latest/ref/returners/all/salt.returners.mysql.html#module-salt.returners.mysql


http://lixcto.blog.51cto.com/4834175/1430638

http://lihuipeng.blog.51cto.com/3064864/1403387


这个就是把saltstack的运行结果存入到mysql数据库中。

先决条件: master 和 minion上都要安装 yum install MySQL-python 


1,saltmaster (192.168.1.212)上安装 mysql,

mysql -uroot -p

use mysql

GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘192.168.1.215‘ IDENTIFIED BY ‘mysqlpw‘ WITH GRANT OPTION;

 FLUSH  PRIVILEGES;

// 允许root用户从minion (192.168.1.215)登陆,


//创建数据库 

 CREATE DATABASE  `salt`
      DEFAULT CHARACTER SET utf8
      DEFAULT COLLATE utf8_general_ci;
 
    USE `salt`;
     
    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;


2, 在saltmaster(192.168.1.212)上: cat /srv/salt/_returners/testmysql.py

# -*- coding: utf-8 -*-



import sys

import json

import time

import MySQLdb

import salt.returners


try:

    import MySQLdb

    HAS_MYSQL = True

except ImportError:

    HAS_MYSQL = False

 

__virtualname__ = ‘testmysql‘

 

def __virtual__():

   if not HAS_MYSQL:

       return False

   else:

       return  __virtualname__

def returner(ret):

       conn=MySQLdb.connect(host=‘192.168.1.212‘,user=‘root‘,passwd=‘mysqlpw‘,db=‘salt‘,

                            port=3306)

       cursor=conn.cursor()

       sql = ‘‘‘INSERT INTO `salt_returns`(`fun`,`jid`,`return`,`id`,`success`,`full_ret`)

             VALUES (%s,%s,%s,%s,%s,%s)‘‘‘

       cursor.execute(sql % (str(json.dumps(ret[‘fun‘])),str(json.dumps(ret[‘jid‘])),

                      str(json.dumps(ret[‘return‘])),str(json.dumps(ret[‘id‘])),

                      ‘"‘+str(ret[‘success‘])+‘"‘,"‘"+json.dumps(ret)+"‘"))

       conn.commit()

       cursor.close()

       conn.close()

//注意 virtualname


# salt ‘*‘ saltutil.sync_returners

# salt ‘centos5‘ cmd.run ‘df -H‘ --return testmysql



mysql> select * from salt_returns\G

*************************** 1. row ***************************

       fun: cmd.run

       jid: 20140923164910093656

    return: Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/vg_centos2-lv_root

                       19G  2.6G   16G  15% /

tmpfs                 981M     0  981M   0% /dev/shm

/dev/sda1             508M   79M  403M  17% /boot

        id: centos5

   success: True

  full_ret: {"fun_args": ["df -H"], "jid": "20140923164910093656", "return": "Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/vg_centos2-lv_root

                       19G  2.6G   16G  15% /

tmpfs                 981M     0  981M   0% /dev/shm

/dev/sda1             508M   79M  403M  17% /boot", "retcode": 0, "success": true, "fun": "cmd.run", "id": "centos5"}

alter_time: 2014-09-23 16:49:10

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。