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

Fixed: Naemon stops executing checks and doesnt respawn Core Worker #419 #421

Merged
merged 19 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
5 changes: 3 additions & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
//"/usr/lib/gcc/x86_64-redhat-linux/12/include/**", // Fedora
"/usr/local/include/**",
"/usr/include/**",
"/usr/lib64/**"
"/usr/lib64/**",
"/usr/lib/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
Expand All @@ -19,4 +20,4 @@
}
],
"version": 4
}
}
8 changes: 7 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
// This will trigger the "make all" task from tasks.json
"preLaunchTask": "make all",
// We are not using the binary from the build folder because we do not want to run the "make install" task.
// The "make install" task is only to generate the default naemon configurtion files
// The "make install" task is only to generate the default naemon configurtion files
"program": "${workspaceFolder}/src/naemon/.libs/naemon",
"args": [
//"--help",
"${workspaceFolder}/build/etc/naemon/naemon.cfg"
],
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "LD_LIBRARY_PATH",
"value": "${workspaceFolder}/.libs"
}
],

// Optional parameter. If true, the debugger should stop at the entrypoint of the target.
"stopAtEntry": true,
Expand Down
1 change: 1 addition & 0 deletions naemon-core.spec
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Requires(pre): systemd
Requires(post): systemd
Requires(preun): systemd
Requires(postun): systemd
BuildRequires: pkgconfig(systemd)
%if 0%{suse_version} < 1230
Requires(pre): pwdutils
%else
Expand Down
52 changes: 27 additions & 25 deletions src/naemon/checks_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,33 +633,35 @@ static void handle_worker_host_check(wproc_result *wpres, void *arg, int flags)
if (currently_running_host_checks > 0)
currently_running_host_checks--;

hst = find_host(cr->host_name);
if (hst && wpres) {
hst->is_executing = FALSE;
memcpy(&cr->rusage, &wpres->rusage, sizeof(wpres->rusage));
cr->start_time.tv_sec = wpres->start.tv_sec;
cr->start_time.tv_usec = wpres->start.tv_usec;
cr->finish_time.tv_sec = wpres->stop.tv_sec;
cr->finish_time.tv_usec = wpres->stop.tv_usec;
if (WIFEXITED(wpres->wait_status)) {
cr->return_code = WEXITSTATUS(wpres->wait_status);
} else {
cr->return_code = STATE_UNKNOWN;
}
if (wpres) {
hst = find_host(cr->host_name);
if (hst) {
hst->is_executing = FALSE;
memcpy(&cr->rusage, &wpres->rusage, sizeof(wpres->rusage));
cr->start_time.tv_sec = wpres->start.tv_sec;
cr->start_time.tv_usec = wpres->start.tv_usec;
cr->finish_time.tv_sec = wpres->stop.tv_sec;
cr->finish_time.tv_usec = wpres->stop.tv_usec;
if (WIFEXITED(wpres->wait_status)) {
cr->return_code = WEXITSTATUS(wpres->wait_status);
} else {
cr->return_code = STATE_UNKNOWN;
}

if (wpres->outstd && *wpres->outstd) {
cr->output = nm_strdup(wpres->outstd);
} else if (wpres->outerr && *wpres->outerr) {
nm_asprintf(&cr->output, "(No output on stdout) stderr: %s", wpres->outerr);
} else {
cr->output = NULL;
}
if (wpres->outstd && *wpres->outstd) {
cr->output = nm_strdup(wpres->outstd);
} else if (wpres->outerr && *wpres->outerr) {
nm_asprintf(&cr->output, "(No output on stdout) stderr: %s", wpres->outerr);
} else {
cr->output = NULL;
}

cr->early_timeout = wpres->early_timeout;
cr->exited_ok = wpres->exited_ok;
cr->engine = NULL;
cr->source = wpres->source;
process_check_result(cr);
cr->early_timeout = wpres->early_timeout;
cr->exited_ok = wpres->exited_ok;
cr->engine = NULL;
cr->source = wpres->source;
process_check_result(cr);
}
}
free_check_result(cr);
nm_free(cr);
Expand Down
2 changes: 0 additions & 2 deletions src/naemon/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,6 @@ NAGIOS_END_DECL

#define MAX_FILENAME_LENGTH 256 /* max length of path/filename that Nagios will process */
#define MAX_INPUT_BUFFER 1024 /* size in bytes of max. input buffer (for reading files, misc stuff) */
#define MAX_COMMAND_BUFFER 8192 /* max length of raw or processed command line */

#define MAX_DATETIME_LENGTH 48


Expand Down
72 changes: 39 additions & 33 deletions src/naemon/macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ static int grab_custom_object_macro_r(nagios_macros *mac, char *macro_name, cust
/* given a "raw" command, return the "expanded" or "whole" command line */
int get_raw_command_line_r(nagios_macros *mac, command *cmd_ptr, char *cmd, char **full_command, int macro_options)
{
char temp_arg[MAX_COMMAND_BUFFER] = "";
char *temp_arg = NULL;
char *arg_buffer = NULL;
size_t cmd_len = 0;
register int x = 0;
register int y = 0;
register int arg_index = 0;
Expand All @@ -139,51 +140,56 @@ int get_raw_command_line_r(nagios_macros *mac, command *cmd_ptr, char *cmd, char
/* get the full command line */
*full_command = nm_strdup((cmd_ptr->command_line == NULL) ? "" : cmd_ptr->command_line);

/* XXX: Crazy indent */
/* get the command arguments */
if (cmd != NULL) {
if (cmd == NULL) {
log_debug_info(DEBUGL_COMMANDS | DEBUGL_CHECKS | DEBUGL_MACROS, 2, "Expanded Command Output: %s\n", *full_command);
return OK;
}

/* skip the command name (we're about to get the arguments)... */
for (arg_index = 0;; arg_index++) {
if (cmd[arg_index] == '!' || cmd[arg_index] == '\x0')
break;
}
cmd_len = strlen(cmd);
temp_arg = nm_malloc(cmd_len);

/* get each command argument */
for (x = 0; x < MAX_COMMAND_ARGUMENTS; x++) {
/* get the command arguments */
/* skip the command name (we're about to get the arguments)... */
for (arg_index = 0;; arg_index++) {
if (cmd[arg_index] == '!' || cmd[arg_index] == '\x0')
break;
}

/* we reached the end of the arguments... */
if (cmd[arg_index] == '\x0')
break;
/* get each command argument */
for (x = 0; x < MAX_COMMAND_ARGUMENTS; x++) {

/* get the next argument */
/* can't use strtok(), as that's used in process_macros... */
for (arg_index++, y = 0; y < (int)sizeof(temp_arg) - 1; arg_index++) {
/* we reached the end of the arguments... */
if (cmd[arg_index] == '\x0')
break;

/* handle escaped argument delimiters */
if (cmd[arg_index] == '\\' && cmd[arg_index + 1] == '!') {
arg_index++;
} else if (cmd[arg_index] == '!' || cmd[arg_index] == '\x0') {
/* end of argument */
break;
}
/* get the next argument */
/* can't use strtok(), as that's used in process_macros... */
for (arg_index++, y = 0; y < (int)cmd_len - 1; arg_index++) {

/* copy the character */
temp_arg[y] = cmd[arg_index];
y++;
/* handle escaped argument delimiters */
if (cmd[arg_index] == '\\' && cmd[arg_index + 1] == '!') {
arg_index++;
} else if (cmd[arg_index] == '!' || cmd[arg_index] == '\x0') {
/* end of argument */
break;
}
temp_arg[y] = '\x0';

/* ADDED 01/29/04 EG */
/* process any macros we find in the argument */
process_macros_r(mac, temp_arg, &arg_buffer, macro_options);

mac->argv[x] = arg_buffer;
/* copy the character */
temp_arg[y] = cmd[arg_index];
y++;
}
temp_arg[y] = '\x0';

/* ADDED 01/29/04 EG */
/* process any macros we find in the argument */
process_macros_r(mac, temp_arg, &arg_buffer, macro_options);

mac->argv[x] = arg_buffer;
}

log_debug_info(DEBUGL_COMMANDS | DEBUGL_CHECKS | DEBUGL_MACROS, 2, "Expanded Command Output: %s\n", *full_command);

nm_free(temp_arg);
return OK;
}

Expand Down
8 changes: 5 additions & 3 deletions src/naemon/nebmods.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,11 @@ int neb_load_module(nebmodule *mod)

/* check the module API version */
if (module_version_ptr == NULL || ((*module_version_ptr) != CURRENT_NEB_API_VERSION)) {

nm_log(NSLOG_RUNTIME_ERROR, "Error: Module '%s' is using an old or unspecified version of the event broker API. Module will be unloaded.\n", mod->filename);

if (module_version_ptr == NULL) {
nm_log(NSLOG_RUNTIME_ERROR, "Error: Module '%s' did not specify a version of the event broker API. Module will be unloaded.\n", mod->filename);
} else {
nm_log(NSLOG_RUNTIME_ERROR, "Error: Module '%s' is using an incompatible version (v%d) of the event broker API (current version: v%d). Module will be unloaded.\n", mod->filename, *module_version_ptr, CURRENT_NEB_API_VERSION);
}
neb_unload_module(mod, NEBMODULE_FORCE_UNLOAD, NEBMODULE_ERROR_API_VERSION);

return ERROR;
Expand Down
59 changes: 54 additions & 5 deletions src/naemon/objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "logging.h"
#include "globals.h"
#include "nm_alloc.h"
#include "utils.h"

int __nagios_object_structure_version = CURRENT_OBJECT_STRUCTURE_VERSION;

Expand All @@ -20,17 +21,32 @@ int fcache_objects(char *cache_file)
FILE *fp = NULL;
time_t current_time = 0L;
unsigned int i;
char *tmp_file = NULL;
int fd = 0;
int result = OK;

/* some people won't want to cache their objects */
if (!cache_file || !strcmp(cache_file, "/dev/null"))
return OK;

time(&current_time);

nm_asprintf(&tmp_file, "%sXXXXXX", cache_file);
if (tmp_file == NULL)
return ERROR;

if ((fd = mkstemp(tmp_file)) == -1) {
nm_log(NSLOG_RUNTIME_ERROR, "Error: Unable to create temp file '%s' for writing object cache data: %s\n", tmp_file, strerror(errno));
nm_free(tmp_file);
return ERROR;
}

/* open the cache file for writing */
fp = fopen(cache_file, "w");
fp = (FILE *)fopen(tmp_file, "w");
if (fp == NULL) {
nm_log(NSLOG_CONFIG_WARNING, "Warning: Could not open object cache file '%s' for writing!\n", cache_file);
unlink(tmp_file);
nm_log(NSLOG_CONFIG_WARNING, "Warning: Could not open object cache data file '%s' for writing!\n", tmp_file);
nm_free(tmp_file);
return ERROR;
}

Expand All @@ -44,7 +60,6 @@ int fcache_objects(char *cache_file)
fprintf(fp, "# Created: %s", ctime(&current_time));
fprintf(fp, "########################################\n\n");


/* cache timeperiods */
for (i = 0; i < num_objects.timeperiods; i++)
fcache_timeperiod(fp, timeperiod_ary[i]);
Expand Down Expand Up @@ -109,7 +124,41 @@ int fcache_objects(char *cache_file)
fcache_hostescalation(fp, esclist->object_ptr);
}

fclose(fp);
/* reset file permissions */
fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);

/* flush the file to disk */
fflush(fp);

/* fsync the file so that it is completely written out before moving it */
fsync(fd);

/* close the temp file */
result = ferror(fp) | fclose(fp);

/* save/close was successful */
if (result == 0) {

result = OK;

/* move the temp file to the objects data file (overwrite the old objects.cache) */
if (my_rename(tmp_file, cache_file)) {
unlink(tmp_file);
nm_log(NSLOG_RUNTIME_ERROR, "Error: Unable to update cache data file '%s': %s", cache_file, strerror(errno));
result = ERROR;
}
}

/* a problem occurred saving the file */
else {
result = ERROR;

/* remove temp file and log an error */
unlink(tmp_file);
nm_log(NSLOG_RUNTIME_ERROR, "Error: Unable to save cache data file: %s", strerror(errno));
}

nm_free(tmp_file);

return OK;
return result;
}
Loading
Loading