---

- name: Install prerequisite apt packages on target
  apt:
    name:
      - sysstat
      - curl
    state: present

- name: Install prerequisite yum packages on AWX
  delegate_to: 127.0.0.1
  yum:
    name:
      - bind-utils
    state: present

- name: Install prerequisite pip packages on AWX
  delegate_to: 127.0.0.1
  pip:
    name:
      - dnspython
    state: present

- name: Calculate MAU value
  shell: |
    curl -s localhost:9000 | grep "^synapse_admin_mau_current "
  register: awx_mau_stat
  no_log: true

- name: Calculate CPU usage statistics
  shell: iostat -c
  register: awx_cpu_usage_stat
  no_log: true

- name: Calculate RAM usage statistics
  shell: free -mh
  register: awx_ram_usage_stat
  no_log: true

- name: Calculate free disk space
  shell: df -h
  register: awx_disk_space_stat
  no_log: true

- name: Calculate size of Synapse database
  shell: du -sh /matrix/postgres/data
  register: awx_db_size_stat
  no_log: true

- name: Calculate size of local media repository
  shell: du -sh /matrix/synapse/storage/media-store/local*
  register: awx_local_media_size_stat
  async: 600
  ignore_errors: true
  no_log: true

- name: Calculate size of remote media repository
  shell: du -sh /matrix/synapse/storage/media-store/remote*
  register: awx_remote_media_size_stat
  async: 600
  ignore_errors: true
  no_log: true

- name: Calculate docker container statistics
  shell: docker stats --all --no-stream
  register: awx_docker_stats
  ignore_errors: true
  no_log: true

- name: Print size of remote media repository
  debug:
    msg: "{{ awx_remote_media_size_stat.stdout.split('\n') }}"
  when: awx_remote_media_size_stat is defined

- name: Print size of local media repository
  debug:
    msg: "{{ awx_local_media_size_stat.stdout.split('\n') }}"
  when: awx_local_media_size_stat is defined

- name: Print size of Synapse database
  debug:
    msg: "{{ awx_db_size_stat.stdout.split('\n') }}"
  when: awx_db_size_stat is defined

- name: Print free disk space
  debug:
    msg: "{{ awx_disk_space_stat.stdout.split('\n') }}"
  when: awx_disk_space_stat is defined

- name: Print RAM usage statistics
  debug:
    msg: "{{ awx_ram_usage_stat.stdout.split('\n') }}"
  when: awx_ram_usage_stat is defined

- name: Print CPU usage statistics
  debug:
    msg: "{{ awx_cpu_usage_stat.stdout.split('\n') }}"
  when: awx_cpu_usage_stat is defined

- name: Print MAU value
  debug:
    msg: "{{ awx_mau_stat.stdout.split('\n') }}"
  when: awx_mau_stat is defined

- name: Print docker container statistics
  debug:
    msg: "{{ awx_docker_stats.stdout.split('\n') }}"
  when: awx_docker_stats is defined