zabbix-agent2/src/zabbix_server/ha/ha_manager.c
/******************************************************************************
 *                                                                            *
 * Function: zbx_ha_recv_status                                               *
 *                                                                            *
 * Purpose: handle HA manager notifications                                   *
 *                                                                            *
 * Comments: This function also monitors heartbeat notifications and          *
 *           returns standby status if no heartbeats are received for         *
 *           failover delay - poll period seconds. This would make main       *
 *           process to switch to standby mode and initiate teardown process  *
 *                                                                            *
 ******************************************************************************/
zbx_ha_recv_status

Purpose: handle HA manager notifications

Comments: This function also monitors heartbeat notifications and
returns standby status if no heartbeats are received for
failover delay - poll period seconds. This would make main
process to switch to standby mode and initiate teardown process
zbx_ha_recv_status Purpose : handle HA manager notifications Comments : This function also monitors heartbeat notifications and returns standby status if no heartbeats are received for failover delay - poll period seconds . This would make main process to switch to standby mode and initiate teardown process
int	zbx_ha_recv_status(int timeout, int *ha_status, char **error)
{
	zbx_ipc_message_t	*message = NULL;
	int			ret = SUCCEED, ha_status_old;
	time_t			now;
	static time_t		last_hb;
	static int		ha_failover_delay = ZBX_HA_DEFAULT_FAILOVER_DELAY;
	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
	while (1)
	{
		unsigned char	*ptr;
		zbx_uint32_t	len;
		if (SUCCEED != zbx_ipc_async_socket_recv(&ha_socket, timeout, &message))
		{
			*ha_status = ZBX_NODE_STATUS_ERROR;
			*error = zbx_strdup(NULL, "cannot receive message from HA manager service");
			ret = FAIL;
			goto out;
		}
		now = time(NULL);
		if (NULL == message)
			break;
		switch (message->code)
		{
			case ZBX_IPC_SERVICE_HA_UPDATE:
				ha_status_old = *ha_status;
				ptr = message->data;
				ptr += zbx_deserialize_value(ptr, ha_status);
				ptr += zbx_deserialize_value(ptr, &ha_failover_delay);
				(void)zbx_deserialize_str(ptr, error, len);
				if (ZBX_NODE_STATUS_ERROR == *ha_status)
				{
					zbx_ipc_message_free(message);
					ret = FAIL;
					goto out;
				}
				if (ha_status_old != *ha_status)
					last_hb = now;
				break;
			case ZBX_IPC_SERVICE_HA_HEARTBEAT:
				last_hb = now;
				break;
		}
		zbx_ipc_message_free(message);
		timeout = 0;
	}
	if (ZBX_HA_IS_CLUSTER() && *ha_status == ZBX_NODE_STATUS_ACTIVE && 0 != last_hb)
	{
		if (last_hb + ha_failover_delay - ZBX_HA_POLL_PERIOD <= now || now < last_hb)
			*ha_status = ZBX_NODE_STATUS_STANDBY;
	}
out:
	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
	return ret;
}
int zbx_ha_recv_status ( int timeout , int * ha_status , char * * error ) { zbx_ipc_message_t * message = NULL ; int ret = SUCCEED , ha_status_old ; time_t now ; static time_t last_hb ; static int ha_failover_delay = ZBX_HA_DEFAULT_FAILOVER_DELAY ; zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ ) ; while ( 1 ) { unsigned char * ptr ; zbx_uint32_t len ; if ( SUCCEED != zbx_ipc_async_socket_recv ( & ha_socket , timeout , & message ) ) { * ha_status = ZBX_NODE_STATUS_ERROR ; * error = zbx_strdup ( NULL , " " ) ; ret = FAIL ; goto out ; } now = time ( NULL ) ; if ( NULL == message ) break ; switch ( message -> code ) { case ZBX_IPC_SERVICE_HA_UPDATE : ha_status_old = * ha_status ; ptr = message -> data ; ptr += zbx_deserialize_value ( ptr , ha_status ) ; ptr += zbx_deserialize_value ( ptr , & ha_failover_delay ) ; ( void ) zbx_deserialize_str ( ptr , error , len ) ; if ( ZBX_NODE_STATUS_ERROR == * ha_status ) { zbx_ipc_message_free ( message ) ; ret = FAIL ; goto out ; } if ( ha_status_old != * ha_status ) last_hb = now ; break ; case ZBX_IPC_SERVICE_HA_HEARTBEAT : last_hb = now ; break ; } zbx_ipc_message_free ( message ) ; timeout = 0 ; } if ( ZBX_HA_IS_CLUSTER ( ) && * ha_status == ZBX_NODE_STATUS_ACTIVE && 0 != last_hb ) { if ( last_hb + ha_failover_delay - ZBX_HA_POLL_PERIOD <= now || now < last_hb ) * ha_status = ZBX_NODE_STATUS_STANDBY ; } out : zabbix_log ( LOG_LEVEL_DEBUG , " " , __func__ , zbx_result_string ( ret ) ) ; return ret ; }
Function: zbx_ha_recv_status                                               
                                                                            
 Purpose: handle HA manager notifications                                   
                                                                            
 Comments: This function also monitors heartbeat notifications and          
           returns standby status if no heartbeats are received for         
           failover delay - poll period seconds.
Function : zbx_ha_recv_status Purpose : handle HA manager notifications Comments : This function also monitors heartbeat notifications and returns standby status if no heartbeats are received for failover delay - poll period seconds .
/* reset heartbeat on status change */ /* reset timeout for getting pending messages */