/******************************************************************************
 *                                                                            *
 * Function: main_discoverer_loop                                             *
 *                                                                            *
 * Purpose: periodically try to find new hosts and services                   *
 *                                                                            *
 * Author: Alexei Vladishev                                                   *
 *                                                                            *
 * Comments: executes once per 30 seconds (hardcoded)                         *
 *                                                                            *
 ******************************************************************************/
void	main_discoverer_loop()
{
	int	now, nextcheck, sleeptime;
	double	sec;

	zabbix_log(LOG_LEVEL_DEBUG, "In main_discoverer_loop() process_num:%d", process_num);

	zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type));

	DBconnect(ZBX_DB_CONNECT_NORMAL);

	for (;;)
	{
		zbx_setproctitle("%s [performing discovery]", get_process_type_string(process_type));

		now = time(NULL);
		sec = zbx_time();
		process_discovery(now);
		sec = zbx_time() - sec;

		zabbix_log(LOG_LEVEL_DEBUG, "%s #%d spent " ZBX_FS_DBL " seconds while processing rules",
				get_process_type_string(process_type), process_num, sec);

		nextcheck = get_minnextcheck(now);
		sleeptime = calculate_sleeptime(nextcheck, DISCOVERER_DELAY);

		zbx_sleep_loop(sleeptime);
	}
}
Example #2
0
/******************************************************************************
 *                                                                            *
 * Function: main_discoverer_loop                                             *
 *                                                                            *
 * Purpose: periodically try to find new hosts and services                   *
 *                                                                            *
 * Author: Alexei Vladishev                                                   *
 *                                                                            *
 * Comments: executes once per 30 seconds (hardcoded)                         *
 *                                                                            *
 ******************************************************************************/
void	main_discoverer_loop(void)
{
	int	now, nextcheck, sleeptime = -1, rule_count = 0, old_rule_count = 0;
	double	sec, total_sec = 0.0, old_total_sec = 0.0;
	time_t	last_stat_time;

#define STAT_INTERVAL	5	/* if a process is busy and does not sleep then update status not faster than */
				/* once in STAT_INTERVAL seconds */

	zbx_setproctitle("%s #%d [connecting to the database]", get_process_type_string(process_type), process_num);
	last_stat_time = time(NULL);

	DBconnect(ZBX_DB_CONNECT_NORMAL);

	for (;;)
	{
		if (0 != sleeptime)
		{
			zbx_setproctitle("%s #%d [processed %d rules in " ZBX_FS_DBL " sec, performing discovery]",
					get_process_type_string(process_type), process_num, old_rule_count,
					old_total_sec);
		}

		now = time(NULL);
		sec = zbx_time();
		rule_count += process_discovery(now);
		total_sec += zbx_time() - sec;

		nextcheck = get_minnextcheck(now);
		sleeptime = calculate_sleeptime(nextcheck, DISCOVERER_DELAY);

		if (0 != sleeptime || STAT_INTERVAL <= time(NULL) - last_stat_time)
		{
			if (0 == sleeptime)
			{
				zbx_setproctitle("%s #%d [processed %d rules in " ZBX_FS_DBL " sec, performing "
						"discovery]", get_process_type_string(process_type), process_num,
						rule_count, total_sec);
			}
			else
			{
				zbx_setproctitle("%s #%d [processed %d rules in " ZBX_FS_DBL " sec, idle %d sec]",
						get_process_type_string(process_type), process_num, rule_count,
						total_sec, sleeptime);
				old_rule_count = rule_count;
				old_total_sec = total_sec;
			}
			rule_count = 0;
			total_sec = 0.0;
			last_stat_time = time(NULL);
		}

		zbx_sleep_loop(sleeptime);
	}

#undef STAT_INTERVAL
}
void do_udp_config(uint8_t sock)
{
	uint8_t sckState;
	uint16_t len;
	Remote_Info remote_info;
	WIZnet_Header wiznet_header;

	getsockopt(sock, SO_STATUS, &sckState);
	switch (sckState) {
	case SOCK_UDP:
		getsockopt(sock, SO_RECVBUF, &len);
		if (len >= sizeof(WIZnet_Header)) {
			recvfrom(sock, (uint8_t *)&wiznet_header, sizeof(WIZnet_Header), remote_info.ip, &remote_info.port);

			if(wiznet_header.stx != STX)
				break;

			if(wiznet_header.op_code[1] != WIZNET_REQUEST)
				break;

			switch(wiznet_header.op_code[0] & 0xF0) {
			case DISCOVERY:
				process_discovery(sock, &remote_info, &wiznet_header);
				break;
			case GET_INFO:
				process_get_info(sock, &remote_info, &wiznet_header);
				break;
			case SET_INFO:
				process_set_info(sock, &remote_info, &wiznet_header);
				break;
			case FIRMWARE_UPLOAD:
				process_firmware_upload_init(sock, &remote_info, &wiznet_header);
				break;
			case REMOTE_RESET:
				process_remote_reset(sock, &remote_info, &wiznet_header);
				break;
			case FACTORY_RESET:
				process_factory_reset(sock, &remote_info, &wiznet_header);
				break;
			default:
				break;
			}
		}

#if 0
		// flushing remained data..
		getsockopt(sock, SO_REMAINSIZE, &len);
		if(len > 0) {
			do {
				if(len > BUFFER_SIZE) {
					recvfrom(sock, buffer, BUFFER_SIZE, rIP, &rPort);
					len -= BUFFER_SIZE;

				} else {
					recvfrom(sock, buffer, len, rIP, &rPort);
					len = 0;

				}
			} while(len != 0);
		}
#endif
		break;
	case SOCK_CLOSED:
		socket(sock, Sn_MR_UDP, REMOTE_CLIENT_PORT, 0);
		break;
	}
}