aboutsummaryrefslogtreecommitdiff
path: root/libvirt-cputime
blob: 86394ac0362075946d2bc0f4191b85cfd7314ab0 (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
#!/usr/bin/python
# vim: set fileencoding=utf-8 :
#
# Munin plugin to show the percent of cputime of 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, stack):
    """print the plugin config, determine the domains"""

    print """graph_title Virtual Domain Cpu Time
graph_vlabel CPU Time percentage
graph_category Virtual Machines
graph_info This graph shows the cpu time percentage of each virtual machine
graph_args --base 1000 -l 0
total_pc.type DERIVE
total_pc.graph no
total_pc.min 0
total_pc.max 100
total_pc.label total
total_pc.info cputime used by all virtual machines
total_pc.warning 90
total_pc.critical 95"""

    draw = [ "LINE1", "AREA"][stack]
    conn = libvirt.openReadOnly(uri)
    ids = conn.listDomainsID()
    for id in ids:
        dom = conn.lookupByID(id)
        name = dom.name()
        print "%s_cputime.label %s" % (canon(name), name)
        print "%s_cputime.type DERIVE" % canon(name)
        print "%s_cputime.min 0" % canon(name)
        print "%s_cputime.draw %s" % (canon(name), draw)
        if draw == "AREA":
            draw = "STACK"

def fetch_values(uri):
    conn = libvirt.openReadOnly(uri)
    ids = conn.listDomainsID()
    total = 0

    processors = float(conn.getInfo()[2])
    for id in ids:
        dom = conn.lookupByID(id)
        name = dom.name()
        cputime = float(dom.info()[4])
        cputime_percentage = 1.0e-7 * cputime / processors
        total += cputime_percentage
        print "%s_cputime.value %.0f" % (canon(name), cputime_percentage)
    print "total_pc.value %.0f" % total


def main(sys):
    uri = os.getenv("uri", "qemu:///system")
    stack = [ False, True ][os.getenv("stack") == "1"]

    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':
            print_config(uri, stack)
            return 0
    fetch_values(uri)
    return 0

if __name__ == "__main__":
    sys.exit(main(sys.argv))

# vim:et:ts=4:sw=4: