add kvm plugins
authormh <mh@immerda.ch>
Wed, 12 Sep 2012 11:04:33 +0000 (13:04 +0200)
committermh <mh@immerda.ch>
Wed, 12 Sep 2012 11:05:19 +0000 (13:05 +0200)
files/plugins/kvm_cpu [new file with mode: 0644]
files/plugins/kvm_io [new file with mode: 0644]
files/plugins/kvm_mem [new file with mode: 0644]
files/plugins/kvm_net [new file with mode: 0644]
manifests/plugins/kvm.pp [new file with mode: 0644]

diff --git a/files/plugins/kvm_cpu b/files/plugins/kvm_cpu
new file mode 100644 (file)
index 0000000..f48d9f0
--- /dev/null
@@ -0,0 +1,101 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# vim: set fileencoding=utf-8
+#
+# Munin plugin to show CPU used by vm
+#
+# Copyright Maxence Dunnewind, Rodolphe Quiédeville
+#
+# License : GPLv3
+#
+# parsed environment variables:
+# vmsuffix: part of vm name to be removed
+#
+#%# capabilities=autoconf
+#%# family=contrib
+
+import re, os, sys
+from subprocess import Popen, PIPE
+
+def config(vm_names):
+    ''' Print the plugin's config
+    @param vm_names : a list of "cleaned" vms' name
+    '''
+    percent = len(filter(lambda x: x[0:3] == 'cpu' and x[3] != ' ', open('/proc/stat', 'r').readlines())) * 100
+
+    base_config = """graph_title KVM Virtual Machine CPU usage
+graph_vlabel %%
+graph_category KVM
+graph_scale no
+graph_period second
+graph_info This graph shows the current CPU used by virtual machines
+graph_args --base 1000 -r --lower-limit 0 --upper-limit %d""" % percent
+    print base_config
+    draw = "AREA"
+    for vm in vm_names:
+        print "%s_cpu.label %s" % (vm, vm)
+        print "%s_cpu.min 0" % vm
+        print "%s_cpu.type DERIVE" % vm
+        print "%s_cpu.draw %s" % (vm, draw)
+        print "%s_cpu.info percent of cpu time used by virtual machine" % vm
+       draw = "STACK"
+
+
+def clean_vm_name(vm_name):
+    ''' Replace all special chars
+    @param vm_name : a vm's name
+    @return cleaned vm's name
+    '''
+
+    # suffix part defined in conf
+    suffix = os.getenv('vmsuffix')
+    if suffix:
+        vm_name = re.sub(suffix,'',vm_name)
+
+    return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
+
+def detect_kvm():
+    ''' Check if kvm is installed
+    '''
+    kvm = Popen("which kvm", shell=True, stdout=PIPE)
+    kvm.communicate()
+    return not bool(kvm.returncode)
+
+def find_vm_names(pids):
+    '''Find and clean vm names from pids
+    @return a dictionnary of {pids : cleaned vm name}
+    '''
+    result = {}
+    for pid in pids:
+        cmdline = open("/proc/%s/cmdline" % pid, "r")
+        result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-]*)\x00\-.*$",r"\1", cmdline.readline()))
+    return result
+    
+def list_pids():
+    ''' Find the pid of kvm processes
+    @return a list of pids from running kvm
+    '''
+    pid = Popen("pidof qemu-kvm kvm", shell=True, stdout=PIPE)
+    return pid.communicate()[0].split()
+
+def fetch(vms):
+    ''' Fetch values for a list of pids
+    @param dictionnary {kvm_pid: cleaned vm name}
+    '''
+    for ( pid, name ) in vms.iteritems():
+        ( user, system ) = open("/proc/%s/stat" % pid, 'r').readline().split(' ')[13:15]
+        print '%s_cpu.value %d' % ( name, int(user) + int(system) )
+        
+if __name__ == "__main__":
+    if len(sys.argv) > 1:
+        if sys.argv[1] in ['autoconf', 'detect']:
+            if detect_kvm():
+                print "yes"
+            else:
+                print "no"
+        elif sys.argv[1] == "config":
+            config(find_vm_names(list_pids()).values())
+        else:
+            fetch(find_vm_names(list_pids()))
+    else:
+        fetch(find_vm_names(list_pids()))
diff --git a/files/plugins/kvm_io b/files/plugins/kvm_io
new file mode 100644 (file)
index 0000000..602b2ca
--- /dev/null
@@ -0,0 +1,110 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# vim: set fileencoding=utf-8
+#
+# Munin plugin to show io by vm
+#
+# Copyright Maxence Dunnewind, Rodolphe Quiédeville
+#
+# License : GPLv3
+#
+# parsed environment variables:
+# vmsuffix: part of vm name to be removed
+#
+#%# capabilities=autoconf
+#%# family=contrib
+
+import re, os, sys
+from subprocess import Popen, PIPE
+
+def config(vm_names):
+    ''' Print the plugin's config
+    @param vm_names : a list of "cleaned" vms' name
+    '''
+    base_config = """graph_title KVM Virtual Machine IO usage
+graph_vlabel Bytes read(-)/written(+) per second
+graph_category KVM
+graph_info This graph shows the block device I/O used of virtual machines
+graph_args --base 1024
+    """
+    print base_config
+
+    for vm in vm_names:
+        print "%s_read.label %s" % (vm, vm)
+        print "%s_read.type COUNTER" % vm
+        print "%s_read.min 0" % vm
+        print "%s_read.draw LINE1" % vm
+        print "%s_read.info I/O used by virtual machine %s" % (vm, vm)
+        print "%s_write.label %s" % (vm, vm)
+        print "%s_write.type COUNTER" % vm
+        print "%s_write.min 0" % vm        
+        print "%s_write.draw LINE1" % vm
+        print "%s_write.negative %s_read" % (vm, vm)
+        print "%s_write.info I/O used by virtual machine %s" % (vm, vm)
+
+def clean_vm_name(vm_name):
+    ''' Replace all special chars
+    @param vm_name : a vm's name
+    @return cleaned vm's name
+    '''
+    # suffix part defined in conf
+    suffix = os.getenv('vmsuffix')
+    if suffix:
+        vm_name = re.sub(suffix,'',vm_name)
+
+    return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
+    
+def fetch(vms):
+    ''' Fetch values for a list of pids
+    @param dictionnary {kvm_pid: cleaned vm name}
+    '''
+    res = {}
+    for pid in vms:
+        f = open("/proc/%s/io" % pid, "r")
+        for line in f.readlines():
+            if "read_bytes" in line:
+                read = line.split()[1]
+                print "%s_read.value %s" % (vms[pid], read) 
+            if "write_bytes" in line:
+                write = line.split()[1]
+                print "%s_write.value %s" % (vms[pid], write) 
+                break            
+        f.close()
+
+def detect_kvm():
+    ''' Check if kvm is installed
+    '''
+    kvm = Popen("which kvm", shell=True, stdout=PIPE)
+    kvm.communicate()
+    return not bool(kvm.returncode)
+
+def find_vm_names(pids):
+    '''Find and clean vm names from pids
+    @return a dictionnary of {pids : cleaned vm name}
+    '''
+    result = {}
+    for pid in pids:
+        cmdline = open("/proc/%s/cmdline" % pid, "r")
+        result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-]*)\x00\-.*$",r"\1", cmdline.readline()))
+    return result
+    
+def list_pids():
+    ''' Find the pid of kvm processes
+    @return a list of pids from running kvm
+    '''
+    pid = Popen("pidof qemu-kvm kvm", shell=True, stdout=PIPE)
+    return pid.communicate()[0].split()
+    
+if __name__ == "__main__":
+    if len(sys.argv) > 1:
+        if sys.argv[1] in ['autoconf', 'detect']:
+            if detect_kvm():
+                print "yes"
+            else:
+                print "no"
+        elif sys.argv[1] == "config":
+            config(find_vm_names(list_pids()).values())
+        else:
+            fetch(find_vm_names(list_pids()))
+    else:
+        fetch(find_vm_names(list_pids()))
diff --git a/files/plugins/kvm_mem b/files/plugins/kvm_mem
new file mode 100644 (file)
index 0000000..c64d8ce
--- /dev/null
@@ -0,0 +1,107 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# vim: set fileencoding=utf-8
+#
+# Munin plugin to show amount of memory used by vm
+#
+# Copyright Maxence Dunnewind, Rodolphe Quiédeville, Adrien Pujol
+#
+# License : GPLv3
+#
+# parsed environment variables:
+# vmsuffix: part of vm name to be removed
+#
+#%# capabilities=autoconf
+#%# family=contrib
+
+import re, os, sys
+from subprocess import Popen, PIPE
+
+def config(vm_names):
+    ''' Print the plugin's config
+    @param vm_names : a list of "cleaned" vms' name
+    '''
+    base_config = """graph_title KVM Virtual Machine Memory usage
+graph_vlabel Bytes
+graph_category KVM
+graph_info This graph shows the current amount of memory used by virtual machines
+graph_args --base 1024
+    """
+    print base_config
+    draw = "AREA"
+    for vm in vm_names:
+        print "%s_mem.label %s" % (vm, vm)
+        print "%s_mem.type GAUGE" % vm
+        if draw == 'AREA':
+            print "%s_mem.min 0" % vm
+        print "%s_mem.draw %s" % (vm, draw)
+        print "%s_mem.info memory used by virtual machine %s" % (vm, vm)
+       draw = "STACK"
+
+
+def clean_vm_name(vm_name):
+    ''' Replace all special chars
+    @param vm_name : a vm's name
+    @return cleaned vm's name
+    '''
+    # suffix part defined in conf
+    suffix = os.getenv('vmsuffix')
+    if suffix:
+        vm_name = re.sub(suffix,'',vm_name)
+
+    return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
+    
+def fetch(vms):
+    ''' Fetch values for a list of pids
+    @param dictionnary {kvm_pid: cleaned vm name}
+    '''
+    res = {}
+    for pid in vms:
+        try:
+            cmdline = open("/proc/%s/cmdline" % pid, "r")
+            amount = re.sub(r"^.*-m\x00(.*)\x00-smp.*$",r"\1", cmdline.readline())
+            ammount = int(amount) * 1024 * 1024
+            print "%s_mem.value %s" % (vms[pid], ammount) 
+        except:
+            cmdline = open("/proc/%s/cmdline" % pid, "r")
+            amount = re.sub(r"^.*-m\x00(\d+).*$",r"\1", cmdline.readline())
+            ammount = int(amount) * 1024 * 1024
+            print "%s_mem.value %s" % (vms[pid], ammount) 
+
+def detect_kvm():
+    ''' Check if kvm is installed
+    '''
+    kvm = Popen("which kvm", shell=True, stdout=PIPE)
+    kvm.communicate()
+    return not bool(kvm.returncode)
+
+def find_vm_names(pids):
+    '''Find and clean vm names from pids
+    @return a dictionnary of {pids : cleaned vm name}
+    '''
+    result = {}
+    for pid in pids:
+        cmdline = open("/proc/%s/cmdline" % pid, "r")
+        result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-]*)\x00\-.*$",r"\1", cmdline.readline()))
+    return result
+    
+def list_pids():
+    ''' Find the pid of kvm processes
+    @return a list of pids from running kvm
+    '''
+    pid = Popen("pidof qemu-kvm kvm", shell=True, stdout=PIPE)
+    return pid.communicate()[0].split()
+    
+if __name__ == "__main__":
+    if len(sys.argv) > 1:
+        if sys.argv[1] in ['autoconf', 'detect']:
+            if detect_kvm():
+                print "yes"
+            else:
+                print "no"
+        elif sys.argv[1] == "config":
+            config(find_vm_names(list_pids()).values())
+        else:
+            fetch(find_vm_names(list_pids()))
+    else:
+        fetch(find_vm_names(list_pids()))
diff --git a/files/plugins/kvm_net b/files/plugins/kvm_net
new file mode 100644 (file)
index 0000000..6da0d90
--- /dev/null
@@ -0,0 +1,143 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# vim: set fileencoding=utf-8
+#
+# Munin plugin to show the network I/O per vm
+# On redhat based systems
+#
+# Copyright Igor Borodikhin
+# Copyright Peter Meier
+#
+# License : GPLv3
+#
+#
+# parsed environment variables:
+# vmsuffix: part of vm name to be removed
+#
+#%# capabilities=autoconf
+#%# family=contrib
+
+import re, os, sys
+from subprocess import Popen, PIPE
+
+def config(vm_names):
+    ''' Print the plugin's config
+    @param vm_names : a list of "cleaned" vms' name
+    '''
+    base_config = """graph_title KVM Network I/O
+graph_vlabel Bytes rx(-)/tx(+) per second
+graph_category KVM
+graph_info This graph shows the network I/O of the virtual machines
+graph_args --base 1024
+    """
+    print base_config
+    for vm in vm_names:
+        print "%s_in.label %s" % (vm, vm)
+        print "%s_in.type COUNTER" % vm
+        print "%s_in.min 0" % vm
+        print "%s_in.draw LINE2" % vm
+        print "%s_out.negative %s_in" % (vm, vm)
+        print "%s_out.label %s" % (vm, vm)
+        print "%s_out.type COUNTER" % vm
+        print "%s_out.min 0" % vm
+        print "%s_out.draw LINE2" % vm
+
+def clean_vm_name(vm_name):
+    ''' Replace all special chars
+    @param vm_name : a vm's name
+    @return cleaned vm's name
+    '''
+    # suffix part defined in conf
+    suffix = os.getenv('vmsuffix')
+    if suffix:
+        vm_name = re.sub(suffix,'',vm_name)
+
+    return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
+    
+def fetch(vms):
+    ''' Fetch values for a list of pids
+    @param dictionnary {kvm_pid: cleaned vm name}
+    '''
+    res = {}
+    macs_to_inf = find_macs_to_inf()
+    interfaces = {}
+    for e in Popen('cat /proc/net/dev | awk \'{ print $1  ":" $9 }\'', shell=True, stdout=PIPE).communicate()[0].split('\n'):
+        s = e.split(':')
+        if len(s) == 3:
+            interfaces[s[0]] = (s[1],s[2])
+    for pid in vms:
+        macs = get_vm_macs(pid)
+        for mac in macs:
+            inf = macs_to_inf[mac]
+            values = interfaces[inf]
+            if len(values) == 2:
+                print "%s_%s_in.value %s" % (vms[pid], inf, values[0])
+                print "%s_%s_out.value %s" % (vms[pid], inf, values[1])
+
+def detect_kvm():
+    ''' Check if kvm is installed
+    '''
+    kvm = Popen("which kvm", shell=True, stdout=PIPE)
+    kvm.communicate()
+    return not bool(kvm.returncode)
+
+def find_vm_names(pids):
+    '''Find and clean vm names from pids
+    @return a dictionnary of {pids : cleaned vm name}
+    '''
+    result = {}
+    for pid in pids:
+        cmdline = open("/proc/%s/cmdline" % pid, "r")
+        result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-]*)\x00\-.*$",r"\1", cmdline.readline()))        
+    return result
+    
+def get_vm_macs(pid):
+    '''Find macs for a pid
+    @return the mac addresses for a specified pid
+    '''
+    cmdline = open("/proc/%s/cmdline" % pid, "r")
+    line = cmdline.readline()
+    # macs are fe:... on the host
+    macs = [ re.sub(r"^\d{2}",'fe',p.split('=')[1]) for p in line.split(",") if re.match(r"^mac(addr)?=",p) ]
+    return macs
+
+def list_pids():
+    ''' Find the pid of kvm processes
+    @return a list of pids from running kvm
+    '''
+    pid = Popen("pidof qemu-kvm kvm", shell=True, stdout=PIPE)
+    return pid.communicate()[0].split()
+
+def find_macs_to_inf():
+    ''' Find interfaces for vms
+    @return a dictionary of macs to inf
+    '''
+    result = {}
+    inf = ""
+    kvm = Popen("ip a | grep -E -A 1 '(tap|vnet)' | awk '{print $2}' | grep -v '^$'", shell=True, stdout=PIPE)
+    res = kvm.communicate()[0].split('\n')
+    for line in res:
+        if len(line) > 0:
+            if re.match(r"^tap.*", line):
+                inf = re.sub(r"(tap[^:]+):", r"\1", line)
+            elif re.match(r"^vnet.*", line):
+                inf = re.sub(r"(vnet[^:]+):", r"\1", line)
+            else:
+                result[line] = inf
+
+    return result
+    
+if __name__ == "__main__":
+    if len(sys.argv) > 1:
+        if sys.argv[1] in ['autoconf', 'detect']:
+            if detect_kvm():
+                print "yes"
+            else:
+                print "no"
+        elif sys.argv[1] == "config":
+            config(find_vm_names(list_pids()).values())
+        else:
+            fetch(find_vm_names(list_pids()))
+    else:
+        fetch(find_vm_names(list_pids()))
+
diff --git a/manifests/plugins/kvm.pp b/manifests/plugins/kvm.pp
new file mode 100644 (file)
index 0000000..7a1430f
--- /dev/null
@@ -0,0 +1,7 @@
+class munin::plugins::kvm {
+  munin::plugin::deploy {
+    [ 'kvm_cpu', 'kvm_mem', 'kvm_net' ]:;
+    'kvm_io':
+      config => 'user root';
+  }
+}