Skip to content

Commit

Permalink
add global notification handler
Browse files Browse the repository at this point in the history
global notification handler are like global event handler. They are fired each time
a notification would be sent out (regardless whether there is a contact or not)

    global_host_notification_handler=somecommand
    global_service_notification_handler=somecommand

global notification handler work like normal notifications, except they don't have contact
related macros set.
  • Loading branch information
sni committed Sep 13, 2024
1 parent fc93bc0 commit f285220
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 12 deletions.
13 changes: 13 additions & 0 deletions sample-config/naemon.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,19 @@ log_passive_checks=1



# GLOBAL HOST AND SERVICE NOTIFICATION HANDLERS
# These options allow you to specify a host and service notification handler
# command that is to be run for every host or service.
# The global notification handler is executed immediately prior to the other
# notifications that you have optionally specified in each host or
# service definition. The command argument is the short name of a
# command definition that you define in your host configuration file.

#global_host_notification_handler=somecommand
#global_service_notification_handler=somecommand



# MAXIMUM CONCURRENT SERVICE CHECKS
# This option allows you to specify the maximum number of
# service checks that can be run in parallel at any given time.
Expand Down
10 changes: 8 additions & 2 deletions src/naemon/broker.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ void broker_program_status(int type, int flags, int attr)
ds.modified_service_attributes = modified_service_process_attributes;
ds.global_host_event_handler = global_host_event_handler;
ds.global_service_event_handler = global_service_event_handler;
ds.global_host_notification_handler = global_host_notification_handler;
ds.global_service_notification_handler = global_service_notification_handler;

/* make callbacks */
neb_make_callbacks(NEBCALLBACK_PROGRAM_STATUS_DATA, (void *)&ds);
Expand Down Expand Up @@ -582,7 +584,9 @@ int broker_contact_notification_data(int type, int flags, int attr, int notifica
ds.start_time = start_time;
ds.end_time = end_time;
ds.reason_type = reason_type;
ds.contact_name = cntct->name;
ds.contact_name = NULL;
if(cntct != NULL)
ds.contact_name = cntct->name;
if (notification_type == SERVICE_NOTIFICATION) {
temp_service = (service *)data;
ds.host_name = temp_service->host_name;
Expand Down Expand Up @@ -640,7 +644,9 @@ int broker_contact_notification_method_data(int type, int flags, int attr, int n
ds.start_time = start_time;
ds.end_time = end_time;
ds.reason_type = reason_type;
ds.contact_name = cntct->name;
ds.contact_name = NULL;
if(cntct != NULL)
ds.contact_name = cntct->name;
ds.command_name = command_name;
ds.command_args = command_args;
if (notification_type == SERVICE_NOTIFICATION) {
Expand Down
7 changes: 7 additions & 0 deletions src/naemon/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,13 @@ static int global_command_handler(const struct external_command *ext_command, ti
case CMD_PROCESS_FILE:
return process_external_commands_from_file(GV_STRING("file_name"), GV_BOOL("delete"));

case CMD_CHANGE_GLOBAL_SVC_EVENT_HANDLER:
/* disabled */
return ERROR;
case CMD_CHANGE_GLOBAL_HOST_EVENT_HANDLER:
/* disabled */
return ERROR;

default:
nm_log(NSLOG_RUNTIME_ERROR, "Unknown global command ID %d", ext_command->id);
return ERROR;
Expand Down
1 change: 1 addition & 0 deletions src/naemon/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,5 @@ NAGIOS_END_DECL
#define MODATTR_CHECK_TIMEPERIOD 16384
#define MODATTR_CUSTOM_VARIABLE 32768
#define MODATTR_NOTIFICATION_TIMEPERIOD 65536
#define MODATTR_NOTIFICATION_HANDLER_COMMAND 131072
#endif /* INCLUDE_COMMON_H */
35 changes: 33 additions & 2 deletions src/naemon/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ read_config_file(const char *main_config_file, nagios_macros *mac)
global_service_event_handler = nm_strdup(value);
}

else if (!strcmp(variable, "global_host_notification_handler")) {
nm_free(global_host_notification_handler);
global_host_notification_handler = nm_strdup(value);
}

else if (!strcmp(variable, "global_service_notification_handler")) {
nm_free(global_service_notification_handler);
global_service_notification_handler = nm_strdup(value);
}

else if (!strcmp(variable, "ocsp_command")) {
nm_free(ocsp_command);
ocsp_command = nm_strdup(value);
Expand Down Expand Up @@ -1342,6 +1352,27 @@ int pre_flight_check(void)
}


/********************************************/
/* check global notification handler commands... */
/********************************************/
if (verify_config)
printf("Checking global notification handlers...\n");
if (global_host_notification_handler != NULL) {
global_host_notification_handler_ptr = find_bang_command(global_host_notification_handler);
if (global_host_notification_handler_ptr == NULL) {
nm_log(NSLOG_VERIFICATION_ERROR, "Error: Global host notification handler command '%s' is not defined anywhere!", global_host_notification_handler);
errors++;
}
}
if (global_service_notification_handler != NULL) {
global_service_notification_handler_ptr = find_bang_command(global_service_notification_handler);
if (global_service_notification_handler_ptr == NULL) {
nm_log(NSLOG_VERIFICATION_ERROR, "Error: Global service notification handler command '%s' is not defined anywhere!", global_service_notification_handler);
errors++;
}
}


/**************************************************/
/* check obsessive processor commands... */
/**************************************************/
Expand Down Expand Up @@ -1455,7 +1486,7 @@ int pre_flight_object_check(int *w, int *e)
}

/* check to see if there is at least one contact/group */
if (temp_service->contacts == NULL && temp_service->contact_groups == NULL) {
if (temp_service->contacts == NULL && temp_service->contact_groups == NULL && global_service_event_handler == NULL) {
nm_log(NSLOG_VERIFICATION_WARNING, "Warning: Service '%s' on host '%s' has no default contacts or contactgroups defined!", temp_service->description, temp_service->host_name);
warnings++;
}
Expand Down Expand Up @@ -1499,7 +1530,7 @@ int pre_flight_object_check(int *w, int *e)
}

/* check to see if there is at least one contact/group */
if (temp_host->contacts == NULL && temp_host->contact_groups == NULL) {
if (temp_host->contacts == NULL && temp_host->contact_groups == NULL && global_host_notification_handler == NULL) {
nm_log(NSLOG_VERIFICATION_WARNING, "Warning: Host '%s' has no default contacts or contactgroups defined!", temp_host->name);
warnings++;
}
Expand Down
5 changes: 5 additions & 0 deletions src/naemon/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ extern char *global_service_event_handler;
extern command *global_host_event_handler_ptr;
extern command *global_service_event_handler_ptr;

extern char *global_host_notification_handler;
extern char *global_service_notification_handler;
extern command *global_host_notification_handler_ptr;
extern command *global_service_notification_handler_ptr;

extern int use_regexp_matches;
extern int use_true_regexp_matching;

Expand Down
2 changes: 2 additions & 0 deletions src/naemon/nebstructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ typedef struct nebstruct_program_status_struct {
unsigned long modified_service_attributes;
char *global_host_event_handler;
char *global_service_event_handler;
char *global_host_notification_handler;
char *global_service_notification_handler;
} nebstruct_program_status_data;


Expand Down
Loading

0 comments on commit f285220

Please sign in to comment.