Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-gtools / collectd / plugins / ganeti-stats.py @ 56359d19

History | View | Annotate | Download (2.3 kB)

1
#!/usr/bin/env python
2

    
3
import os
4
import collectd
5

    
6
from hashlib import md5
7

    
8
from glob import glob
9

    
10

    
11
def read_int(file):
12
    f = open(file, "r")
13
    try:
14
        val = int(f.read())
15
    except ValueError:
16
        val = None
17
    finally:
18
        f.close()
19

    
20
    return val
21

    
22

    
23
def anonymize_hostname(hostname):
24
    #return md5(hostname).hexdigest()
25
    return hostname
26

    
27

    
28
def get_vcpus(pid):
29
    """Get a KVM instance vCPU count by looking at its fd's"""
30
    vcpus = 0
31
    for fd in glob("/proc/%d/fd/*" % pid):
32
        # XXX: sad but trueeeeeeeeeeee
33
        if os.readlink(fd) == "anon_inode:kvm-vcpu":
34
            vcpus += 1
35
    return vcpus
36

    
37

    
38
def netstats(data=None):
39
    for dir in glob("/var/run/ganeti/kvm-hypervisor/nic/*"):
40
        if not os.path.isdir(dir):
41
            continue
42

    
43
        hostname = os.path.basename(dir)
44

    
45
        for nic in glob(os.path.join(dir, "*")):
46
            idx = int(os.path.basename(nic))
47
            with open(nic) as nicfile:
48
                try:
49
                    iface = nicfile.readline().strip()
50
                except EnvironmentError:
51
                    continue
52

    
53
            if not os.path.isdir("/sys/class/net/%s" % iface):
54
                continue
55
            
56
            bytes_in = read_int("/sys/class/net/%s/statistics/rx_bytes" % iface)
57
            bytes_out = read_int("/sys/class/net/%s/statistics/tx_bytes" % iface)
58

    
59
            vl = collectd.Values(type="counter")
60
            vl.host = anonymize_hostname(hostname)
61
            vl.plugin = "interface"
62
            vl.type = "if_octets"
63
            vl.type_instance = "eth%d" % idx
64
            vl.dispatch(values=[bytes_out, bytes_in])
65

    
66

    
67
def cpustats(data=None):
68
    for file in glob("/var/run/ganeti/kvm-hypervisor/pid/*"):
69
        instance = os.path.basename(file)
70
        try:
71
            pid = int(open(file, "r").read())
72
            proc = open("/proc/%d/stat" % pid, "r")
73
            cputime = [int(proc.readline().split()[42])]
74
        except EnvironmentError:
75
            continue
76
        vcpus = get_vcpus(pid)
77
        proc.close()
78

    
79
        vl = collectd.Values(type="counter")
80
        vl.host = anonymize_hostname(instance)
81
        vl.plugin = "cpu"
82
        vl.type = "virt_cpu_total"
83
        total = sum(cputime) * 100 / (vcpus * os.sysconf("SC_CLK_TCK"))
84
        vl.dispatch(values=[total])
85

    
86
collectd.register_read(netstats)
87
collectd.register_read(cpustats)
88

    
89
# vim: set ts=4 sts=4 et sw=4 :