Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature global notification handler #477

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sample-config/naemon.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ log_passive_checks=1



# GLOBAL NOTIFICATIONS LOGGING OPTION
# If you don't want Naemon to log global notifications, set
# this value to 0. If they should be logged, set
# this value to 1.

log_global_notifications=1



# GLOBAL HOST AND SERVICE EVENT HANDLERS
# These options allow you to specify a host and service event handler
# command that is to be run for every host or service state change.
Expand All @@ -288,6 +297,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
17 changes: 17 additions & 0 deletions src/naemon/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,17 @@ 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;

case CMD_LOG:
nm_log(NSLOG_EXT_CUSTOM, "%s", ext_command->raw_arguments);
return OK;

default:
nm_log(NSLOG_RUNTIME_ERROR, "Unknown global command ID %d", ext_command->id);
return ERROR;
Expand Down Expand Up @@ -3286,6 +3297,10 @@ void register_core_commands(void)
core_command = command_create("CHANGE_RETRY_HOST_CHECK_INTERVAL", host_command_handler,
"Changes the retry check interval for a particular host.", "host=host_name;timestamp=check_interval");
command_register(core_command, CMD_CHANGE_RETRY_HOST_CHECK_INTERVAL);

core_command = command_create("LOG", global_command_handler,
"Adds custom entry to the default log file.", NULL);
command_register(core_command, CMD_LOG);
}

/******************************************************************/
Expand Down Expand Up @@ -3427,6 +3442,8 @@ int process_external_command(char *cmd, int mode, GError **error)
/* passive checks are logged in checks.c as well, as some my bypass external commands by getting dropped in checkresults dir */
if (log_passive_checks == TRUE)
nm_log(NSLOG_PASSIVE_CHECK, "%s", temp_buffer);
} else if (id == CMD_LOG) {
/* skip loging same message twice */
} else if (log_external_commands == TRUE) {
nm_log(NSLOG_EXTERNAL_COMMAND, "%s", temp_buffer);
}
Expand Down
3 changes: 3 additions & 0 deletions src/naemon/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ NAGIOS_END_DECL
#define CMD_ACKNOWLEDGE_HOST_PROBLEM_EXPIRE 173
#define CMD_ACKNOWLEDGE_SVC_PROBLEM_EXPIRE 174

#define CMD_LOG 175

/* custom command introduced in Nagios 3.x */
#define CMD_CUSTOM_COMMAND 999

Expand Down Expand Up @@ -494,4 +496,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 */
46 changes: 44 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 @@ -365,6 +375,17 @@ read_config_file(const char *main_config_file, nagios_macros *mac)
log_current_states = (atoi(value) > 0) ? TRUE : FALSE;
}

else if (!strcmp(variable, "log_global_notifications")) {

if (strlen(value) != 1 || value[0] < '0' || value[0] > '1') {
nm_asprintf(&error_message, "Illegal value for log_global_notifications");
error = TRUE;
break;
}

log_global_notifications = (atoi(value) > 0) ? TRUE : FALSE;
}

else if (!strcmp(variable, "retain_state_information")) {

if (strlen(value) != 1 || value[0] < '0' || value[0] > '1') {
Expand Down Expand Up @@ -1342,6 +1363,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 +1497,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 +1541,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
1 change: 1 addition & 0 deletions src/naemon/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#define DEFAULT_LOG_CURRENT_STATES 1 /* log current service and host states after rotating log */
#define DEFAULT_LOG_EXTERNAL_COMMANDS 1 /* log external commands */
#define DEFAULT_LOG_PASSIVE_CHECKS 1 /* log passive service checks */
#define DEFAULT_log_global_notifications 1 /* log global notifications */

#define DEFAULT_DEBUG_LEVEL 0 /* don't log any debugging information */
#define DEFAULT_DEBUG_VERBOSITY 1
Expand Down
6 changes: 6 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 All @@ -59,6 +64,7 @@ extern int log_host_retries;
extern int log_event_handlers;
extern int log_external_commands;
extern int log_passive_checks;
extern int log_global_notifications;
extern unsigned long logging_options;
extern unsigned long syslog_options;

Expand Down
1 change: 1 addition & 0 deletions src/naemon/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#define NSLOG_HOST_NOTIFICATION 524288
#define NSLOG_SERVICE_NOTIFICATION 1048576
#define NSLOG_EXT_CUSTOM 2097152

/***************** DEBUGGING LEVELS *******************/

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
Loading