---
# file: roles/netdata/tasks/main.yml

- name: "NetData Role"
  set_fact: role_netdata_started=true
  tags: always

- block:

  - name: "Install required packages"
    apt:
      pkg={{ item }}
      state=installed
      update_cache=yes
    with_items:
      - zlib1g-dev
      - gcc
      - make
      - git
      - autoconf
      - autogen
      - automake
      - pkg-config
      - uuid-dev
      - python-dev
      - python-yaml
      #FireQOS available from 15.04
      #- firehol

  - name: "Remove existing components"
    file:
      path: '{{ item }}'
      state: 'absent'
    with_items:
      - '/opt/netdata'
      - '/etc/netdata'
      - '/etc/init.d/netdata'
      - '/etc/logrotate.d/netdata'
    when: netdata_force_reset

  - name: "Ensure Directory"
    file:
      path='/etc/netdata/python.d'
      state='directory'

  - include: install.yml

  - name: "Add User to Admins"
    user:
      name: 'netdata'
      groups: 'adm'
      append: yes

  - name: "Configure Registry"
    ini_file:
      dest='/etc/netdata/netdata.conf'
      create=yes
      section='registry'
      option='{{ item.option }}'
      value='{{ item.value }}'
    with_items:
      - option: 'enabled'
        value: '{{ (netdata_registry|default("pmon1") == inventory_hostname)|ternary("yes", "no") }}'
      - option: 'registry to announce'
        value: 'http://{{ netdata_registry|default("pmon1") }}:19999'
    notify:
      - "Restart NetData"

  - name: "Configure NetData"
    template:
      src='{{ item }}'
      dest='/etc/netdata/{{ item }}'
      owner='root'
      group='root'
      mode=644
      backup=yes
    with_items:
      - 'health_alarm_notify.conf'
    notify:
      - "Restart NetData"

  - name: "Patch Startup Script"
    lineinfile:
      dest: '/etc/init.d/netdata'
      regexp: 'killproc -p \$\{PIDFILE\} \$DAEMON_PATH/\$DAEMON'
      line: '        killproc -p ${PIDFILE} $DAEMON_PATH/$DAEMON && sleep 1'
    when: ansible_lsb.major_release == '12'

  - block:

    - name: "Enable Apache Status Module"
      apache2_module:
        name=status
        state=present
      notify: "Restart Apache"

    - name: "Apache Status Site"
      template:
        src=apache.conf
        dest=/etc/apache2/sites-available/status{{ apache_conf_ext|default('.conf') }}
        owner=root
        group=root
        mode=0644
      notify: "Restart Apache"

    - name: "Enable Apache Status Site"
      command: a2ensite status creates=/etc/apache2/sites-enabled/status{{ apache_conf_ext|default('.conf') }}
      notify: "Restart Apache"

    when: groups['webserver'] is defined and inventory_hostname in groups['webserver']

  - name: "Configure Plugin: MySQL"
    replace:
      dest: '/etc/netdata/python.d/mysql.conf'
      regexp: '#[\s]*pass[\s]*:[\s]*'''''
      replace: 'pass     : ''{{ mysql_root_password|default("root") }}'''
    notify:
      - "Restart NetData"

  - name: "Check if KSM is available"
    stat: path='/sys/kernel/mm/ksm'
    register: ksm

  # Note: copy module doesn't work here because parent directory /sys
  # is not writable, not even by root
  - name: "Configure KSM"
    shell: echo {{ item.value }} >/sys/kernel/mm/ksm/{{ item.file }}
    with_items:
      - file: 'run'
        value: '1'
      - file: 'sleep_millisecs'
        value: '1000'
    when: ksm.stat.exists
    notify:
      - "Restart NetData"

  when: '"netdata" not in excluded_roles'
