aboutsummaryrefslogtreecommitdiff
path: root/libvirt-mem
blob: c7bf63d3bc87b21d631f2f19de0b22d5e1a338c6 (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
#!/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
#
#%# capabilities=autoconf
#%# family=contrib

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"""
    try:
        conn = libvirt.openReadOnly(uri)
    except libvirt.libvirtError, err:
        print >>sys.stderr, "Error opening to %s connection: %s" % (uri, err)
        return 1

    hostname = conn.getHostname()

    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
host_mem.label %(hostname)s (host)
host_mem.type GAUGE
host_mem.min 0
host_mem.draw LINE1
total.type GAUGE
total.label total
total.info 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.graph no
total_pc.min 0
total_pc.max 100
total_pc.warning 90
total_pc.critical 95""" % dict(hostname=hostname)

    ids = conn.listDomainsID()
    draw = "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), draw)
        if draw == "AREA":
            draw = "STACK"
    return 0


def fetch_values(uri):
    total = 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()
    hostmem = conn.getInfo()[1] * 1024 * 1024
    print "host_mem.value %d" % hostmem
    for id in ids:
        dom = conn.lookupByID(id)
        name = dom.name()
        mem = dom.info()[2] * 1024
        total += mem
        print "%s_mem.value %d" % (canon(name), mem)
    print "total.value %d" % total
    print "total_pc.value %.0f" % (100.0 * total / float(hostmem))
    return 0


def main(sys):
    uri = os.getenv("uri", "qemu:///system")

    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: