Skip to content

Commit

Permalink
convert problem_id and notification_id to uuids (fixes #362)
Browse files Browse the repository at this point in the history
this makes problems and notifications distinguishable over different instances.
Using glibs g_uuid_string_random implemantion so it does not introduce any
new dependency. Since the uuid is not used for cryptographic purposes this
should be fine.

Existing IDs will be converted to strings.
  • Loading branch information
sni committed Jan 26, 2024
1 parent c749cab commit d85e21c
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 66 deletions.
6 changes: 3 additions & 3 deletions src/naemon/checks_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -1008,16 +1008,16 @@ static int handle_host_state(host *hst, int *alert_recorded)
/* update the problem id when transitioning to a problem state */
if (hst->last_state == STATE_UP) {
/* don't reset last problem id, or it will be zero the next time a problem is encountered */
hst->current_problem_id = next_problem_id;
next_problem_id++;
nm_free(hst->current_problem_id);
hst->current_problem_id = (char*)g_uuid_string_random();
hst->problem_start = current_time;
hst->problem_end = 0L;
}

/* clear the problem id when transitioning from a problem state to an UP state */
if (hst->current_state == STATE_UP) {
hst->last_problem_id = hst->current_problem_id;
hst->current_problem_id = 0L;
hst->current_problem_id = NULL;
hst->problem_end = current_time;
}

Expand Down
7 changes: 3 additions & 4 deletions src/naemon/checks_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,17 +686,16 @@ int handle_async_service_check_result(service *temp_service, check_result *queue
/* update the problem id when transitioning to a problem state */
if (temp_service->last_state == STATE_OK) {
/* don't reset last problem id, or it will be zero the next time a problem is encountered */
temp_service->current_problem_id = next_problem_id;
next_problem_id++;
nm_free(temp_service->current_problem_id);
temp_service->current_problem_id = (char*)g_uuid_string_random();
temp_service->problem_start = current_time;
temp_service->problem_end = 0L;
}

/* clear the problem id when transitioning from a problem state to an OK state */
if (temp_service->current_state == STATE_OK) {
temp_service->last_problem_id = temp_service->current_problem_id;
temp_service->current_problem_id = 0L;
temp_service->current_problem_id = 0L;
temp_service->current_problem_id = NULL;
temp_service->problem_end = current_time;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/naemon/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ extern int currently_running_service_checks;
extern int currently_running_host_checks;

extern unsigned long next_event_id;
extern unsigned long next_problem_id;
extern unsigned long next_comment_id;
extern unsigned long next_notification_id;

extern unsigned long modified_process_attributes;
extern unsigned long modified_host_process_attributes;
Expand Down
14 changes: 8 additions & 6 deletions src/naemon/macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ static int grab_standard_host_macro_r(nagios_macros *mac, int macro_type, host *
*output = (char *)mkstr("%d", temp_host->current_notification_number);
break;
case MACRO_HOSTNOTIFICATIONID:
*output = (char *)mkstr("%lu", temp_host->current_notification_id);
*output = temp_host->current_notification_id;
break;
case MACRO_HOSTEVENTID:
*output = (char *)mkstr("%lu", temp_host->current_event_id);
Expand All @@ -744,10 +744,12 @@ static int grab_standard_host_macro_r(nagios_macros *mac, int macro_type, host *
*output = (char *)mkstr("%lu", temp_host->last_event_id);
break;
case MACRO_HOSTPROBLEMID:
*output = (char *)mkstr("%lu", temp_host->current_problem_id);
if(temp_host->current_problem_id != NULL)
*output = temp_host->current_problem_id;
break;
case MACRO_LASTHOSTPROBLEMID:
*output = (char *)mkstr("%lu", temp_host->last_problem_id);
if(temp_host->last_problem_id != NULL)
*output = temp_host->last_problem_id;
break;
case MACRO_HOSTPROBLEMSTART:
*output = (char *)mkstr("%lu", (unsigned long)temp_host->problem_start);
Expand Down Expand Up @@ -1059,7 +1061,7 @@ static int grab_standard_service_macro_r(nagios_macros *mac, int macro_type, ser
*output = (char *)mkstr("%d", temp_service->current_notification_number);
break;
case MACRO_SERVICENOTIFICATIONID:
*output = (char *)mkstr("%lu", temp_service->current_notification_id);
*output = temp_service->current_notification_id;
break;
case MACRO_SERVICEEVENTID:
*output = (char *)mkstr("%lu", temp_service->current_event_id);
Expand All @@ -1068,10 +1070,10 @@ static int grab_standard_service_macro_r(nagios_macros *mac, int macro_type, ser
*output = (char *)mkstr("%lu", temp_service->last_event_id);
break;
case MACRO_SERVICEPROBLEMID:
*output = (char *)mkstr("%lu", temp_service->current_problem_id);
*output = temp_service->current_problem_id;
break;
case MACRO_LASTSERVICEPROBLEMID:
*output = (char *)mkstr("%lu", temp_service->last_problem_id);
*output = temp_service->last_problem_id;
break;
case MACRO_SERVICEPROBLEMSTART:
*output = (char *)mkstr("%lu", (unsigned long)temp_service->problem_start);
Expand Down
12 changes: 6 additions & 6 deletions src/naemon/notifications.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ int service_notification(service *svc, int type, char *not_author, char *not_dat
log_debug_info(DEBUGL_NOTIFICATIONS, 1, "Current notification number: %d (%s)\n", svc->current_notification_number, (increment_notification_number == TRUE) ? "incremented" : "changed");

/* save and increase the current notification id */
svc->current_notification_id = next_notification_id;
next_notification_id++;
nm_free(svc->current_notification_id);
svc->current_notification_id = g_uuid_string_random();

log_debug_info(DEBUGL_NOTIFICATIONS, 2, "Creating list of contacts to be notified.\n");

Expand Down Expand Up @@ -478,7 +478,7 @@ int service_notification(service *svc, int type, char *not_author, char *not_dat
mac.x[MACRO_NOTIFICATIONNUMBER] = nm_strdup(mac.x[MACRO_SERVICENOTIFICATIONNUMBER]);

/* set the notification id macro */
nm_asprintf(&mac.x[MACRO_SERVICENOTIFICATIONID], "%lu", svc->current_notification_id);
nm_asprintf(&mac.x[MACRO_SERVICENOTIFICATIONID], "%s", svc->current_notification_id);

/* notify each contact (duplicates have been removed) */
for (temp_notification = notification_list; temp_notification != NULL; temp_notification = temp_notification->next) {
Expand Down Expand Up @@ -1283,8 +1283,8 @@ int host_notification(host *hst, int type, char *not_author, char *not_data, int
log_debug_info(DEBUGL_NOTIFICATIONS, 1, "Current notification number: %d (%s)\n", hst->current_notification_number, (increment_notification_number == TRUE) ? "incremented" : "unchanged");

/* save and increase the current notification id */
hst->current_notification_id = next_notification_id;
next_notification_id++;
nm_free(hst->current_notification_id);
hst->current_notification_id = g_uuid_string_random();

log_debug_info(DEBUGL_NOTIFICATIONS, 2, "Creating list of contacts to be notified.\n");

Expand Down Expand Up @@ -1360,7 +1360,7 @@ int host_notification(host *hst, int type, char *not_author, char *not_data, int
mac.x[MACRO_NOTIFICATIONNUMBER] = nm_strdup(mac.x[MACRO_HOSTNOTIFICATIONNUMBER]);

/* set the notification id macro */
nm_asprintf(&mac.x[MACRO_HOSTNOTIFICATIONID], "%lu", hst->current_notification_id);
nm_asprintf(&mac.x[MACRO_HOSTNOTIFICATIONID], "%s", hst->current_notification_id);

/* notify each contact (duplicates have been removed) */
for (temp_notification = notification_list; temp_notification != NULL; temp_notification = temp_notification->next) {
Expand Down
3 changes: 3 additions & 0 deletions src/naemon/objects_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ void destroy_host(host *this_host)
nm_free(this_host->icon_image_alt);
nm_free(this_host->vrml_image);
nm_free(this_host->statusmap_image);
nm_free(this_host->current_notification_id);
nm_free(this_host->last_problem_id);
nm_free(this_host->current_problem_id);
nm_free(this_host);
}

Expand Down
6 changes: 3 additions & 3 deletions src/naemon/objects_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ struct host {
int current_attempt;
unsigned long current_event_id;
unsigned long last_event_id;
unsigned long current_problem_id;
unsigned long last_problem_id;
char *current_problem_id;
char *last_problem_id;
time_t problem_start;
time_t problem_end;
double latency;
Expand All @@ -112,7 +112,7 @@ struct host {
int notified_on;
int current_notification_number;
int no_more_notifications;
unsigned long current_notification_id;
char *current_notification_id;
int check_flapping_recovery_notification;
int scheduled_downtime_depth;
int pending_flex_downtime; /* UNUSED */
Expand Down
3 changes: 3 additions & 0 deletions src/naemon/objects_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ void destroy_service(service *this_service, int truncate_lists)
nm_free(this_service->action_url);
nm_free(this_service->icon_image);
nm_free(this_service->icon_image_alt);
nm_free(this_service->current_notification_id);
nm_free(this_service->last_problem_id);
nm_free(this_service->current_problem_id);
nm_free(this_service);
}

Expand Down
6 changes: 3 additions & 3 deletions src/naemon/objects_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ struct service {
int current_attempt;
unsigned long current_event_id;
unsigned long last_event_id;
unsigned long current_problem_id;
unsigned long last_problem_id;
char *current_problem_id;
char *last_problem_id;
time_t problem_start;
time_t problem_end;
time_t last_notification;
Expand All @@ -101,7 +101,7 @@ struct service {
int is_being_freshened;
unsigned int notified_on;
int current_notification_number;
unsigned long current_notification_id;
char *current_notification_id;
double latency;
double execution_time;
int is_executing;
Expand Down
3 changes: 0 additions & 3 deletions src/naemon/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ unsigned long retained_process_host_attribute_mask = 0L;
unsigned long retained_process_service_attribute_mask = 0L;

unsigned long next_event_id = 0L;
unsigned long next_problem_id = 0L;
unsigned long next_comment_id = 0L;
unsigned long next_notification_id = 0L;

int verify_config = FALSE;
int precache_objects = FALSE;
Expand Down Expand Up @@ -1140,7 +1138,6 @@ int reset_variables(void)
next_comment_id = 0L; /* comment and downtime id get initialized to nonzero elsewhere */
next_downtime_id = 0L;
next_event_id = 1;
next_notification_id = 1;

status_update_interval = DEFAULT_STATUS_UPDATE_INTERVAL;

Expand Down
56 changes: 28 additions & 28 deletions src/naemon/xrddefault.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ int xrddefault_save_state_information(void)
fprintf(fp, "next_comment_id=%lu\n", next_comment_id);
fprintf(fp, "next_downtime_id=%lu\n", next_downtime_id);
fprintf(fp, "next_event_id=%lu\n", next_event_id);
fprintf(fp, "next_problem_id=%lu\n", next_problem_id);
fprintf(fp, "next_notification_id=%lu\n", next_notification_id);
fprintf(fp, "}\n");

/* save host state information */
Expand All @@ -178,8 +176,8 @@ int xrddefault_save_state_information(void)
fprintf(fp, "last_hard_state=%d\n", temp_host->last_hard_state);
fprintf(fp, "last_event_id=%lu\n", temp_host->last_event_id);
fprintf(fp, "current_event_id=%lu\n", temp_host->current_event_id);
fprintf(fp, "current_problem_id=%lu\n", temp_host->current_problem_id);
fprintf(fp, "last_problem_id=%lu\n", temp_host->last_problem_id);
fprintf(fp, "current_problem_id=%s\n", (temp_host->current_problem_id == NULL) ? "" : temp_host->current_problem_id);
fprintf(fp, "last_problem_id=%s\n", (temp_host->last_problem_id == NULL) ? "" : temp_host->last_problem_id);
fprintf(fp, "problem_start=%lu\n", temp_host->problem_start);
fprintf(fp, "problem_end=%lu\n", temp_host->problem_end);
fprintf(fp, "plugin_output=%s\n", (temp_host->plugin_output == NULL) ? "" : temp_host->plugin_output);
Expand All @@ -202,7 +200,7 @@ int xrddefault_save_state_information(void)
fprintf(fp, "notified_on_unreachable=%d\n", flag_isset(temp_host->notified_on, OPT_UNREACHABLE));
fprintf(fp, "last_notification=%lu\n", temp_host->last_notification);
fprintf(fp, "current_notification_number=%d\n", temp_host->current_notification_number);
fprintf(fp, "current_notification_id=%lu\n", temp_host->current_notification_id);
fprintf(fp, "current_notification_id=%s\n", (temp_host->current_notification_id == NULL) ? "" : temp_host->current_notification_id);
if (conf_host && conf_host->notifications_enabled != temp_host->notifications_enabled) {
fprintf(fp, "config:notifications_enabled=%d\n", conf_host->notifications_enabled);
fprintf(fp, "notifications_enabled=%d\n", temp_host->notifications_enabled);
Expand Down Expand Up @@ -275,8 +273,8 @@ int xrddefault_save_state_information(void)
fprintf(fp, "last_hard_state=%d\n", temp_service->last_hard_state);
fprintf(fp, "last_event_id=%lu\n", temp_service->last_event_id);
fprintf(fp, "current_event_id=%lu\n", temp_service->current_event_id);
fprintf(fp, "current_problem_id=%lu\n", temp_service->current_problem_id);
fprintf(fp, "last_problem_id=%lu\n", temp_service->last_problem_id);
fprintf(fp, "current_problem_id=%s\n", (temp_service->current_problem_id == NULL) ? "" : temp_service->current_problem_id);
fprintf(fp, "last_problem_id=%s\n", (temp_service->last_problem_id == NULL) ? "" : temp_service->last_problem_id);
fprintf(fp, "problem_start=%lu\n", temp_service->problem_start);
fprintf(fp, "problem_end=%lu\n", temp_service->problem_end);
fprintf(fp, "current_attempt=%d\n", temp_service->current_attempt);
Expand All @@ -300,7 +298,7 @@ int xrddefault_save_state_information(void)
fprintf(fp, "notified_on_warning=%d\n", flag_isset(temp_service->notified_on, OPT_WARNING));
fprintf(fp, "notified_on_critical=%d\n", flag_isset(temp_service->notified_on, OPT_CRITICAL));
fprintf(fp, "current_notification_number=%d\n", temp_service->current_notification_number);
fprintf(fp, "current_notification_id=%lu\n", temp_service->current_notification_id);
fprintf(fp, "current_notification_id=%s\n", (temp_service->current_notification_id == NULL) ? "" : temp_service->current_notification_id);
fprintf(fp, "last_notification=%lu\n", temp_service->last_notification);
if (conf_svc && conf_svc->notifications_enabled != temp_service->notifications_enabled) {
fprintf(fp, "config:notifications_enabled=%d\n", conf_svc->notifications_enabled);
Expand Down Expand Up @@ -1002,10 +1000,6 @@ int xrddefault_read_state_information(void)
next_downtime_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "next_event_id"))
next_event_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "next_problem_id"))
next_problem_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "next_notification_id"))
next_notification_id = strtoul(val, NULL, 10);
}
break;

Expand Down Expand Up @@ -1064,11 +1058,13 @@ int xrddefault_read_state_information(void)
temp_host->current_event_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "last_event_id"))
temp_host->last_event_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "current_problem_id"))
temp_host->current_problem_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "last_problem_id"))
temp_host->last_problem_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "problem_start"))
else if (!strcmp(var, "current_problem_id")) {
nm_free(temp_host->current_problem_id);
temp_host->current_problem_id = nm_strdup(val);
} else if (!strcmp(var, "last_problem_id")) {
nm_free(temp_host->last_problem_id);
temp_host->last_problem_id = nm_strdup(val);
} else if (!strcmp(var, "problem_start"))
temp_host->problem_start = strtoul(val, NULL, 10);
else if (!strcmp(var, "problem_end"))
temp_host->problem_end = strtoul(val, NULL, 10);
Expand All @@ -1094,9 +1090,10 @@ int xrddefault_read_state_information(void)
temp_host->last_notification = strtoul(val, NULL, 10);
else if (!strcmp(var, "current_notification_number"))
temp_host->current_notification_number = atoi(val);
else if (!strcmp(var, "current_notification_id"))
temp_host->current_notification_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "is_flapping"))
else if (!strcmp(var, "current_notification_id")) {
nm_free(temp_host->current_notification_id);
temp_host->current_notification_id = nm_strdup(val);
} else if (!strcmp(var, "is_flapping"))
temp_host->is_flapping = atoi(val);
else if (!strcmp(var, "percent_state_change"))
temp_host->percent_state_change = strtod(val, NULL);
Expand Down Expand Up @@ -1314,11 +1311,13 @@ int xrddefault_read_state_information(void)
temp_service->current_event_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "last_event_id"))
temp_service->last_event_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "current_problem_id"))
temp_service->current_problem_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "last_problem_id"))
temp_service->last_problem_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "problem_start"))
else if (!strcmp(var, "current_problem_id")) {
nm_free(temp_service->current_problem_id);
temp_service->current_problem_id = nm_strdup(val);
} else if (!strcmp(var, "last_problem_id")) {
nm_free(temp_service->last_problem_id);
temp_service->last_problem_id = nm_strdup(val);
} else if (!strcmp(var, "problem_start"))
temp_service->problem_start = strtoul(val, NULL, 10);
else if (!strcmp(var, "problem_end"))
temp_service->problem_end = strtoul(val, NULL, 10);
Expand Down Expand Up @@ -1363,9 +1362,10 @@ int xrddefault_read_state_information(void)
temp_service->notified_on |= ((atoi(val) > 0) ? OPT_CRITICAL : 0);
else if (!strcmp(var, "current_notification_number"))
temp_service->current_notification_number = atoi(val);
else if (!strcmp(var, "current_notification_id"))
temp_service->current_notification_id = strtoul(val, NULL, 10);
else if (!strcmp(var, "last_notification"))
else if (!strcmp(var, "current_notification_id")) {
nm_free(temp_service->current_notification_id);
temp_service->current_notification_id = nm_strdup(val);
} else if (!strcmp(var, "last_notification"))
temp_service->last_notification = strtoul(val, NULL, 10);
else if (!strcmp(var, "is_flapping"))
temp_service->is_flapping = atoi(val);
Expand Down
Loading

0 comments on commit d85e21c

Please sign in to comment.