Skip to content

Commit

Permalink
add display_status property to hosts and services objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Ren committed May 20, 2024
1 parent 0f417c4 commit 834f773
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 122 deletions.
104 changes: 37 additions & 67 deletions src/naemon/macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ static int grab_standard_host_macro_r(nagios_macros *mac, int macro_type, host *
objectlist *temp_objectlist = NULL;
time_t current_time = 0L;
unsigned long duration = 0L;
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
char *buf1 = NULL;
char *buf2 = NULL;
int total_host_services = 0;
Expand Down Expand Up @@ -704,10 +708,19 @@ static int grab_standard_host_macro_r(nagios_macros *mac, int macro_type, host *
case MACRO_HOSTDURATION:
time(&current_time);
duration = (unsigned long)(current_time - temp_host->last_state_change);

if (macro_type == MACRO_HOSTDURATIONSEC)
*output = (char *)mkstr("%lu", duration);
else {
*output = (char *)mkstr("%s", duration_string(duration));

days = duration / 86400;
duration -= (days * 86400);
hours = duration / 3600;
duration -= (hours * 3600);
minutes = duration / 60;
duration -= (minutes * 60);
seconds = duration;
*output = (char *)mkstr("%dd %dh %dm %ds", days, hours, minutes, seconds);
}
break;
case MACRO_HOSTEXECUTIONTIME:
Expand Down Expand Up @@ -735,7 +748,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 = temp_host->current_notification_id;
*output = (char *)mkstr("%lu", temp_host->current_notification_id);
break;
case MACRO_HOSTEVENTID:
*output = (char *)mkstr("%lu", temp_host->current_event_id);
Expand All @@ -744,32 +757,10 @@ 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:
if(temp_host->current_problem_id != NULL)
*output = temp_host->current_problem_id;
*output = (char *)mkstr("%lu", temp_host->current_problem_id);
break;
case MACRO_LASTHOSTPROBLEMID:
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);
break;
case MACRO_HOSTPROBLEMEND:
*output = (char *)mkstr("%lu", (unsigned long)temp_host->problem_end);
break;
case MACRO_HOSTPROBLEMDURATIONSEC:
case MACRO_HOSTPROBLEMDURATION:
if(temp_host->problem_end > 0) {
duration = (unsigned long)(temp_host->problem_end - temp_host->problem_start);
} else if(temp_host->problem_start > 0) {
time(&current_time);
duration = (unsigned long)(current_time - temp_host->problem_start);
}
if (macro_type == MACRO_HOSTPROBLEMDURATIONSEC)
*output = (char *)mkstr("%lu", duration);
else {
*output = (char *)mkstr("%s", duration_string(duration));
}
*output = (char *)mkstr("%lu", temp_host->last_problem_id);
break;
case MACRO_HOSTACTIONURL:
if (temp_host->action_url)
Expand Down Expand Up @@ -959,6 +950,10 @@ static int grab_standard_service_macro_r(nagios_macros *mac, int macro_type, ser
objectlist *temp_objectlist = NULL;
time_t current_time = 0L;
unsigned long duration = 0L;
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
char *buf1 = NULL;
char *buf2 = NULL;

Expand Down Expand Up @@ -1049,19 +1044,31 @@ static int grab_standard_service_macro_r(nagios_macros *mac, int macro_type, ser
break;
case MACRO_SERVICEDURATIONSEC:
case MACRO_SERVICEDURATION:

time(&current_time);
duration = (unsigned long)(current_time - temp_service->last_state_change);

/* get the state duration in seconds */
if (macro_type == MACRO_SERVICEDURATIONSEC)
*output = (char *)mkstr("%lu", duration);

/* get the state duration */
else {
*output = (char *)mkstr("%s", duration_string(duration));
days = duration / 86400;
duration -= (days * 86400);
hours = duration / 3600;
duration -= (hours * 3600);
minutes = duration / 60;
duration -= (minutes * 60);
seconds = duration;
*output = (char *)mkstr("%dd %dh %dm %ds", days, hours, minutes, seconds);
}
break;
case MACRO_SERVICENOTIFICATIONNUMBER:
*output = (char *)mkstr("%d", temp_service->current_notification_number);
break;
case MACRO_SERVICENOTIFICATIONID:
*output = temp_service->current_notification_id;
*output = (char *)mkstr("%lu", temp_service->current_notification_id);
break;
case MACRO_SERVICEEVENTID:
*output = (char *)mkstr("%lu", temp_service->current_event_id);
Expand All @@ -1070,30 +1077,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 = temp_service->current_problem_id;
*output = (char *)mkstr("%lu", temp_service->current_problem_id);
break;
case MACRO_LASTSERVICEPROBLEMID:
*output = temp_service->last_problem_id;
break;
case MACRO_SERVICEPROBLEMSTART:
*output = (char *)mkstr("%lu", (unsigned long)temp_service->problem_start);
break;
case MACRO_SERVICEPROBLEMEND:
*output = (char *)mkstr("%lu", (unsigned long)temp_service->problem_end);
break;
case MACRO_SERVICEPROBLEMDURATIONSEC:
case MACRO_SERVICEPROBLEMDURATION:
if(temp_service->problem_end > 0) {
duration = (unsigned long)(temp_service->problem_end - temp_service->problem_start);
} else if(temp_service->problem_start > 0) {
time(&current_time);
duration = (unsigned long)(current_time - temp_service->problem_start);
}
if (macro_type == MACRO_SERVICEPROBLEMDURATIONSEC)
*output = (char *)mkstr("%lu", duration);
else {
*output = (char *)mkstr("%s", duration_string(duration));
}
*output = (char *)mkstr("%lu", temp_service->last_problem_id);
break;
case MACRO_SERVICEACTIONURL:
if (temp_service->action_url)
Expand Down Expand Up @@ -1582,11 +1569,6 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
case MACRO_LASTHOSTPROBLEMID:
case MACRO_LASTHOSTSTATE:
case MACRO_LASTHOSTSTATEID:
case MACRO_HOSTPROBLEMSTART:
case MACRO_HOSTPROBLEMEND:
case MACRO_HOSTPROBLEMDURATIONSEC:
case MACRO_HOSTPROBLEMDURATION:


/* a standard host macro */
if (arg2 == NULL) {
Expand Down Expand Up @@ -1706,10 +1688,6 @@ static int grab_macrox_value_r(nagios_macros *mac, int macro_type, char *arg1, c
case MACRO_LASTSERVICEPROBLEMID:
case MACRO_LASTSERVICESTATE:
case MACRO_LASTSERVICESTATEID:
case MACRO_SERVICEPROBLEMSTART:
case MACRO_SERVICEPROBLEMEND:
case MACRO_SERVICEPROBLEMDURATIONSEC:
case MACRO_SERVICEPROBLEMDURATION:

/* use saved service pointer */
if (arg1 == NULL && arg2 == NULL) {
Expand Down Expand Up @@ -2713,14 +2691,6 @@ int init_macrox_names(void)
add_macrox_name(HOSTVALUE);
add_macrox_name(SERVICEVALUE);
add_macrox_name(PROBLEMVALUE);
add_macrox_name(HOSTPROBLEMSTART);
add_macrox_name(HOSTPROBLEMEND);
add_macrox_name(HOSTPROBLEMDURATIONSEC);
add_macrox_name(HOSTPROBLEMDURATION);
add_macrox_name(SERVICEPROBLEMSTART);
add_macrox_name(SERVICEPROBLEMEND);
add_macrox_name(SERVICEPROBLEMDURATIONSEC);
add_macrox_name(SERVICEPROBLEMDURATION);

return OK;
}
Expand Down
29 changes: 26 additions & 3 deletions src/naemon/objects_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ int setup_host_variables(host *new_host, const char *display_name, const char *a
new_host->last_hard_state = initial_state;
new_host->current_attempt = (initial_state == STATE_UP) ? 1 : max_attempts;
new_host->notifications_enabled = (notifications_enabled > 0) ? TRUE : FALSE;
new_host->display_status = initial_state;

return 0;
}
Expand Down Expand Up @@ -326,9 +327,6 @@ 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 Expand Up @@ -596,6 +594,31 @@ int log_host_event(host *hst)
{
unsigned long log_options = 0L;

/* set displays status */
/* handle Downtime and ACK */
// if ( (hst->scheduled_downtime_depth > 0) && (hst->problem_has_been_acknowledged == FALSE) ) {
if ( hst->scheduled_downtime_depth > 0 ) {
hst->display_status = 1;
}
else if (hst->problem_has_been_acknowledged == TRUE) {
hst->display_status = 2;
}
/* Flapping*/
else if (hst->is_flapping > 0) {
hst->display_status = 3;
}
/* Unreachable */
else if (hst->current_state == STATE_UNREACHABLE) {
hst->display_status = 7;
}
/* Down */
else if (hst->current_state == STATE_DOWN) {
hst->display_status = 8;
}
else if (hst->current_state == STATE_OK) {
hst->display_status = 0;
}

/* get the log options */
if (hst->current_state == STATE_DOWN)
log_options = NSLOG_HOST_DOWN;
Expand Down
9 changes: 4 additions & 5 deletions src/naemon/objects_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ struct host {
int current_attempt;
unsigned long current_event_id;
unsigned long last_event_id;
char *current_problem_id;
char *last_problem_id;
time_t problem_start;
time_t problem_end;
unsigned long current_problem_id;
unsigned long last_problem_id;
double latency;
double execution_time;
int is_executing;
Expand All @@ -112,7 +110,7 @@ struct host {
int notified_on;
int current_notification_number;
int no_more_notifications;
char *current_notification_id;
unsigned long current_notification_id;
int check_flapping_recovery_notification;
int scheduled_downtime_depth;
int pending_flex_downtime; /* UNUSED */
Expand All @@ -124,6 +122,7 @@ struct host {
struct objectlist *comments_list;
double percent_state_change;
int total_services;
int display_status;
unsigned long modified_attributes;
struct command *event_handler_ptr;
struct command *check_command_ptr;
Expand Down
30 changes: 27 additions & 3 deletions src/naemon/objects_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ int setup_service_variables(service *new_service, const char *display_name, cons
new_service->current_state = initial_state;
new_service->last_state = initial_state;
new_service->last_hard_state = initial_state;
new_service->display_status = initial_state;

/* check the service check_command */

Expand Down Expand Up @@ -339,9 +340,6 @@ 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 Expand Up @@ -519,6 +517,32 @@ int log_service_event(service *svc)
{
unsigned long log_options = 0L;

if ( svc->scheduled_downtime_depth > 0 ) {
svc->display_status = 1;
}
else if (svc->problem_has_been_acknowledged == TRUE) {
svc->display_status = 2;
}
/* Flapping*/
else if (svc->is_flapping > 0) {
svc->display_status = 3;
}
/* Warning */
else if (svc->current_state == STATE_WARNING) {
svc->display_status = 4;
}
/* Unknown */
else if (svc->current_state == STATE_UNKNOWN) {
svc->display_status = 5;
}
/* CRITICAL */
else if (svc->current_state == STATE_CRITICAL) {
svc->display_status = 6;
}
else if (svc->current_state == STATE_OK) {
svc->display_status = 0;
}

/* don't log soft errors if the user doesn't want to */
if (svc->state_type == SOFT_STATE && !log_service_retries)
return OK;
Expand Down
9 changes: 4 additions & 5 deletions src/naemon/objects_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ struct service {
int current_attempt;
unsigned long current_event_id;
unsigned long last_event_id;
char *current_problem_id;
char *last_problem_id;
time_t problem_start;
time_t problem_end;
unsigned long current_problem_id;
unsigned long last_problem_id;
time_t last_notification;
time_t next_notification;
int no_more_notifications;
Expand All @@ -101,7 +99,7 @@ struct service {
int is_being_freshened;
unsigned int notified_on;
int current_notification_number;
char *current_notification_id;
unsigned long current_notification_id;
double latency;
double execution_time;
int is_executing;
Expand All @@ -118,6 +116,7 @@ struct service {
struct host *host_ptr;
struct command *event_handler_ptr;
char *event_handler_args;
int display_status;
struct command *check_command_ptr;
struct timeperiod *check_period_ptr;
struct timeperiod *notification_period_ptr;
Expand Down
Loading

0 comments on commit 834f773

Please sign in to comment.