From 62d3469cf60a0ca8bdea429a2feb4131e2722619 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Mon, 17 Nov 2008 11:28:56 +0100 Subject: also calculate the maximum memory vms can balloon to as absolute and percent value. --- libvirt-mem | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/libvirt-mem b/libvirt-mem index 0c2073c..88bc2c2 100644 --- a/libvirt-mem +++ b/libvirt-mem @@ -38,19 +38,29 @@ host_mem.label %(hostname)s (host) host_mem.type GAUGE host_mem.min 0 host_mem.draw LINE1 +host_mem.info total memory of host '%(hostname)s' total.type GAUGE -total.label total -total.info memory used by virtual machines on host %(hostname)s +total.label total memory +total.info total memory used by virtual machines on host '%(hostname)s' total.graph no total.min 0 total_pc.type GAUGE total_pc.label used memory percentage -total_pc.info memory in percent used by virtual machines on host %(hostname)s +total_pc.info memory in percent used by virtual machines on host '%(hostname)s' total_pc.graph no total_pc.min 0 total_pc.max 100 total_pc.warning 90 -total_pc.critical 95""" % dict(hostname=hostname) +total_pc.critical 95 +total_max.type GAUGE +total_max.label total max. mem +total_max.info maximum memory virtual machines can balloon to on host '%(hostname)s' +total_max.min 0 +total_max_pc.type GAUGE +total_max_pc.label total maximum memory percentage +total_max_pc.graph no +total_max_pc.info maximum memory in percent virtual machines can balloon to on host '%(hostname)s' +total_max_pc.min 0""" % dict(hostname=hostname) ids = conn.listDomainsID() draw = "AREA" @@ -67,6 +77,7 @@ total_pc.critical 95""" % dict(hostname=hostname) print "%s_mem.type GAUGE" % canon(name) print "%s_mem.min 0" % canon(name) print "%s_mem.draw %s" % (canon(name), draw) + print "%s_mem.info memory used by virtual machine '%s'" % (canon(name), name) if draw == "AREA": draw = "STACK" return 0 @@ -74,6 +85,7 @@ total_pc.critical 95""" % dict(hostname=hostname) def fetch_values(uri): total = 0 + total_max = 0 try: conn = libvirt.openReadOnly(uri) except libvirt.libvirtError, err: @@ -91,11 +103,16 @@ def fetch_values(uri): continue if name == "Domain-0": continue - mem = dom.info()[2] * 1024 - print "%s_mem.value %d" % (canon(name), mem) + maxmem, mem = dom.info()[1:3] + mem *= 1024 + maxmem *= 1024 total += mem + total_max += maxmem + print "%s_mem.value %d" % (canon(name), mem) print "total.value %d" % total print "total_pc.value %.0f" % (100.0 * total / float(hostmem)) + print "total_max.value %d" % total_max + print "total_max_pc.value %.0f" % (100.0 * total_max / float(hostmem)) return 0 -- cgit v1.2.3 From 532cac06c0779ec953eeb474981c6348ee2a7d51 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Mon, 17 Nov 2008 11:29:52 +0100 Subject: add info line for each host --- libvirt-cputime | 1 + 1 file changed, 1 insertion(+) diff --git a/libvirt-cputime b/libvirt-cputime index 9684cdc..f2d658d 100644 --- a/libvirt-cputime +++ b/libvirt-cputime @@ -52,6 +52,7 @@ total_pc.critical 95""" print "%s_cputime.type DERIVE" % canon(name) print "%s_cputime.min 0" % canon(name) print "%s_cputime.draw %s" % (canon(name), draw) + print "%s_cputime.info percent of cputime used by virtual machine '%s'" % (canon(name), name) if draw == "AREA": draw = "STACK" -- cgit v1.2.3 From de0c1a3e21ce0bcf4eeed7bbf952a38c8983ef75 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Thu, 20 Nov 2008 14:17:14 +0100 Subject: detect script for autoconfiguration --- Makefile | 29 +++++++--- munin-libvirt-plugins-detect.in | 119 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 munin-libvirt-plugins-detect.in diff --git a/Makefile b/Makefile index f972c82..0054d77 100644 --- a/Makefile +++ b/Makefile @@ -1,29 +1,40 @@ VERSION=0.0.4 -PKG=munin-libvirt-plugins -PLUGINDIR=/usr/share/munin/plugins/ +PACKAGE=munin-libvirt-plugins +SBINDIR=/usr/sbin +PLUGINDIR=/usr/share/munin/plugins +MUNINCONFDIR=/etc/munin PLUGINS=libvirt-cputime \ libvirt-blkstat \ libvirt-ifstat \ libvirt-mem -ALL=$(PLUGINS) Makefile COPYING INSTALL -PYFILES=$(patsubst %,%.py,$(PLUGINS)) +DETECT=$(PACKAGE)-detect +PYFILES=$(patsubst %,%.py,$(PLUGINS) $(DETECT)) -install: $(PLUGINS) +all: $(DETECT) + +install: build install -d $(DESTDIR)$(PLUGINDIR) install -m 755 $(PLUGINS) $(DESTDIR)$(PLUGINDIR) + install -m 755 $(DETECT) $(DESTDIR)$(SBINDIR) + +$(DETECT): $(DETECT).in + sed -e "s,::MUNINCONFDIR::,$(MUNINCONFDIR),"\ + -e "s,::PLUGINDIR::,$(PLUGINDIR)," \ + -e "s,::VERSION::,$(VERSION)," \ + < $< > $@ %.py: % ln -s $< $@ pychecker -q -e Error $@ -check: $(PYFILES) +check: $(DETECT) $(PYFILES) clean: - rm -f *.py *.pyc + rm -f *.py *.pyc $(DETECT) dist: clean check - git-archive --format=tar --prefix=$(PKG)-$(VERSION)/ HEAD | gzip -c > ../$(PKG)-$(VERSION).tar.gz + git-archive --format=tar --prefix=$(PACKAGE)-$(VERSION)/ HEAD | gzip -c > ../$(PACKAGE)-$(VERSION).tar.gz -.PHONY: clean check dist install +.PHONY: clean check dist install build diff --git a/munin-libvirt-plugins-detect.in b/munin-libvirt-plugins-detect.in new file mode 100644 index 0000000..29012d3 --- /dev/null +++ b/munin-libvirt-plugins-detect.in @@ -0,0 +1,119 @@ +#/!/usr/bin/python +# vim: set fileencoding=utf-8 : +# +# Configure and enable munin libvirt plugins +# +# Copyright 2008 Guido Guenther +# +# Licesnse: GPLv2 +# +# Detect connection uri and enable plugins + +import os +import sys +from optparse import OptionParser + +try: + import libvirt + has_libvirt = True +except ImportError: + has_libvirt = False + +URIS = [ "xen:///", "qemu:///system" ] +PLUGIN_CONF = "::MUNINCONFDIR::/plugin-conf.d/libvirt" +PLUGINS = [ "blkstat", "cputime", "ifstat", "mem" ] +PLUGIN_SRC = "::PLUGINDIR::" +PLUGIN_DST = "::MUNINCONFDIR::/plugins/" + +def check_uris(uris, force=False): + detected = None + + for uri in uris: + try: + libvirt.openReadOnly(uri) + detected = uri + break + except libvirt.libvirtError, err: + pass + + if detected: + print "Hypervisor at %s detected." % detected + if not os.path.exists(PLUGIN_CONF) or force: + try: + conf = file(PLUGIN_CONF, "w+") + print >>conf, "[libvirt-*]" + print >>conf, "env.uri %s""" % detected + conf.close() + except IOError, err: + print >>sys.stderr, err + return 1 + else: + print >>sys.stderr, "Plugin configuration '%s' already exists - doing nothing" % PLUGIN_CONF + return 0 + + +def enable_plugins(force=False): + for plugin in map(lambda x: "libvirt-" + x, PLUGINS): + src = os.path.join(PLUGIN_SRC, plugin) + dst = os.path.join(PLUGIN_DST, plugin) + if force: + try: + os.unlink(dst) + except OSError: + pass + if os.path.exists(dst): + print >>sys.stderr, "Plugin '%s' already enabled - doing nothing" % plugin + else: + print >>sys.stderr, "Enabling plugin '%s'" % plugin + os.symlink(src, dst) + return 0 + + +def restart_munin_node(): + act = dict(service='munin-node', action='restart') + + for path in [ '/usr/sbin/invoke-rc.d', + '/sbin/service' ]: + if os.path.exists(path): + act['exec'] = path + ret = os.system('%(exec)s %(service)s %(action)s' % act) + return ret + else: + if os.path.exists('/etc/init.d/%(service)s' % act): + ret = os.system('/etc/init.d/%(service)s %(action)s' % act) + return ret + return 1 + + +def main(args): + parser = OptionParser(usage="%prog [-f] [uris]", version="%prog ::VERSION::") + parser.add_option("-f", "--force", action="store_true", dest="force", default=False, + help="overwrite files and symlinks") + parser.add_option("-r", "--restart", action="store_true", dest="restart", default=False, + help="restart munin-node to let changes take effect") + (options, args) = parser.parse_args(args) + + if len(args) > 1: + uris = args[1:] + else: + uris = URIS + + if not has_libvirt: + print >>sys.stderr, "Libvirt not available." + return 1 + + ret = check_uris(uris, options.force) + if ret: + return ret + ret = enable_plugins(options.force) + if ret: + return ret + if options.restart: + ret = restart_munin_node() + return ret + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) + +# vim:et:ts=4:sw=4: -- cgit v1.2.3 From 81d6301ff4826ec359355ac3cdd59ced8e75984e Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Thu, 20 Nov 2008 14:33:37 +0100 Subject: bump version to 0.0.5 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0054d77..3e6f950 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=0.0.4 +VERSION=0.0.5 PACKAGE=munin-libvirt-plugins SBINDIR=/usr/sbin PLUGINDIR=/usr/share/munin/plugins -- cgit v1.2.3