Merge pull request #8 from simonpasquier/harden-rabbitmq-plugin

Make the RabbitMQ collectd plugin more robust
diff --git a/collectd/client.sls b/collectd/client.sls
index a5b2128..5937f04 100644
--- a/collectd/client.sls
+++ b/collectd/client.sls
@@ -71,6 +71,22 @@
 {%- endif %}
 {%- endfor %}
 
+{%- set remote_plugin = {} %}
+
+{%- if client.remote_collector %}
+
+{%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').iteritems() %}
+
+{%- if node_grains.collectd is defined %}
+
+{%- set remote_plugin = salt['grains.filter_by']({'default': remote_plugin}, merge=node_grains.collectd.get('remote_plugin', {})) %}
+
+{%- endif %}
+
+{%- endfor %}
+
+{%- endif %}
+
 collectd_client_grain:
   file.managed:
   - name: /etc/salt/grains.d/collectd
@@ -94,7 +110,7 @@
 
 {%- for plugin_name, plugin in service_grains.collectd.local_plugin.iteritems() %}
 
-{%- if (plugin.get('execution', 'local') == 'local' or client.remote_collector) and plugin.get('plugin', 'native') not in ['python'] %}
+{%- if plugin.get('plugin', 'native') not in ['python'] %}
 
 {{ client.config_dir }}/{{ plugin_name }}.conf:
   file.managed:
@@ -119,6 +135,37 @@
 
 {%- endfor %}
 
+{%- if client.remote_collector %}
+
+{%- for plugin_name, plugin in remote_plugin.iteritems() %}
+
+{%- if plugin.get('plugin', 'native') not in ['python'] %}
+
+{{ client.config_dir }}/{{ plugin_name }}.conf:
+  file.managed:
+  {%- if plugin.template is defined %}
+  - source: salt://{{ plugin.template }}
+  - template: jinja
+  - defaults:
+    plugin: {{ plugin|yaml }}
+  {%- else %}
+  - contents: "<LoadPlugin {{ plugin.plugin }}>\n  Globals false\n</LoadPlugin>\n"
+  {%- endif %}
+  - user: root
+  - mode: 660
+  - require:
+    - file: collectd_client_conf_dir
+  - require_in:
+    - file: collectd_client_conf_dir_clean
+  - watch_in:
+    - service: collectd_service
+
+{%- endif %}
+
+{%- endfor %}
+
+{%- endif %}
+
 {%- if client.file_logging %}
 
 /etc/collectd/conf.d/00_collectd_logfile.conf:
@@ -144,7 +191,8 @@
   - group: root
   - mode: 660
   - defaults:
-      plugin: {{ service_grains.collectd.local_plugin|yaml }}
+      local_plugin: {{ service_grains.collectd.local_plugin|yaml }}
+      remote_plugin: {{ remote_plugin|yaml }}
   - watch_in:
     - service: collectd_service
   - require:
@@ -189,6 +237,7 @@
   - mode: 640
   - defaults:
     service_grains: {{ service_grains|yaml }}
+    remote_plugin: {{ remote_plugin|yaml }}
   - require:
     - file: collectd_client_conf_dir
   - require_in:
diff --git a/collectd/files/collectd.conf b/collectd/files/collectd.conf
index fc48a19..c839530 100644
--- a/collectd/files/collectd.conf
+++ b/collectd/files/collectd.conf
@@ -15,7 +15,11 @@
 # Global settings for the daemon.                                            #
 ##############################################################################
 
+{%- if client.use_fqdn %}
 Hostname "{{ system.name }}.{{ system.domain }}"
+{%- else %}
+Hostname "{{ system.name }}"
+{%- endif %}
 FQDNLookup false
 
 #BaseDir "/var/lib/collectd"
@@ -882,10 +886,16 @@
 Include "/etc/collectd/conf.d/00_collectd_logfile.conf"
 {%- endif %}
 {%- for plugin_name, plugin in service_grains.collectd.local_plugin.iteritems() %}
-{%- if (plugin.get('execution', 'local') == 'local' or client.remote_collector) and plugin.get('plugin', 'native') not in ['python'] %}
+{%- if plugin.get('plugin', 'native') not in ['python'] %}
 Include "{{ client.config_dir }}/{{ plugin_name }}.conf"
 {%- endif %}
 {%- endfor %}
+{%- if client.remote_collector %}
+{%- for plugin_name, plugin in remote_plugin.iteritems() %}
+{%- if plugin.get('plugin', 'native') not in ['python'] %}
+{%- endif %}
+{%- endfor %}
+{%- endif %}
 Include "{{ client.config_dir }}/collectd_python.conf"
 {%- for backend_name, backend in client.backend.iteritems() %}
 Include "{{ client.config_dir }}/collectd_writer_{{ backend_name }}.conf"
diff --git a/collectd/files/collectd_python.conf b/collectd/files/collectd_python.conf
index 6142345..7a279e7 100644
--- a/collectd/files/collectd_python.conf
+++ b/collectd/files/collectd_python.conf
@@ -1,3 +1,4 @@
+{%- from "collectd/map.jinja" import client with context -%}
 <LoadPlugin python>
   Globals false
 </LoadPlugin>
@@ -7,10 +8,18 @@
   LogTraces false
   Interactive false
 
-  {%- for plugin_name, plugin in plugin.iteritems() %}
-  {%- if (plugin.get('execution', 'local') == 'local' or client.remote_collector) and plugin.get('plugin', 'native') == 'python' %}
+  {%- for plugin_name, plugin in local_plugin.iteritems() %}
+  {%- if plugin.get('plugin', 'native') == 'python' %}
   {%- include plugin.template %}
   {%- endif %}
   {%- endfor %}
 
+  {%- if client.remote_collector %}
+  {%- for plugin_name, plugin in remote_plugin.iteritems() %}
+  {%- if plugin.get('plugin', 'native') == 'python' %}
+  {%- include plugin.template %}
+  {%- endif %}
+  {%- endfor %}
+  {%- endif %}
+
 </Plugin>
diff --git a/collectd/map.jinja b/collectd/map.jinja
index dd0cdf7..c25b5cb 100644
--- a/collectd/map.jinja
+++ b/collectd/map.jinja
@@ -8,15 +8,17 @@
         'read_interval': 60,
         'file_logging': True,
         'remote_collector': False
+        'use_fqdn': True,
     },
     'Debian': {
-        'pkgs': ['collectd-core', 'snmp', 'python-yaml'],
+        'pkgs': ['collectd-core', 'snmp', 'python-yaml', 'python-simplejson'],
         'service': 'collectd',
         'config_file': '/etc/collectd/collectd.conf',
         'config_dir': '/etc/collectd/conf.d',
         'read_interval': 60,
         'file_logging': True,
         'remote_collector': False
+        'use_fqdn': True,
     },
     'RedHat': {
         'pkgs': ['collectd', 'collectd-ping', 'net-snmp', 'PyYAML'],
@@ -26,5 +28,6 @@
         'read_interval': 60,
         'file_logging': True,
         'remote_collector': False
+        'use_fqdn': True,
     },
 }, merge=salt['pillar.get']('collectd:client')) %}
diff --git a/metadata/service/client/init.yml b/metadata/service/client/init.yml
index cbf9f47..6ef1257 100644
--- a/metadata/service/client/init.yml
+++ b/metadata/service/client/init.yml
@@ -7,3 +7,4 @@
     client:
       enabled: true
       read_interval: 60
+      use_fqdn: true