Search in sources :

Example 1 with InvalidSnmpVersion

use of fish.payara.notification.snmp.exception.InvalidSnmpVersion in project Payara by payara.

the class SnmpNotifierService method bootstrap.

@Override
public void bootstrap() {
    register(NotifierType.SNMP, SnmpNotifier.class, SnmpNotifierConfiguration.class, this);
    execOptions = (SnmpNotifierConfigurationExecutionOptions) getNotifierConfigurationExecutionOptions();
    if (execOptions != null && execOptions.isEnabled()) {
        try {
            TransportMapping transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);
            CommunityTarget cTarget = new CommunityTarget();
            cTarget.setCommunity(new OctetString(execOptions.getCommunity()));
            int snmpVersion = decideOnSnmpVersion(execOptions.getVersion());
            cTarget.setVersion(snmpVersion);
            cTarget.setAddress(new UdpAddress(execOptions.getHost() + ADDRESS_SEPARATOR + execOptions.getPort()));
            initializeExecutor();
            scheduledFuture = scheduleExecutor(new SnmpNotificationRunnable(queue, execOptions, snmp, cTarget, snmpVersion));
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Error occurred while creating UDP transport", e);
        } catch (InvalidSnmpVersion invalidSnmpVersion) {
            logger.log(Level.SEVERE, "Error occurred while configuring SNMP version: " + invalidSnmpVersion.getMessage());
        }
    }
}
Also used : InvalidSnmpVersion(fish.payara.notification.snmp.exception.InvalidSnmpVersion) TransportMapping(org.snmp4j.TransportMapping) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) IOException(java.io.IOException) CommunityTarget(org.snmp4j.CommunityTarget)

Example 2 with InvalidSnmpVersion

use of fish.payara.notification.snmp.exception.InvalidSnmpVersion in project Payara by payara.

the class TestSnmpNotifier method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport actionReport = context.getActionReport();
    Config config = targetUtil.getConfig(target);
    if (config == null) {
        context.getActionReport().setMessage("No such config named: " + target);
        context.getActionReport().setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    SnmpNotifierConfiguration snmpConfig = config.getExtensionByType(SnmpNotifierConfiguration.class);
    if (community == null) {
        community = snmpConfig.getCommunity();
    }
    if (oid == null) {
        oid = snmpConfig.getOid();
    }
    if (version == null) {
        version = snmpConfig.getVersion();
    }
    if (hostName == null) {
        hostName = snmpConfig.getHost();
    }
    if (port == null) {
        port = snmpConfig.hashCode();
    }
    // prepare SNMP message
    SnmpNotificationEvent event = factory.buildNotificationEvent(SUBJECT, MESSAGE);
    SnmpMessageQueue queue = new SnmpMessageQueue();
    queue.addMessage(new SnmpMessage(event, event.getSubject(), event.getMessage()));
    SnmpNotifierConfigurationExecutionOptions options = new SnmpNotifierConfigurationExecutionOptions();
    options.setCommunity(community);
    options.setOid(oid);
    options.setVersion(version);
    options.setHost(hostName);
    options.setPort(port);
    SnmpNotificationRunnable notifierRun = null;
    try {
        TransportMapping transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        CommunityTarget cTarget = new CommunityTarget();
        cTarget.setCommunity(new OctetString(options.getCommunity()));
        int snmpVersion = SnmpNotifierService.decideOnSnmpVersion(options.getVersion());
        cTarget.setVersion(snmpVersion);
        cTarget.setAddress(new UdpAddress(options.getHost() + SnmpNotifierService.ADDRESS_SEPARATOR + options.getPort()));
        notifierRun = new SnmpNotificationRunnable(queue, options, snmp, cTarget, snmpVersion);
    } catch (IOException e) {
        Logger.getLogger(TestSnmpNotifier.class.getCanonicalName()).log(Level.SEVERE, "Error occurred while creating UDP transport", e);
        actionReport.setMessage("Error occurred while creating UDP transport");
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    } catch (InvalidSnmpVersion invalidSnmpVersion) {
        Logger.getLogger(TestSnmpNotifier.class.getCanonicalName()).log(Level.SEVERE, "Error occurred while configuring SNMP version: " + invalidSnmpVersion.getMessage());
        actionReport.setMessage("Error occurred while configuring SNMP version: " + invalidSnmpVersion.getMessage());
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    }
    // set up logger to store result
    Logger logger = Logger.getLogger(SnmpNotificationRunnable.class.getCanonicalName());
    BlockingQueueHandler bqh = new BlockingQueueHandler(10);
    bqh.setLevel(Level.FINE);
    Level oldLevel = logger.getLevel();
    logger.setLevel(Level.FINE);
    logger.addHandler(bqh);
    // send message, this occurs in its own thread
    Thread notifierThread = new Thread(notifierRun, "test-snmp-notifier-thread");
    notifierThread.start();
    try {
        notifierThread.join();
    } catch (InterruptedException ex) {
        Logger.getLogger(TestSnmpNotifier.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        logger.setLevel(oldLevel);
    }
    LogRecord message = bqh.poll();
    bqh.clear();
    if (message == null) {
        // something's gone wrong
        Logger.getLogger(TestSnmpNotifier.class.getName()).log(Level.SEVERE, "Failed to send SNMP message");
        actionReport.setMessage("Failed to send SNMP message");
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    } else {
        actionReport.setMessage(message.getMessage());
        if (message.getLevel() == Level.FINE) {
            actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        } else {
            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        }
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Config(com.sun.enterprise.config.serverbeans.Config) TransportMapping(org.snmp4j.TransportMapping) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) RestEndpoint(org.glassfish.api.admin.RestEndpoint) InvalidSnmpVersion(fish.payara.notification.snmp.exception.InvalidSnmpVersion) BlockingQueueHandler(fish.payara.nucleus.notification.BlockingQueueHandler) LogRecord(java.util.logging.LogRecord) Snmp(org.snmp4j.Snmp) Level(java.util.logging.Level) CommunityTarget(org.snmp4j.CommunityTarget)

Aggregations

InvalidSnmpVersion (fish.payara.notification.snmp.exception.InvalidSnmpVersion)2 IOException (java.io.IOException)2 CommunityTarget (org.snmp4j.CommunityTarget)2 Snmp (org.snmp4j.Snmp)2 TransportMapping (org.snmp4j.TransportMapping)2 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)2 Config (com.sun.enterprise.config.serverbeans.Config)1 BlockingQueueHandler (fish.payara.nucleus.notification.BlockingQueueHandler)1 Level (java.util.logging.Level)1 LogRecord (java.util.logging.LogRecord)1 Logger (java.util.logging.Logger)1 ActionReport (org.glassfish.api.ActionReport)1 RestEndpoint (org.glassfish.api.admin.RestEndpoint)1 OctetString (org.snmp4j.smi.OctetString)1 UdpAddress (org.snmp4j.smi.UdpAddress)1