Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration readability improvement, fix Vagrantfile and fix service watch #7

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ test -d /srv/pillar || mkdir /srv/pillar
test -f /tmp/travis/top.sls && cp /{tmp/travis,srv/salt}/top.sls
cp {/tmp/travis,/srv/salt}/top.sls
cp /tmp/travis/top_pillar.sls /srv/pillar/top.sls
cp /srv/{salt/pillar.example,pillar/monit.sls}
ln -s /srv/salt/pillar.example /srv/pillar/monit.sls
SCRIPT

Vagrant.configure(2) do |config|
config.vm.define "xenial" do |xenial|
xenial.vm.box = "xenial"
config.vm.provider 'virtualbox' do |v|
v.linked_clone = true if Vagrant::VERSION =~ /^1.8/
end

config.vm.define "monit" do |monit|
monit.vm.box = "ubuntu/xenial64"

xenial.vm.synced_folder ".", "/vagrant", disabled: true
xenial.vm.synced_folder ".", "/srv/salt"
xenial.vm.synced_folder ".travis", "/tmp/travis"
monit.vm.synced_folder ".", "/vagrant", disabled: true
monit.vm.synced_folder ".", "/srv/salt"
monit.vm.synced_folder ".travis", "/tmp/travis"

xenial.vm.provision "shell", inline: $script
xenial.vm.provision :salt do |salt|
salt.bootstrap_script = "../salt-bootstrap/bootstrap-salt.sh"
monit.vm.provision "shell", inline: $script
monit.vm.provision :salt do |salt|
salt.masterless = true
salt.minion_config = ".travis/minion"
salt.run_highstate = false
Expand Down
20 changes: 13 additions & 7 deletions monit/config.sls
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
- template: jinja
- makedirs: True
- mode: '0700'
- context:
config_includes: {{ monit.config_includes }}
http_access: {{ monit.http_access }}
{%- if monit.service.status == 'running' %}
- watch_in:
- service: {{ monit.service.name }}
{%- endif %}

{#- This is the mail alert configuration #}
{% if monit.mail_alert is defined %}
Expand All @@ -18,8 +19,10 @@
- source: salt://monit/files/mail
- template: jinja
- makedirs: True
- context:
mail_alert: {{ monit.mail_alert }}
{%- if monit.service.status == 'running' %}
- watch_in:
- service: {{ monit.service.name }}
{%- endif %}
{% endif %}

{#- This is populated by modules configuration
Expand All @@ -28,5 +31,8 @@
file.managed:
- source: salt://monit/files/modules
- template: jinja
- context:
modules: {{ monit.modules }}
- makedirs: True
{%- if monit.service.status == 'running' %}
- watch_in:
- service: {{ monit.service.name }}
{%- endif %}
4 changes: 4 additions & 0 deletions monit/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ monit:
config_includes: '/etc/monit/conf.d'
service:
name: monit
enable: True
status: running
daemon_interval: 10
logfile: syslog
http_access:
port: 2812
bind: 127.0.0.1
Expand Down
17 changes: 9 additions & 8 deletions monit/files/mail
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{%- for email in mail_alert.users -%}
set alert {{email}} ON { invalid, timeout, resource, size, timestamp }
{%- from "monit/map.jinja" import monit with context -%}
{%- for email in monit.mail_alert.users -%}
set alert {{ email }} ON { invalid, timeout, resource, size, timestamp }
{% endfor %}

{%- if mail_alert.account.email -%}
set mail-format { from: {{mail_alert.account.email}} }
{%- if monit.mail_alert.account.email -%}
set mail-format { from: {{ monit.mail_alert.account.email }} }
{%- endif -%}

{%- if mail_alert.account.server and mail_alert.account.port %}
set mailserver {{mail_alert.account.server}} port {{mail_alert.account.port}}
{%- if mail_alert.account.email and 'password' in mail_alert.account and mail_alert.account.password %}
username "{{mail_alert.account.email}}" password "{{mail_alert.account.password}}"
{%- if monit.mail_alert.account.server and monit.mail_alert.account.port %}
set mailserver {{ monit.mail_alert.account.server }} port {{ monit.mail_alert.account.port }}
{%- if monit.mail_alert.account.email and 'password' in monit.mail_alert.account and monit.mail_alert.account.password %}
username "{{ monit.mail_alert.account.email }}" password "{{ monit.mail_alert.account.password }}"
using tlsv1
{%- endif %}
with timeout 30 seconds
Expand Down
19 changes: 12 additions & 7 deletions monit/files/modules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{%- from "monit/map.jinja" import monit with context -%}
# -*- coding: utf-8 -*-
# vim: ft=jinja

Expand All @@ -22,7 +23,7 @@
-#}
{#- module = nginx_init,
module_v = {'process': {'with': {'pidfile':... #}
{%- for module, module_v in modules.iteritems() -%}
{%- for module, module_v in monit.modules.iteritems() -%}

{#- mod_name = process
mod_name_v = {'custom': {'name': 'nginx'}, 'with': {'pidfile':... #}
Expand All @@ -46,10 +47,10 @@

{#- if it has 'pidfile' or 'path' then this is the 'check' line #}
{%- if 'pidfile' in kind_v or 'path' in kind_v %}
check {{ mod_name }} {{ name }} with {{ element }} {{ element_v }}
check {{ mod_name|replace('\\n', '\n') }} {{ name|replace('\\n', '\n') }} with {{ element|replace('\\n', '\n') }} "{{ element_v|replace('\\n', '\n') }}"
{%- endif -%}
{% endfor %}
{% endfor %}
{%- endfor -%}
{%- endfor -%}
{%- for kind, kind_v in mod_name_v.iteritems() -%}
{%- for element, element_v in kind_v.iteritems() -%}

Expand All @@ -58,9 +59,9 @@ check {{ mod_name }} {{ name }} with {{ element }} {{ element_v }}

{#- filter 'start' to add 'program =' into this line #}
{%- if 'start' in element or 'stop' in element %}
{{ element }} program = "{{ element_v }}"
{{ element|replace('\\n', '\n') }} program = "{{ element_v|replace('\\n', '\n') }}"
{%- else %}
{{ element }} {{ element_v }}
{{ element|replace('\\n', '\n') }} {{ element_v|replace('\\n', '\n') }}
{%- endif %}
{%- endif -%}

Expand All @@ -69,7 +70,11 @@ check {{ mod_name }} {{ name }} with {{ element }} {{ element_v }}

{#- 'action' is appended to the 'if failed' line #}
{%- if 'action' not in element %}
if {{ element }} {{ element_v }} then {{ kind_v.action }}
if {{ element|replace('\\n', '\n') }}
{%- if element_v is not none %}
{{ element_v|replace('\\n', '\n')| indent(4) }}
{%- endif %}
then {{ kind_v.action|replace('\\n', '\n') }}
{%- endif %}
{%- endif %}
{%- endfor %}
Expand Down
17 changes: 11 additions & 6 deletions monit/files/monitrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
set daemon 10
{%- from "monit/map.jinja" import monit with context -%}
set daemon {{ monit.daemon_interval }}
{%- if monit.daemon_start_delay %}
with start delay {{ monit.daemon_start_delay }}
{%- endif %}
set logfile {{ monit.logfile }}

set httpd port {{ http_access.port }} and
use address {{ http_access.bind }}
{%- for host in http_access.allowed.hosts %}
set httpd port {{ monit.http_access.port }} and
use address {{ monit.http_access.bind }}
{%- for host in monit.http_access.allowed.hosts %}
allow {{ host }}
{%- endfor %}
{%- for userdata in http_access.allowed.users %}
{%- for userdata in monit.http_access.allowed.users %}
allow {{ userdata[0] }}:{{ userdata[1] }}
{%- endfor %}

include {{ config_includes }}/*
include {{ monit.config_includes }}/*
4 changes: 4 additions & 0 deletions monit/map.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ that differ from whats in defaults.yaml
{% set os_family_map = salt['grains.filter_by']({
'Debian': {},
'Kali': {},
'FreeBSD': {
'config': '/usr/local/etc/monitrc',
'config_includes': '/usr/local/etc/monit/conf.d'
},
'Gentoo': {
'pkg': 'app-admin/monit',
'config': '/etc/monitrc',
Expand Down
8 changes: 2 additions & 6 deletions monit/service.sls
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
{% from "monit/map.jinja" import monit with context %}

{{ monit.service.name }}:
service.running:
- enable: True
service.{{ monit.service.status }}:
- enable: {{ monit.service.enable }}
- restart: True
- watch:
- file: {{ monit.config }}
- file: {{ monit.config_includes }}/mail
- file: {{ monit.config_includes }}/modules
5 changes: 5 additions & 0 deletions pillar.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
monit:
service:
enable: True
status: running
daemon_interval: 30
daemon_start_delay: 60
mail_alert:
account:
server: smtp.gmail.com
Expand Down