# DO NOT EDIT THIS FILE -- OPNsense auto-generated file

{% from 'OPNsense/Macros/interface.macro' import physical_interface %}
{% if helpers.exists('OPNsense.monit.general') %}
{%  if helpers.exists('OPNsense.monit.general.httpdEnabled') and OPNsense.monit.general.httpdEnabled|default('0') == '1' %}
{%  set httpdCredentials = OPNsense.monit.general.httpdUsername ~ ':"' ~ OPNsense.monit.general.httpdPassword ~ '"' %}
{%    set httpdport = "port " ~ OPNsense.monit.general.httpdPort %}
set httpd unixsocket /var/run/monit.sock {{ httpdport }}
    allow localhost
    allow {{  httpdCredentials }}
{%    if helpers.exists('OPNsense.monit.general.httpdAllow') %}
{%      for allow in OPNsense.monit.general.httpdAllow.split(",") %}
{%        set cred = allow.split(":") %}
{%        if cred[1] %}
{%          set pass = cred[1].split() %}
{%          set allow = cred[0] ~ ':"' ~ pass[0] ~ '" ' ~ pass[1] %}
{%        endif %}
    allow {{ allow }}
{%      endfor %}
{%    endif %}
{%    if helpers.exists('OPNsense.monit.general.mmonitUrl') and OPNsense.monit.general.mmonitUrl|default('') != '' %}
{%      set mregister = 'register without credentials' if OPNsense.monit.general.mmonitRegisterCredentials|default('1') == '0' %}

set mmonit {{ OPNsense.monit.general.mmonitUrl }} timeout {{ OPNsense.monit.general.mmonitTimeout }} seconds {{ mregister }}
{%    endif %}
{%  else %}
set httpd unixsocket /var/run/monit.sock
    allow localhost
{%  endif %}

set daemon {{ OPNsense.monit.general.interval }} with start delay {{ OPNsense.monit.general.startdelay }}

{%  if helpers.exists('OPNsense.monit.general.logfile') and OPNsense.monit.general.logfile|default('') != '' %}
set logfile {{ OPNsense.monit.general.logfile }}
{%  else %}
set logfile syslog facility log_daemon
{%  endif %}

{%  if helpers.exists('OPNsense.monit.general.statefile') and OPNsense.monit.general.statefile|default('') != '' %}
set statefile {{ OPNsense.monit.general.statefile }}
{%  endif %}

{%  if helpers.exists('OPNsense.monit.general.eventqueuePath') and OPNsense.monit.general.eventqueuePath|default('') != '' %}
{%    set slots = '' %}
{%    if helpers.exists('OPNsense.monit.general.eventqueueSlots') %}
{%      set slots = "slots " ~ OPNsense.monit.general.eventqueueSlots %}
{%    endif %}

set eventqueue basedir {{ OPNsense.monit.general.eventqueuePath }} {{ slots }}
{%  endif %}

{%  if helpers.exists('OPNsense.monit.general.mailserver') %}
{%    set port = "port " ~ OPNsense.monit.general.port if OPNsense.monit.general.port|default('') != '' %}
{%    set username = '' %}
{%    set password = '' %}
{%    if helpers.exists('OPNsense.monit.general.username') and helpers.exists('OPNsense.monit.general.password') %}
{%      set username = "username " ~ OPNsense.monit.general.username %}
{%      set password = "password " ~ '"' ~ OPNsense.monit.general.password ~ '"' %}
{%    endif %}
{%    if OPNsense.monit.general.ssl|default('0') == '1' %}
{%      set ssl = 'using ssl' %}
{%      set ssloptions = '' %}
{%      if helpers.exists('OPNsense.monit.general.sslversion') and OPNsense.monit.general.sslversion|default('') != '' %}
{%        set ssloptions = "version: " ~ OPNsense.monit.general.sslversion %}
{%      endif %}
{%      if helpers.exists('OPNsense.monit.general.sslverify') %}
{%        if OPNsense.monit.general.sslverify == '1' %}
{%          set ssloptions = ssloptions ~ " verify: enable" %}
{%        else %}
{%          set ssloptions = ssloptions ~ " verify: disable" %}
{%        endif %}
{%      endif %}
{%      if ssloptions != '' %}
{%        set ssl = ssl ~ " with options { " ~ ssloptions ~ " }" %}
{%      endif %}
{%    endif %}
set mailserver {{ OPNsense.monit.general.mailserver }} {{ port }} {{ username }} {{ password }} {{ ssl }}
{%  endif %}

{%  if helpers.exists('OPNsense.monit.alert') %}
{%   for alert in helpers.toList('OPNsense.monit.alert') %}
{%     if alert.enabled|default('0') == '1' %}
{%       set noton = 'not on' if alert.noton|default('0') == '1' %}
{%       set mailformat = "mail-format { " ~ alert.format ~ " }" if alert.format|default('') != '' %}
{%       set reminder = "reminder on " ~ alert.reminder ~ " cycles" if alert.reminder|default('0') != '0' %}
{%       set events = "{ " ~  alert.events ~ " }" if alert.events|default('') != '' %}
set alert {{ alert.recipient }} {{ noton }} {{ events }} {{ mailformat }} {{ reminder }}
{%    endif %}
{%   endfor %}
{%  endif %}

{%  if helpers.exists('OPNsense.monit.service') %}
{%   for service in helpers.toList('OPNsense.monit.service') %}
{%    if service.enabled|default('0') == '1' %}
{%     set pidfile = '' %}
{%     set match = '' %}
{%     set path = '' %}
{%     set address = '' %}
{%     set interface = '' %}
{%     if service.type == 'process' %}
{%      if service.pidfile|default('') != '' %}
{%       set pidfile = "pidfile " ~ service.pidfile %}
{%      elif service.match|default('') != '' %}
{%       set match = "matching " ~ service.match if service.match|default('') != '' %}
{%      endif %}
check {{ service.type }} {{ service.name }} {{ pidfile }} {{ match }}
{%     elif service.type == 'host' %}
{%      set address = "address " ~ service.address %}
check {{ service.type }} {{ service.name }} {{ address }}
{%     elif service.type == 'network' %}
{%      if service.address|default('') != '' %}
{%       set address = "address " ~ service.address %}
{%      elif service.interface|default('') != '' %}
{%       set interface = "interface " ~ physical_interface(service.interface) %}
{%      endif %}
check {{ service.type }} {{ service.name }} {{ address }} {{ interface }}
{%     elif service.type == 'system' %}
check {{ service.type }} {{ service.name }}
{%     else %}
{%      set path = "with path \"" ~ service.path ~ "\"" %}
{%      if service.type == 'custom' %}
{%       set timeout = '' %}
{%       if service.timeout|default('0') != 0 %}
{%         set timeout = "timeout " ~ service.timeout ~ " seconds" %}
{%       endif %}
check program {{ service.name }} {{ path }} {{ timeout }}
{%      else %}
check {{ service.type }} {{ service.name }} {{ path }}
{%      endif %}
{%     endif %}
{%     if service.depends is defined %}
{%      for dependency in service.depends.split(",") %}
{%      set dependency = helpers.getUUID(dependency) %}
   depends on {{dependency.name}}
{%      endfor %}
{%     endif %}
{%     if service.start|default('') != '' %}
   start program = "{{ service.start }}" with timeout {{service.starttimeout|default('30')}} seconds
{%     endif %}
{%     if service.stop|default('') != '' %}
   stop program = "{{ service.stop }}"
{%     endif %}
{%     if service.tests is defined %}
{%      for test in service.tests.split(",") %}
{%       set test = helpers.getUUID(test) %}
{%       if test.condition is defined and test.action is defined %}
{%        set epath = '' %}
{%        if test.action == 'exec' and test.path|default('') != '' %}
{%         set epath = test.path %}
{%        endif %}
   if {{ test.condition }} then {{ test.action }} {{ epath }}
{%       endif %}
{%      endfor %}
{%     endif %}
{%     if service.polltime is defined %}
{%       set polltime = service.polltime %}
{%       if "cycles" not in polltime.lower() %}
{%         set polltime = "\"" ~ polltime ~ "\"" %}
{%       endif %}
   every {{ polltime }}
{%     endif %}
{%    endif %}

{%   endfor %}
{%  endif %}
{% endif %}

include /usr/local/etc/monit.opnsense.d/*.conf
