通过盐堆和信标进行服务监控

通过盐堆和信标进行服务监控

我尝试通过 salt react 和 beacon 来监控服务。如果发生事件(例如:nginx 服务在某个 minion 上停止),则需要将其写入 salt master 中的文件。

我不知道如何调用事件数据并将其传递给write_file.sls。有人能帮助我吗?

root@SALTMASTERLOCAL:/var/log/salt# salt-run reactor.list
event:
    ----------
suffix:
    salt/reactors/manage/list
|_
  ----------
  salt/beacon/*/service/:
      - /srv/salt/monitoring/9spokesBeaconN.sls

文件/srv/salt/monitoring/9spokesBeaconN.sls

load_service_monitoring:
  local.state.apply:
    - tgt: '*MASTER*'
    - arg:
      - monitoring.write_file
    - kwarg:
        new_minion: {{ data }}

文件monitoring/write_file.sls

{% from "monitoring/9spokesBeaconN.sls" import new_minion %}

create /root/beacons_load.confvg file:
  file.append:
    - name: /root/beacons_load.confvg 
    - text: {{ new_minion }}

结果/输出:

Event fired at Sun Dec  4 20:27:02 2016
*************************
Tag: salt/beacon/minion1/service/
Data:
{'_stamp': '2016-12-04T20:27:02.762722',
 'data': {'id': 'minion1',
          'nginx': {'running': False, 'uncleanshutdown': False}},
 'tag': 'salt/beacon/minion1/service/'}
Event fired at Sun Dec  4 20:27:02 2016
*************************
Tag: 20161204202702853337
Data:
{'_stamp': '2016-12-04T20:27:02.853564', 'minions': ['SALTMASTERLOCAL']}
Event fired at Sun Dec  4 20:27:02 2016
*************************
Tag: salt/job/20161204202702853337/new
Data:
{'_stamp': '2016-12-04T20:27:02.853982',
 'arg': ['monitoring.write_file',
         {'__kwarg__': True,
          'new_minion': {'_stamp': '2016-12-04T20:27:02.762722',
                         'data': {'id': 'minion1',
                                  'nginx': {'running': False,
                                            'uncleanshutdown': False}},
                         'tag': 'salt/beacon/minion1/service/'}}],
 'fun': 'state.apply',
 'jid': '20161204202702853337',
 'minions': ['SALTMASTERLOCAL'],
 'tgt': '*MASTER*',
 'tgt_type': 'glob',
 'user': 'root'}
Event fired at Sun Dec  4 20:27:04 2016
*************************
Tag: salt/job/20161204202702853337/ret/SALTMASTERLOCAL
Data:
{'_stamp': '2016-12-04T20:27:04.120195',
 'cmd': '_return',
 'fun': 'state.apply',
 'fun_args': ['monitoring.write_file',
              {'new_minion': {'_stamp': '2016-12-04T20:27:02.762722',
                              'data': {'id': 'minion1',
                                       'nginx': {'running': False,
                                                 'uncleanshutdown': False}},
                              'tag': 'salt/beacon/minion1/service/'}}],
 'id': 'SALTMASTERLOCAL',
 'jid': '20161204202702853337',
 'out': 'highstate',
 'retcode': 1,
 'return': ["Rendering SLS 'base:monitoring.write_file' failed: Jinja variable 'data' is undefined\n/var/cache/salt/minion/files/base/monitoring/9spokesBeaconN.sls(7):\n---\n[...]\n  local.state.apply:\n    - tgt: '*MASTER*'\n    - arg:\n      - monitoring.write_file\n    - kwarg:\n        new_minion: {{ data }}    <======================\n---"],
 'success': True}

日志:

2016-12-04 20:27:04,114 [salt.state       ][CRITICAL][7773] Rendering SLS 'base:monitoring.write_file' failed: Jinja variable 'data' is undefined
/var/cache/salt/minion/files/base/monitoring/9spokesBeaconN.sls(7):
---
[...]
  local.state.apply:
    - tgt: '*MASTER*'
    - arg:
      - monitoring.write_file
    - kwarg:
        new_minion: {{ data }}    <======================
---

相关内容