aboutsummaryrefslogtreecommitdiff
path: root/munin-libvirt-plugins-detect.in
blob: 275c98771daa775906b994576ac99a3009651ff8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python
# vim: set fileencoding=utf-8 :
#
# Configure and enable munin libvirt plugins
#
# Copyright 2008 Guido Guenther <agx@sigxcpu.org>
#
# 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: