forked from innogames/igcollect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xen_vm_cpu.py
executable file
·51 lines (40 loc) · 1.52 KB
/
xen_vm_cpu.py
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
#!/usr/bin/env python
#
# igcollect - Xen VM CPUs
#
# Copyright (c) 2016, InnoGames GmbH
#
from __future__ import print_function
import socket, time
import subprocess
def get_vcpulist_dict(xmdata=False,hostname=False):
vc={}
for line in xmdata:
if not line.startswith(('Name','\n',' ')):
vm_name,vm_id,vm_vcpu,vm_cpu,vm_state,vm_time,vm_affinity = line.split()[:7]
if vm_name=='Domain-0': vm_name=hostname
vm_name=vm_name.replace('.','_')
try:
vc[vm_name]
except KeyError:
vc[vm_name]={}
vc[vm_name]['id'] = vm_id
try:
vc[vm_name]['vcpu']
except KeyError:
vc[vm_name]['vcpu'] = {}
if float(vm_time) > 1:
vc[vm_name]['vcpu'][vm_vcpu] = vm_time
return(vc)
xmdata = subprocess.Popen("/usr/sbin/xm vcpu-list", shell=True, bufsize=32678, stdout=subprocess.PIPE).stdout.readlines()
graphite_data=''
hostname = socket.gethostname().replace('.','_')
now = str(int(time.time()))
vcdict = get_vcpulist_dict(xmdata,hostname)
for vserver in vcdict:
total = 0
for vcpu in vcdict[vserver]['vcpu']:
graphite_data += 'servers.%s.virtualisation.vserver.%s.vcpu.%s.time %s %s\n' % (hostname,vserver,vcpu,vcdict[vserver]['vcpu'][vcpu],now)
total += int(vcdict[vserver]['vcpu'][vcpu].split('.')[0])
graphite_data += 'servers.%s.virtualisation.vserver.%s.vcpu.time %s %s\n' % (hostname,vserver,total,now)
print(graphite_data)