{% set __rsyslog_ovirt_subtype = __rsyslog_input.subtype | d() %}
{% set __ovirt_env_name = __rsyslog_input.ovirt_env_name | d('engine') %}
{% set __ovirt_env_uuid = __rsyslog_input.ovirt_env_uuid | d() %}
{% if __rsyslog_ovirt_subtype == "collectd" %}
{%   set __rsyslog_collectd_port = __rsyslog_input.ovirt_collectd_port | d(44514) %}
{%   set __rsyslog_elasticsearch_index_prefix = __rsyslog_input.ovirt_elasticsearch_index_prefix | d('project.ovirt-metrics') %}
input(name="{{ __rsyslog_input.name }}" type="imtcp" port="{{ __rsyslog_collectd_port }}")
if $inputname == "{{ __rsyslog_input.name }}" then {
    set $!original_raw_message = $msg;
    action(name="collectd_mmjsonparse" type="mmjsonparse" cookie="") # parse entire message as json
    set $!@timestamp = exec_template('cnvt_to_viaq_timestamp');
    unset $!time;
    if $!collectd!plugin == 'virt' then {
        set $!ovirt!entity = 'vms';
    } else {
        set $!ovirt!entity = 'hosts';
    }
{%   if __ovirt_env_uuid is defined %}
    set $.index_prefix = "{{ __rsyslog_elasticsearch_index_prefix }}" & "-" & "{{ __ovirt_env_name }}" & "." & "{{ __ovirt_env_uuid }}" & ".";
{%   else %}
    set $.index_prefix = "{{ __rsyslog_elasticsearch_index_prefix }}" & "-" & "{{ __ovirt_env_name }}" & ".";
{%   endif %}
    set $.input_type = "ovirt";
}
{% else %}
{%   set __rsyslog_elasticsearch_index_prefix = __rsyslog_input.ovirt_elasticsearch_index_prefix | d('project.ovirt-logs') %}
{%   set __ovirt_vds_cluster_name = __rsyslog_input.ovirt_vds_cluster_name | d('') %}
{%   set __ovirt_engine_fqdn = __rsyslog_input.ovirt_engine_fqdn | d('') %}
{%   if __rsyslog_ovirt_subtype == "engine" %}
{%     set __ovirt_input_file = __rsyslog_input.ovirt_input_file | d('/var/log/ovirt-engine/engine.log') %}
input(type="imfile" file="{{ __ovirt_input_file }}" tag="{{ __rsyslog_input.name }}"
      reopenOnTruncate="on" startmsg.regex="^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}")
if $syslogtag == "{{ __rsyslog_input.name }}" then {
  action(name="ovirt-engine_mmnormalize" type="mmnormalize"
         rulebase="{{ __rsyslog_config_dir }}/ovirt_engine.rulebase" path="$.ovirt")
}
{%   elif __rsyslog_ovirt_subtype == "vdsm" %}
{%     set __ovirt_input_file = __rsyslog_input.ovirt_input_file | d('/var/log/vdsm/vdsm.log') %}
input(type="imfile" file="{{ __ovirt_input_file }}" tag="{{ __rsyslog_input.name }}"
      reopenOnTruncate="on" startmsg.regex="^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}")
if $syslogtag == "{{ __rsyslog_input.name }}" then {
  action(name="ovirt-vdsm_mmnormalize" type="mmnormalize"
         rulebase="{{ __rsyslog_config_dir }}/ovirt_vdsm.rulebase" path="$.ovirt")
}
{%   endif %}
if $syslogtag == "{{ __rsyslog_input.name }}" then {
{%   if __ovirt_env_uuid is defined %}
  set $.index_prefix = "{{ __rsyslog_elasticsearch_index_prefix }}" & "-" & "{{ __ovirt_env_name }}" & "." & "{{ __ovirt_env_uuid }}" & ".";
{%   else %}
  set $.index_prefix = "{{ __rsyslog_elasticsearch_index_prefix }}" & "-" & "{{ __ovirt_env_name }}" & ".";
{%   endif %}
  # Add ovirt logs specific metadata
  set $!@timestamp = $.ovirt!date & "T" & $.ovirt!time & "." & $.ovirt!msec & $.ovirt!tz;
  set $!service = $syslogtag;
  set $!message = $.ovirt!message;
  set $!level = $.ovirt!level;
  set $!hostname = '{{ ansible_fqdn }}';
  set $!ovirt!cluster_name = '{{ __ovirt_vds_cluster_name }}';
  set $!ipaddr4 = '{{ ansible_default_ipv4.address }}';
  set $!ovirt!engine_fqdn = '{{ __ovirt_engine_fqdn }}';
  set $.input_type = "ovirt";
  set $!ovirt!class = $.ovirt!class;
  set $!ovirt!thread = $.ovirt!thread;

  unset $!metadata;

  if strlen($.ovirt!correlationid) > 0 then {
      set $!ovirt!correlationid = $.ovirt!correlationid;
  }
}
{% endif %}
{% set outdict = {} %}
{% for flow in logging_flows %}
{%   if flow.inputs | intersect([ __rsyslog_input.name ]) %}
{%     for oname in flow.outputs %}
{%       set _ = outdict.__setitem__(oname, outdict.get(oname,[])|union([ __rsyslog_input.name ])) %}
{%     endfor %}
{%   endif %}
{% endfor %}
{% for output in rsyslog_outputs %}
{%   if outdict[output.name] | d(false) %}
if
{%     for inputname in outdict[output.name] %}
{%       if inputname == __rsyslog_input.name %}
{%         if not loop.first %}
  or
{%         endif %}
{%         if __rsyslog_ovirt_subtype == "collectd" %}
  ($inputname == "{{ __rsyslog_input.name }}")
{%         elif __rsyslog_ovirt_subtype == "engine" or __rsyslog_ovirt_subtype == "vdsm" %}
  ($syslogtag == "{{ __rsyslog_input.name }}")
{%         endif %}
{%       endif %}
{%     endfor %}
  then {
    call {{ output.name }}
}
{%   endif %}
{% endfor %}
