---
# host-textfile-exporters role
# 管理 Docker/systemd Prometheus textfile exporters，補齊 Docker Compose 與 host-level runner 的監控盲區。

- name: "host textfile exporters | 確認目錄存在"
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    owner: "{{ host_textfile_user }}"
    group: "{{ host_textfile_user }}"
    mode: "0755"
  loop:
    - "{{ host_textfile_script_dir }}"
    - "{{ host_textfile_dir }}"
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 Docker stats 匯出器"
  ansible.builtin.copy:
    src: "{{ host_textfile_docker_stats_src }}"
    dest: "{{ host_textfile_script_dir }}/docker-stats-textfile-exporter.py"
    owner: "{{ host_textfile_user }}"
    group: "{{ host_textfile_user }}"
    mode: "0755"
  when: host_textfile_manage_docker_stats
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 Docker stats cron"
  ansible.builtin.cron:
    name: "AWOOOI Docker stats textfile exporter"
    user: "{{ host_textfile_user }}"
    minute: "{{ host_textfile_docker_cron_minute }}"
    job: >-
      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      AIOPS_HOST_LABEL={{ host_textfile_host_label }}
      NODE_EXPORTER_TEXTFILE_DIR={{ host_textfile_dir }}
      {{ host_textfile_script_dir }}/docker-stats-textfile-exporter.py
      >/tmp/awoooi-docker-stats-textfile-exporter.cron.log 2>&1
  when: host_textfile_manage_docker_stats
  tags: textfile_exporters

- name: "host textfile exporters | 立即刷新 Docker stats 指標"
  ansible.builtin.command:
    cmd: "{{ host_textfile_script_dir }}/docker-stats-textfile-exporter.py"
  environment:
    AIOPS_HOST_LABEL: "{{ host_textfile_host_label }}"
    NODE_EXPORTER_TEXTFILE_DIR: "{{ host_textfile_dir }}"
  become: true
  become_user: "{{ host_textfile_user }}"
  changed_when: false
  when:
    - host_textfile_manage_docker_stats
    - not ansible_check_mode
  tags: textfile_exporters

- name: "host textfile exporters | 驗證 Docker stats metric 存在"
  ansible.builtin.command:
    cmd: "grep -q '^docker_container_cpu_cores{' {{ host_textfile_dir }}/docker_stats.prom"
  become: true
  become_user: "{{ host_textfile_user }}"
  changed_when: false
  when:
    - host_textfile_manage_docker_stats
    - not ansible_check_mode
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 storage health 匯出器"
  ansible.builtin.copy:
    src: "{{ host_textfile_storage_health_src }}"
    dest: "{{ host_textfile_script_dir }}/storage-health-textfile-exporter.py"
    owner: "{{ host_textfile_user }}"
    group: "{{ host_textfile_user }}"
    mode: "0755"
  when: host_textfile_manage_storage_health
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 storage health cron"
  ansible.builtin.cron:
    name: "AWOOOI storage health textfile exporter"
    user: "{{ host_textfile_user }}"
    minute: "{{ host_textfile_storage_cron_minute }}"
    job: >-
      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      AIOPS_HOST_LABEL={{ host_textfile_host_label }}
      NODE_EXPORTER_TEXTFILE_DIR={{ host_textfile_dir }}
      {{ host_textfile_script_dir }}/storage-health-textfile-exporter.py
      >/tmp/awoooi-storage-health-textfile-exporter.cron.log 2>&1
  when: host_textfile_manage_storage_health
  tags: textfile_exporters

- name: "host textfile exporters | 立即刷新 storage health 指標"
  ansible.builtin.command:
    cmd: "{{ host_textfile_script_dir }}/storage-health-textfile-exporter.py"
  environment:
    AIOPS_HOST_LABEL: "{{ host_textfile_host_label }}"
    NODE_EXPORTER_TEXTFILE_DIR: "{{ host_textfile_dir }}"
  become: true
  become_user: "{{ host_textfile_user }}"
  changed_when: false
  when:
    - host_textfile_manage_storage_health
    - not ansible_check_mode
  tags: textfile_exporters

- name: "host textfile exporters | 驗證 storage health metric 存在"
  ansible.builtin.command:
    cmd: "grep -q '^awoooi_host_storage_monitor_up{' {{ host_textfile_dir }}/storage_health.prom"
  become: true
  become_user: "{{ host_textfile_user }}"
  changed_when: false
  when:
    - host_textfile_manage_storage_health
    - not ansible_check_mode
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 backup health 匯出器"
  ansible.builtin.copy:
    src: "{{ host_textfile_backup_health_src }}"
    dest: "{{ host_textfile_script_dir }}/backup-health-textfile-exporter.py"
    owner: "{{ host_textfile_user }}"
    group: "{{ host_textfile_user }}"
    mode: "0755"
  when: host_textfile_manage_backup_health
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 backup health cron"
  ansible.builtin.cron:
    name: "AWOOOI backup health textfile exporter"
    user: "{{ host_textfile_user }}"
    minute: "{{ host_textfile_backup_cron_minute }}"
    job: >-
      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      AIOPS_HOST_LABEL={{ host_textfile_host_label }}
      NODE_EXPORTER_TEXTFILE_DIR={{ host_textfile_dir }}
      {{ host_textfile_script_dir }}/backup-health-textfile-exporter.py
      >/tmp/awoooi-backup-health-textfile-exporter.cron.log 2>&1
  when: host_textfile_manage_backup_health
  tags: textfile_exporters

- name: "host textfile exporters | 立即刷新 backup health 指標"
  ansible.builtin.command:
    cmd: "{{ host_textfile_script_dir }}/backup-health-textfile-exporter.py"
  environment:
    AIOPS_HOST_LABEL: "{{ host_textfile_host_label }}"
    NODE_EXPORTER_TEXTFILE_DIR: "{{ host_textfile_dir }}"
  become: true
  become_user: "{{ host_textfile_user }}"
  changed_when: false
  when:
    - host_textfile_manage_backup_health
    - not ansible_check_mode
  tags: textfile_exporters

- name: "host textfile exporters | 驗證 backup health metric 存在"
  ansible.builtin.command:
    cmd: "grep -q '^awoooi_backup_health_monitor_up{' {{ host_textfile_dir }}/backup_health.prom"
  become: true
  become_user: "{{ host_textfile_user }}"
  changed_when: false
  when:
    - host_textfile_manage_backup_health
    - not ansible_check_mode
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 runaway process 匯出器"
  ansible.builtin.copy:
    src: "{{ host_textfile_runaway_process_src }}"
    dest: "{{ host_textfile_script_dir }}/host-runaway-process-exporter.py"
    owner: "{{ host_textfile_user }}"
    group: "{{ host_textfile_user }}"
    mode: "0755"
  when: host_textfile_manage_runaway_process
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 runaway process gated 修復器"
  ansible.builtin.copy:
    src: "{{ host_textfile_runaway_process_remediation_src }}"
    dest: "{{ host_textfile_script_dir }}/host-runaway-process-remediation.py"
    owner: "{{ host_textfile_user }}"
    group: "{{ host_textfile_user }}"
    mode: "0755"
  when: host_textfile_manage_runaway_process
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 runaway process cron"
  ansible.builtin.cron:
    name: "AWOOOI runaway process textfile exporter"
    user: "{{ host_textfile_user }}"
    minute: "{{ host_textfile_runaway_process_cron_minute }}"
    job: >-
      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      AIOPS_HOST_LABEL={{ host_textfile_host_label }}
      NODE_EXPORTER_TEXTFILE_DIR={{ host_textfile_dir }}
      AIOPS_RUNAWAY_PROCESS_MIN_AGE_SECONDS={{ host_textfile_runaway_process_min_age_seconds }}
      AIOPS_RUNAWAY_PROCESS_MIN_CPU_PERCENT={{ host_textfile_runaway_process_min_cpu_percent }}
      {{ host_textfile_script_dir }}/host-runaway-process-exporter.py
      >/tmp/awoooi-host-runaway-process-exporter.cron.log 2>&1
  when: host_textfile_manage_runaway_process
  tags: textfile_exporters

- name: "host textfile exporters | 立即刷新 runaway process 指標"
  ansible.builtin.command:
    cmd: "{{ host_textfile_script_dir }}/host-runaway-process-exporter.py"
  environment:
    AIOPS_HOST_LABEL: "{{ host_textfile_host_label }}"
    NODE_EXPORTER_TEXTFILE_DIR: "{{ host_textfile_dir }}"
    AIOPS_RUNAWAY_PROCESS_MIN_AGE_SECONDS: "{{ host_textfile_runaway_process_min_age_seconds }}"
    AIOPS_RUNAWAY_PROCESS_MIN_CPU_PERCENT: "{{ host_textfile_runaway_process_min_cpu_percent }}"
  become: true
  become_user: "{{ host_textfile_user }}"
  changed_when: false
  when:
    - host_textfile_manage_runaway_process
    - not ansible_check_mode
  tags: textfile_exporters

- name: "host textfile exporters | 驗證 runaway process metric 存在"
  ansible.builtin.command:
    cmd: "grep -q '^awoooi_host_runaway_process_monitor_up{' {{ host_textfile_dir }}/host_runaway_process.prom"
  become: true
  become_user: "{{ host_textfile_user }}"
  changed_when: false
  when:
    - host_textfile_manage_runaway_process
    - not ansible_check_mode
  tags: textfile_exporters

- name: "host textfile exporters | 探測 systemd units"
  ansible.builtin.shell: |
    set -o pipefail
    systemctl list-unit-files '{{ host_textfile_systemd_unit_glob }}' --no-legend --no-pager 2>/dev/null | awk '{print $1}'
  args:
    executable: /bin/bash
  register: host_textfile_systemd_units_raw
  changed_when: false
  failed_when: false
  when:
    - host_textfile_manage_systemd_units
    - host_textfile_systemd_unit_glob | length > 0
  tags: textfile_exporters

- name: "host textfile exporters | 設定 systemd unit 清單"
  ansible.builtin.set_fact:
    host_textfile_effective_systemd_units: >-
      {{
        (
          host_textfile_systemd_units
          + (host_textfile_systemd_units_raw.stdout_lines | default([]))
        )
        | unique
        | list
      }}
  when: host_textfile_manage_systemd_units
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 systemd units 匯出器"
  ansible.builtin.copy:
    src: "{{ host_textfile_systemd_units_src }}"
    dest: "{{ host_textfile_script_dir }}/systemd-units-textfile-exporter.py"
    owner: "{{ host_textfile_user }}"
    group: "{{ host_textfile_user }}"
    mode: "0755"
  when:
    - host_textfile_manage_systemd_units
    - host_textfile_effective_systemd_units | default([]) | length > 0
  tags: textfile_exporters

- name: "host textfile exporters | 安裝 systemd units cron"
  ansible.builtin.cron:
    name: "AWOOOI systemd units textfile exporter"
    user: "{{ host_textfile_user }}"
    minute: "{{ host_textfile_systemd_cron_minute }}"
    job: >-
      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      AIOPS_HOST_LABEL={{ host_textfile_host_label }}
      NODE_EXPORTER_TEXTFILE_DIR={{ host_textfile_dir }}
      AIOPS_SYSTEMD_UNITS={{ host_textfile_effective_systemd_units | join(',') }}
      {{ host_textfile_script_dir }}/systemd-units-textfile-exporter.py
      >/tmp/awoooi-systemd-units-textfile-exporter.cron.log 2>&1
  when:
    - host_textfile_manage_systemd_units
    - host_textfile_effective_systemd_units | default([]) | length > 0
  tags: textfile_exporters

- name: "host textfile exporters | 立即刷新 systemd units 指標"
  ansible.builtin.command:
    cmd: "{{ host_textfile_script_dir }}/systemd-units-textfile-exporter.py"
  environment:
    AIOPS_HOST_LABEL: "{{ host_textfile_host_label }}"
    NODE_EXPORTER_TEXTFILE_DIR: "{{ host_textfile_dir }}"
    AIOPS_SYSTEMD_UNITS: "{{ host_textfile_effective_systemd_units | join(',') }}"
  become: true
  become_user: "{{ host_textfile_user }}"
  changed_when: false
  when:
    - host_textfile_manage_systemd_units
    - host_textfile_effective_systemd_units | default([]) | length > 0
    - not ansible_check_mode
  tags: textfile_exporters

- name: "host textfile exporters | 驗證 systemd unit metric 存在"
  ansible.builtin.command:
    cmd: "grep -q '^systemd_unit_info{' {{ host_textfile_dir }}/systemd_units.prom"
  become: true
  become_user: "{{ host_textfile_user }}"
  changed_when: false
  when:
    - host_textfile_manage_systemd_units
    - host_textfile_effective_systemd_units | default([]) | length > 0
    - not ansible_check_mode
  tags: textfile_exporters
