Esempio n. 1
0
def task_handler(log, task):
	conf = task['conf']
	mctx = conf['modules'][module.ctx_index]
	command = mctx['command']
	stype = conf['ctx']['type']
	timeout = mctx['command_timeout']
	now = time.time()
	try:
		try:
			out, err = cmd.call(command, timeout)
			if len(err):
				log.error('%s "%s" "%s"' %(task['task_info'], mctx['command'], err))
		finally:
			use_time = time.time() - now
	except cmd.error, e:
		log.error('%s "%s" %.6fs "%s"' %(task['task_info'], mctx['command'], use_time, e))
		return None
Esempio n. 2
0
def get_status(log, **kwargs):
	status = {}	
	if 'gets' in kwargs:
		for x in kwargs['gets']:
			tmp = {}
			action = x[0]
			action_args = []
			if len(x) > 1:
				action_args = x[1:]
			if action.handler == None:
				continue
			s = action.handler(log, *action_args)
			if s == None:
				continue
			if len(action_args) == action.nargs:
				for key in s:
					status['%s.%s' %(action.prefix, key)] = s[key]
			else:
				for key in s:
					for akey in action_args[action.nargs:]:
						if action.args[akey] == None:
							continue
						if in_list(action.args[akey], key):
							status['%s.%s' %(action.prefix, key)] = s[key]
	if 'commands' in kwargs:
		for key in kwargs['commands']:
			now = time.time()
			command = kwargs['commands'][key][0]
			timeout = kwargs['command_timeout']
			if kwargs['commands'][key][1] != None:
				timeout = kwargs['commands'][key][1]
			log.debug('%s "%s" execute %s' %(kwargs['info'], key, command))
			try:
				try:
					out, err = cmd.call(command, timeout)
					if len(err):
						log.error('%s "%s" "%s"' %(kwargs['info'], key, err))
				finally:
					use_time = time.time() - now
			except cmd.error, e:
				log.error('%s "%s" %.6fs "%s"' %(kwargs['info'], key, use_time, e))
				return None
			log.info('%s "%s" %.6fs' %(kwargs['info'], key, use_time))
			status[key] = re.sub(r'\n$', '',  out)