码迷,mamicode.com
首页 > 数据库 > 详细

saltstack mysql returner

时间:2014-09-23 20:52:26      阅读:529      评论:0      收藏:0      [点我收藏+]

标签:salt 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

saltstack mysql returner

标签:salt mysql returner

原文地址:http://jasonyu37.blog.51cto.com/8877469/1557447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!