JohnMurray / jsendnsca

Automatically exported from code.google.com/p/jsendnsca
0 stars 0 forks source link

jsendnsca sends an extra character in the host name, service name and message field #15

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

this code sends an alert to nagios using the maximum size for all fields:

import java.io.IOException;

import com.googlecode.jsendnsca.core.MessagePayload;
import com.googlecode.jsendnsca.core.NagiosException;
import com.googlecode.jsendnsca.core.NagiosPassiveCheckSender;
import com.googlecode.jsendnsca.core.NagiosSettings;
import com.googlecode.jsendnsca.core.builders.MessagePayloadBuilder;
import com.googlecode.jsendnsca.core.builders.NagiosSettingsBuilder;

public class Example {
    public static void main(String argv[]){
        NagiosSettings nagiosSettings = NagiosSettingsBuilder
        .withNagiosHost("localhost")
        .withPort(5667)
        .withConnectionTimeout(5000)
        .withResponseTimeout(15000)
        .withPassword("hasturrocks")
        .create();

        NagiosPassiveCheckSender sender = new NagiosPassiveCheckSender(
                nagiosSettings);

        final int MAX_HOSTNAME_SIZE = 64;
        final int MAX_SERVICE_NAME_SIZE = 128;
        final int MAX_MESSAGE_SIZE = 512;

        byte[] maxHostnameBytes = new byte[MAX_HOSTNAME_SIZE];
        byte[] maxServiceNameBytes = new byte[MAX_SERVICE_NAME_SIZE];
        byte[] maxMessageBytes = new byte[MAX_MESSAGE_SIZE];

        for(int i = 0; i < MAX_HOSTNAME_SIZE; i++){
            maxHostnameBytes[i] = 'X';
        }

        String maxHostname = new String(maxHostnameBytes);

        for(int i = 0; i < MAX_SERVICE_NAME_SIZE; i++){
            maxServiceNameBytes[i] = 'Y';
        }

        String maxServiceName = new String(maxServiceNameBytes);

        for(int i = 0; i < MAX_MESSAGE_SIZE; i++){
            maxMessageBytes[i] = 'Z';
        }

        String maxMessage = new String(maxMessageBytes);

        MessagePayload payload = MessagePayloadBuilder
        .withHostname(maxHostname)
        .withLevel(0)
        .withServiceName(maxServiceName)
        .withMessage(maxMessage)
        .create();

        try {
            sender.send(payload);
        } catch (NagiosException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

What is the expected output? What do you see instead?

nsca only shows 63 characters for the host name, 127 characters for the
service name and 511 characters for the message. Since nsca is written in C
it needs any string to be null terminated meaning that even though a nsca
packet has 64 bytes for the host name the last one would always need to be
'\0'.

Rather than imply that a 64 character host name sent by jsendnsca would
actually make it to nsca, it would probably be better to only try sending
63 in the first place.

The same kind of problem exists for the service name and the message too.

Original issue reported on code.google.com by jeremy.s...@gmail.com on 19 Dec 2009 at 9:43

GoogleCodeExporter commented 9 years ago
Hi jeremy

As discussed, will fix this soon and make a new release

Original comment by rajneeshpatel on 21 Dec 2009 at 9:28

GoogleCodeExporter commented 9 years ago
I've checked in code to fix this and a test to prove it.

There should be another test to confirm that NagiosStub would allow the too long
messages so the test that proves the fields are trimmed down to the appropriate
length is valid.

I've proved this externally, but since I've changed the code to not allow the 
too
long fields a broken version us needed to prove the fixed version works.

Original comment by jeremy.s...@gmail.com on 26 Dec 2009 at 11:00

GoogleCodeExporter commented 9 years ago
Hi Jeremy

Will chat to you about next steps this week and get a release out as soon as

Original comment by rajneeshpatel on 28 Dec 2009 at 8:41