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

Fix logbuilder for target 'agent' on <localfile> #4942

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/logcollector/logcollector.c
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,7 @@ void * w_output_thread(void * args){
// When dealing with this type of messages we don't want any of them to be lost
// Continuously attempt to reconnect to the queue and send the message.

if(SendMSG(logr_queue, message->buffer, message->file, message->queue_mq) != 0) {
if(SendMSGtoSCK(logr_queue, message->buffer, message->file, message->queue_mq, message->log_target) != 0) {
#ifdef CLIENT
merror("Unable to send message to '%s' (ossec-agentd might be down). Attempting to reconnect.", DEFAULTQPATH);
#else
Expand Down
120 changes: 64 additions & 56 deletions src/shared/mq_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,77 +116,85 @@ int SendMSGtoSCK(int queue, const char *message, const char *locmsg, __attribute

tmpstr[OS_MAXSTR] = '\0';

int sock_type;
const char * strmode;

switch (target->log_socket->mode) {
case IPPROTO_UDP:
sock_type = SOCK_DGRAM;
strmode = "udp";
break;
case IPPROTO_TCP:
sock_type = SOCK_STREAM;
strmode = "tcp";
break;
default:
merror("At %s(): undefined protocol. This shouldn't happen.", __FUNCTION__);
free(_message);
return -1;
}

// create message and add prefix
if (target->log_socket->prefix && *target->log_socket->prefix) {
snprintf(tmpstr, OS_MAXSTR, "%s%s", target->log_socket->prefix, _message);
} else {
snprintf(tmpstr, OS_MAXSTR, "%s", _message);
}

// Connect to socket if disconnected
if (target->log_socket->socket < 0) {
if (mtime = time(NULL), mtime > target->log_socket->last_attempt + sock_fail_time) {
if (target->log_socket->socket = OS_ConnectUnixDomain(target->log_socket->location, sock_type, OS_MAXSTR + 256), target->log_socket->socket < 0) {
target->log_socket->last_attempt = mtime;
merror("Unable to connect to socket '%s': %s (%s)", target->log_socket->name, target->log_socket->location, strmode);
free(_message);
return -1;
}
if (strcmp(target->log_socket->name, "agent") == 0) {
if(SendMSG(queue, _message, locmsg, loc) != 0) {
free(_message);
return -1;
}
FernandoCP marked this conversation as resolved.
Show resolved Hide resolved
}else{
int sock_type;
const char * strmode;

switch (target->log_socket->mode) {
case IPPROTO_UDP:
sock_type = SOCK_DGRAM;
strmode = "udp";
break;
case IPPROTO_TCP:
sock_type = SOCK_STREAM;
strmode = "tcp";
break;
default:
merror("At %s(): undefined protocol. This shouldn't happen.", __FUNCTION__);
free(_message);
return -1;
}

mdebug1("Connected to socket '%s' (%s)", target->log_socket->name, target->log_socket->location);
// create message and add prefix
if (target->log_socket->prefix && *target->log_socket->prefix) {
snprintf(tmpstr, OS_MAXSTR, "%s%s", target->log_socket->prefix, _message);
} else {
mdebug2("Discarding event from '%s' due to connection issue with '%s'", locmsg, target->log_socket->name);
free(_message);
return 0;
snprintf(tmpstr, OS_MAXSTR, "%s", _message);
}
}

// Send msg to socket
if (__mq_rcode = OS_SendUnix(target->log_socket->socket, tmpstr, strlen(tmpstr)), __mq_rcode < 0) {
if (__mq_rcode == OS_SOCKTERR) {
// Connect to socket if disconnected
if (target->log_socket->socket < 0) {
if (mtime = time(NULL), mtime > target->log_socket->last_attempt + sock_fail_time) {
close(target->log_socket->socket);

if (target->log_socket->socket = OS_ConnectUnixDomain(target->log_socket->location, sock_type, OS_MAXSTR + 256), target->log_socket->socket < 0) {
merror("Unable to connect to socket '%s': %s (%s)", target->log_socket->name, target->log_socket->location, strmode);
target->log_socket->last_attempt = mtime;
} else {
mdebug1("Connected to socket '%s' (%s)", target->log_socket->name, target->log_socket->location);
merror("Unable to connect to socket '%s': %s (%s)", target->log_socket->name, target->log_socket->location, strmode);
free(_message);
return -1;
}

mdebug1("Connected to socket '%s' (%s)", target->log_socket->name, target->log_socket->location);
} else {
mdebug2("Discarding event from '%s' due to connection issue with '%s'", locmsg, target->log_socket->name);
free(_message);
return 0;
}
}

if (OS_SendUnix(target->log_socket->socket, tmpstr, strlen(tmpstr)), __mq_rcode < 0) {
merror("Cannot send message to socket '%s'. (Retry)", target->log_socket->name);
SendMSG(queue, "Cannot send message to socket.", "logcollector", LOCALFILE_MQ);
// Send msg to socket
if (__mq_rcode = OS_SendUnix(target->log_socket->socket, tmpstr, strlen(tmpstr)), __mq_rcode < 0) {
if (__mq_rcode == OS_SOCKTERR) {
if (mtime = time(NULL), mtime > target->log_socket->last_attempt + sock_fail_time) {
close(target->log_socket->socket);

if (target->log_socket->socket = OS_ConnectUnixDomain(target->log_socket->location, sock_type, OS_MAXSTR + 256), target->log_socket->socket < 0) {
merror("Unable to connect to socket '%s': %s (%s)", target->log_socket->name, target->log_socket->location, strmode);
target->log_socket->last_attempt = mtime;
} else {
mdebug1("Connected to socket '%s' (%s)", target->log_socket->name, target->log_socket->location);

if (OS_SendUnix(target->log_socket->socket, tmpstr, strlen(tmpstr)), __mq_rcode < 0) {
merror("Cannot send message to socket '%s'. (Retry)", target->log_socket->name);
SendMSG(queue, "Cannot send message to socket.", "logcollector", LOCALFILE_MQ);
target->log_socket->last_attempt = mtime;
}
}
} else {
mdebug2("Discarding event from '%s' due to connection issue with '%s'", locmsg, target->log_socket->name);
}
} else {
mdebug2("Discarding event from '%s' due to connection issue with '%s'", locmsg, target->log_socket->name);
merror("Cannot send message to socket '%s'. (Retry)", target->log_socket->name);
SendMSG(queue, "Cannot send message to socket.", "logcollector", LOCALFILE_MQ);
}
} else {
merror("Cannot send message to socket '%s'. (Retry)", target->log_socket->name);
SendMSG(queue, "Cannot send message to socket.", "logcollector", LOCALFILE_MQ);
}
}

free(_message);
free(_message);
return (0);
}
return (0);
}

Expand Down