Just created a python openvz plugin. It's not finished, but it's in a
working state. Any comments or suggestions are welcome.

config:

<Plugin python>
        ModulePath "/etc/collectd/plugins/python"
        LogTraces true
        #Interactive true
        Import "openvz"

        <Module openvz>
                # defaults
                vzlist_binary "/usr/sbin/vzlist"
                vzctl_binary "/usr/sbin/vzctl"
        </Module>
</Plugin>


Regards,
Vincent


#!/usr/bin/python

# openvz collectd python plugin
#
# Copyright (C) 2010  Vincent van Gelder <vincent at dmmw.nl> 

import collectd
import re,sys,os
from subprocess import Popen,PIPE

vzlist_bin = '/usr/sbin/vzlist'
vzctl_bin = '/usr/sbin/vzctl'
proc_ubc = '/proc/bc'

def ubc_config(config):
	global vzlist_bin
	
	collectd.info('Module openvz started')

	for child in config.children:
		if child.key == 'vzlist_binary':
			collectd.info('Setting vzlist binary to %s' % child.values[0])
			vzlist_bin = child.values[0]
			
		if child.key == 'vzctl_binary':
			collectd.info('Setting vzctl binary to %s' % child.values[0])
			vzctl_bin = child.values[0]
			
def ubc_init():

	# check if vzlist binary exists
	if not os.path.exists(vzlist_bin):
		collectd.error("Can't find vzlist binary at %s. Disabling plugin." % (vzlist_bin)) 
		collectd.unregister_read(ubc_read)
		return
	
	# check if vzctl binary exists
	if not os.path.exists(vzctl_bin):
		collectd.error("Can't find vzctl binary at %s. Disabling plugin." % (vzctl_bin)) 
		collectd.unregister_read(ubc_read)
		return

	if not os.path.exists(proc_ubc):
		collectd.error("%s not found, is this openvz kernel? Disabling plugin." % (ubc_read)) 
		collectd.unregister_read(ubc_read)
		return
		
def ubc_parse(hostname,ctid):

	stats = dict()

	lines = open(proc_ubc + '/' + ctid + '/resources').readlines()

	# column headers
	headers = ('held','maxheld','barrier','limit','failcnt')

	for line in lines:
		# Split on whitespace
		cells = re.split('\s+', line.strip())
		
		row_stats = dict()

		row_title = cells.pop(0)
		for index, cell in enumerate(cells):
			row_stats[headers[index]] = int(cell)

		stats[row_title] = row_stats

	return stats

def ubc_interface(hostname,ctid):

	# vzctl exec <ctid> cat /proc/net/dev
	p = Popen([vzctl_bin,'exec',str(ctid),'cat /proc/net/dev'], stdin=PIPE,stdout=PIPE)
	dev = p.stdout.readlines()
	for line in dev:
		# search venet interface(s)
		if line.find('venet') == 0:
			venet,values = re.split(':', line)
			v = re.split('\s+', values)
			ubc_dispatch(hostname,'interface','ipt_bytes', venet + '-in',v[1])
			ubc_dispatch(hostname,'interface','ipt_bytes', venet + '-out',v[9])

def ubc_dispatch(hostname,plugin,type, type_instance,value):
	
	stats = collectd.Values();
	stats.plugin = 'openvz.' + plugin
	stats.host = hostname
	stats.type = type
	stats.type_instance = type_instance
	stats.values = [value]
	stats.dispatch()

def ubc_read(data=None):

	try:
		# query vzlist for hostnames
		p = Popen([vzlist_bin,'-H','-o','ctid,hostname'], stdin=PIPE,stdout=PIPE)
		ctids = p.stdout.readlines()

		for ct in ctids:
			
			ctid,hostname = re.split('\s+', ct.strip())
			
			if not os.path.exists(proc_ubc + '/' + ctid + '/resources'):
				continue
			
			ct = ubc_parse(hostname,ctid)
		
			# kmemsize + allsocketbuf
			kernelbuf = ct['kmemsize']['held'] + ct['tcpsndbuf']['held'] + ct['tcprcvbuf']['held'] + ct['othersockbuf']['held'] + ct['dgramrcvbuf']['held']

			# Ram utilization
			ram = ct['physpages']['held'] * 4096 + kernelbuf
			# Ram + swap utilization
			ramswap = ct['oomguarpages']['held'] * 4096 + kernelbuf
			# memory allocation
			alloc = ct['privvmpages']['held'] * 4096 + kernelbuf
			# swap
			swap = (ct['oomguarpages']['held'] * 4096) - (ct['physpages']['held'] * 4096)
	
			# dispatch
			ubc_dispatch(hostname,'memory','bytes','ram',ram)
			ubc_dispatch(hostname,'memory','bytes','ram_swap',ramswap)
			ubc_dispatch(hostname,'memory','bytes','swap',swap)
			ubc_dispatch(hostname,'memory','bytes','allocated',alloc)

			for name,values in ct.iteritems():
				ubc_dispatch(hostname,'failcount','counter',name,values['failcnt'])

			ubc_interface(hostname,ctid)

	except Exception:
		#raise
		collectd.error('Openvz plugin: Something went wrong')
		return

collectd.register_config(ubc_config)
collectd.register_init(ubc_init)
collectd.register_read(ubc_read)
_______________________________________________
collectd mailing list
[email protected]
http://mailman.verplant.org/listinfo/collectd

Reply via email to