Search in sources :

Example 1 with NagiosSettings

use of com.googlecode.jsendnsca.NagiosSettings in project pentaho-kettle by pentaho.

the class JobEntrySendNagiosPassiveCheck method execute.

public Result execute(Result previousResult, int nr) {
    log.logBasic(BaseMessages.getString(PKG, "JobEntrySendNagiosPassiveCheck.Started", serverName));
    Result result = previousResult;
    result.setNrErrors(1);
    result.setResult(false);
    // Target
    String realServername = environmentSubstitute(serverName);
    String realPassword = Utils.resolvePassword(variables, password);
    int realPort = Const.toInt(environmentSubstitute(port), DEFAULT_PORT);
    int realResponseTimeOut = Const.toInt(environmentSubstitute(responseTimeOut), DEFAULT_RESPONSE_TIME_OUT);
    int realConnectionTimeOut = Const.toInt(environmentSubstitute(connectionTimeOut), DEFAULT_CONNECTION_TIME_OUT);
    // Sender
    String realSenderServerName = environmentSubstitute(senderServerName);
    String realSenderServiceName = environmentSubstitute(senderServiceName);
    try {
        if (Utils.isEmpty(realServername)) {
            throw new KettleException(BaseMessages.getString(PKG, "JobSendNagiosPassiveCheck.Error.TargetServerMissing"));
        }
        String realMessageString = environmentSubstitute(message);
        if (Utils.isEmpty(realMessageString)) {
            throw new KettleException(BaseMessages.getString(PKG, "JobSendNagiosPassiveCheck.Error.MessageMissing"));
        }
        Level level = Level.UNKNOWN;
        switch(getLevel()) {
            case LEVEL_TYPE_OK:
                level = Level.OK;
                break;
            case LEVEL_TYPE_CRITICAL:
                level = Level.CRITICAL;
                break;
            case LEVEL_TYPE_WARNING:
                level = Level.WARNING;
                break;
            default:
                break;
        }
        Encryption encr = Encryption.NONE;
        switch(getEncryptionMode()) {
            case ENCRYPTION_MODE_TRIPLEDES:
                encr = Encryption.TRIPLE_DES;
                break;
            case ENCRYPTION_MODE_XOR:
                encr = Encryption.XOR;
                break;
            default:
                break;
        }
        // settings
        NagiosSettingsBuilder ns = new NagiosSettingsBuilder();
        ns.withNagiosHost(realServername);
        ns.withPort(realPort);
        ns.withConnectionTimeout(realConnectionTimeOut);
        ns.withResponseTimeout(realResponseTimeOut);
        ns.withEncryption(encr);
        if (!Utils.isEmpty(realPassword)) {
            ns.withPassword(realPassword);
        } else {
            ns.withNoPassword();
        }
        // target nagios host
        NagiosSettings settings = ns.create();
        // sender
        MessagePayloadBuilder pb = new MessagePayloadBuilder();
        if (!Utils.isEmpty(realSenderServerName)) {
            pb.withHostname(realSenderServerName);
        }
        pb.withLevel(level);
        if (!Utils.isEmpty(realSenderServiceName)) {
            pb.withServiceName(realSenderServiceName);
        }
        pb.withMessage(realMessageString);
        MessagePayload payload = pb.create();
        NagiosPassiveCheckSender sender = new NagiosPassiveCheckSender(settings);
        sender.send(payload);
        result.setNrErrors(0);
        result.setResult(true);
    } catch (Exception e) {
        log.logError(BaseMessages.getString(PKG, "JobEntrySendNagiosPassiveCheck.ErrorGetting", e.toString()));
    }
    return result;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) NagiosSettingsBuilder(com.googlecode.jsendnsca.builders.NagiosSettingsBuilder) NagiosPassiveCheckSender(com.googlecode.jsendnsca.NagiosPassiveCheckSender) Level(com.googlecode.jsendnsca.Level) Encryption(com.googlecode.jsendnsca.encryption.Encryption) NagiosSettings(com.googlecode.jsendnsca.NagiosSettings) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) Result(org.pentaho.di.core.Result) MessagePayloadBuilder(com.googlecode.jsendnsca.builders.MessagePayloadBuilder) MessagePayload(com.googlecode.jsendnsca.MessagePayload)

Example 2 with NagiosSettings

use of com.googlecode.jsendnsca.NagiosSettings in project LogHub by fbacchella.

the class Nsca method configure.

@Override
public boolean configure(Properties properties) {
    try {
        NagiosSettings settings = new NagiosSettings();
        if (port > 0) {
            settings.setPort(port);
        }
        if (nagiosServer != null) {
            settings.setNagiosHost(nagiosServer);
        }
        if (encryption != null) {
            settings.setEncryption(encryption);
        }
        if (password != null) {
            settings.setPassword(password);
        }
        if (connectTimeout >= 0) {
            settings.setConnectTimeout(connectTimeout);
        }
        if (timeout >= 0) {
            settings.setTimeout(timeout);
        }
        if (largeMessageSupport) {
            settings.enableLargeMessageSupport();
        }
        sender = new NagiosPassiveCheckSender(settings);
        // Uses a map to ensure that each field is tested, for easier debuging
        return MAPFIELD.enumerate().map(i -> {
            if (!mapping.containsKey(i)) {
                logger.error("NSCA mapping field '{}' missing", i);
                return false;
            } else {
                return true;
            }
        }).allMatch(i -> i) && super.configure(properties);
    } catch (IllegalArgumentException e) {
        logger.error("invalid NSCA configuration: {}", e.getMessage());
        return false;
    }
}
Also used : Arrays(java.util.Arrays) MessagePayload(com.googlecode.jsendnsca.MessagePayload) NagiosException(com.googlecode.jsendnsca.NagiosException) NagiosSettings(com.googlecode.jsendnsca.NagiosSettings) IOException(java.io.IOException) HashMap(java.util.HashMap) BlockingQueue(java.util.concurrent.BlockingQueue) NagiosPassiveCheckSender(com.googlecode.jsendnsca.NagiosPassiveCheckSender) Level(com.googlecode.jsendnsca.Level) Stream(java.util.stream.Stream) Encryption(com.googlecode.jsendnsca.encryption.Encryption) Sender(loghub.Sender) Map(java.util.Map) Event(loghub.Event) Properties(loghub.configuration.Properties) NagiosPassiveCheckSender(com.googlecode.jsendnsca.NagiosPassiveCheckSender) NagiosSettings(com.googlecode.jsendnsca.NagiosSettings)

Aggregations

Level (com.googlecode.jsendnsca.Level)2 MessagePayload (com.googlecode.jsendnsca.MessagePayload)2 NagiosPassiveCheckSender (com.googlecode.jsendnsca.NagiosPassiveCheckSender)2 NagiosSettings (com.googlecode.jsendnsca.NagiosSettings)2 Encryption (com.googlecode.jsendnsca.encryption.Encryption)2 NagiosException (com.googlecode.jsendnsca.NagiosException)1 MessagePayloadBuilder (com.googlecode.jsendnsca.builders.MessagePayloadBuilder)1 NagiosSettingsBuilder (com.googlecode.jsendnsca.builders.NagiosSettingsBuilder)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 Stream (java.util.stream.Stream)1 Event (loghub.Event)1 Sender (loghub.Sender)1 Properties (loghub.configuration.Properties)1 Result (org.pentaho.di.core.Result)1 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)1 KettleException (org.pentaho.di.core.exception.KettleException)1