JohnMurray / jsendnsca

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

Not working with nsca-2.7.2 #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I tried using the Command line client and api to send NSCA message and the
application says it is sent, but I can't see any message in nagios.log

But at the same time actual C implementation of NSCA client works properly.

Original issue reported on code.google.com by rajupr...@gmail.com on 19 May 2009 at 4:06

GoogleCodeExporter commented 9 years ago
My Source code as below

package atom.nagios.nsca;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Properties;

import com.googlecode.jsendnsca.core.MessagePayload;
import com.googlecode.jsendnsca.core.NagiosException;
import com.googlecode.jsendnsca.core.NagiosPassiveCheckSender;
import com.googlecode.jsendnsca.core.NagiosSettings;

public class NMSNotifier 
{
    public static final int LEVEL_OK = 0;
    public static final int LEVEL_WARNING = 1;
    public static final int LEVEL_CRITICAL = 2;
    public static final int LEVEL_UNKNOWN = 3;

    public static  NMSNotifier Instance()
    {
        return new NMSNotifier();
    }

    public boolean sendNMSNotification(String sServiceName, int iNotificationLevel,
String sMessage) 
    {
        Properties props = new Properties();
        try
        {
            props.load(new FileInputStream("/etc/send_nsca.ini"));

        }catch(Exception ex)
        {
            ex.printStackTrace();
            return false;
        }

        System.out.println("Nagios Host: " + props.getProperty("NagiosHost"));
        System.out.println("Encryption Method: " + props.getProperty("EncryptionMethod"));
        System.out.println("Timeout: " + props.getProperty("Timeout"));
        System.out.println("Port: " + props.getProperty("Port"));

        final NagiosSettings nagiosSettings = new NagiosSettings();
        nagiosSettings.setNagiosHost(props.getProperty("NagiosHost"));

        //nagiosSettings.se
        if(Integer.parseInt(props.getProperty("EncryptionMethod")) != 0)
        {
            nagiosSettings.setPassword(props.getProperty("Password"));

nagiosSettings.setEncryptionMethod(Integer.parseInt(props.getProperty("Encryptio
nMethod")));
// 0 - No Encryption, 1 - XOR_ENCRYPTION, 3 - 3DES
        }
        else
        {
            nagiosSettings.setEncryptionMethod(NagiosSettings.NO_ENCRYPTION);
        }
        nagiosSettings.setTimeout(Integer.parseInt(props.getProperty("Timeout")));
        nagiosSettings.setPort(Integer.parseInt(props.getProperty("Port")));

        final MessagePayload messagePayload = new MessagePayload();

        try
        {
            InetAddress thisIp = InetAddress.getLocalHost();
            //messagePayload.setHostname(thisIp.getHostAddress());
            messagePayload.setHostname(true);
            messagePayload.setHostname("192.168.50.1");
            System.out.println("LocalHost : " + thisIp.getHostAddress());
        }catch(Exception ex)
        {
            ex.printStackTrace();
            return false;
        }

        messagePayload.setLevel(iNotificationLevel);
        messagePayload.setServiceName(sServiceName);
        messagePayload.setMessage(sMessage);

        final NagiosPassiveCheckSender nagiosPassiveCheckSender = new
NagiosPassiveCheckSender(nagiosSettings);
        try 
        {
            nagiosPassiveCheckSender.send(messagePayload);

            System.out.println("Returning True");
            return true;
        } catch (NagiosException e) 
        {
            e.printStackTrace();
        } catch (IOException e) 
        {
            e.printStackTrace();
        }

        return false;
    }

    public static void main(String[] args)
    {
        NMSNotifier.Instance().sendNMSNotification("PG
Timeout",NMSNotifier.LEVEL_CRITICAL,"NSCA Test");       
    }
}

Original comment by rajupr...@gmail.com on 19 May 2009 at 4:11

GoogleCodeExporter commented 9 years ago
I got it working. In my ini configuration file there was a space at the end of 
the
password. 

Thanks. Great Library.

Original comment by rajupr...@gmail.com on 19 May 2009 at 4:26

GoogleCodeExporter commented 9 years ago
Glad you found you're issue and got it working.

Regards

Raj

Original comment by rajneeshpatel on 19 May 2009 at 6:08