void setup_events_with_host(time_t time) { timed_event *new_event = NULL; /* First service is a normal one */ if (svc3 == NULL) svc3 = (service *)malloc(sizeof(service)); svc3->host_name = strdup("Host0"); svc3->description = strdup("Normal service"); svc3->check_options = 0; svc3->next_check = time; svc3->state_type = SOFT_STATE; svc3->current_state = STATE_OK; svc3->retry_interval = 1; svc3->check_interval = 5; new_event = (timed_event *)malloc(sizeof(timed_event)); new_event->event_type = EVENT_SERVICE_CHECK; new_event->event_data = (void *)svc3; new_event->event_args = (void *)NULL; new_event->event_options = 0; new_event->run_time = 0L; /* Straight away */ new_event->recurring = FALSE; new_event->event_interval = 0L; new_event->timing_func = NULL; new_event->compensate_for_time_change = TRUE; reschedule_event(new_event, &event_list_low, &event_list_low_tail); if (host1 == NULL) host1 = (host *)malloc(sizeof(host)); host1->name = strdup("Host1"); host1->address = strdup("127.0.0.1"); host1->address = strdup("::1"); host1->retry_interval = 1; host1->check_interval = 5; host1->check_options = 0; host1->next_check = time; new_event->recurring = TRUE; host1->state_type = SOFT_STATE; host1->current_state = STATE_OK; new_event = (timed_event *)malloc(sizeof(timed_event)); new_event->event_type = EVENT_HOST_CHECK; new_event->event_data = (void *)host1; new_event->event_args = (void *)NULL; new_event->event_options = 0; new_event->run_time = 0L; /* Straight away */ new_event->recurring = TRUE; new_event->event_interval = 0L; new_event->timing_func = NULL; new_event->compensate_for_time_change = TRUE; reschedule_event(new_event, &event_list_low, &event_list_low_tail); }
void setup_events(time_t time) { timed_event *new_event = NULL; /* First service is a normal one */ svc1 = (service *)malloc(sizeof(service)); svc1->host_name = strdup("Host1"); svc1->description = strdup("Normal service"); svc1->check_options = 0; svc1->next_check = time; svc1->state_type = SOFT_STATE; svc1->current_state = STATE_OK; svc1->retry_interval = 1; svc1->check_interval = 5; new_event = (timed_event *)malloc(sizeof(timed_event)); new_event->event_type = EVENT_SERVICE_CHECK; new_event->event_data = (void *)svc1; new_event->event_args = (void *)NULL; new_event->event_options = 0; new_event->run_time = 0L; /* Straight away */ new_event->recurring = FALSE; new_event->event_interval = 0L; new_event->timing_func = NULL; new_event->compensate_for_time_change = TRUE; reschedule_event(new_event, &event_list_low, &event_list_low_tail); /* Second service is one that will get nudged forward */ svc2 = (service *)malloc(sizeof(service)); svc2->host_name = strdup("Host1"); svc2->description = strdup("To be nudged"); svc2->check_options = 0; svc2->next_check = time; svc2->state_type = SOFT_STATE; svc2->current_state = STATE_OK; svc2->retry_interval = 1; svc2->check_interval = 5; new_event = (timed_event *)malloc(sizeof(timed_event)); new_event->event_type = EVENT_SERVICE_CHECK; new_event->event_data = (void *)svc2; new_event->event_args = (void *)NULL; new_event->event_options = 0; new_event->run_time = 0L; /* Straight away */ new_event->recurring = FALSE; new_event->event_interval = 0L; new_event->timing_func = NULL; new_event->compensate_for_time_change = TRUE; reschedule_event(new_event, &event_list_low, &event_list_low_tail); }
static void remove_tx_locks(int tx_id, data_set_entry *data_set, state_type *state, double now) { SERVER_lp_state_type *pointer = &state->type.server_state; data_set_entry *entry = data_set; while (entry != NULL) { int need_to_unlock = 0; if (pointer->configuration.concurrency_control_type == ETL_2PL || pointer->configuration.concurrency_control_type == CTL_2PL) need_to_unlock = is_owner(entry->object_key_id, pointer->server_id, state->num_servers, state->num_clients, state->object_replication_degree); else if (pointer->configuration.concurrency_control_type == PRIMARY_OWNER_CTL_2PL) need_to_unlock = is_primary(entry->object_key_id, pointer->server_id, state->num_servers, state->num_clients); if (need_to_unlock) { if ((pointer->cc_metadata->locks[entry->object_key_id] != tx_id)) { if (pointer->configuration.cc_verbose) printf("cc%d - lock sull'oggetto %i della tx %d non trovato\n", pointer->server_id, entry->object_key_id, tx_id); } else { pointer->cc_metadata->locks[entry->object_key_id] = -1; if (pointer->configuration.cc_verbose) printf("cc%d - oggetto %d per la transizione %i unlockato al tempo %f \n", pointer->server_id, entry->object_key_id, tx_id, now); //Wake up waiting event for key 'object_key_id'; reschedule_event(state, now, entry->object_key_id); } } entry = entry->next; } }
/* this is the main event handler loop */ int event_execution_loop(void) { timed_event *temp_event, *last_event = NULL; time_t last_time = 0L; time_t current_time = 0L; time_t last_status_update = 0L; int poll_time_ms; log_debug_info(DEBUGL_FUNCTIONS, 0, "event_execution_loop() start\n"); time(&last_time); while (1) { struct timeval now; const struct timeval *event_runtime; int inputs; /* super-priority (hardcoded) events come first */ /* see if we should exit or restart (a signal was encountered) */ if (sigshutdown == TRUE || sigrestart == TRUE) break; /* get the current time */ time(¤t_time); if (sigrotate == TRUE) { rotate_log_file(current_time); update_program_status(FALSE); } /* hey, wait a second... we traveled back in time! */ if (current_time < last_time) compensate_for_system_time_change((unsigned long)last_time, (unsigned long)current_time); /* else if the time advanced over the specified threshold, try and compensate... */ else if ((current_time - last_time) >= time_change_threshold) compensate_for_system_time_change((unsigned long)last_time, (unsigned long)current_time); /* get next scheduled event */ current_event = temp_event = (timed_event *)squeue_peek(nagios_squeue); /* if we don't have any events to handle, exit */ if (!temp_event) { log_debug_info(DEBUGL_EVENTS, 0, "There aren't any events that need to be handled! Exiting...\n"); break; } /* keep track of the last time */ last_time = current_time; /* update status information occassionally - NagVis watches the NDOUtils DB to see if Nagios is alive */ if ((unsigned long)(current_time - last_status_update) > 5) { last_status_update = current_time; update_program_status(FALSE); } event_runtime = squeue_event_runtime(temp_event->sq_event); if (temp_event != last_event) { log_debug_info(DEBUGL_EVENTS, 1, "** Event Check Loop\n"); log_debug_info(DEBUGL_EVENTS, 1, "Next Event Time: %s", ctime(&temp_event->run_time)); log_debug_info(DEBUGL_EVENTS, 1, "Current/Max Service Checks: %d/%d (%.3lf%% saturation)\n", currently_running_service_checks, max_parallel_service_checks, ((float)currently_running_service_checks / (float)max_parallel_service_checks) * 100); } last_event = temp_event; gettimeofday(&now, NULL); poll_time_ms = tv_delta_msec(&now, event_runtime); if (poll_time_ms < 0) poll_time_ms = 0; else if (poll_time_ms >= 1500) poll_time_ms = 1500; log_debug_info(DEBUGL_SCHEDULING, 2, "## Polling %dms; sockets=%d; events=%u; iobs=%p\n", poll_time_ms, iobroker_get_num_fds(nagios_iobs), squeue_size(nagios_squeue), nagios_iobs); inputs = iobroker_poll(nagios_iobs, poll_time_ms); if (inputs < 0 && errno != EINTR) { logit(NSLOG_RUNTIME_ERROR, TRUE, "Error: Polling for input on %p failed: %s", nagios_iobs, iobroker_strerror(inputs)); break; } log_debug_info(DEBUGL_IPC, 2, "## %d descriptors had input\n", inputs); /* * if the event we peaked was removed from the queue from * one of the I/O operations, we must take care not to * try to run at, as we're (almost) sure to access free'd * or invalid memory if we do. */ if (!current_event) { log_debug_info(DEBUGL_EVENTS, 0, "Event was cancelled by iobroker input\n"); continue; } gettimeofday(&now, NULL); if (tv_delta_msec(&now, event_runtime) >= 0) continue; /* move on if we shouldn't run this event */ if (should_run_event(temp_event) == FALSE) continue; /* handle the event */ handle_timed_event(temp_event); /* * we must remove the entry we've peeked, or * we'll keep getting the same one over and over. * This also maintains sync with broker modules. */ remove_event(nagios_squeue, temp_event); /* reschedule the event if necessary */ if (temp_event->recurring == TRUE) reschedule_event(nagios_squeue, temp_event); /* else free memory associated with the event */ else my_free(temp_event); } log_debug_info(DEBUGL_FUNCTIONS, 0, "event_execution_loop() end\n"); return OK; }
static int should_run_event(timed_event *temp_event) { int run_event = TRUE; /* default action is to execute the event */ int nudge_seconds = 0; /* we only care about jobs that cause processes to run */ if (temp_event->event_type != EVENT_HOST_CHECK && temp_event->event_type != EVENT_SERVICE_CHECK) { return TRUE; } /* if we can't spawn any more jobs, don't bother */ if (!wproc_can_spawn(&loadctl)) { wproc_reap(100, 3000); return FALSE; } /* run a few checks before executing a service check... */ if (temp_event->event_type == EVENT_SERVICE_CHECK) { service *temp_service = (service *)temp_event->event_data; /* forced checks override normal check logic */ if ((temp_service->check_options & CHECK_OPTION_FORCE_EXECUTION)) return TRUE; /* don't run a service check if we're already maxed out on the number of parallel service checks... */ if (max_parallel_service_checks != 0 && (currently_running_service_checks >= max_parallel_service_checks)) { nudge_seconds = ranged_urand(5, 17); logit(NSLOG_RUNTIME_WARNING, TRUE, "\tMax concurrent service checks (%d) has been reached. Nudging %s:%s by %d seconds...\n", max_parallel_service_checks, temp_service->host_name, temp_service->description, nudge_seconds); run_event = FALSE; } /* don't run a service check if active checks are disabled */ if (execute_service_checks == FALSE) { log_debug_info(DEBUGL_EVENTS | DEBUGL_CHECKS, 1, "We're not executing service checks right now, so we'll skip check event for service '%s;%s'.\n", temp_service->host_name, temp_service->description); run_event = FALSE; } /* reschedule the check if we can't run it now */ if (run_event == FALSE) { remove_event(nagios_squeue, temp_event); if (nudge_seconds) { /* We nudge the next check time when it is due to too many concurrent service checks */ temp_service->next_check = (time_t)(temp_service->next_check + nudge_seconds); } else { temp_service->next_check += check_window(temp_service); } temp_event->run_time = temp_service->next_check; reschedule_event(nagios_squeue, temp_event); update_service_status(temp_service, FALSE); run_event = FALSE; } } /* run a few checks before executing a host check... */ else if (temp_event->event_type == EVENT_HOST_CHECK) { host *temp_host = (host *)temp_event->event_data; /* forced checks override normal check logic */ if ((temp_host->check_options & CHECK_OPTION_FORCE_EXECUTION)) return TRUE; /* don't run a host check if active checks are disabled */ if (execute_host_checks == FALSE) { log_debug_info(DEBUGL_EVENTS | DEBUGL_CHECKS, 1, "We're not executing host checks right now, so we'll skip host check event for host '%s'.\n", temp_host->name); run_event = FALSE; } /* reschedule the host check if we can't run it right now */ if (run_event == FALSE) { remove_event(nagios_squeue, temp_event); temp_host->next_check += check_window(temp_host); temp_event->run_time = temp_host->next_check; reschedule_event(nagios_squeue, temp_event); update_host_status(temp_host, FALSE); run_event = FALSE; } } return run_event; }