int init_objects_service(int elems)
{
    service_ary = nm_calloc(elems, sizeof(service*));
    service_hash_table = g_hash_table_new_full(nm_service_hash, nm_service_equal,
                         (GDestroyNotify) nm_service_key_destroy, NULL);
    return OK;
}
Beispiel #2
0
host *create_host(const char *name)
{
	host *new_host = NULL;

	if (name == NULL || !strcmp(name, "")) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Host name is NULL\n");
		return NULL;
	}

	if (contains_illegal_object_chars(name) == TRUE) {
		nm_log(NSLOG_VERIFICATION_ERROR, "Error: The name of host '%s' contains one or more illegal characters.", name);
		return NULL;
	}

	new_host = nm_calloc(1, sizeof(*new_host));

	new_host->name = new_host->display_name = new_host->alias = new_host->address = nm_strdup(name);
	new_host->child_hosts = g_tree_new_full((GCompareDataFunc)g_strcmp0, NULL, g_free, NULL);
	new_host->parent_hosts = g_tree_new_full((GCompareDataFunc)g_strcmp0, NULL, g_free, NULL);
	new_host->check_type = CHECK_TYPE_ACTIVE;
	new_host->state_type = HARD_STATE;
	new_host->acknowledgement_type = ACKNOWLEDGEMENT_NONE;
	new_host->check_options = CHECK_OPTION_NONE;


	return new_host;
}
service *create_service(host *hst, const char *description)
{
    service *new_service = NULL;
    servicesmember *new_servicesmember = NULL;

    if (!hst) {
        nm_log(NSLOG_CONFIG_ERROR, "Error: No host provided for service '%s'\n",
               description);
        return NULL;
    }

    if (description == NULL || !*description) {
        nm_log(NSLOG_CONFIG_ERROR, "Error: Found service on host '%s' with no service description\n", hst->name);
        return NULL;
    }

    if (contains_illegal_object_chars(description) == TRUE) {
        nm_log(NSLOG_VERIFICATION_ERROR, "Error: The description string for service '%s' on host '%s' contains one or more illegal characters.", description, hst->name);
        return NULL;
    }

    /* allocate memory */
    new_service = nm_calloc(1, sizeof(*new_service));

    new_service->host_ptr = hst;
    new_service->host_name = hst->name;

    new_servicesmember = nm_calloc(1, sizeof(servicesmember));
    new_servicesmember->host_name = new_service->host_name;
    new_servicesmember->service_description = new_service->description;
    new_servicesmember->service_ptr = new_service;
    new_servicesmember->next = hst->services;
    hst->services = new_servicesmember;
    hst->total_services++;

    new_service->description = nm_strdup(description);
    new_service->display_name = new_service->description;
    new_service->acknowledgement_type = ACKNOWLEDGEMENT_NONE;
    new_service->check_type = CHECK_TYPE_ACTIVE;
    new_service->state_type = HARD_STATE;
    new_service->check_options = CHECK_OPTION_NONE;

    return new_service;
}
Beispiel #4
0
/* handles service check results in an obsessive compulsive manner... */
int obsessive_compulsive_service_check_processor(service *svc)
{
	char *raw_command = NULL;
	char *processed_command = NULL;
	int macro_options = STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS;
	nagios_macros mac;
	struct obsessive_compulsive_job *ocj;

	if (svc == NULL)
		return ERROR;

	/* bail out if we shouldn't be obsessing */
	if (obsess_over_services == FALSE || svc->obsess == FALSE)
		return OK;

	/* if there is no valid command, exit */
	if (ocsp_command == NULL)
		return ERROR;

	/* update service macros */
	memset(&mac, 0, sizeof(mac));
	grab_service_macros_r(&mac, svc);

	get_raw_command_line_r(&mac, ocsp_command_ptr, ocsp_command, &raw_command, macro_options);
	if (raw_command == NULL) {
		clear_volatile_macros_r(&mac);
		return ERROR;
	}

	log_debug_info(DEBUGL_CHECKS, 2, "Raw obsessive compulsive service processor command line: %s\n", raw_command);

	/* process any macros in the raw command line */
	process_macros_r(&mac, raw_command, &processed_command, macro_options);
	nm_free(raw_command);
	if (processed_command == NULL) {
		clear_volatile_macros_r(&mac);
		return ERROR;
	}

	log_debug_info(DEBUGL_CHECKS, 2, "Processed obsessive compulsive service processor command line: %s\n", processed_command);

	/* run the command through a worker */
	ocj = nm_calloc(1,sizeof(struct obsessive_compulsive_job));
	ocj->hst = svc->host_ptr;
	ocj->svc = svc;
	if(ERROR == wproc_run_callback(processed_command, ocsp_timeout, obsessive_compulsive_job_handler, ocj, &mac)) {
		nm_log(NSLOG_RUNTIME_ERROR, "Unable to start OCSP job for service '%s on host '%s' to worker\n", svc->description, svc->host_ptr->name);
		free(ocj);
	}

	clear_volatile_macros_r(&mac);
	nm_free(processed_command);

	return OK;
}
Beispiel #5
0
/* schedule a new timed event */
timed_event *schedule_new_event(int event_type, int high_priority, time_t run_time, int recurring, unsigned long event_interval, void *timing_func, int compensate_for_time_change, void *event_data, void *event_args, int event_options)
{
	timed_event *new_event;
	char run_time_string[MAX_DATETIME_LENGTH] = "";

	log_debug_info(DEBUGL_FUNCTIONS, 0, "schedule_new_event()\n");

	get_datetime_string(&run_time, run_time_string, MAX_DATETIME_LENGTH,
	                    SHORT_DATE_TIME);
	log_debug_info(DEBUGL_EVENTS, 0, "New Event Details:\n");
	log_debug_info(DEBUGL_EVENTS, 0, " Type:                       EVENT_%s\n",
	               EVENT_TYPE_STR(event_type));
	log_debug_info(DEBUGL_EVENTS, 0, " High Priority:              %s\n",
	               (high_priority ? "Yes" : "No"));
	log_debug_info(DEBUGL_EVENTS, 0, " Run Time:                   %s\n",
	               run_time_string);
	log_debug_info(DEBUGL_EVENTS, 0, " Recurring:                  %s\n",
	               (recurring ? "Yes" : "No"));
	log_debug_info(DEBUGL_EVENTS, 0, " Event Interval:             %lu\n",
	               event_interval);
	log_debug_info(DEBUGL_EVENTS, 0, " Compensate for Time Change: %s\n",
	               (compensate_for_time_change ? "Yes" : "No"));
	log_debug_info(DEBUGL_EVENTS, 0, " Event Options:              %d\n",
	               event_options);

	new_event = nm_calloc(1, sizeof(timed_event));
	if (new_event != NULL) {
		new_event->event_type = event_type;
		new_event->event_data = event_data;
		new_event->event_args = event_args;
		new_event->event_options = event_options;
		new_event->run_time = run_time;
		new_event->recurring = recurring;
		new_event->event_interval = event_interval;
		new_event->timing_func = timing_func;
		new_event->compensate_for_time_change = compensate_for_time_change;
		new_event->priority = high_priority;
	} else
		return NULL;

	log_debug_info(DEBUGL_EVENTS, 0, " Event ID:                   %p\n", new_event);

	/* add the event to the event list */
	add_event(nagios_squeue, new_event);

	return new_event;
}
serviceescalation *add_serviceescalation(char *host_name, char *description, int first_notification, int last_notification, double notification_interval, char *escalation_period, int escalation_options)
{
	serviceescalation *new_serviceescalation = NULL;
	service *svc;
	timeperiod *tp = NULL;

	/* make sure we have the data we need */
	if (host_name == NULL || !*host_name || description == NULL || !*description) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Service escalation host name or description is NULL\n");
		return NULL;
	}
	if (!(svc = find_service(host_name, description))) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Service '%s' on host '%s' has an escalation but is not defined anywhere!\n",
		       host_name, description);
		return NULL;
	}
	if (escalation_period && !(tp = find_timeperiod(escalation_period))) {
		nm_log(NSLOG_VERIFICATION_ERROR, "Error: Escalation period '%s' specified in service escalation for service '%s' on host '%s' is not defined anywhere!\n",
		       escalation_period, description, host_name);
		return NULL ;
	}

	new_serviceescalation = nm_calloc(1, sizeof(*new_serviceescalation));

	if (prepend_object_to_objectlist(&svc->escalation_list, new_serviceescalation) != OK) {
		nm_log(NSLOG_CONFIG_ERROR, "Could not add escalation to service '%s' on host '%s'\n",
		       svc->host_name, svc->description);
		return NULL;
	}

	/* assign vars. object names are immutable, so no need to copy */
	new_serviceescalation->host_name = svc->host_name;
	new_serviceescalation->description = svc->description;
	new_serviceescalation->service_ptr = svc;
	new_serviceescalation->escalation_period_ptr = tp;
	if (tp)
		new_serviceescalation->escalation_period = tp->name;

	new_serviceescalation->first_notification = first_notification;
	new_serviceescalation->last_notification = last_notification;
	new_serviceescalation->notification_interval = (notification_interval <= 0) ? 0 : notification_interval;
	new_serviceescalation->escalation_options = escalation_options;

	new_serviceescalation->id = num_objects.serviceescalations++;
	return new_serviceescalation;
}
servicesmember *add_parent_to_service(service *svc, service *parent)
{
    servicesmember *sm;

    if (!svc || !parent)
        return NULL;

    sm = nm_calloc(1, sizeof(*sm));

    sm->host_name = parent->host_name;
    sm->service_description = parent->description;
    sm->service_ptr = parent;
    sm->next = svc->parents;
    svc->parents = sm;

    return sm;
}
hostescalation *add_hostescalation(char *host_name, int first_notification, int last_notification, double notification_interval, char *escalation_period, int escalation_options)
{
	hostescalation *new_hostescalation = NULL;
	host *h;
	timeperiod *tp = NULL;

	/* make sure we have the data we need */
	if (host_name == NULL || !*host_name) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Host escalation host name is NULL\n");
		return NULL;
	}
	if (!(h = find_host(host_name))) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Host '%s' has an escalation, but is not defined anywhere!\n", host_name);
		return NULL;
	}
	if (escalation_period && !(tp = find_timeperiod(escalation_period))) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Unable to locate timeperiod '%s' for hostescalation '%s'\n",
		       escalation_period, host_name);
		return NULL;
	}

	new_hostescalation = nm_calloc(1, sizeof(*new_hostescalation));

	/* add the escalation to its host */
	if (prepend_object_to_objectlist(&h->escalation_list, new_hostescalation) != OK) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Could not add hostescalation to host '%s'\n", host_name);
		free(new_hostescalation);
		return NULL;
	}

	/* assign vars. Object names are immutable, so no need to copy */
	new_hostescalation->host_name = h->name;
	new_hostescalation->host_ptr = h;
	new_hostescalation->escalation_period = tp ? tp->name : NULL;
	new_hostescalation->escalation_period_ptr = tp;
	new_hostescalation->first_notification = first_notification;
	new_hostescalation->last_notification = last_notification;
	new_hostescalation->notification_interval = (notification_interval <= 0) ? 0 : notification_interval;
	new_hostescalation->escalation_options = escalation_options;

	new_hostescalation->id = num_objects.hostescalations++;
	return new_hostescalation;
}
timeperiod *create_timeperiod(const char *name, const char *alias)
{
	timeperiod *new_timeperiod = NULL;

	/* make sure we have the data we need */
	if ((name == NULL || !strcmp(name, "")) || (alias == NULL || !strcmp(alias, ""))) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Name or alias for timeperiod is NULL\n");
		return NULL;
	}
	if (contains_illegal_object_chars(name) == TRUE) {
		nm_log(NSLOG_VERIFICATION_ERROR, "Error: The name of time period '%s' contains one or more illegal characters.", name);
		return NULL;
	}

	new_timeperiod = nm_calloc(1, sizeof(*new_timeperiod));

	/* copy string vars */
	new_timeperiod->name = nm_strdup(name);
	new_timeperiod->alias = alias ? nm_strdup(alias) : new_timeperiod->name;

	return new_timeperiod;
}
Beispiel #10
0
int qh_register_handler(const char *name, const char *description, unsigned int options, qh_handler handler)
{

	struct query_handler *qh;

	g_return_val_if_fail(qh_table != NULL, -1);
	g_return_val_if_fail(name != NULL, -1);

	if (!handler) {
		nm_log(NSLOG_RUNTIME_ERROR, "qh: Failed to register handler '%s': No handler function specified\n", name);
		return -1;
	}

	if (strlen(name) > 128) {
		nm_log(NSLOG_RUNTIME_ERROR, "qh: Failed to register handler '%s': Name too long\n", name);
		return -ENAMETOOLONG;
	}

	/* names must be unique */
	if (qh_find_handler(name)) {
		nm_log(NSLOG_RUNTIME_WARNING, "qh: Handler '%s' registered more than once\n", name);
		return -1;
	}

	qh = nm_calloc(1, sizeof(*qh));
	qh->name = name;
	qh->description = description;
	qh->handler = handler;
	qh->options = options;
	qh->next_qh = qhandlers;
	if (qhandlers)
		qhandlers->prev_qh = qh;
	qhandlers = qh;

	g_hash_table_insert(qh_table, nm_strdup(qh->name), qh);

	return 0;
}
int init_objects_timeperiod(int elems)
{
	timeperiod_ary = nm_calloc(elems, sizeof(timeperiod*));
	timeperiod_hash_table = g_hash_table_new(g_str_hash, g_str_equal);
	return OK;
}
Beispiel #12
0
int init_objects_host(int elems)
{
	host_ary = nm_calloc(elems, sizeof(host*));
	host_hash_table = g_hash_table_new(g_str_hash, g_str_equal);
	return OK;
}