Saltstack Monit Map Services

Install Monit configuration file for each service that should be monitored based on mapping file and minion role(s)


Sample `role_services.yaml` mapping file

default:
  - netdata
  - salt-minion
by_role:
  admin:
    - proxysql
  user:
    - proxysql
  guest:
    - haproxy

map.jinja

{% import_yaml "monit/role_services.yaml" as monitors %}
{#
    Default set of Monit config files to install.  Includes any files named
    after the minion's role(s)
#}
{% set services = salt['pillar.get']('role') | union(monitors.default) %}
{% set monit_config = { 'monitors': services } %}

{#
    Generate a full list of Monit config files provided by the role to service map YAML file
#}
{% for role in salt['pillar.get']('role') %}
{% if role in monitors.by_role %}
{% set monitors = monit_config.monitors | union(monitors.by_role[role]) %}
{% if monit_config.update({'monitors': monitors}) %}{% endif %}
{% endif %}
{% endfor %}

{#
    Filter out any 'services' that are not monitorable based on the existence
    of a Monit config file of the same name
#}
{% set services = salt['cp.list_master'](prefix='monit/files/services') | map('method_call', 'split', '/', -1) | map('method_call', 'pop', -1) | intersect(monit_config.monitors) %}
{% if monit_config.update({'monitors': services}) %}{% endif %}

services.sls

{% from "monit/map.jinja" import monit_config with context %}

{% for service in monit_config.monitors %}
monit {{ service }}:
  file.managed:
    - name: /etc/monit/conf-enabled/{{ service }}
    - source: salt://{{ slspath }}/files/services/{{ service }}
    - require:
      - pkg: monit
  cmd.run:
    - name: service monit restart
    - onchanges:
      - file: /etc/monit/conf-enabled/{{ service }}
    - require:
      - pkg: monit
{% endfor %}