root / snf-cyclades-gtools / collectd / plugins / ganeti-cpustats.py @ 2439c9d6
History | View | Annotate | Download (1 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
import os |
4 |
import collectd |
5 |
|
6 |
from glob import glob |
7 |
|
8 |
|
9 |
def get_vcpus(pid): |
10 |
"""Get a KVM instance vCPU count by looking at its fd's"""
|
11 |
vcpus = 0
|
12 |
for fd in glob("/proc/%d/fd/*" % pid): |
13 |
# XXX: sad but trueeeeeeeeeeee
|
14 |
if os.readlink(fd) == "anon_inode:kvm-vcpu": |
15 |
vcpus += 1
|
16 |
return vcpus
|
17 |
|
18 |
|
19 |
def cpustats(data=None): |
20 |
for file in glob("/var/run/ganeti/kvm-hypervisor/pid/*"): |
21 |
instance = os.path.basename(file)
|
22 |
try:
|
23 |
pid = int(open(file, "r").read()) |
24 |
proc = open("/proc/%d/stat" % pid, "r") |
25 |
cputime = [int(proc.readline().split()[42])] |
26 |
except EnvironmentError: |
27 |
continue
|
28 |
vcpus = get_vcpus(pid) |
29 |
proc.close() |
30 |
|
31 |
vl = collectd.Values(type="derive")
|
32 |
vl.host = instance |
33 |
vl.plugin = "cpu"
|
34 |
vl.type = "virt_cpu_total"
|
35 |
total = sum(cputime) * 100 / (vcpus * os.sysconf("SC_CLK_TCK")) |
36 |
vl.dispatch(values=[total]) |
37 |
|
38 |
collectd.register_read(cpustats) |
39 |
|
40 |
# vim: set ts=4 sts=4 et sw=4 :
|