# role monit
# installs monit and adds perfact configuration

- name: install pf-monit
  tags: monit
  become: true
  ansible.builtin.apt:
    pkg:
      - monit
      - pfmonit
    state: present
  notify: restart monit

- name: setup monit for perfact
  tags: monit
  become: true

  block:

    - name: copy monit configuration files
      ansible.builtin.template:
        src: "etc/monit/conf.d/{{ item }}.monitrc"
        dest: "/etc/monit/conf.d/{{ item }}.monitrc"
      loop:
        - perfact-ssh
        - perfact.reboot
      notify: restart monit

    - name: copy monit configuration files (j2)
      ansible.builtin.template:
        src: "etc/monit/conf.d/{{ item }}.monitrc.j2"
        dest: "/etc/monit/conf.d/{{ item }}.monitrc"
      loop:
        - perfact.devices
        - perfact
      notify: restart monit

    - name: copy perfact-appl.monitrc
      ansible.builtin.template:
        src: etc/monit/conf.d/perfact-appl.monitrc
        dest: /etc/monit/conf.d/perfact-appl.monitrc
      notify: restart monit
      when: "'perfact-file-' in inventory_hostname"

    - name: enable monitor_firewall
      ansible.builtin.file:
        src: ../conf-available/pffirewall
        dest: /etc/monit/conf-enabled/pffirewall
        state: link
        # when running the playbook the first time, the src does not exist
        force: true
      notify: restart monit

    # enable measure check
    - name: enable perfact-measure check
      ansible.builtin.file:
        src: ../conf-available/measure
        dest: /etc/monit/conf-enabled/measure
        state: link
      notify: restart monit
      when: software is defined and "measure" in software

    # remove cronjob for monitoring postgres. This will send mail to root every 2 Minutes.
    # /etc/cron.d/pfmonit:*/2 * * * * root /usr/bin/postgres_available.monit perfactema
    # /usr/bin/postgres_available.monit: line 6: pg_isready: command not found
    - name: monit config pfmonit cronjob
      ansible.builtin.template:
        src: etc/cron.d/pfmonit.j2
        dest: /etc/cron.d/pfmonit

