Skip to content

Commit

Permalink
memory-monitor: Log last lines of failed handler script to syslog.
Browse files Browse the repository at this point in the history
Add functionality to capture and log the last 10 lines of the handler
script output when it exits with a non-zero status. This ensures that
relevant log information is sent to syslog, making it accessible to log
aggregation tools for better visibility.

* Introduced `get_tail()` to extract the last n lines of a log file.
* Updated the script handler logic to send the last 10 lines to syslog
  upon failure.

This enhancement improves error diagnosis by ensuring failed handler
output is readily available in centralized logging systems.

Signed-off-by: Nikolay Martyanov <[email protected]>
  • Loading branch information
OhmSpectator committed Oct 15, 2024
1 parent c65ba9e commit 5c73b71
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/memory-monitor/src/monitor/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ int run_handler(const char *script_name, const char *event_msg) {
pthread_mutex_unlock(&handler_mutex);
return 1;
}
} else {
// If the status is not 0, print several lines from the end of the log file
char *tail = get_tail(LOG_DIR "/" HANDLER_LOG_FILE, 10);
if (tail != NULL) {
syslog(LOG_ERR, "Handler script output (last 10 lines):\n%s\n", tail);
free(tail);
} else {
syslog(LOG_ERR, "Failed to read the handler log file\n");
}
}
} else {
syslog(LOG_INFO, "Handler script exited abnormally by signal %d\n", WTERMSIG(status));
Expand Down
63 changes: 63 additions & 0 deletions pkg/memory-monitor/src/monitor/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,66 @@ int convert_mb_to_bytes_signed(long mb, long *bytes_out) {
}
return 0;
}

char *get_tail(const char *file_path, size_t num_lines) {
// Open the file
FILE *file = fopen(file_path, "r");
if (file == NULL) {
syslog(LOG_ERR, "Failed to open file: %s", strerror(errno));
return NULL;
}

// Seek to the end of the file
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
if (file_size == -1) {
syslog(LOG_ERR, "Failed to get file size: %s", strerror(errno));
fclose(file);
return NULL;
}

// Start reading the file backwards to find the last `num_lines` lines
int lines_read = 0;
long i;
char c;
for (i = file_size - 1; i >= 0; i--) {
fseek(file, i, SEEK_SET);
c = fgetc(file);
if (c == '\n') {
lines_read++;
if (lines_read == num_lines + 1) { // +1 to include the current line
break;
}
}
}

// If we didn't find enough lines, reset i to start of file
if (i < 0) {
i = 0;
} else {
i += 2; // Move past the '\n' we stopped at
}

// Allocate memory for the buffer to hold the result
size_t buffer_size = file_size - i + 1; // Include space for null terminator
char *buffer = (char *) malloc(buffer_size);
if (buffer == NULL) {
syslog(LOG_ERR, "Failed to allocate memory: %s", strerror(errno));
fclose(file);
return NULL;
}

// Read from the file from the correct position
fseek(file, i, SEEK_SET);
size_t bytes_read = fread(buffer, 1, buffer_size - 1, file);
if (bytes_read != buffer_size - 1) {
syslog(LOG_ERR, "Failed to read file: %s", strerror(errno));
free(buffer);
fclose(file);
return NULL;
}
buffer[buffer_size - 1] = '\0'; // Null-terminate the buffer

fclose(file);
return buffer;
}
1 change: 1 addition & 0 deletions pkg/memory-monitor/src/monitor/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ int get_eve_release(char *eve_release);
void log_event(const time_t *t, const char *format, ...);
long strtodec(const char *str, bool *error);
unsigned long strtoudec(const char *str, bool *error);
char* get_tail(const char *file_path, size_t lines);

#endif //MM_UTILS_H

0 comments on commit 5c73b71

Please sign in to comment.