Exemple #1
0
void simulate(state before, state& next)
{
    vector3 wind_map[2][180][360];
    double volume_flux[2][180][360];
    double temp_sum[2][180][360];
    int i,j,k;
    for(i=0;i<2;++i) for(j=0;j<180;++j) for(k=0;k<360;++k){
        wind_map    [i][j][k]=getWind(i,j)*wind_size;
        volume_flux [i][j][k]=0;
        temp_sum    [i][j][k]=0;
    }
    int dir;
    int next_i, next_j, next_k;
    double my_out, my_left;
    int dx,dy,dz;
    for(i=0;i<2;++i) for(j=0;j<180;++j) for(k=0;k<360;++k){
        my_out=0;
        for(dir=0;dir<26;++dir)
        {
            for(dx=-1; dx<=1; ++dx)
            for(dy=-1; dy<=1; ++dy)
            for(dz=-1; dz<=1; ++dz){
                if(dx==0 && dy==0 && dz==0) continue;
                next_i = i + dx;
                next_j = j + dy;
                next_k = k + dz;
                if(next_i>=0 && next_j>=0  && next_k>=0 &&
                   next_i<2  && next_j<180 && next_k<360){
                    double cur_volume = dot(wind_map[i][j][k],dr[dir].inv());
                    if(cur_volume<0) continue;
                    volume_flux[next_i][next_j][next_k]+=cur_volume;
                    temp_sum   [next_i][next_j][next_k]+=cur_volume*before.temperature[i][j][k];
                    my_out+=cur_volume;
                }
            }
        }
        my_left = total_volume - my_out;
        volume_flux[i][j][k] += my_left;
        temp_sum   [i][j][k] += my_left*before.temperature[i][j][k];
    }
    for(i=0;i<2;++i) for(j=0;j<180;++j) for(k=0;k<360;++k) next.temperature[i][j][k]=temp_sum[i][j][k]/volume_flux[i][j][k];
    next.year  = before.year;
    next.month = before.month;
    next.day   = before.day;
    next.hour  = before.hour + 1;
    if(next.hour == 24) {
        ++next.day;
        next.hour=0;
        if(next.day > day_in_month(next.year,next.month)){
            next.day=1; ++next.month;
            if(next.month==13){
                next.month=1; ++next.year;
            }
        }
    }
Exemple #2
0
static int	process_maintenance(void)
{
	const char			*__function_name = "process_maintenance";
	DB_RESULT			result;
	DB_ROW				row;
	int				day, week, wday, sec;
	struct tm			*tm;
	zbx_uint64_t			db_maintenanceid;
	time_t				now, db_active_since, active_since, db_start_date, maintenance_from;
	zbx_timeperiod_type_t		db_timeperiod_type;
	int				db_every, db_month, db_dayofweek, db_day, db_start_time,
					db_period, db_maintenance_type;
	static zbx_host_maintenance_t	*hm = NULL;
	static int			hm_alloc = 4;
	int				hm_count = 0, res;

	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);

	if (NULL == hm)
		hm = zbx_malloc(hm, sizeof(zbx_host_maintenance_t) * hm_alloc);

	now = time(NULL);
	tm = localtime(&now);
	sec = tm->tm_hour * SEC_PER_HOUR + tm->tm_min * SEC_PER_MIN + tm->tm_sec;

	result = DBselect(
			"select m.maintenanceid,m.maintenance_type,m.active_since,"
				"tp.timeperiod_type,tp.every,tp.month,tp.dayofweek,"
				"tp.day,tp.start_time,tp.period,tp.start_date"
			" from maintenances m,maintenances_windows mw,timeperiods tp"
			" where m.maintenanceid=mw.maintenanceid"
				" and mw.timeperiodid=tp.timeperiodid"
				" and m.active_since<=%d"
				" and m.active_till>%d",
			now, now);

	while (NULL != (row = DBfetch(result)))
	{
		ZBX_STR2UINT64(db_maintenanceid, row[0]);
		db_maintenance_type	= atoi(row[1]);
		db_active_since		= atoi(row[2]);
		db_timeperiod_type	= atoi(row[3]);
		db_every		= atoi(row[4]);
		db_month		= atoi(row[5]);
		db_dayofweek		= atoi(row[6]);
		db_day			= atoi(row[7]);
		db_start_time		= atoi(row[8]);
		db_period		= atoi(row[9]);
		db_start_date		= atoi(row[10]);

		switch (db_timeperiod_type)
		{
			case TIMEPERIOD_TYPE_ONETIME:
				break;
			case TIMEPERIOD_TYPE_DAILY:
				db_start_date = now - sec + db_start_time;
				if (sec < db_start_time)
					db_start_date -= SEC_PER_DAY;

				if (db_start_date < db_active_since)
					continue;

				tm = localtime(&db_active_since);
				active_since = db_active_since - (tm->tm_hour * SEC_PER_HOUR + tm->tm_min * SEC_PER_MIN
						+ tm->tm_sec);

				day = (db_start_date - active_since) / SEC_PER_DAY;
				db_start_date -= SEC_PER_DAY * (day % db_every);
				break;
			case TIMEPERIOD_TYPE_WEEKLY:
				db_start_date = now - sec + db_start_time;
				if (sec < db_start_time)
					db_start_date -= SEC_PER_DAY;

				if (db_start_date < db_active_since)
					continue;

				tm = localtime(&db_active_since);
				wday = (0 == tm->tm_wday ? 7 : tm->tm_wday) - 1;
				active_since = db_active_since - (wday * SEC_PER_DAY + tm->tm_hour * SEC_PER_HOUR +
						tm->tm_min * SEC_PER_MIN + tm->tm_sec);

				for (; db_start_date >= db_active_since; db_start_date -= SEC_PER_DAY)
				{
					/* check for every x week(s) */
					week = (db_start_date - active_since) / SEC_PER_WEEK;
					if (0 != week % db_every)
						continue;

					/* check for day of the week */
					tm = localtime(&db_start_date);
					wday = (0 == tm->tm_wday ? 7 : tm->tm_wday) - 1;
					if (0 == (db_dayofweek & (1 << wday)))
						continue;

					break;
				}
				break;
			case TIMEPERIOD_TYPE_MONTHLY:
				db_start_date = now - sec + db_start_time;
				if (sec < db_start_time)
					db_start_date -= SEC_PER_DAY;

				for (; db_start_date >= db_active_since; db_start_date -= SEC_PER_DAY)
				{
					/* check for month */
					tm = localtime(&db_start_date);
					if (0 == (db_month & (1 << tm->tm_mon)))
						continue;

					if (0 != db_day)
					{
						/* check for day of the month */
						if (db_day != tm->tm_mday)
							continue;
					}
					else
					{
						/* check for day of the week */
						wday = (0 == tm->tm_wday ? 7 : tm->tm_wday) - 1;
						if (0 == (db_dayofweek & (1 << wday)))
							continue;

						/* check for number of day (first, second, third, fourth or last) */
						day = (tm->tm_mday - 1) / 7 + 1;
						if (5 == db_every && 4 == day)
						{
							if (tm->tm_mday + 7 <= day_in_month(tm->tm_year, tm->tm_mon))
								continue;
						}
						else if (db_every != day)
						{
							continue;
						}
					}

					break;
				}
				break;
			default:
				continue;
		}

		/* allow one time periods to start before active time */
		if (db_start_date < db_active_since && TIMEPERIOD_TYPE_ONETIME != db_timeperiod_type)
			continue;

		if (db_start_date > now || now >= db_start_date + db_period)
			continue;

		maintenance_from = db_start_date;

		if (maintenance_from < db_active_since)
			maintenance_from = db_active_since;

		process_maintenance_hosts(&hm, &hm_alloc, &hm_count, maintenance_from, db_maintenanceid, db_maintenance_type);
	}
	DBfree_result(result);

	update_maintenance_hosts(hm, hm_count, (int)now);

	res = hm_count;

	while (0 != hm_count--)
		zbx_free(hm[hm_count].host);

	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
	return res;
}