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; }
/* add a new timerange to a daterange */ timerange *add_timerange_to_daterange(daterange *drange, unsigned long start_time, unsigned long end_time) { timerange *new_timerange = NULL; /* make sure we have the data we need */ if (drange == NULL) return NULL; if (start_time > 86400) { nm_log(NSLOG_CONFIG_ERROR, "Error: Start time %lu is not valid for timeperiod\n", start_time); return NULL; } if (end_time > 86400) { nm_log(NSLOG_CONFIG_ERROR, "Error: End time %lu is not value for timeperiod\n", end_time); return NULL; } /* allocate memory for the new time range */ new_timerange = nm_malloc(sizeof(timerange)); new_timerange->range_start = start_time; new_timerange->range_end = end_time; /* add the new time range to the head of the range list for this date range */ new_timerange->next = drange->times; drange->times = new_timerange; return new_timerange; }
int register_service(service *new_service) { host *h; g_return_val_if_fail(service_hash_table != NULL, ERROR); if (!(h = find_host(new_service->host_name))) { nm_log(NSLOG_CONFIG_ERROR, "Error: Unable to locate host '%s' for service '%s'\n", new_service->host_name, new_service->description); return ERROR; } if ((find_service(new_service->host_name, new_service->description))) { nm_log(NSLOG_CONFIG_ERROR, "Error: Service '%s' on host '%s' has already been defined\n", new_service->description, new_service->host_name); return ERROR; } g_hash_table_insert(service_hash_table, nm_service_key_create(new_service->host_name, new_service->description), new_service); new_service->id = num_objects.services++; service_ary[new_service->id] = new_service; if (new_service->id) service_ary[new_service->id - 1]->next = new_service; else service_list = new_service; return OK; }
/* add a new timerange to a timeperiod */ timerange *add_timerange_to_timeperiod(timeperiod *period, int day, unsigned long start_time, unsigned long end_time) { timerange *prev = NULL, *tr, *new_timerange = NULL; /* make sure we have the data we need */ if (period == NULL) return NULL; if (day < 0 || day > 6) { nm_log(NSLOG_CONFIG_ERROR, "Error: Day %d is not valid for timeperiod '%s'\n", day, period->name); return NULL; } if (start_time > 86400) { nm_log(NSLOG_CONFIG_ERROR, "Error: Start time %lu on day %d is not valid for timeperiod '%s'\n", start_time, day, period->name); return NULL; } if (end_time > 86400) { nm_log(NSLOG_CONFIG_ERROR, "Error: End time %lu on day %d is not value for timeperiod '%s'\n", end_time, day, period->name); return NULL; } /* allocate memory for the new time range */ new_timerange = nm_malloc(sizeof(timerange)); new_timerange->range_start = start_time; new_timerange->range_end = end_time; /* insertion-sort the new time range into the list for this day */ if (!period->days[day] || period->days[day]->range_start > new_timerange->range_start) { new_timerange->next = period->days[day]; period->days[day] = new_timerange; return new_timerange; } for (tr = period->days[day]; tr; tr = tr->next) { if (new_timerange->range_start < tr->range_start) { new_timerange->next = tr; prev->next = new_timerange; break; } if (!tr->next) { tr->next = new_timerange; new_timerange->next = NULL; break; } prev = tr; } return new_timerange; }
void obsessive_compulsive_job_handler(struct wproc_result *wpres, void *data, int flags) { struct obsessive_compulsive_job *ocj = (struct obsessive_compulsive_job *)data; if (wpres) { if (wpres->early_timeout) { if(ocj->svc) { nm_log(NSLOG_RUNTIME_WARNING, "Warning: Timeout while executing OCSP command '%s' for service '%s' on host '%s'\n", wpres->command, ocj->svc->description, ocj->hst->name); } else { nm_log(NSLOG_RUNTIME_WARNING, "Warning: Timeout while executing OCHP command '%s' for host '%s'\n", wpres->command, ocj->hst->name); } } } free(ocj); }
void logger(int priority, const char *loginfo, ...) { va_list ap; va_start(ap, loginfo); /* Only the main process may use the Nagios log methods */ if (g_mainthread_id == pthread_self()) { char buffer[8192]; snprintf(buffer, 20, "livestatus: "); vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), loginfo, ap); va_end(ap); nm_log(priority, "%s", buffer); } else { lock_mutex_or_die(&g_log_file_mutex); if (g_logfile) { /* write date/time */ char timestring[64]; time_t now_t = time(0); struct tm now; localtime_r(&now_t, &now); strftime(timestring, 64, "%F %T ", &now); fputs(timestring, g_logfile); /* write log message */ vfprintf(g_logfile, loginfo, ap); fputc('\n', g_logfile); fflush(g_logfile); va_end(ap); } unlock_mutex_or_die(&g_log_file_mutex); } }
/* handles a host that is flapping */ void set_host_flap(host *hst, double percent_change, double high_threshold, double low_threshold) { char *temp_buffer = NULL; if (hst == NULL) return; log_debug_info(DEBUGL_FLAPPING, 1, "Host '%s' started flapping!\n", hst->name); /* log a notice - this one is parsed by the history CGI */ nm_log(NSLOG_RUNTIME_WARNING, "HOST FLAPPING ALERT: %s;STARTED; Host appears to have started flapping (%2.1f%% change > %2.1f%% threshold)\n", hst->name, percent_change, high_threshold); /* add a non-persistent comment to the host */ nm_asprintf(&temp_buffer, "Notifications for this host are being suppressed because it was detected as having been flapping between different states (%2.1f%% change > %2.1f%% threshold). When the host state stabilizes and the flapping stops, notifications will be re-enabled.", percent_change, high_threshold); add_new_host_comment(FLAPPING_COMMENT, hst->name, time(NULL), "(Naemon Process)", temp_buffer, 0, COMMENTSOURCE_INTERNAL, FALSE, (time_t)0, &(hst->flapping_comment_id)); nm_free(temp_buffer); /* set the flapping indicator */ hst->is_flapping = TRUE; broker_flapping_data(NEBTYPE_FLAPPING_START, NEBFLAG_NONE, NEBATTR_NONE, HOST_FLAPPING, hst, percent_change, high_threshold, low_threshold); /* see if we should check to send a recovery notification out when flapping stops */ if (hst->current_state != STATE_UP && hst->current_notification_number > 0) hst->check_flapping_recovery_notification = TRUE; else hst->check_flapping_recovery_notification = FALSE; /* send a notification */ host_notification(hst, NOTIFICATION_FLAPPINGSTART, NULL, NULL, NOTIFICATION_OPTION_NONE); return; }
static gboolean _handler_read (GKeyFile *keyfile, NMConnection *connection, NMKeyfileReadType type, void *type_data, void *user_data, GError **error) { if (type == NM_KEYFILE_READ_TYPE_WARN) { NMKeyfileReadTypeDataWarn *warn_data = type_data; NMLogLevel level; char *message_free = NULL; if (warn_data->severity > NM_KEYFILE_WARN_SEVERITY_WARN) level = LOGL_ERR; else if (warn_data->severity >= NM_KEYFILE_WARN_SEVERITY_WARN) level = LOGL_WARN; else if (warn_data->severity == NM_KEYFILE_WARN_SEVERITY_INFO_MISSING_FILE) level = LOGL_WARN; else level = LOGL_INFO; nm_log (level, LOGD_SETTINGS, "keyfile: %s", _fmt_warn (warn_data->group, warn_data->setting, warn_data->property_name, warn_data->message, &message_free)); g_free (message_free); return TRUE; } return FALSE; }
/* write a service problem/recovery to the naemon log file */ int log_service_event(service *svc) { unsigned long log_options = 0L; /* don't log soft errors if the user doesn't want to */ if (svc->state_type == SOFT_STATE && !log_service_retries) return OK; /* get the log options */ if (svc->current_state == STATE_UNKNOWN) log_options = NSLOG_SERVICE_UNKNOWN; else if (svc->current_state == STATE_WARNING) log_options = NSLOG_SERVICE_WARNING; else if (svc->current_state == STATE_CRITICAL) log_options = NSLOG_SERVICE_CRITICAL; else log_options = NSLOG_SERVICE_OK; nm_log(log_options, "SERVICE ALERT: %s;%s;%s;%s;%d;%s", svc->host_name, svc->description, service_state_name(svc->current_state), state_type_name(svc->state_type), svc->current_attempt, (svc->plugin_output == NULL) ? "" : svc->plugin_output); return OK; }
/* trap signals so we can exit gracefully */ void setup_sighandler(void) { size_t i; struct sigaction sigact; int handled_signals[] = { SIGQUIT, SIGTERM, SIGHUP, SIGUSR1, SIGINT }; /* remove buffering from stderr, stdin, and stdout */ setbuf(stdin, (char *)NULL); setbuf(stdout, (char *)NULL); setbuf(stderr, (char *)NULL); sigact.sa_handler = &sighandler; g_warn_if_fail(sigemptyset(&(sigact.sa_mask)) == 0); sigact.sa_flags = 0; /* initialize signal handling */ signal(SIGPIPE, SIG_IGN); for (i = 0; i < (sizeof(handled_signals) / sizeof(handled_signals[0])); ++i) { if (sigaction(handled_signals[i], &sigact, NULL) < 0) { nm_log(NSLOG_RUNTIME_ERROR, "Failed to set signal handler for '%s': %s", strsignal(handled_signals[i]), strerror(errno)); } } }
void signal_react() { int signum = sig_id; if (signum <= 0) return; if (sigrestart) { /* we received a SIGHUP, so restart... */ nm_log(NSLOG_PROCESS_INFO, "Caught '%s', restarting...\n", strsignal(signum)); } else if (sigfilesize) { handle_sigxfsz(); } else if (sigshutdown) { /* else begin shutting down... */ nm_log(NSLOG_PROCESS_INFO, "Caught '%s', shutting down...\n", strsignal(signum)); } sig_id = 0; }
/* * only handles logfile for now, which we stash in macros to * make sure we can log *somewhere* in case the new path is * completely inaccessible. */ static int test_configured_paths(void) { FILE *fp; nagios_macros *mac; mac = get_global_macros(); fp = fopen(log_file, "a+"); if (!fp) { /* * The variable trashing is so the logging code can * open the old logfile (if any), in case we got a * restart command or a SIGHUP */ char *value_absolute = log_file; log_file = mac->x[MACRO_LOGFILE]; nm_log(NSLOG_CONFIG_ERROR, "Error: Failed to open logfile '%s' for writing: %s\n", value_absolute, strerror(errno)); return ERROR; } fclose(fp); /* save the macro */ mac->x[MACRO_LOGFILE] = log_file; return OK; }
/* handles a host that has stopped flapping */ void clear_host_flap(host *hst, double percent_change, double high_threshold, double low_threshold) { if (hst == NULL) return; log_debug_info(DEBUGL_FLAPPING, 1, "Host '%s' stopped flapping.\n", hst->name); /* log a notice - this one is parsed by the history CGI */ nm_log(NSLOG_INFO_MESSAGE, "HOST FLAPPING ALERT: %s;STOPPED; Host appears to have stopped flapping (%2.1f%% change < %2.1f%% threshold)\n", hst->name, percent_change, low_threshold); /* delete the comment we added earlier */ if (hst->flapping_comment_id != 0) delete_host_comment(hst->flapping_comment_id); hst->flapping_comment_id = 0; /* clear the flapping indicator */ hst->is_flapping = FALSE; broker_flapping_data(NEBTYPE_FLAPPING_STOP, NEBFLAG_NONE, NEBATTR_FLAPPING_STOP_NORMAL, HOST_FLAPPING, hst, percent_change, high_threshold, low_threshold); /* send a notification */ host_notification(hst, NOTIFICATION_FLAPPINGSTOP, NULL, NULL, NOTIFICATION_OPTION_NONE); /* should we send a recovery notification? */ if (hst->check_flapping_recovery_notification == TRUE && hst->current_state == STATE_UP) host_notification(hst, NOTIFICATION_NORMAL, NULL, NULL, NOTIFICATION_OPTION_NONE); /* clear the recovery notification flag */ hst->check_flapping_recovery_notification = FALSE; return; }
/* handles a service that has stopped flapping */ void clear_service_flap(service *svc, double percent_change, double high_threshold, double low_threshold) { if (svc == NULL) return; log_debug_info(DEBUGL_FLAPPING, 1, "Service '%s' on host '%s' stopped flapping.\n", svc->description, svc->host_name); /* log a notice - this one is parsed by the history CGI */ nm_log(NSLOG_INFO_MESSAGE, "SERVICE FLAPPING ALERT: %s;%s;STOPPED; Service appears to have stopped flapping (%2.1f%% change < %2.1f%% threshold)\n", svc->host_name, svc->description, percent_change, low_threshold); /* delete the comment we added earlier */ if (svc->flapping_comment_id != 0) delete_service_comment(svc->flapping_comment_id); svc->flapping_comment_id = 0; /* clear the flapping indicator */ svc->is_flapping = FALSE; broker_flapping_data(NEBTYPE_FLAPPING_STOP, NEBFLAG_NONE, NEBATTR_FLAPPING_STOP_NORMAL, SERVICE_FLAPPING, svc, percent_change, high_threshold, low_threshold); /* send a notification */ service_notification(svc, NOTIFICATION_FLAPPINGSTOP, NULL, NULL, NOTIFICATION_OPTION_NONE); /* should we send a recovery notification? */ if (svc->check_flapping_recovery_notification == TRUE && svc->current_state == STATE_OK) service_notification(svc, NOTIFICATION_NORMAL, NULL, NULL, NOTIFICATION_OPTION_NONE); /* clear the recovery notification flag */ svc->check_flapping_recovery_notification = FALSE; return; }
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; }
int qh_init(const char *path) { int result, old_umask; if (qh_listen_sock >= 0) iobroker_close(nagios_iobs, qh_listen_sock); if (!path) { nm_log(NSLOG_RUNTIME_ERROR, "qh: query_socket is NULL. What voodoo is this?\n"); return ERROR; } old_umask = umask(0117); errno = 0; qh_listen_sock = nsock_unix(path, NSOCK_TCP | NSOCK_UNLINK); umask(old_umask); if (qh_listen_sock < 0) { nm_log(NSLOG_RUNTIME_ERROR, "qh: Failed to init socket '%s'. %s: %s\n", path, nsock_strerror(qh_listen_sock), strerror(errno)); return ERROR; } /* plugins shouldn't have this socket */ (void)fcntl(qh_listen_sock, F_SETFD, FD_CLOEXEC); /* most likely overkill, but it's small, so... */ qh_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify) qh_remove); errno = 0; result = iobroker_register(nagios_iobs, qh_listen_sock, NULL, qh_registration_input); if (result < 0) { g_hash_table_destroy(qh_table); close(qh_listen_sock); nm_log(NSLOG_RUNTIME_ERROR, "qh: Failed to register socket with io broker: %s\n", iobroker_strerror(result)); return ERROR; } nm_log(NSLOG_INFO_MESSAGE, "qh: Socket '%s' successfully initialized\n", path); /* now register our the in-core handlers */ qh_register_handler("command", "Naemon external commands interface", 0, qh_command); qh_register_handler("echo", "The Echo Service - What You Put Is What You Get", 0, qh_echo); qh_register_handler("help", "Help for the query handler", 0, qh_help); return 0; }
int signal_parent(int sig) { if (write(upipe_fd[PIPE_WRITE], &sig, sizeof(int)) < 0) { nm_log(NSLOG_RUNTIME_ERROR, "Failed to signal parent: %s", strerror(errno)); return ERROR; } return OK; }
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; }
int add_parent_to_host(host *hst, host *parent) { /* make sure we have the data we need */ if (hst == NULL || parent == NULL) { nm_log(NSLOG_CONFIG_ERROR, "Error: Host is NULL or parent host name is NULL\n"); return ERROR; } /* a host cannot be a parent/child of itself */ if (hst == parent) { nm_log(NSLOG_CONFIG_ERROR, "Error: Host '%s' cannot be a child/parent of itself\n", hst->name); return ERROR; } g_tree_insert(hst->parent_hosts, g_strdup(parent->name), parent); g_tree_insert(parent->child_hosts, g_strdup(hst->name), hst); return OK; }
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; }
void event_handler_job_handler(struct wproc_result *wpres, void *data, int flags) { const char *event_type = (const char*)data; if(wpres) { if (wpres->early_timeout) { nm_log(NSLOG_EVENT_HANDLER | NSLOG_RUNTIME_WARNING, "Warning: %s handler command '%s' timed out\n", event_type, wpres->command); } } }
/* 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; }
static int set_working_directory(void) { /* * we shouldn't block the unmounting of * filesystems, so chdir() to the root */ if (chdir("/") != 0) { nm_log(NSLOG_RUNTIME_ERROR, "Error: Aborting. Failed to set daemon working directory (/): %s\n", strerror(errno)); return (ERROR); } return (OK); }
/* flush the perfdata stored in `bq` to the file referred to by `fd`, named by `filename`. Returns -1 on error, 0 on success. */ static int flush_perfdata(nm_bufferqueue *bq, int fd, const char *filename) { if (fd >= 0) { if (nm_bufferqueue_write(bq, fd) >= 0) { return 0; } nm_log(NSLOG_RUNTIME_WARNING, "Warning: Failed to flush performance data to performance file %s", filename); } return -1; }
static int qh_registration_input(int sd, int events, void *bq_) { nm_bufferqueue *bq = (nm_bufferqueue *)bq_; struct sockaddr sa; socklen_t slen = 0; int nsd, result; memset(&sa, 0, sizeof(sa)); /* shut valgrind up */ nsd = accept(sd, &sa, &slen); if (qh_max_running && qh_running >= qh_max_running) { nsock_printf(nsd, "503: Server full"); close(nsd); return 0; } if (!(bq = nm_bufferqueue_create())) { nm_log(NSLOG_RUNTIME_ERROR, "qh: Failed to create iocache for inbound request\n"); nsock_printf(nsd, "500: Internal server error"); close(nsd); return 0; } /* * @todo: Stash the iocache and the socket in some * addressable list so we can release them on deinit */ result = iobroker_register(nagios_iobs, nsd, bq, qh_input); if (result < 0) { nm_log(NSLOG_RUNTIME_ERROR, "qh: Failed to register input socket %d with I/O broker: %s; errno=%d (%s)\n", nsd, iobroker_strerror(result), errno, strerror(errno)); nm_bufferqueue_destroy(bq); close(nsd); return 0; } /* make it non-blocking, but leave kernel buffers unchanged */ worker_set_sockopts(nsd, 0); qh_running++; return 0; }
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; }
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; }
/** * Checks a file to determine whether it exceeds resource limit imposed * limits. Returns the file size if file is OK, 0 if it's status could not * be determined, or -1 if not OK. fudge is the fudge factor (in bytes) for * checking the file size */ static long long check_file_size(char *path, unsigned long fudge, struct rlimit rlim) { struct stat status; /* Make sure we were passed a legitimate file path */ if (NULL == path) return 0; /* Get the status of the file */ if (stat(path, &status) < 0) { nm_log(NSLOG_RUNTIME_ERROR, "Unable to determine status of file %s: %s\n", path, strerror(errno)); return 0; } /* Make sure it is a file */ if (!S_ISREG(status.st_mode)) return 0; /* file size doesn't reach limit, just returns it */ if (status.st_size + fudge <= rlim.rlim_cur) return status.st_size; /* If the file size plus the fudge factor exceeds the current resource limit imposed size limit, log an error */ nm_log(NSLOG_RUNTIME_ERROR, "Size of file '%s' (%llu) " "exceeds (or nearly exceeds) size imposed by resource " "limits (%llu). Consider increasing limits with " "ulimit(1).\n", path, (unsigned long long)status.st_size, (unsigned long long)rlim.rlim_cur); return -1; }
/* logs host states */ int log_host_states(int type, time_t *timestamp) { host *temp_host = NULL;; /* bail if we shouldn't be logging initial states */ if (type == INITIAL_STATES && log_initial_states == FALSE) return OK; for (temp_host = host_list; temp_host != NULL; temp_host = temp_host->next) { nm_log(NSLOG_INFO_MESSAGE, "%s HOST STATE: %s;%s;%s;%d;%s\n", (type == INITIAL_STATES) ? "INITIAL" : "CURRENT", temp_host->name, host_state_name(temp_host->current_state), state_type_name(temp_host->state_type), temp_host->current_attempt, (temp_host->plugin_output == NULL) ? "" : temp_host->plugin_output); } return OK; }
/* renames a file */ int my_rename(char *source, char *dest) { int rename_result = 0; /* make sure we have something */ if (source == NULL || dest == NULL) return -1; /* first see if we can rename file with standard function */ rename_result = rename(source, dest); /* handle any errors... */ if (rename_result == -1) { nm_log(NSLOG_RUNTIME_ERROR, "Error: Unable to rename file '%s' to '%s': %s\n", source, dest, strerror(errno)); return rename_result; } return rename_result; }