---
- hosts: all
  roles:
    - role: common
    - role: test-config-generator
  tasks:
  - name: "install nagios4 / livestatus build dependencies"
    include_role:
      name: yum_apt_retry
    vars:
      package:
        - wget
        - gcc
        - make
        - gd-devel
        - gcc-c++
        - libstdc++-static
  - name: "omd config change"
    shell: "omd config demo change"
    args:
      stdin: |
        APACHE_MODE=own
        LIVESTATUS_TCP=on
        PNP4NAGIOS=off
  - name: "copy example files"
    copy:
      src: /omd/sites/demo/share/doc/naemon/example.cfg
      dest: /omd/sites/demo/etc/naemon/conf.d/example.cfg
      owner: demo
      group: demo
  - name: "set test secret.key"
    shell: echo "testkey" > /omd/sites/demo/var/thruk/secret.key
  - name: "fix secret.key permissions"
    file:
      path: /omd/sites/demo/var/thruk/secret.key
      mode: 0600
      owner: demo
      group: demo
  - name: "copy test.cfg config file"
    copy:
      src: /root/test.cfg
      dest: /omd/sites/demo/etc/naemon/conf.d/test.cfg
      owner: demo
      group: demo

- hosts: all
  become: true
  become_user: demo
  vars:
    nagios_version: "4.4.6"
    livestatus_version: "1.2.8p27"
  tasks:
  - name: "wget nagios-{{ nagios_version }}.tar.gz"
    shell: wget "https://download.thruk.org/nagios-{{ nagios_version }}.tar.gz"
    args:
      chdir: /tmp/
      creates: /tmp/nagios-{{ nagios_version }}.tar.gz
  - name: "tar zxf nagios-{{ nagios_version }}.tar.gz"
    shell: tar zxf nagios-{{ nagios_version }}.tar.gz
    args:
      chdir: /tmp/
  - name: "make nagios"
    shell: "cd /tmp/nagios-{{ nagios_version }} && ./configure && make nagios"
  - name: "cp nagios bin/"
    shell: "cp /tmp/nagios-{{ nagios_version }}/base/nagios ~/local/bin/nagios-{{ nagios_version }}"
  - name: "change path in init script"
    lineinfile:
     path: /omd/sites/demo/etc/init.d/naemon
     state: present
     regexp: '^DAEMON='
     line: 'DAEMON=$OMD_ROOT/local/bin/nagios-{{ nagios_version }}'
  - name: "wget check_mk-{{ livestatus_version }}.tar.gz"
    shell: wget "https://download.thruk.org/check_mk-{{ livestatus_version }}.tar.gz"
    args:
      chdir: /tmp/
      creates: /tmp/check_mk-{{ livestatus_version }}.tar.gz
  - name: "tar zxf check_mk-{{ livestatus_version }}.tar.gz"
    shell: tar zxf check_mk-{{ livestatus_version }}.tar.gz
    args:
      chdir: /tmp/
  - name: "mkdir /tmp/livestatus"
    shell: "mkdir -p /tmp/livestatus"
  - name: "tar zxf livestatus.tar.gz"
    shell: tar zxf ../check_mk-{{ livestatus_version }}/livestatus.tar.gz
    args:
      chdir: /tmp/livestatus
  - name: "fix builds"
    lineinfile:
     path: /tmp/livestatus/src/logger.c
     state: present
     regexp: '^pthread_t g_mainthread_id'
     line: 'extern pthread_t g_mainthread_id;'
  - name: "make livestatus"
    shell: "cd /tmp/livestatus && ./configure --with-nagios4 && make"
  - name: "cp livestatus.o"
    shell: "cp /tmp/livestatus/src/livestatus.o ~/local/lib"
  - name: "enable broker module"
    lineinfile:
     path: /omd/sites/demo/etc/naemon/naemon.d/livestatus.cfg
     state: present
     regexp: '^broker_module='
     line: 'broker_module=/omd/sites/demo/local/lib/livestatus.o num_client_threads=20 pnp_path=/omd/sites/demo/var/pnp4nagios/perfdata /omd/sites/demo/tmp/run/live'
  - name: "remove config options allow_circular_dependencies nagios does not know"
    lineinfile:
     path: /omd/sites/demo/etc/naemon/naemon.d/dependency.cfg
     state: absent
     regexp: 'allow_circular_dependencies'
  - name: "remove config options enable_notification_suppression_reason_logging nagios does not know"
    lineinfile:
     path: /omd/sites/demo/etc/naemon/naemon.d/logging.cfg
     state: absent
     regexp: 'enable_notification_suppression_reason_logging'
  - name: "remove config options retained_scheduling_randomize_window nagios does not know"
    lineinfile:
     path: /omd/sites/demo/etc/naemon/naemon.d/retention.cfg
     state: absent
     regexp: 'retained_scheduling_randomize_window'
