From 229739377dce13e91713f1d86da8f0e190f0693f Mon Sep 17 00:00:00 2001
From: Kevin Carter <kevin.carter@rackspace.com>
Date: Thu, 8 Dec 2016 17:02:41 -0600
Subject: [PATCH] Added kvm_virsh to telegraf plugins

The KVM virsh plugin already existed however the setup was not using the
new playbook plugin system. This change moves the kvm vish plugin into
that system and updates the plugin to use the influxdb line format
instead of the json format which was recently deprecated.

Change-Id: Ib23a0a231044389aab5669dc0c467175cd220423
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
---
 cluster_metrics/playbook-influx-telegraf.yml         |  8 ++++++++
 .../telegraf-plugins}/kvm_virsh.py                   | 12 ++++++++----
 2 files changed, 16 insertions(+), 4 deletions(-)
 rename cluster_metrics/{files => templates/telegraf-plugins}/kvm_virsh.py (63%)

diff --git a/cluster_metrics/playbook-influx-telegraf.yml b/cluster_metrics/playbook-influx-telegraf.yml
index 07d46a61..eeb5d29d 100644
--- a/cluster_metrics/playbook-influx-telegraf.yml
+++ b/cluster_metrics/playbook-influx-telegraf.yml
@@ -92,5 +92,13 @@
           - "python /opt/telegraf/vm_quota.py"
         group: "{{ groups['utility_all'][0] }}"
         when_group: "{{ (groups['nova_compute'] | length) > 0 }}"
+      kvm:
+        plugin_name: "kvm_virsh.py"
+        command:
+          - "python /opt/telegraf/kvm_virsh.py"
+        group: "{{ groups['nova_compute'] }}"
+        when_group: "{{ (groups['nova_compute'] | length) > 0 and (nova_virt_type | default('qemu') in ['kvm', 'qemu']) }}"
+
+
     influx_telegraf_targets:
       - "{{ influxdb_host|default(internal_lb_vip_address) }}:{{ influxdb_port }}"
diff --git a/cluster_metrics/files/kvm_virsh.py b/cluster_metrics/templates/telegraf-plugins/kvm_virsh.py
similarity index 63%
rename from cluster_metrics/files/kvm_virsh.py
rename to cluster_metrics/templates/telegraf-plugins/kvm_virsh.py
index d670dbfd..0b09282c 100644
--- a/cluster_metrics/files/kvm_virsh.py
+++ b/cluster_metrics/templates/telegraf-plugins/kvm_virsh.py
@@ -1,5 +1,4 @@
 #!/usr/bin/env python
-import json
 import libvirt
 import socket
 
@@ -15,9 +14,14 @@ try:
             domain
         ).maxVcpus()
     return_data['kvm_host_id'] = abs(hash(socket.getfqdn()))
-except Exception:
-    raise SystemExit('Plugin failure')
+except Exception as exp:
+    raise SystemExit('Plugin failure -- Reason: "%s"' % exp)
 else:
-    print(json.dumps(return_data))
+    line_data = 'kvm '
+    for key, value in return_data.items():
+        line_data += '%s=%s,' % (key.replace(' ', '_'), value)
+    else:
+        line_data = line_data.rstrip(',')
+    print(line_data)
 finally:
     conn.close()