---

- name: install prometheus-node-exporter
  pacman: name=prometheus-node-exporter,arch-audit,pacman-contrib,jq,hq,sudo state=present

- name: install prometheus-blackbox-exporter
  pacman: name=prometheus-blackbox-exporter state=present
  when: "'prometheus' in group_names"

- name: install prometheus-memcached-exporter
  pacman: name=prometheus-memcached-exporter state=present
  when: "'memcached' in group_names"

- name: add node_exporter to rebuilderd group
  user: name=node_exporter groups=rebuilderd append=yes
  when: "'rebuilderd' in group_names"

- name: install prometheus-mysqld-exporter
  pacman: name=prometheus-mysqld-exporter state=present
  when: "'mysql_servers' in group_names"

- name: create prometheus mysqld database user
  mysql_user:
    name: '{{ prometheus_mysqld_user }}'
    password: '{{ vault_monitoring_mysql_password }}'
    priv: "*.*:PROCESS,REPLICATION CLIENT"
    state: present
    resource_limits:
      MAX_USER_CONNECTIONS: 3
  when: "'mysql_servers' in group_names"

- name: copy prometheus mysqld exporter configuration
  template: src=prometheus-mysqld-exporter.j2 dest=/etc/conf.d/prometheus-mysqld-exporter owner=root group=root mode=600
  when: "'mysql_servers' in group_names"

- name: enable prometheus-mysqld-exporter service
  systemd: name=prometheus-mysqld-exporter enabled=yes daemon_reload=yes state=started
  when: "'mysql_servers' in group_names"

- name: copy prometheus memcached exporter configuration
  template: src=prometheus-memcached-exporter.j2 dest=/etc/conf.d/prometheus-memcached-exporter owner=root group=root mode=600
  when: "'memcached' in group_names"

- name: install node exporter configuration
  template: src=prometheus-node-exporter.env.j2 dest=/etc/conf.d/prometheus-node-exporter owner=root group=root mode=600

- name: create textcollector directory
  file: path="{{ prometheus_textfile_dir }}" state=directory owner=node_exporter group=node_exporter mode=700

- name: install node exporter textcollector scripts
  copy: src={{ item }} dest=/usr/local/bin/{{ item }} owner=root group=root mode=0755
  with_items:
    - arch-textcollector.sh
    - borg-textcollector.sh
    - borg-offsite-textcollector.sh
    - hetzner-textcollector.sh
    - rebuilderd-textcollector.sh
    - rebuilderd-status-textcollector.py
    - archive-textcollector.sh
    - repository-textcollector.sh
    - btrfs-textcollector.sh
    - aur-textcollector.sh
    - fail2ban-textcollector.sh

- name: install arch textcollector service
  template: src=prometheus-arch-textcollector.service.j2 dest=/etc/systemd/system/prometheus-arch-textcollector.service owner=root group=root mode=644

- name: install arch textcollector timer
  template: src=prometheus-arch-textcollector.timer.j2 dest=/etc/systemd/system/prometheus-arch-textcollector.timer owner=root group=root mode=644

- name: enable and start prometheus arch textcollector timer
  systemd: name=prometheus-arch-textcollector.timer enabled=yes daemon_reload=yes state=started

- name: install borg textcollector services
  template: src=prometheus-borg-textcollector.service.j2 dest=/etc/systemd/system/prometheus-{{ item.name }}-textcollector.service owner=root group=root mode=644
  loop:
    - { name: borg, service: borg-backup }
    - { name: borg-offsite, service: borg-backup-offsite }
  when: "'borg_clients' in group_names"

- name: enable borg textcollector services
  systemd: name=prometheus-{{ item.name }}-textcollector.service enabled=yes daemon_reload=yes
  loop:
    - { name: borg, service: borg-backup }
    - { name: borg-offsite, service: borg-backup-offsite }
  when: "'borg_clients' in group_names"

- name: install hetzner textcollector service
  template: src=prometheus-hetzner-textcollector.service.j2 dest=/etc/systemd/system/prometheus-hetzner-textcollector.service owner=root group=root mode=644
  when: "inventory_hostname == 'monitoring.archlinux.org'"

- name: install hetzner textcollector timer
  template: src=prometheus-hetzner-textcollector.timer.j2 dest=/etc/systemd/system/prometheus-hetzner-textcollector.timer owner=root group=root mode=644
  when: "inventory_hostname == 'monitoring.archlinux.org'"

- name: enable and start prometheus hetzner textcollector timer
  systemd: name=prometheus-hetzner-textcollector.timer enabled=yes daemon_reload=yes state=started
  when: "inventory_hostname == 'monitoring.archlinux.org'"

- name: install fail2ban textcollector service
  template: src=prometheus-fail2ban-textcollector.service.j2 dest=/etc/systemd/system/prometheus-fail2ban-textcollector.service owner=root group=root mode=644

- name: install fail2ban textcollector timer
  template: src=prometheus-fail2ban-textcollector.timer.j2 dest=/etc/systemd/system/prometheus-fail2ban-textcollector.timer owner=root group=root mode=644

- name: enable and start prometheus fail2ban textcollector timer
  systemd: name=prometheus-fail2ban-textcollector.timer enabled=yes daemon_reload=yes state=started

- name: install blackbox exporter configuration
  copy: src=blackbox.yml dest=/etc/prometheus/blackbox.yml owner=root group=root mode=0644
  when: "'prometheus' in group_names"

- name: install rebuilderd textcollector service
  template: src=prometheus-rebuilderd-textcollector.service.j2 dest=/etc/systemd/system/prometheus-rebuilderd-textcollector.service owner=root group=root mode=644
  when: "'rebuilderd' in group_names"

- name: install rebuilderd textcollector timer
  template: src=prometheus-rebuilderd-textcollector.timer.j2 dest=/etc/systemd/system/prometheus-rebuilderd-textcollector.timer owner=root group=root mode=644
  when: "'rebuilderd' in group_names"

- name: enable and start prometheus rebuilderd textcollector timer
  systemd: name=prometheus-rebuilderd-textcollector.timer enabled=yes daemon_reload=yes state=started
  when: "'rebuilderd' in group_names"

- name: install rebuilderd textcollector service
  template: src=prometheus-archive-textcollector.service.j2 dest=/etc/systemd/system/prometheus-archive-textcollector.service owner=root group=root mode=644
  when: "'archive_mirrors' in group_names or inventory_hostname == 'gemini.archlinux.org'"

- name: install rebuilderd textcollector service
  template: src=prometheus-repository-textcollector.service.j2 dest=/etc/systemd/system/prometheus-repository-textcollector.service owner=root group=root mode=644
  when: "inventory_hostname == 'gemini.archlinux.org'"

- name: install rebuilderd textcollector timer
  template: src=prometheus-archive-textcollector.timer.j2 dest=/etc/systemd/system/prometheus-archive-textcollector.timer owner=root group=root mode=644
  when: "'archive_mirrors' in group_names or inventory_hostname == 'gemini.archlinux.org'"

- name: enable and start prometheus archive textcollector timer
  systemd: name=prometheus-archive-textcollector.timer enabled=yes daemon_reload=yes state=started
  when: "'archive_mirrors' in group_names or inventory_hostname == 'gemini.archlinux.org'"

- name: install rebuilderd textcollector timer
  template: src=prometheus-repository-textcollector.timer.j2 dest=/etc/systemd/system/prometheus-repository-textcollector.timer owner=root group=root mode=644
  when: "inventory_hostname == 'gemini.archlinux.org'"

- name: enable and start prometheus repository textcollector timer
  systemd: name=prometheus-repository-textcollector.timer enabled=yes daemon_reload=yes state=started
  when: "inventory_hostname == 'gemini.archlinux.org'"

- name: install sudoers for btrfs
  copy: src=sudoers dest=/etc/sudoers.d/node_exporter owner=root group=root mode=0440
  when: filesystem == "btrfs"

- name: install btrfs textcollector service
  template: src=prometheus-btrfs-textcollector.service.j2 dest=/etc/systemd/system/prometheus-btrfs-textcollector.service owner=root group=root mode=644
  when: filesystem == "btrfs"

- name: install btrfs textcollector timer
  template: src=prometheus-btrfs-textcollector.timer.j2 dest=/etc/systemd/system/prometheus-btrfs-textcollector.timer owner=root group=root mode=644
  when: filesystem == "btrfs"

- name: enable and start prometheus btrfs textcollector timer
  systemd: name=prometheus-btrfs-textcollector.timer enabled=yes daemon_reload=yes state=started
  when: filesystem == "btrfs"

- name: install aur textcollector service
  template: src=prometheus-aur-textcollector.service.j2 dest=/etc/systemd/system/prometheus-aur-textcollector.service owner=root group=root mode=644
  when: "'prometheus' in group_names"

- name: install aur textcollector timer
  template: src=prometheus-aur-textcollector.timer.j2 dest=/etc/systemd/system/prometheus-aur-textcollector.timer owner=root group=root mode=644
  when: "'prometheus' in group_names"

- name: enable and start prometheus aur textcollector timer
  systemd: name=prometheus-aur-textcollector.timer enabled=yes daemon_reload=yes state=started
  when: "'prometheus' in group_names"

- name: enable prometheus-node-exporter service
  systemd: name=prometheus-node-exporter enabled=yes daemon_reload=yes state=started

- name: enable prometheus-blackbox-exporter service
  systemd: name=prometheus-blackbox-exporter enabled=yes daemon_reload=yes state=started
  when: "'prometheus' in group_names"

- name: enable prometheus-memcached-exporter service
  systemd: name=prometheus-memcached-exporter enabled=yes daemon_reload=yes state=started
  when: "'memcached' in group_names"

- name: open prometheus-node-exporter ipv4 port for monitoring.archlinux.org
  ansible.posix.firewalld: state=enabled permanent=true immediate=yes
        rich_rule="rule family=ipv4 source address={{ hostvars['monitoring.archlinux.org']['ipv4_address'] }} port protocol=tcp port={{ prometheus_exporter_port }} accept"
  when: "'prometheus' not in group_names"

- name: open gitlab exporter ipv4 port for monitoring.archlinux.org
  ansible.posix.firewalld: state=enabled permanent=true immediate=yes
        rich_rule="rule family=ipv4 source address={{ hostvars['monitoring.archlinux.org']['ipv4_address'] }} port protocol=tcp port={{ gitlab_runner_exporter_port }} accept"
  when: "'gitlab_runners' in group_names"

- name: open prometheus mysqld exporter ipv4 port for monitoring.archlinux.org
  ansible.posix.firewalld: state=enabled permanent=true immediate=yes
        rich_rule="rule family=ipv4 source address={{ hostvars['monitoring.archlinux.org']['ipv4_address'] }} port protocol=tcp port={{ prometheus_mysqld_exporter_port }} accept"
  when: "'mysql_servers' in group_names"

- name: open prometheus memcached exporter ipv4 port for monitoring.archlinux.org
  ansible.posix.firewalld: state=enabled permanent=true immediate=yes
        rich_rule="rule family=ipv4 source address={{ hostvars['monitoring.archlinux.org']['ipv4_address'] }} port protocol=tcp port={{ prometheus_memcached_exporter_port }} accept"
  when: "'memcached' in group_names"
