salt初窥——mine介绍

参考文档
http://docs.saltstack.com/topics/mine/index.html
http://docs.saltstack.com/ref/modules/all/salt.modules.mine.html
http://www.shencan.net/index.php/2013/07/06/saltstack%E4%BA%94-mine/

介绍

The Salt Mine is used to bridge the gap between setting static variables and gathering live data.

就是在minion上定义一些模块(自带的自定义的模块都行), 按照一定时间间隔采集数据.

示例

minion:

编辑/etc/salt/minion,添加如下内容:

mine_functions:
disk.usage: []
pillar.data: []
network.ip_addrs:

- eth0

network.interfaces: []
cmd.run:

- date

mine_interval: 1 #时间间隔,单位是分钟

service salt-minion restart
master:

# salt ‘web5-88’ mine.get ‘*’ pillar.data

web5-88:

web5-88:
    ----------
    apache:
        httpd

salt ‘web5-88’ mine.get ‘*’ cmd.run

web5-88:

web5-88:
    Wed Oct 16 16:58:58 CST 2013

salt ‘web5-88’ mine.get ‘*’ network.ip_addrs

web5-88:

web5-88:
    - 10.0.5.88</pre>

实战

需求:10分钟收集一次/u01分区的使用率

minion:

修改/usr/lib/python2.6/site-packages/salt/modules/disk.py,在末尾添加如下内容:

def percent(args=None):
‘’’
Return usage information for volumes mounted on this minion

CLI Example::

    salt '*' disk.percent /var
'''
if __grains__['kernel'] == 'Linux':
    cmd = 'df -P'
elif __grains__['kernel'] == 'OpenBSD':
    cmd = 'df -kP'
else:
    cmd = 'df'
ret = {}
out = __salt__['cmd.run'](cmd).splitlines()
for line in out:
    if not line:
        continue
    if line.startswith('Filesystem'):
        continue
    comps = line.split()
    while not comps[1].isdigit():
        comps[0] = '{0} {1}'.format(comps[0], comps[1])
        comps.pop(1)
    try:
        if __grains__['kernel'] == 'Darwin':
            ret[comps[8]] = comps[4]
        else:
            ret[comps[5]] = comps[4]
    except IndexError:
        log.warn("Problem parsing disk usage information")
        ret = {}
if args:
    return ret[args]
else:
    return ret</pre>

修改etc/salt/minion,添加如下内容:

mine_functions:
disk.percent:

- /u01

mine_interval: 10

service salt-minion restart
master:

# salt ‘web5-88’ mine.get ‘*’ disk.percent 

web5-88:

web5-88:
    27%</pre>