saltstack return数据实时返回收集

   这段时间一直再研究saltstack,折腾了两天终于把return数据返回数据库搞定了,这里写个博客备忘,前面的几个后面慢慢补充上来:

    returners模块确实强大,他是把客户端执行命令返回的值可以直接返回到文件,或者保存到数据库里,这样我们做运维平台就可以有思路了,可以通过返回的数据库,平台的日志,或者通过返回的结果进行实时监控,把数据返回页面前端,好了开始。

步骤

1、mkdir /srv/salt/_returners  #建立目录     

2、cd /srv/salt/_returners

3、新建模块:

[root@mail ~]# cat /srv/salt/_returners/mysql_return.py
#!/usr/bin/python
import json
import MySQLdb
def __virtual__():
    return 'mysql_return'
def returner(ret):
    conn=MySQLdb.connect(host='192.168.10.205',user='salt',passwd='salt',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()

4、推送模块:

root@salt-master:~# salt '*' saltutil.sync_returners

5、建立数据库:(主要数据库的字段格式是固定的,跟官方一样)

CREATE DATABASE `salt`

DEFAULT CHARACTER SET utf8

DEFAULT COLLATE utf8_general_ci;

建表

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;

5、给数据库授权:

GRANT ALL PRIVILEGES ON salt.* TO 'salt'@'%' IDENTIFIED BY 'salt';

6、执行命令:

[root@mail ~]# salt 'mail.shihuasuan.com' cmd.run 'df -h ' --return mysql_return
mail.shihuasuan.com:
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                           50G  9.3G   38G  20% /
    tmpfs                  32G     0   32G   0% /dev/shm
    /dev/sda3             477M   73M  379M  17% /boot
    /dev/mapper/VolGroup-lv_home
                          196G  3.6G  183G   2% /home

7、查看数据库返回的结果

mysql> select * from salt_returns;
+
| fun     | jid                  | return                                                                                                                                                                                                                                                                                                                  | id                  | success | full_ret                                                                                  
| cmd.run | 20150205215339022527 | Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       50G  9.3G   38G  20% /
tmpfs                  32G     0   32G   0% /dev/shm
/dev/sda3             477M   73M  379M  17% /boot
/dev/mapper/VolGroup-lv_home
                      196G  3.6G  183G   2% /home | mail.shihuasuan.com | True    | {"fun_args": ["df -h"], "jid": "20150205215339022527", "return": "Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       50G  9.3G   38G  20% /
tmpfs                  32G     0   32G   0% /dev/shm
/dev/sda3             477M   73M  379M  17% /boot
/dev/mapper/VolGroup-lv_home
                      196G  3.6G  183G   2% /home", "retcode": 0, "success": true, "fun": "cmd.run", "id": "mail.shihuasuan.com"} |



------------------------------------------------------------------------------------+

好了这个到此结束,研究了一晚好累,休息一下。

你可能感兴趣的:(数据库,return,客户端)