Exemplo n.º 1
0
/**
 * \brief Compare old and new channels and produce change flags
 *
 * This function compares channels and as result it produces flags with
 * changes. For more information see #PEVENTS_CHANGE.
 * \note The function MUST be successfully mapped, otherwise information
 *   do not make sense.
 * \param[in] ch_old Old channel (from a profiler)
 * \param[in] ch_new New channel (from a profiler)
 * \return Flags with changes.
 */
static uint16_t
pevents_update_mapper_change_flags(void *ch_old, void *ch_new)
{
	void *prfl_new = channel_get_profile(ch_new);
	void *prfl_old = channel_get_profile(ch_old);

	uint16_t flags = 0U;

	// Check storage directory
	const char *dir_new = profile_get_directory(prfl_new);
	const char *dir_old = profile_get_directory(prfl_old);
	if (strcmp(dir_new, dir_old) != 0) {
		// The directory has been changed
		flags |= PEVENTS_CHANGE_DIR;
	}

	// Check profile type
	enum PROFILE_TYPE type_new = profile_get_type(prfl_new);
	enum PROFILE_TYPE type_old = profile_get_type(prfl_old);
	if (type_new != type_old) {
		// The type has been changed
		flags |= PEVENTS_CHANGE_TYPE;
	}

	return flags;
}
Exemplo n.º 2
0
/**
 * \brief Store metadata information
 */
void Storage::storeMetadata(metadata* mdata)
{
	std::stringstream ss;
	
	/* Geolocation info */
	ss << "\"srcAS\": \"" << mdata->srcAS << "\", ";
	ss << "\"dstAS\": \"" << mdata->dstAS << "\", ";
	ss << "\"srcCountry\": \"" << mdata->srcCountry << "\", ";
	ss << "\"dstCountry\": \"" << mdata->dstCountry << "\", ";
	ss << "\"srcName\": \"" << mdata->srcName << "\", ";
	ss << "\"dstName\": \"" << mdata->dstName << "\", ";

	record += ss.str();

	
	/* Profiles */
	STR_APPEND(record, "\"profiles\": [");
	if (mdata->channels) {
		// Get name of root profile
		void *profile_ptr = NULL;
		void *prev_profile_ptr = NULL;
		const char *root_profile_name;

		profile_ptr = channel_get_profile(mdata->channels[0]);
		while (profile_ptr != NULL) {
			prev_profile_ptr = profile_ptr;
			profile_ptr = profile_get_parent(profile_ptr);
		}
		root_profile_name = profile_get_name(prev_profile_ptr);

		// Process all channels
		for (int i = 0; mdata->channels[i] != 0; ++i) {
			if (i > 0) {
				STR_APPEND(record, ", ");
			}

			STR_APPEND(record, "{\"profile\": \"");
			record += root_profile_name;
			STR_APPEND(record, "/");
			record += profile_get_path(channel_get_profile(mdata->channels[i]));

			STR_APPEND(record, "\", \"channel\": \"");
			record += channel_get_name(mdata->channels[i]);
			STR_APPEND(record, "\"}");
		}
	}
	record += ']';
}
Exemplo n.º 3
0
/**
 * \brief Load a new profile tree and apply changes to the current
 *   configuration.
 * \warning All bitsets will be cleared because order of profiles/channels can
 *   be changed.
 * \param[in] mgr     Event manager
 * \param[in] channel Any channel from the new profile tree
 * \return On success returns 0. Otherwise returns a non-zero value and
 *   the current configuration is not modified.
 */
static int
pevents_reload(pevents_t *mgr, const void *channel)
{
	// Find root profile of the new tree
	void *tree_root = channel_get_profile((void *)channel); // UGLY!
	void *tmp_profile = tree_root;

	while (tmp_profile) {
		tree_root = tmp_profile;
		tmp_profile = profile_get_parent(tmp_profile);
	}

	// Create an update structure and parse the tree
	struct pevents_update *update;
	update = pevents_update_create(mgr, tree_root);
	if (!update) {
		return 1;
	}

	// Map the current channels/profiles to the new channels/profiles
	pevents_update_mapper(mgr, update);
	// Apply the update
	pevents_update_apply(mgr, update);
	// The update structure has been adsorbed by update... (no free)
	return 0;
}
Exemplo n.º 4
0
/**
 * \brief Store metadata information
 */
void Storage::storeMetadata(metadata* mdata)
{
	std::stringstream ss;
	
	/* Geolocation info */
	ss << "\"srcAS\": \"" << mdata->srcAS << "\", ";
	ss << "\"dstAS\": \"" << mdata->dstAS << "\", ";
	ss << "\"srcCountry\": \"" << mdata->srcCountry << "\", ";
	ss << "\"dstCountry\": \"" << mdata->dstCountry << "\", ";
	ss << "\"srcName\": \"" << mdata->srcName << "\", ";
	ss << "\"dstName\": \"" << mdata->dstName << "\", ";

	record += ss.str();

	
	/* Profiles */
	if (mdata->channels) {
		record += "\"profiles\": [";

		for (int i = 0; mdata->channels[i] != 0; ++i) {
			if (i > 0) {
				record += ", ";
			}

			record += "{\"profile\": \"";
			record += channel_get_name(mdata->channels[i]);
			record += "\", \"channel\": \"";
			record += profile_get_name(channel_get_profile(mdata->channels[i]));
			record += "\"}";
		}

		record += "]";
	}
}
Exemplo n.º 5
0
void store_record(struct metadata* mdata, struct lnfstore_conf *conf)
{
	static uint8_t buffer[(uint16_t)-1];
	
	static lnf_rec_t *recp = NULL;
	static lnf_file_t *lfp = NULL; 
	stack_t *smap = conf->pst;

	if( conf->profiles && !mdata->channels ){
		//Record wont be stored, it does not belong to any channel and profiling is activated
		return;
	}

	if(recp == NULL) {
		lnf_rec_init(&recp);
	} else { 
		lnf_rec_clear(recp);
	}

    uint16_t offset, length;
    offset = 0;
    struct ipfix_template *templ = mdata->record.templ;
    uint8_t *data_record = (uint8_t*) mdata->record.record;

    /* get all fields */
    for (uint16_t count = 0, index = 0; count < templ->field_count; ++count, ++index) {
		
		struct ipfix_lnf_map *item, key;
        
        /* Get Enterprise number and ID */
        key.ie = templ->fields[index].ie.id;
        length = templ->fields[index].ie.length;
        key.en = 0;

        if (key.ie & 0x8000) {
			key.ie &= 0x7fff;
            key.en = templ->fields[++index].enterprise_number;
        }

		item = bsearch(&key, tr_table, MAX_TABLE , sizeof(struct ipfix_lnf_map), ipfix_lnf_map_compare);
		
		int problem = 1;
		if(item != NULL){
			problem = item->func(data_record, &offset, &length, buffer, item);	
			lnf_rec_fset(recp, item->lnf_id, buffer);
		}
		
		if(problem){
			length = real_length(data_record, &offset, length);
		}

        offset += length;
    } //end of element processing

	//!< Decide whether close close files and create new time window
    time_t now = time(NULL);
    if(difftime(now, conf->t_vars->window_start) > conf->time_window){

		mktime_window(now, conf);
		if( conf->profiles ){
			prec_t* item = NULL;
			//Close all old files
			for(unsigned x = 0; x < smap->top; x += al4B(sizeof(prec_t))){
				item = (prec_t*)smap->data + x; 
				if(item->lfp == NULL)
					continue;
				lnf_close(item->lfp);
				item->lfp = NULL;
			}
		} else {
			lnf_close(lfp);
			lfp = NULL;
		}
    }
    
    if( conf->profiles ){
		//On stack allocation of bit array
		int ba_size = smap->top/(8*sizeof(int))+1;
		int ba[ba_size];
		memset(&ba[0], 0, ba_size*sizeof(int));
		int status = 0;
		
		prec_t *item = NULL;

		for( int i = 0; mdata->channels[i] != 0; i++ ){
			
			void* rec_prof = channel_get_profile(mdata->channels[i]);

			item = bsearch(&rec_prof, smap->data, smap->top/al4B(sizeof(prec_t)),
					al4B(sizeof(prec_t)), prec_compare);
			
			if( item == NULL ){
				//Profile is not in configuration
				prec_t entry;
				entry.address = rec_prof;
				entry.lfp = NULL;
				
				const char* prpath = profile_get_path(rec_prof);
				char* path = mkpath_string(conf, prpath);
				mkdir_hierarchy(path);
				status = lnf_open(&entry.lfp, path, LNF_WRITE | (conf->compress? LNF_COMP : 0), (char*)conf->ident);

				stack_push(smap, &entry, sizeof(prec_t));
				//Add resilience
				qsort(smap->data, smap->top/al4B(sizeof(prec_t)), 
						al4B(sizeof(prec_t)), prec_compare);
				
				free(path);
			} else if( item->lfp == NULL ){
				//Profile file is closed
				const char* prpath = profile_get_path(rec_prof);
				char* path = mkpath_string(conf, prpath);
				mkdir_hierarchy(path);
				status = lnf_open(&item->lfp, path, LNF_WRITE | (conf->compress? LNF_COMP : 0), (char*)conf->ident);
				//Get prof name and open aprop file
				free(path);
			}
		}
		void* profile = NULL;
		int i = 0;
		for(profile = channel_get_profile(mdata->channels[i=0]);
				mdata->channels[i] != 0;
				profile = channel_get_profile(mdata->channels[i++]) ){

			item = bsearch(&profile, smap->data, smap->top/al4B(sizeof(prec_t)),
					al4B(sizeof(prec_t)), prec_compare);
				
			int index = (((base_t*)item) - smap->data)/al4B(sizeof(prec_t));
			if( !GETb(ba, index) ){ //And profile is not shadow
				status = lnf_write(item->lfp, recp);
				SETb(ba, index, 1);
			}
		}
	} else {
		if( lfp == NULL ){
			int status;
			char* path = mkpath_string(conf, NULL);
			status = mkdir_hierarchy(path);
			status |= lnf_open(&lfp, path, LNF_WRITE | (conf->compress ? LNF_COMP : 0), (char*)conf->ident);
			if(status != LNF_OK) {
				MSG_ERROR(msg_module, "Failed to open file! ...at path: %s \n", path);
			}
			free(path);
		}
		lnf_write(lfp, recp);
	}
	return;
}