/* saves a service downtime entry */ int add_new_service_downtime(char *host_name, char *service_description, time_t entry_time, char *author, char *comment_data, time_t start_time, time_t end_time, int fixed, unsigned long triggered_by, unsigned long duration, unsigned long *downtime_id, int is_in_effect, time_t trigger_time) { int result = OK; if (host_name == NULL || service_description == NULL) return ERROR; /* find the next valid downtime id */ while (find_service_downtime(next_downtime_id) != NULL) next_downtime_id++; /* add downtime to list in memory */ result = add_service_downtime(host_name, service_description, entry_time, author, comment_data, start_time, end_time, fixed, triggered_by, duration, next_downtime_id, is_in_effect, trigger_time); /* save downtime id */ if (downtime_id != NULL) *downtime_id = next_downtime_id; #ifdef USE_EVENT_BROKER /* send data to event broker */ broker_downtime_data(NEBTYPE_DOWNTIME_ADD, NEBFLAG_NONE, NEBATTR_NONE, SERVICE_DOWNTIME, host_name, service_description, entry_time, author, comment_data, start_time, end_time, fixed, triggered_by, duration, next_downtime_id, NULL, is_in_effect, trigger_time); #endif /* increment the downtime id */ next_downtime_id++; return result; }
/* removes invalid and old downtime entries from the downtime file */ int xdddefault_validate_downtime_data(void) { scheduled_downtime *temp_downtime; scheduled_downtime *next_downtime; int update_file = FALSE; int save = TRUE; /* remove stale downtimes */ for (temp_downtime = scheduled_downtime_list; temp_downtime != NULL; temp_downtime = next_downtime) { next_downtime = temp_downtime->next; save = TRUE; /* delete downtimes with invalid host names */ if (find_host(temp_downtime->host_name) == NULL) save = FALSE; /* delete downtimes with invalid service descriptions */ if (temp_downtime->type == SERVICE_DOWNTIME && find_service(temp_downtime->host_name, temp_downtime->service_description) == NULL) save = FALSE; /* delete downtimes that have expired */ if (temp_downtime->end_time < time(NULL)) save = FALSE; /* delete the downtime */ if (save == FALSE) { update_file = TRUE; delete_downtime(temp_downtime->type, temp_downtime->downtime_id); } } /* remove triggered downtimes without valid parents */ for (temp_downtime = scheduled_downtime_list; temp_downtime != NULL; temp_downtime = next_downtime) { next_downtime = temp_downtime->next; save = TRUE; if (temp_downtime->triggered_by == 0) continue; if (find_host_downtime(temp_downtime->triggered_by) == NULL && find_service_downtime(temp_downtime->triggered_by) == NULL) save = FALSE; /* delete the downtime */ if (save == FALSE) { update_file = TRUE; delete_downtime(temp_downtime->type, temp_downtime->downtime_id); } } /* update downtime file */ if (update_file == TRUE) xdddefault_save_downtime_data(); return OK; }
/* adds a new scheduled service downtime entry */ int xdddefault_add_new_service_downtime(char *host_name, char *service_description, time_t entry_time, char *author, char *comment, time_t start_time, time_t end_time, int fixed, unsigned long triggered_by, unsigned long duration, unsigned long *downtime_id, int is_in_effect, int start_notification_sent){ /* find the next valid downtime id */ while(find_service_downtime(next_downtime_id) != NULL) next_downtime_id++; /* add downtime to list in memory */ add_service_downtime(host_name, service_description, entry_time, author, comment, start_time, (time_t)0, end_time, fixed, triggered_by, duration, next_downtime_id, is_in_effect, start_notification_sent); /* return the id for the downtime we are about to add (this happens in the main code) */ if(downtime_id != NULL) *downtime_id = next_downtime_id; /* increment the downtime id */ next_downtime_id++; return OK; }
void test_host_commands(void) { char *host_name = "host1"; host *target_host = NULL; int pre = 0, prev_comment_id = next_comment_id; unsigned int prev_downtime_id; time_t check_time =0; char *cmdstr = NULL; target_host = find_host(host_name); target_host->obsess = FALSE; pre = number_of_host_comments(host_name); ok(CMD_ERROR_OK == process_external_command2(CMD_ADD_HOST_COMMENT, check_time, "host1;0;myself;my comment"), "process_external_command2: ADD_HOST_COMMENT"); ok(pre+1 == number_of_host_comments(host_name), "ADD_HOST_COMMENT (through process_external_command2) adds a host comment"); ++pre; ok(CMD_ERROR_OK == process_external_command1("[1234567890] ADD_HOST_COMMENT;host1;0;myself;my comment"), "core command: ADD_HOST_COMMENT"); ok(pre+1 == number_of_host_comments(host_name), "ADD_HOST_COMMENT adds a host comment"); nm_asprintf(&cmdstr, "[1234567890] DEL_HOST_COMMENT;%i", prev_comment_id); ok(CMD_ERROR_OK == process_external_command1(cmdstr), "core command: DEL_HOST_COMMENT"); free(cmdstr); ok(pre == number_of_host_comments(host_name), "DEL_HOST_COMMENT deletes a host comment"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] DELAY_HOST_NOTIFICATION;host1;1927587190"), "core command: DELAY_HOST_NOTIFICATION"); ok(1927587190 == target_host->next_notification, "DELAY_HOST_NOTIFICATION delays host notifications"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] DISABLE_HOST_SVC_CHECKS;host1"), "core command: DISABLE_HOST_SVC_CHECKS"); ok(!target_host->services->service_ptr->checks_enabled, "DISABLE_HOST_SVC_CHECKS disables active checks for services on a host"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] ENABLE_HOST_SVC_CHECKS;host1"), "core command: ENABLE_HOST_SVC_CHECKS"); ok(target_host->services->service_ptr->checks_enabled, "ENABLE_HOST_SVC_CHECKS enables active checks for services on a host"); check_time = target_host->services->service_ptr->next_check - 20; nm_asprintf(&cmdstr, "[1234567890] SCHEDULE_HOST_SVC_CHECKS;host1;%llu", (long long unsigned int)check_time); ok(CMD_ERROR_OK == process_external_command1(cmdstr), "core command: SCHEDULE_HOST_SVC_CHECKS"); ok(check_time == target_host->services->service_ptr->next_check, "SCHEDULE_HOST_SVC_CHECKS schedules host service checks"); free(cmdstr); assert(CMD_ERROR_OK == process_external_command1("[1234567890] ADD_HOST_COMMENT;host1;0;myself;comment 1")); assert(CMD_ERROR_OK == process_external_command1("[1234567890] ADD_HOST_COMMENT;host1;1;myself;comment 2")); assert(CMD_ERROR_OK == process_external_command1("[1234567890] ADD_HOST_COMMENT;host1;0;myself;comment 3")); ok(CMD_ERROR_OK == process_external_command1("[1234567890] DEL_ALL_HOST_COMMENTS;host1"), "core command: DEL_ALL_HOST_COMMENTS"); ok(0 == number_of_host_comments(host_name), "DEL_ALL_HOST_COMMENTS deletes all host comments"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] DISABLE_HOST_NOTIFICATIONS;host1"), "core command: DISABLE_HOST_NOTIFICATIONS"); ok(!target_host->notifications_enabled, "DISABLE_HOST_NOTIFICATIONS disables host notifications"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] ENABLE_HOST_NOTIFICATIONS;host1"), "core command: ENABLE_HOST_NOTIFICATIONS"); ok(target_host->notifications_enabled, "ENABLE_HOST_NOTIFICATIONS enables host notifications"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] DISABLE_ALL_NOTIFICATIONS_BEYOND_HOST;host1"), "core command: DISABLE_ALL_NOTIFICATIONS_BEYOND_HOST"); ok(!((find_host("childofhost1"))->notifications_enabled), "DISABLE_ALL_NOTIFICATIONS_BEYOND_HOST disables notifications beyond host"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] ENABLE_ALL_NOTIFICATIONS_BEYOND_HOST;host1"), "core command: ENABLE_ALL_NOTIFICATIONS_BEYOND_HOST"); ok(((find_host("childofhost1"))->notifications_enabled), "ENABLE_ALL_NOTIFICATIONS_BEYOND_HOST enables notifications beyond host"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] DISABLE_HOST_SVC_NOTIFICATIONS;host1"), "core command: DISABLE_HOST_SVC_NOTIFICATIONS"); ok(!(target_host->services->service_ptr->notifications_enabled), "DISABLE_HOST_SVC_NOTIFICATIONS disables notifications for services on a host"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] ENABLE_HOST_SVC_NOTIFICATIONS;host1"), "core command: ENABLE_HOST_SVC_NOTIFICATIONS"); ok(target_host->services->service_ptr->notifications_enabled, "ENABLE_HOST_SVC_NOTIFICATIONS enables notifications for services on a host"); target_host->current_state = STATE_DOWN; ok(CMD_ERROR_OK == process_external_command1("[1234567890] ACKNOWLEDGE_HOST_PROBLEM;host1;2;0;0;myself;my ack comment"), "core command: ACKNOWLEDGE_HOST_PROBLEM"); ok(target_host->problem_has_been_acknowledged, "ACKNOWLEDGE_HOST_PROBLEM acknowledges a host problem"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] REMOVE_HOST_ACKNOWLEDGEMENT;host1"), "core command: REMOVE_HOST_ACKNOWLEDGEMENT"); ok(!target_host->problem_has_been_acknowledged, "REMOVE_HOST_ACKNOWLEDGEMENT removes a host acknowledgement"); target_host->current_state = STATE_UP; ok(CMD_ERROR_OK == process_external_command1("[1234567890] DISABLE_HOST_EVENT_HANDLER;host1"), "core command: DISABLE_HOST_EVENT_HANDLER"); ok(!target_host->event_handler_enabled, "DISABLE_HOST_EVENT_HANDLER disables event handler for a host"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] ENABLE_HOST_EVENT_HANDLER;host1"), "core command: ENABLE_HOST_EVENT_HANDLER"); ok(target_host->event_handler_enabled, "ENABLE_HOST_EVENT_HANDLER enables event handler for a host"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] DISABLE_HOST_CHECK;host1"), "core command: DISABLE_HOST_CHECK"); ok(!target_host->checks_enabled, "DISABLE_HOST_CHECK disables active host checks"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] ENABLE_HOST_CHECK;host1"), "core command: ENABLE_HOST_CHECK"); ok(target_host->checks_enabled, "ENABLE_HOST_CHECK enables active host checks"); check_time = target_host->services->service_ptr->next_check + 2000; nm_asprintf(&cmdstr, "[1234567890] SCHEDULE_FORCED_HOST_SVC_CHECKS;host1;%llu", (unsigned long long int)check_time); ok(CMD_ERROR_OK == process_external_command1(cmdstr), "core command: SCHEDULE_FORCED_HOST_SVC_CHECKS"); ok(check_time == target_host->services->service_ptr->next_check, "SCHEDULE_FORCED_HOST_SVC_CHECKS schedules forced checks for services on a host"); free(cmdstr); prev_downtime_id = next_downtime_id; nm_asprintf(&cmdstr, "[1234567890] SCHEDULE_HOST_DOWNTIME;host1;%llu;%llu;1;0;0;myself;my downtime comment", (unsigned long long int)time(NULL), (unsigned long long int)time(NULL) + 1500); ok(CMD_ERROR_OK == process_external_command1(cmdstr), "core command: SCHEDULE_HOST_DOWNTIME"); ok(prev_downtime_id != next_downtime_id, "SCHEDULE_HOST_DOWNTIME schedules one new downtime"); ok(NULL != find_host_downtime(prev_downtime_id), "SCHEDULE_HOST_DOWNTIME schedules downtime for a host"); free(cmdstr); nm_asprintf(&cmdstr, "[1234567890] DEL_HOST_DOWNTIME;%i", prev_downtime_id); ok(CMD_ERROR_OK == process_external_command1(cmdstr), "core command: DEL_HOST_DOWNTIME"); ok(!find_host_downtime(prev_downtime_id), "DEL_HOST_DOWNTIME deletes a scheduled host downtime"); free(cmdstr); ok(CMD_ERROR_OK == process_external_command1("[1234567890] DISABLE_HOST_FLAP_DETECTION;host1"), "core command: DISABLE_HOST_FLAP_DETECTION"); ok(!target_host->flap_detection_enabled, "DISABLE_HOST_FLAP_DETECTION disables host flap detection"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] ENABLE_HOST_FLAP_DETECTION;host1"), "core command: ENABLE_HOST_FLAP_DETECTION"); ok(target_host->flap_detection_enabled, "ENABLE_HOST_FLAP_DETECTION enables host flap detection"); assert(NULL == find_service_downtime(0)); nm_asprintf(&cmdstr, "[1234567890] SCHEDULE_HOST_SVC_DOWNTIME;host1;%llu;%llu;1;0;0;myself;my downtime comment", (unsigned long long int)time(NULL), (unsigned long long int)time(NULL) + 1500); ok(CMD_ERROR_OK == process_external_command1(cmdstr), "core command: SCHEDULE_HOST_SVC_DOWNTIME"); strcmp(host_name, find_service_downtime(0)->host_name); ok(0 == 0, "SCHEDULE_HOST_SVC_DOWNTIME schedules downtime for services on a host"); free(cmdstr); ok(CMD_ERROR_OK == process_external_command1("[1234567890] PROCESS_HOST_CHECK_RESULT;host1;1;some plugin output"), "core command: PROCESS_HOST_CHECK_RESULT"); ok(target_host->current_state == STATE_DOWN, "PROCESS_HOST_CHECK_RESULT processes host check results"); check_time = target_host->next_check - 20; nm_asprintf(&cmdstr, "[1234567890] SCHEDULE_HOST_CHECK;host1;%llu", (unsigned long long int)check_time); ok(CMD_ERROR_OK == process_external_command1(cmdstr), "core command: SCHEDULE_HOST_CHECK"); ok(check_time == target_host->next_check, "SCHEDULE_HOST_CHECK schedules a host check"); free(cmdstr); nm_asprintf(&cmdstr, "[1234567890] SCHEDULE_HOST_CHECK;host1;%llu", (unsigned long long int)check_time); ok(CMD_ERROR_OK == process_external_command1(cmdstr), "core command: SCHEDULE_FORCED_HOST_CHECK"); ok(check_time == target_host->next_check, "SCHEDULE_FORCED_HOST_CHECK schedules a host check"); free(cmdstr); assert(!(target_host->obsess)); ok(CMD_ERROR_OK == process_external_command1("[1234567890] START_OBSESSING_OVER_HOST;host1"), "core command: START_OBSESSING_OVER_HOST"); ok(target_host->obsess, "START_OBSESSING_OVER_HOST enables OCHP for host"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] STOP_OBSESSING_OVER_HOST;host1"), "core command: STOP_OBSESSING_OVER_HOST"); ok(!target_host->obsess, "STOP_OBSESSING_OVER_HOST disables OCHP for host"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] CHANGE_NORMAL_HOST_CHECK_INTERVAL;host1;42"), "core command: CHANGE_NORMAL_HOST_CHECK_INTERVAL"); ok(42 == target_host->check_interval, "CHANGE_NORMAL_HOST_CHECK_INTERVAL changes the host check inteval for host"); ok(CMD_ERROR_OK == process_external_command1("[1234567890] CHANGE_MAX_HOST_CHECK_ATTEMPTS;host1;9"), "core command: CHANGE_MAX_HOST_CHECK_ATTEMPTS"); ok(9 == target_host->max_attempts, "CHANGE_MAX_HOST_CHECK_ATTEMPTS changes the maximum number of check attempts for host"); }
/* removes invalid and old downtime entries from the downtime file */ int xdddefault_validate_downtime_data(void) { scheduled_downtime *temp_downtime; scheduled_downtime *next_downtime; int save = TRUE; /* remove stale downtimes */ for(temp_downtime = scheduled_downtime_list; temp_downtime != NULL; temp_downtime = next_downtime) { next_downtime = temp_downtime->next; save = TRUE; /* delete downtimes with invalid host names */ if(find_host(temp_downtime->host_name) == NULL) { log_debug_info(DEBUGL_DOWNTIME, 1, "Deleting downtime with invalid host name: %s\n", temp_downtime->host_name); save = FALSE; } /* delete downtimes with invalid service descriptions */ if(temp_downtime->type == SERVICE_DOWNTIME && find_service(temp_downtime->host_name, temp_downtime->service_description) == NULL) { log_debug_info(DEBUGL_DOWNTIME, 1, "Deleting downtime with invalid service description: %s\n", temp_downtime->service_description); save = FALSE; } /* delete fixed downtimes that have expired */ if((TRUE == temp_downtime->fixed) && (temp_downtime->end_time < time(NULL))) { log_debug_info(DEBUGL_DOWNTIME, 1, "Deleting fixed downtime that expired at: %lu\n", temp_downtime->end_time); save = FALSE; } /* delete flexible downtimes that never started and have expired */ if((FALSE == temp_downtime->fixed) && (0 == temp_downtime->flex_downtime_start) && (temp_downtime->end_time < time(NULL))) { log_debug_info(DEBUGL_DOWNTIME, 1, "Deleting flexible downtime that expired at: %lu\n", temp_downtime->end_time); save = FALSE; } /* delete flexible downtimes that started but whose duration has completed */ if((FALSE == temp_downtime->fixed) && (0 != temp_downtime->flex_downtime_start) && ((temp_downtime->flex_downtime_start + temp_downtime->duration) < time(NULL))) { log_debug_info(DEBUGL_DOWNTIME, 1, "Deleting flexible downtime whose duration ended at: %lu\n", temp_downtime->flex_downtime_start + temp_downtime->duration); save = FALSE; } /* delete the downtime */ if(save == FALSE) { delete_downtime(temp_downtime->type, temp_downtime->downtime_id); } } /* remove triggered downtimes without valid parents */ for(temp_downtime = scheduled_downtime_list; temp_downtime != NULL; temp_downtime = next_downtime) { next_downtime = temp_downtime->next; save = TRUE; if(temp_downtime->triggered_by == 0) continue; if(find_host_downtime(temp_downtime->triggered_by) == NULL && find_service_downtime(temp_downtime->triggered_by) == NULL) save = FALSE; /* delete the downtime */ if(save == FALSE) { delete_downtime(temp_downtime->type, temp_downtime->downtime_id); } } return OK; }
int main(int argc, char **argv) { int result; int error = FALSE; char *buffer = NULL; int display_license = FALSE; int display_help = FALSE; int c = 0; struct tm *tm; time_t now; char datestring[256]; host *temp_host = NULL; hostgroup *temp_hostgroup = NULL; hostsmember *temp_member = NULL; plan_tests(14); /* reset program variables */ reset_variables(); printf("Reading configuration data...\n"); config_file = strdup("smallconfig/nagios.cfg"); /* read in the configuration files (main config file, resource and object config files) */ result = read_main_config_file(config_file); ok(result == OK, "Read main configuration file okay - if fails, use nagios -v to check"); result = read_all_object_data(config_file); ok(result == OK, "Read all object config files"); result = pre_flight_check(); ok(result == OK, "Preflight check okay"); for(temp_hostgroup = hostgroup_list; temp_hostgroup != NULL; temp_hostgroup = temp_hostgroup->next) { c++; //printf("Hostgroup=%s\n", temp_hostgroup->group_name); } ok(c == 2, "Found all hostgroups"); temp_hostgroup = find_hostgroup("hostgroup1"); for(temp_member = temp_hostgroup->members; temp_member != NULL; temp_member = temp_member->next) { //printf("host pointer=%d\n", temp_member->host_ptr); } temp_hostgroup = find_hostgroup("hostgroup2"); for(temp_member = temp_hostgroup->members; temp_member != NULL; temp_member = temp_member->next) { //printf("host pointer=%d\n", temp_member->host_ptr); } temp_host = find_host("host1"); ok(temp_host->current_state == 0, "State is assumed OK on initial load"); xrddefault_retention_file = strdup("smallconfig/retention.dat"); ok(xrddefault_read_state_information() == OK, "Reading retention data"); ok(temp_host->current_state == 1, "State changed due to retention file settings"); ok(find_host_comment(418) != NULL, "Found host comment id 418"); ok(find_service_comment(419) != NULL, "Found service comment id 419"); ok(find_service_comment(420) == NULL, "Did not find service comment id 420 as not persistent"); ok(find_host_comment(1234567888) == NULL, "No such host comment"); ok(find_host_downtime(1102) != NULL, "Found host downtime id 1102"); ok(find_service_downtime(1110) != NULL, "Found service downtime 1110"); ok(find_host_downtime(1234567888) == NULL, "No such host downtime"); cleanup(); my_free(config_file); return exit_status(); }
int main(int argc, char **argv) { int result; int c = 0; struct host *host1, *host2; hostgroup *temp_hostgroup = NULL; hostsmember *temp_member = NULL; plan_tests(19); /* reset program variables */ reset_variables(); /* * avoid updating the checked-in retention data * file when testing */ retain_state_information = FALSE; printf("Reading configuration data...\n"); config_file = strdup(get_default_config_file()); config_file_dir = nspath_absolute_dirname(config_file, NULL); /* read in the configuration files (main config file, resource and object config files) */ result = read_main_config_file(config_file); ok(result == OK, "Read main configuration file okay - if fails, use nagios -v to check"); result = read_all_object_data(config_file); ok(result == OK, "Read all object config files"); result = pre_flight_check(); ok(result == OK, "Preflight check okay"); for (temp_hostgroup = hostgroup_list; temp_hostgroup != NULL; temp_hostgroup = temp_hostgroup->next) { c++; //printf("Hostgroup=%s\n", temp_hostgroup->group_name); } ok(c == 2, "Found all hostgroups"); temp_hostgroup = find_hostgroup("hostgroup1"); for (temp_member = temp_hostgroup->members; temp_member != NULL; temp_member = temp_member->next) { //printf("host pointer=%d\n", temp_member->host_ptr); } temp_hostgroup = find_hostgroup("hostgroup2"); for (temp_member = temp_hostgroup->members; temp_member != NULL; temp_member = temp_member->next) { //printf("host pointer=%d\n", temp_member->host_ptr); } host1 = find_host("host1"); host2 = find_host("host2"); ok(host1 != NULL && host2 != NULL, "find_host() should work"); ok(host1->current_state == 0, "State is assumed OK on initial load"); ok(host1->notifications_enabled == 1, "host1 notifications_enabled set from config"); ok(host2->notifications_enabled == 1, "host2 notifications_enabled set from config"); initialize_retention_data(NULL); initialize_downtime_data(); init_event_queue(); ok(xrddefault_read_state_information() == OK, "Reading retention data"); ok(host1->current_state == 1, "State changed due to retention file settings"); ok(host1->notifications_enabled == 1, "Config change should override notifications_enabled for host1"); ok(host2->notifications_enabled == 0, "Retention data should win on no config change"); ok(find_host_comment(418) != NULL, "Found host comment id 418"); ok(find_service_comment(419) != NULL, "Found service comment id 419"); ok(find_service_comment(420) == NULL, "Did not find service comment id 420 as not persistent"); ok(find_host_comment(1234567888) == NULL, "No such host comment"); ok(find_host_downtime(1102) != NULL, "Found host downtime id 1102"); ok(find_service_downtime(1110) != NULL, "Found service downtime 1110"); ok(find_host_downtime(1234567888) == NULL, "No such host downtime"); cleanup(); my_free(config_file); return exit_status(); }