

# collectd client setup

# install pkg
- name: install collectd
  package: name=collectd state=present
  tags:
  - collectd
  when: ansible_distribution_major_version|int <= 7 and ansible_distribution == 'RedHat'

# install pkg
- name: install collectd
  dnf: name=collectd state=present
  tags:
  - collectd
  when: ansible_distribution_major_version|int > 7 and ansible_distribution == 'RedHat'

# install pkg
- name: install collectd
  dnf: name=collectd state=present
  tags:
  - collectd
  when: ansible_distribution_major_version|int >= 29 and ansible_distribution == 'Fedora'

# install collectd-disk (it was split out)
- name: install collectd-disk
  dnf: name=collectd-disk state=present
  tags:
  - collectd
  when: ansible_distribution_major_version|int > 7 and ansible_distribution == 'RedHat'

# install collectd-disk on F25+ (it was split out)
- name: install collectd-disk
  dnf: name=collectd-disk state=present
  tags:
  - collectd
  when: ansible_distribution_major_version|int > 24 and ansible_distribution == 'Fedora'

# install collected.conf
- name: /etc/collectd.conf
  template: src=collectd.conf.j2 dest=/etc/collectd.conf
  tags:
  - collectd
  notify:
  - restart collectd

# install collectd-network config
- name: /etc/collectd.d/network.conf
  template: src=network-client.conf.j2 dest=/etc/collectd.d/network.conf
  tags:
  - collectd
  notify:
  - restart collectd
  when: not inventory_hostname.startswith('log')

# install collectd-network config
- name: /etc/collectd.d/network.conf
  copy: src=network-server.conf dest=/etc/collectd.d/network.conf
  tags:
  - collectd
  notify:
  - restart collectd
  when: inventory_hostname.startswith('log')

# apache - localhost only - pretty much any apache server
- name: install collectd-apache (yum)
  package: state=present name=collectd-apache
  tags:
  - collectd
  notify:
  - restart collectd
  when: collectd_apache and ansible_distribution_major_version|int <= 7 and ansible_distribution == 'RedHat'

- name: install collectd-apache (dnf)
  dnf: state=present name=collectd-apache
  tags:
  - collectd
  notify:
  - restart collectd
  when: collectd_apache and ansible_distribution_major_version|int > 7 and ansible_distribution == 'RedHat'

- name: install collectd-apache (dnf)
  dnf: state=present name=collectd-apache
  tags:
  - collectd
  notify:
  - restart collectd
  when: collectd_apache and ansible_distribution_major_version|int >= 29 and ansible_distribution == 'Fedora'

- name: /etc/collectd/apache.conf
  copy: src=apache.conf dest=/etc/collectd.d/apache.conf
  tags:
  - collectd
  notify:
  - restart collectd
  when: collectd_apache

- name: Install libsemanage-python so we can set an sebool below
  package: name=libsemanage-python state=present
  tags:
  - collectd
  when: collectd_apache is defined and ansible_distribution_major_version|int <= 7 and ansible_distribution == 'RedHat'

- name: Let collectd talk to things over tcp
  seboolean: name=collectd_tcp_network_connect state=yes persistent=yes
  tags:
  - collectd
  ignore_errors: True
  notify:
  - restart collectd
  when: ( collectd_apache is defined ) and ansible_selinux.status != "disabled"

- name: enable collectd nfs module
  copy: src=nfs.conf dest=/etc/collectd.d/nfs.conf
  tags:
  - collectd
  notify:
  - restart collectd

# Three tasks for handling our (two) custom selinux modules.
- name: ensure a directory exists for our custom selinux module
  file: dest=/usr/share/collectd state=directory
  tags:
  - collectd
  - selinux

- name: copy over our general collectd selinux module
  copy: src=selinux/fi-collectd.pp dest=/usr/share/collectd/fi-collectd.pp
  register: ficgeneral_module
  tags:
  - collectd
  - selinux

# TODO: consider using selinux_modules from https://galaxy.ansible.com/linux-system-roles/selinux instead
- name: check to see what version is installed (if any)
  shell: "semodule -l -m | grep fi-collectd | cut -d: -f2"
  register: ficgeneral_installed_version
  check_mode: no
  changed_when: false
  tags:
  - collectd
  - selinux

# This cmd comes from the last example of the semodule man page
- name: check to see what version we have
  shell: /usr/libexec/selinux/hll/pp /usr/share/collectd/fi-collectd.pp | sha256sum | cut -d ' ' -f1
  register: ficgeneral_local_version
  check_mode: no
  changed_when: false
  tags:
  - collectd
  - selinux

- name: install our general collectd selinux module
  command: semodule -i /usr/share/collectd/fi-collectd.pp
  when: ficgeneral_module is changed or ficgeneral_installed_version != ficgeneral_local_version
  tags:
  - collectd
  - selinux

- name: copy over our pstorefs/collectd selinux module (rhel6 has no pstorefs)
  copy: src=selinux/fi-pstorefs.pp dest=/usr/share/collectd/fi-pstorefs.pp
  register: ficpstorefs_module
  tags:
  - collectd
  - selinux

- name: check to see if its even installed yet
  shell: semodule -l | grep fi-pstorefs | wc -l
  register: ficpstorefs_grep
  check_mode: no
  changed_when: "'0' in ficpstorefs_grep.stdout"
  tags:
  - collectd
  - selinux

- name: install our pstorefs/collectd selinux module
  command: semodule -i /usr/share/collectd/fi-pstorefs.pp
  when: (ficpstorefs_module is changed or ficpstorefs_grep is changed)
  tags:
  - collectd
  - selinux



# each of the below should move to a separate task list
# since they are odd-balls and one-offs

# fedmsg - busgateway## only
  # add /usr/share/collectd/fedmsg-types.db

# memcached - memcached only

# postgres - this is a conn check
## add /usr/share/collectd/pgconn-types.db

# openvpn - for bastion/openvpn gateways only

# mysql
## collectd-mysql

# haproxy
## add /usr/share/collectd/haproxy-types.db
## add socat pkg
##

# webproxy

# enable collectd
- name: enable collectd svc
  service: state=started enabled=true name=collectd
  tags:
  - collectd
