diff options
Diffstat (limited to 'libvirt-mem')
-rw-r--r-- | libvirt-mem | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/libvirt-mem b/libvirt-mem new file mode 100644 index 0000000..fc9a572 --- /dev/null +++ b/libvirt-mem @@ -0,0 +1,96 @@ +#!/usr/bin/python +# vim: set fileencoding=utf-8 : +# +# Munin plugin to show the amount of memory used by libvirt managed virtual +# machines +# +# Copyright 2008 Guido Guenther <agx@sigxcpu.org> +# +# License: GPLv2 +# +# depends: python-libvirt +# +# If you don't want to use the default uri use: +# +# [libvirt-*] +# env.uri qemu:///system +# +# in your plugin configuration + +import re, sys, os +import libvirt + +def canon(name): + return re.sub(r"[^a-zA-Z0-9_]", "_", name) + +def print_config(uri): + """print the plugin config, determine the domains""" + + print """graph_title Virtual Domain Memory Usage +graph_vlabel Memory Usage / Bytes +graph_category Virtual Machines +graph_info This graph shows the current amount of memory used by each virtual machine +graph_args --base 1024 -l 0""" + + try: + conn = libvirt.openReadOnly(uri) + except libvirt.libvirtError, err: + print >>sys.stderr, "Error opening to %s connection: %s" % (uri, err) + return 1 + + ids = conn.listDomainsID() + type = "AREA" + for id in ids: + dom = conn.lookupByID(id) + name = dom.name() + print "%s_mem.label %s" % (canon(name), name) + print "%s_mem.type GAUGE" % canon(name) + print "%s_mem.min 0" % canon(name) + print "%s_mem.draw %s" % (canon(name), type) + if type == "AREA": + type = "STACK" + + hostname = conn.getHostname() + print "host_mem.label %s (host)" % hostname + print "host_mem.type GAUGE" + print "host_mem.min 0" + print "host_mem.draw LINE1" + return 0 + + +def fetch_values(uri): + try: + conn = libvirt.openReadOnly(uri) + except libvirt.libvirtError, err: + print >>sys.stderr, "Error opening to %s connection: %s" % (uri, err) + return 1 + ids = conn.listDomainsID() + hostmem = conn.getInfo()[1] * 1024 * 1024 + print "host_mem.value %d" % hostmem + for id in ids: + dom = conn.lookupByID(id) + name = dom.name() + mem = float(dom.info()[2]) * 1024 + print "%s_mem.value %d" % (canon(name), mem) + return 0 + + +def main(sys): + uri = os.getenv("uri") + + if len(sys) > 1: + if sys[1] in [ 'autoconf', 'detect' ]: + if libvirt.openReadOnly(uri): + print "yes" + return 0 + else: + print "no" + return 1 + elif sys[1] == 'config': + return print_config(uri) + return fetch_values(uri) + +if __name__ == "__main__": + sys.exit(main(sys.argv)) + +# vim:et:ts=4:sw=4: |