---
# OS RELEASE: RHEL 7
# SAP Note: 2382421 - Optimizing the Network Configuration on HANA- and OS-Level
#

- name: Assert - Display SAP note number 2382421 and its version
  debug:
    msg: "SAP note {{ (__sap_hana_preconfigure_sapnotes_versions | selectattr('number', 'match', '^2382421$') | first).number }}
          (version {{ (__sap_hana_preconfigure_sapnotes_versions | selectattr('number', 'match', '^2382421$') | first).version }}): Recommended network settings for SAP HANA"

- name: Check SAP HANA sysctl conf file
  block:
    - name: Get info about file {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}
      stat:
        path: "{{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}"
      register: __sap_hana_preconfigure_register_stat_sysctl_saphana_conf_assert

    - name: Assert that file {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }} exists
      assert:
        that: __sap_hana_preconfigure_register_stat_sysctl_saphana_conf_assert.stat.exists
        fail_msg: "FAIL: File {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }} does not exist!"
        success_msg: "PASS: File {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }} exists."
      ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"

    - name: Assert that file {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }} is a regular file
      assert:
        that: __sap_hana_preconfigure_register_stat_sysctl_saphana_conf_assert.stat.isreg
        fail_msg: "FAIL: File {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }} is not a regular file!"
        success_msg: "PASS: File {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }} is a regular file."
      ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"
      when: __sap_hana_preconfigure_register_stat_sysctl_saphana_conf_assert.stat.exists

  when: sap_hana_preconfigure_config_all|d(true) or sap_hana_preconfigure_2382421|d(false)

- name: Get and assert kernel tunables
  include_tasks: ../RedHat/generic/assert-2382421-loop-block.yml
  loop: "{{ __sap_hana_preconfigure_kernel_parameters_default }}"
  loop_control:
    loop_var: line_item
  when: sap_hana_preconfigure_config_all|d(true) or sap_hana_preconfigure_2382421|d(false)

- name: Check net.core.wmem_max and net.core.rmem_max if necessary
  block:
    - name: 2382421 - Get the maximum value of net.ipv4.tcp_wmem
      command: sysctl net.ipv4.tcp_wmem
      register: __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem_assert
      changed_when: no
      ignore_errors: yes

    - name: Display the maximum value of net.ipv4.tcp_wmem
      debug:
        msg: "INFO: The maximum value of net.ipv4.tcp_wmem is '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem_assert.stdout.split()[-1] }}'"

    - name: 2382421 - Get net.core.wmem_max from {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}
      command: awk 'BEGIN{FS="="}/net.core.wmem_max/{print $NF}' {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}
      register: __sap_hana_preconfigure_register_sysctl_saphana_conf_wmem_max_assert
      changed_when: no
      ignore_errors: yes
      when: __sap_hana_preconfigure_register_stat_sysctl_saphana_conf_assert.stat.exists

    - name: 2382421 - Assert that net.core.wmem_max is set correctly in {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}
      assert:
        that: "__sap_hana_preconfigure_register_sysctl_saphana_conf_wmem_max_assert.stdout ==
               '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem_assert.stdout.split()[-1] }}'"
        fail_msg: "FAIL: The value of 'net.core.wmem_max' in {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }} is
                   '{{ __sap_hana_preconfigure_register_sysctl_saphana_conf_wmem_max_assert.stdout }}' but the expected value is
                   '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem_assert.stdout.split()[-1] }}'!"
        success_msg: "PASS: The value of 'net.core.wmem_max' in {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }} is
                      '{{ __sap_hana_preconfigure_register_sysctl_saphana_conf_wmem_max_assert.stdout }}'."
      ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"
      when: __sap_hana_preconfigure_register_stat_sysctl_saphana_conf_assert.stat.exists

    - name: 2382421 - Get the current value of net.core.wmem_max from sysctl
      command: sysctl net.core.wmem_max
      register: __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem_max_assert
      changed_when: no
      ignore_errors: yes

    - name: 2382421 - Assert that net.core.wmem_max is set correctly as per sysctl
      assert:
        that: "'{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem_max_assert.stdout.split()[-1] }}' ==
               '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem_assert.stdout.split()[-1] }}'"
        fail_msg: "FAIL: The current value of 'net.core.wmem_max as per sysctl is
                   '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem_max_assert.stdout.split()[-1] }}' but the expected value is
                   '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem_assert.stdout.split()[-1] }}'!"
        success_msg: "PASS: The current value of 'net.core.wmem_max' as per sysctl is
                      '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem_assert.stdout.split()[-1] }}'."
      ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"

    - name: 2382421 - Get the maximum value of net.ipv4.tcp_rmem
      command: sysctl net.ipv4.tcp_rmem
      register: __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem_assert
      changed_when: no
      ignore_errors: yes

    - name: 2382421 - Display the maximum value of net.ipv4.tcp_rmem
      debug:
        msg: "INFO: The maximum value of net.ipv4.tcp_rmem is '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem_assert.stdout.split()[-1] }}'"

    - name: 2382421 - Get net.core.rmem_max from {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}
      command: awk 'BEGIN{FS="="}/net.core.rmem_max/{print $NF}' {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}
      register: __sap_hana_preconfigure_register_sysctl_saphana_conf_rmem_max_assert
      changed_when: no
      ignore_errors: yes
      when: __sap_hana_preconfigure_register_stat_sysctl_saphana_conf_assert.stat.exists

    - name: 2382421 - Assert that net.core.rmem_max is set correctly in {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}
      assert:
        that: "__sap_hana_preconfigure_register_sysctl_saphana_conf_rmem_max_assert.stdout ==
               '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem_assert.stdout.split()[-1] }}'"
        fail_msg: "FAIL: The value of 'net.core.rmem_max' in {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }} is
                   '{{ __sap_hana_preconfigure_register_sysctl_saphana_conf_rmem_max_assert.stdout }}' but the expected value is
                   '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem_assert.stdout.split()[-1] }}'!"
        success_msg: "PASS: The value of 'net.core.rmem_max' in {{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }} is
                      '{{ __sap_hana_preconfigure_register_sysctl_saphana_conf_rmem_max_assert.stdout }}'."
      ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"
      when: __sap_hana_preconfigure_register_stat_sysctl_saphana_conf_assert.stat.exists

    - name: 2382421 - Get the current value of net.core.rmem_max from sysctl
      command: sysctl net.core.rmem_max
      register: __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem_max_assert
      changed_when: no
      ignore_errors: yes

    - name: 2382421 - Assert that net.core.rmem_max is set correctly as per sysctl
      assert:
        that: "'{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem_max_assert.stdout.split()[-1] }}' ==
               '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem_assert.stdout.split()[-1] }}'"
        fail_msg: "FAIL: The current value of 'net.core.rmem_max as per sysctl is
                   '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem_max_assert.stdout.split()[-1] }}' but the expected value is
                   '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem_assert.stdout.split()[-1] }}'!"
        success_msg: "PASS: The current value of 'net.core.rmem_max' as per sysctl is
                      '{{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem_assert.stdout.split()[-1] }}'."
      ignore_errors: "{{ sap_hana_preconfigure_assert_ignore_errors|d(false) }}"

  when:
    - sap_hana_preconfigure_config_all|d(true) or sap_hana_preconfigure_2382421|d(false)
    - not sap_hana_preconfigure_use_netapp_settings_nfs|d(false)
    - not ansible_architecture == 'ppc64le'
