Fix exporters configuration to be generic

Change-Id: If5cf3662f96311a5f37055c0f62934ba5407a978
diff --git a/metadata/service/exporters/jmx.yml b/metadata/service/exporters/jmx.yml
index ba23709..50c8df8 100644
--- a/metadata/service/exporters/jmx.yml
+++ b/metadata/service/exporters/jmx.yml
@@ -6,9 +6,10 @@
   prometheus:
     exporters:
       jmx:
-        enabled: true
         packages:
           - jmx-exporter
-        bind:
-          address: 0.0.0.0
-          port: ${_param:prometheus_exporter_jmx_port}
+        services:
+          cassandra:
+            bind:
+              address: 0.0.0.0
+              port: ${_param:prometheus_exporter_jmx_port}
diff --git a/metadata/service/exporters/libvirt.yml b/metadata/service/exporters/libvirt.yml
index 9f277ea..2c910bb 100644
--- a/metadata/service/exporters/libvirt.yml
+++ b/metadata/service/exporters/libvirt.yml
@@ -6,9 +6,10 @@
   prometheus:
     exporters:
       libvirt:
-        enabled: true
         packages:
           - libvirt-exporter
-        bind:
-          address: 0.0.0.0
-          port: ${_param:prometheus_exporter_libvirt_port}
+        services:
+          qemu:
+            bind:
+              address: 0.0.0.0
+              port: ${_param:prometheus_exporter_libvirt_port}
diff --git a/prometheus/_exporters_config.sls b/prometheus/_exporters_config.sls
new file mode 100644
index 0000000..35b9ff7
--- /dev/null
+++ b/prometheus/_exporters_config.sls
@@ -0,0 +1,42 @@
+    {%- if exporters is defined %}
+      {%- if new_exporters_cfg is defined %}
+        {%- do salt['defaults.merge'](exporters, new_exporters_cfg['exporters']) %}
+      {%- endif %}
+      {%- set host = grains.get('host', "") %}
+      {%- set host_ip_address = grains['fqdn_ip4'][0] %}
+      {%- load_yaml as svc_configs %}
+        {%- for exporter, parameters in exporters.iteritems() %}
+          {%- if parameters.get('enabled', False) %}
+            {%- for svc, svc_parameters in parameters.get('services', {}).iteritems() %}
+              {%- if svc_parameters.get('enabled', False) %}
+                {%- if svc_parameters.bind is defined %}
+                  {%- if svc_parameters.bind.get('address', '0.0.0.0') == '0.0.0.0' %}
+                    {%- set address = host_ip_address %}
+                  {%- else %}
+                    {%- set address = svc_parameters.bind.address %}
+                  {%- endif %}
+                {%- endif %}
+              {%- endif %}
+              {%- if address is defined and svc_parameters.bind.port is defined %}
+      {{ exporter }}_{{ svc }}_exporter:
+        endpoint:
+          - address: {{ address }}
+            port: {{ svc_parameters.bind.port }}
+                {%- if host is defined and host|length > 0 %}
+        metric_relabel:
+          - regex: {{ address }}:{{ svc_parameters.bind.port }}
+            replacement: {{ host }}
+            source_labels: "instance"
+            target_label: "host"
+                {%- endif %}
+              {%- endif %}
+            {%- endfor %}
+          {%- endif %}
+        {%- endfor %}
+      {%- endload %}
+      {%- if svc_configs %}
+  target:
+    static:
+{{ svc_configs|yaml(False)|indent(6, True) }}
+      {%- endif %}
+    {%- endif %}
diff --git a/prometheus/exporters.sls b/prometheus/exporters.sls
index 8baba39..c44126f 100644
--- a/prometheus/exporters.sls
+++ b/prometheus/exporters.sls
@@ -1,5 +1,4 @@
 {% from "prometheus/map.jinja" import exporters with context %}
-
 {%- for exporter, parameters in exporters.iteritems() %}
   {%- if parameters.get('enabled', False) %}
     {%- if parameters.get('packages', False) %}
diff --git a/prometheus/map.jinja b/prometheus/map.jinja
index 7b6887a..5a66470 100644
--- a/prometheus/map.jinja
+++ b/prometheus/map.jinja
@@ -16,3 +16,4 @@
   'default': {
   },
 }, merge=salt['pillar.get']('prometheus:exporters')) %}
+{%- do salt['defaults.merge'](exporters, grains.get('prometheus', {}).get('exporters', {}))  %}
diff --git a/prometheus/meta/salt.yml b/prometheus/meta/salt.yml
index a886376..35c49b0 100644
--- a/prometheus/meta/salt.yml
+++ b/prometheus/meta/salt.yml
@@ -1,6 +1,6 @@
 grain:
   {%- if pillar.get('prometheus', {}).get('collector') %}
-    {%- from "prometheus/map.jinja" import server as prometheus_server with context %}
+    {%- from "prometheus/map.jinja" import exporters,server as prometheus_server with context %}
   prometheus:
     {%- set service_grains = {'prometheus': {'server': {'alert': {}, 'recording': {}, 'target': {}}}} %}
     {%- for service_name, service in pillar.items() %}