- name: Sync Config
  hosts: workers
  become: true
  tasks:
    - name: Template a file to /etc/nagios-mod-gearman/worker.conf
      ansible.builtin.template:
        src: /usr/local/nagiosxi/html/config/nmg/ansible/templates/worker.conf.j2
        dest: /etc/nagios-mod-gearman/worker.conf
        owner: nagios
        group: nagios
        mode: '0600'

    # Oracle config file
    - name: Check if Oracle plugin file exists locally
      ansible.builtin.stat:
        path: /usr/local/nagiosxi/etc/configwizards/oracle/oracle
      become: false
      delegate_to: localhost
      register: oracle_file

    - name: Ensure Oracle directory exists remotely
      ansible.builtin.file:
        path: /usr/local/nagiosxi/etc/configwizards/oracle
        state: directory
        owner: nagios
        group: nagios
        mode: '0755'
      when: oracle_file.stat.exists

    - name: Copy Oracle plugin environment file
      ansible.builtin.copy:
        src: /usr/local/nagiosxi/etc/configwizards/oracle/oracle
        dest: /usr/local/nagiosxi/etc/configwizards/oracle/oracle
        owner: nagios
        group: nagios
        mode: '0644'
      when: oracle_file.stat.exists

    # Vsphere config files
    - name: Check if VSphere plugin folder exists locally
      ansible.builtin.stat:
        path: /usr/local/nagiosxi/etc/components/vsphere
      become: false
      delegate_to: localhost
      register: vsphere_dir

    - name: Copy VSphere plugin config files
      ansible.builtin.copy:
        src: /usr/local/nagiosxi/etc/components/vsphere/
        dest: /usr/local/nagiosxi/etc/components/vsphere/
        owner: nagios
        group: nagios
        mode: '0644'
      when: vsphere_dir.stat.exists

    # Oauth2 config files
    - name: Check if Oauth2 plugin folder exists locally
      ansible.builtin.stat:
        path: /usr/local/nagiosxi/etc/components/oauth2
      become: false
      delegate_to: localhost
      register: oauth2_dir

    - name: Copy Oauth2 plugin config files
      ansible.builtin.copy:
        src: /usr/local/nagiosxi/etc/components/oauth2/
        dest: /usr/local/nagiosxi/etc/components/oauth2/
        owner: nagios
        group: nagios
        mode: '0644'
      when: oauth2_dir.stat.exists
    
    # WebInject config files
    - name: Check if WebInject plugin folder exists locally
      ansible.builtin.stat:
        path: /usr/local/nagiosxi/etc/components/webinject
      become: false
      delegate_to: localhost
      register: webinject_dir

    - name: Copy WebInject plugin config files
      ansible.builtin.copy:
        src: /usr/local/nagiosxi/etc/components/webinject/
        dest: /usr/local/nagiosxi/etc/components/webinject/
        owner: nagios
        group: nagios
        mode: '0644'
      when: webinject_dir.stat.exists

    # Install runtime dependencies for SNMP-based plugins (check_throughput, check_snmp_interface_status)
    - name: Install SNMP plugin runtime deps on EL
      ansible.builtin.dnf:
        name:
          - net-snmp-libs
          - net-snmp-utils
          - sqlite
        state: present
      when: ansible_os_family == "RedHat"

    - name: Install SNMP plugin runtime deps on Debian-based
      ansible.builtin.apt:
        name:
          - snmp
          - libsnmp40
          - sqlite3
          - libsqlite3-0
        state: present
        update_cache: true
      when: ansible_os_family == "Debian"

    # Ensure /usr/local/nagios/var exists for throughput database
    - name: Ensure /usr/local/nagios/var directory exists
      ansible.builtin.file:
        path: /usr/local/nagios/var
        state: directory
        owner: nagios
        group: nagios
        mode: '0755'

    # Copy throughput database so workers have baseline data from wizard setup
    - name: Check if throughput database exists locally
      ansible.builtin.stat:
        path: /usr/local/nagios/var/nagios_snmp_throughput.db
      become: false
      delegate_to: localhost
      register: throughput_db_file

    - name: Copy throughput database and reset timestamps for first-run on worker
      when: throughput_db_file.stat.exists
      block:
        - ansible.builtin.copy:
            src: /usr/local/nagios/var/nagios_snmp_throughput.db
            dest: /usr/local/nagios/var/nagios_snmp_throughput.db
            owner: nagios
            group: nagios
            mode: '0644'
        - ansible.builtin.shell: |
            sqlite3 /usr/local/nagios/var/nagios_snmp_throughput.db \
              "SELECT name FROM sqlite_master WHERE type='table';" \
              | xargs -I{} sqlite3 /usr/local/nagios/var/nagios_snmp_throughput.db \
              "UPDATE \"{}\" SET timestamp=0;"

    - name: Restart worker
      ansible.builtin.systemd:
        name: nagios-mod-gearman-worker
        state: restarted
