Example #1
0
	def get_yaml(self, yamlfile):
		#return self.load_yaml(yamlfile)
		logging.debug('getting yamlfile %s' % yamlfile)
		inicio = util.start_counter()
		yaml_content = {}
		if yamlfile.endswith(".yaml"):
			yaml_content = cache.get(yamlfile)
			if not yaml_content:
				logging.debug('yamlfile %s not cached' % yamlfile)
				yaml_content = util.load_yaml(self.reportdir + '/' + self.name + '/' + yamlfile)
				cache.set(yamlfile, yaml_content)
			else:
				logging.debug('yamlfile %s returned from cache' % yamlfile)
				
			self.yamls.append(yaml_content)
		util.elapsed(inicio)
		return yaml_content
Example #2
0
def run():

	"""start program run"""

	print 'PROJECT_ROOT: %s' % PROJECT_ROOT
	print 'CACHE_TIMEOUT: %s' % CACHE_TIMEOUT

	inicio = util.start_counter()
	
	print 'listing monitors and users...'
	user_list = UserRepository.list_user()
	monitor_list = MonitorRepository.list_monitor()

	print 'monitor list: %s' % monitor_list
	print 'user list: %s' % user_list

	print 'done...'

	user_hosts_dict = {}
	hosts =  os.listdir(settings.REPORTDIR)
	#lista com os emails para serem enviados
	
	for monitor in monitor_list:
		print '#' * 80
		print '##Checking monitors for user %s' % monitor.user.email
		print 'email buffer: %s' % email_buffer
		host_monitors = monitor.get_host_monitors()
		#check if the user has a monitor for the host, if he has then it means that he wants everything from host
		monitoring_host = False
		#caches the hosts for the user
		hosts_for_user = user_hosts_dict.get(monitor.user.email,None)
		if not hosts_for_user:
			hosts_for_user = monitor.get_hosts()
			user_hosts_dict[monitor.user.email] = hosts_for_user

		for host in hosts:
			print 'Reading host: %s' % host
			puppet_host = puppetHost(host, settings.REPORTDIR)
			yamlfile = puppet_host.get_last_yaml()
			if not yamlfile:
				continue
				
			print 'last yamlfile: %s' % yamlfile
			yaml = puppet_host.get_yaml(yamlfile)
			logs = yaml['logs']
			if len(logs) == 0:
				continue		
			
			##
			for host_ in hosts_for_user:
				if host.startswith(host_):
					monitoring_host = True
					break
			
			if monitoring_host:
				print '####user %s is monitoring %s' % (monitor.user.email, host)
				email_body = ''
				for log in logs:
					source = log['source']
					email_body_aux = '''
	<tr class=""><td>%s</td><td colspan=6>%s</td></tr>
	<tr class=""><td></td><td colspan=6>&nbsp;&nbsp;%s</td></tr>
	''' % (log['level'], log['message'], source)
					email_body = email_body + email_body_aux
					
				key_cache = monitor.user.email + '_' + host
				#key_cache = monitor.user.email + '_' + email
				#send_email(email_body, monitor, yamlfile, key_cache, host)
				add_to_email_buffer(email_body, monitor, yamlfile, key_cache, host)
			
			else:
				resource_monitors = monitor.get_resource_monitors()
				for resource_monitor in resource_monitors:
					pattern = resource_monitor.pattern
					email_body = ''
					for log in logs:
						source = log['source']
						#print 'pattern: %s' % monitor.pattern
						#regex = re.compile(monitor.pattern)
						#matchobj = regex.search(source)
						matchobj = source.find(pattern)
						if matchobj != -1:
							email_body_aux = '''
		<tr class=""><td>%s</td><td colspan=6>%s</td></tr>
		<tr class=""><td></td><td colspan=6>&nbsp;&nbsp;%s</td></tr>
		''' % (log['level'], log['message'], source)
							#red color on the pattern
							email_body_aux = email_body_aux.replace(pattern,'<b><font color=red>'+pattern+'</b></font>')
							email_body = email_body + email_body_aux
							
					#sends email if not already sent
					key_cache = monitor.user.email
					#send_email(email_body, monitor, yamlfile, key_cache, host)
					add_to_email_buffer(email_body, monitor, yamlfile, key_cache, host)
			
			#send_email_from_buffer
			print 'sending email from buffer'
			send_email_from_buffer(monitor)
		monitor.update_last_run()
		print '*' * 60

	gen_time = '%.2f' % util.elapsed(inicio)
	print '%s min' % gen_time