---
# Application that populates InfluxDB with metric data.
# https://docs.influxdata.com/telegraf/v1.17/introduction/getting-started/

# Variables #

- name: General | Telegraf | Variables 1/4
  set_fact:
    telegraf_tar: telegraf-1.18.0_linux_amd64.tar.gz
    telegraf_path: /usr/local/bin
    telegraf_config_path: /usr/local/etc
    telegraf_user: "{{ user_ansible.name }}"
    telegraf_log: "{{ user_ansible.home }}/telegraf.log"

- name: General | Telegraf | Variables 2/4
  set_fact:
    telegraf_exec: "{{ telegraf_path }}/telegraf"
    telegraf_config: "{{ telegraf_config_path }}/telegraf.conf"

- name: General | Telegraf | Variables 3/4
  set_fact:
    telegraf_cmd: "{{ telegraf_exec }} --config {{ telegraf_config }} >> {{ telegraf_log }} 2>&1"

- name: General | Telegraf | Variables 4/4
  set_fact:
    telegraf_watcher: "killall telegraf; {{ telegraf_cmd }}"

- name: General | Telegraf | Variables | Linux
  set_fact:
    telegraf_input_temp: "[[inputs.temp]]"
  when: ansible_system == "Linux"

- name: General | Telegraf | Variables | FreeBSD
  set_fact:
    telegraf_input_temp: ""
  when: ansible_system == "FreeBSD"

# Dependencies #

- name: General | Telegraf | Pre-Reqs
  package:
    name: wget
  when: ansible_system == "Linux"

# Install #

- name: General | Telegraf | Linux | Install
  shell: "{{ item }}"
  args:
    chdir: "{{ ansible_env.HOME }}/Downloads/"
    warn: false
  loop:
    - mkdir -p {{ telegraf_path }}
    - mkdir -p {{ telegraf_config_path }}
    - wget --no-check-certificate "https://dl.influxdata.com/telegraf/releases/{{ telegraf_tar }}"
    - tar xvf {{ telegraf_tar }}
    - mv {{ telegraf_tar }} ~/TRASH/
    - mv telegraf*/usr/bin/telegraf {{ telegraf_exec }}
    - rm -r telegraf*
  when: ansible_system == "Linux"

- name: General | Telegraf | FreeBSD | Install 1/2
  shell: /usr/sbin/pwd_mkdb -p /etc/master.passwd
  when: ansible_system == "FreeBSD"

- name: General | Telegraf | FreeBSD | Install 2/2
  package:
    name: telegraf
  when: ansible_system == "FreeBSD"

# Configuration #

- name: General | Telegraf | Config 1/2
  shell: mv {{ telegraf_config }} ~/TRASH/
  ignore_errors: yes

- name: General | Telegraf | Config 2/2
  blockinfile:
    path: "{{ telegraf_config }}"
    block: |
      [global_tags]
        # dc = "us-east-1" # will tag all metrics with dc=us-east-1
        # rack = "1a"
        ## Environment variables can be used as tags, and throughout the config file
        # user = "$USER"
      
      [agent]
        interval = "10s"
        metric_batch_size = 1000
        metric_buffer_limit = 10000
        collection_jitter = "0s"
        flush_interval = "10s"
        flush_jitter = "0s"
        precision = ""
        # debug = false
        # quiet = false
        # logtarget = "file"
        # logfile = ""
        # logfile_rotation_interval = "0d"
        # logfile_rotation_max_size = "0MB"
        # logfile_rotation_max_archives = 5
        hostname = ""
        omit_hostname = false

      [[outputs.influxdb]]
        urls = ["http://influxdb.hyperling.com:8086"]
        database = "main"
        # database_tag = ""
        # exclude_database_tag = false
        # skip_database_creation = false
        # retention_policy = ""
        # retention_policy_tag = ""
        # exclude_retention_policy_tag = false
        # write_consistency = "any"
        # timeout = "5s"
        # username = "telegraf"
        # password = "metricsmetricsmetricsmetrics"
        # user_agent = "telegraf"
        # udp_payload = "512B"
        # tls_ca = "/etc/telegraf/ca.pem"
        # tls_cert = "/etc/telegraf/cert.pem"
        # tls_key = "/etc/telegraf/key.pem"
        # insecure_skip_verify = false
        # http_proxy = "http://corporate.proxy:3128"
        # http_headers = {"X-Special-Header" = "Special-Value"}
        # content_encoding = "gzip"
        # influx_uint_support = false

      [[inputs.cpu]]
        percpu = false
        totalcpu = true
        collect_cpu_time = false
        report_active = false

      [[inputs.disk]]
        # mount_points = ["/"]
        ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs", "fdescfs"]

      [[inputs.diskio]]
        # devices = ["sda", "sdb", "vd*"]
        # skip_serial_number = false
        # device_tags = ["ID_FS_TYPE", "ID_FS_USAGE"]
        # name_templates = ["$ID_FS_LABEL","$DM_VG_NAME/$DM_LV_NAME"]

      [[inputs.kernel]]
      
      [[inputs.mem]]
      
      [[inputs.processes]]
      
      [[inputs.swap]]
      
      [[inputs.system]]
        # fielddrop = ["uptime_format"]

      {{ telegraf_input_temp }}

    marker: '# {mark} MANAGED BY ANSIBLE - telegraf.yml'
    state: present
    create: yes

# Run #

- name: General | Telegraf | Schedule
  cron:
    user: "{{ telegraf_user }}"
    name: "{{ item.name }}"
    job: "{{ item.command }}"
    special_time: "{{ item.freq }}"
    state: present
    disabled: no
  loop:
    - { "name": "Telegraf Reboot Job"    , "freq": "reboot", "command": "{{ telegraf_cmd }}"}
    - { "name": "Telegraf Keep-Alive Job", "freq": "hourly", "command": "{{ telegraf_watcher }}"}