Search in sources :

Example 31 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project OpenAM by OpenRock.

the class Agent method main.

/**
     * Main entry point.
     * When calling the program, you can specify:
     *  1) nb_traps: number of traps the SNMP agent will send.
     * If not specified, the agent will send traps continuously.
     */
public static void main(String[] args) {
    final MBeanServer server;
    final ObjectName htmlObjName;
    final ObjectName snmpObjName;
    final ObjectName sunMibObjName;
    final ObjectName forgerockCtsMibObjName;
    final ObjectName forgerockPolicyMibObjName;
    final ObjectName forgerockSessionMibObjName;
    final ObjectName trapGeneratorObjName;
    int htmlPort = 8082;
    int snmpPort = 11161;
    //
    if ((args.length != 0) && (args.length != 1)) {
        usage();
        java.lang.System.exit(1);
    } else if (args.length == 1) {
        try {
            nbTraps = (new Integer(args[0])).intValue();
            if (nbTraps < 0) {
                usage();
                System.exit(1);
            }
        } catch (java.lang.NumberFormatException e) {
            usage();
            System.exit(1);
        }
    }
    try {
        List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
        if ((servers != null) && !servers.isEmpty()) {
            server = servers.get(0);
        } else {
            server = MBeanServerFactory.createMBeanServer();
        }
        String domain = server.getDefaultDomain();
        // Create and start the HTML adaptor.
        //
        htmlObjName = new ObjectName(domain + ":class=HtmlAdaptorServer,protocol=html,port=" + htmlPort);
        println("Adding HTML adaptor to MBean server with name \n    " + htmlObjName);
        println("NOTE: HTML adaptor is bound on TCP port " + htmlPort);
        HtmlAdaptorServer htmlAdaptor = new HtmlAdaptorServer(htmlPort);
        server.registerMBean(htmlAdaptor, htmlObjName);
        htmlAdaptor.start();
        //
        // SNMP specific code:
        //
        // Create and start the SNMP adaptor.
        // Specify the port to use in the constructor. 
        // If you want to use the standard port (161) comment out the 
        // following line:
        //   snmpPort = 8085;
        //
        snmpPort = 11161;
        snmpObjName = new ObjectName(domain + ":class=SnmpAdaptorServer,protocol=snmp,port=" + snmpPort);
        println("Adding SNMP adaptor to MBean server with name \n    " + snmpObjName);
        println("NOTE: SNMP Adaptor is bound on UDP port " + snmpPort);
        snmpAdaptor = new SnmpAdaptorServer(snmpPort);
        server.registerMBean(snmpAdaptor, snmpObjName);
        snmpAdaptor.start();
        // Send a coldStart SNMP Trap. 
        // Use port = snmpPort+1.
        //
        print("NOTE: Sending a coldStart SNMP trap" + " to each destination defined in the ACL file...");
        snmpAdaptor.setTrapPort(new Integer(snmpPort + 1));
        snmpAdaptor.snmpV1Trap(0, 0, null);
        println("Done.");
        // Create an RMI connector and start it
        try {
            JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
            JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
            cs.start();
        } catch (Exception ex) {
            println("Error starting RMI : execute rmiregistry 9999; ex=" + ex);
        }
        // Create the MIB II (RFC 1213) and add it to the MBean server.
        //
        sunMibObjName = new ObjectName("snmp:class=SUN_OPENSSO_SERVER_MIB");
        println("Adding SUN_OPENSSO_SERVER_MIB-MIB to MBean server with name" + "\n    " + sunMibObjName);
        // Create an instance of the customized MIB
        //
        SUN_OPENSSO_SERVER_MIB mib2 = new SUN_OPENSSO_SERVER_MIB();
        server.registerMBean(mib2, sunMibObjName);
        forgerockCtsMibObjName = new ObjectName("snmp:class=FORGEROCK_OPENAM_CTS_MIB");
        println("Adding FORGEROCK_OPENAM_CTS_MIB-MIB to MBean server with name" + "\n    " + forgerockCtsMibObjName);
        forgerockPolicyMibObjName = new ObjectName("snmp:class=FORGEROCK_OPENAM_POLICY_MIB");
        println("Adding FORGEROCK_OPENAM_POLICY_MIB-MIB to MBean server with name" + "\n    " + forgerockPolicyMibObjName);
        forgerockSessionMibObjName = new ObjectName("snmp:class=FORGEROCK_OPENAM_SESSION_MIB");
        println("Adding FORGEROCK_OPENAM_SESSION_MIB-MIB to MBean server with name" + "\n    " + forgerockSessionMibObjName);
        FORGEROCK_OPENAM_CTS_MIB mib3 = new FORGEROCK_OPENAM_CTS_MIB();
        server.registerMBean(mib3, forgerockCtsMibObjName);
        FORGEROCK_OPENAM_POLICY_MIB mib4 = new FORGEROCK_OPENAM_POLICY_MIB();
        server.registerMBean(mib4, forgerockPolicyMibObjName);
        FORGEROCK_OPENAM_SESSION_MIB mib5 = new FORGEROCK_OPENAM_SESSION_MIB();
        server.registerMBean(mib5, forgerockSessionMibObjName);
        // Bind the SNMP adaptor to the MIB in order to make the MIB 
        // accessible through the SNMP protocol adaptor.
        // If this step is not performed, the MIB will still live in 
        // the Java DMK agent:
        // its objects will be addressable through HTML but not SNMP.
        //
        mib2.setSnmpAdaptor(snmpAdaptor);
        // Create a LinkTrapGenerator.
        // Specify the ifIndex to use in the object name.
        //
        int ifIndex = 1;
        trapGeneratorObjName = new ObjectName("trapGenerator" + ":class=LinkTrapGenerator,ifIndex=" + ifIndex);
        println("Adding LinkTrapGenerator to MBean server with name" + "\n    " + trapGeneratorObjName);
        LinkTrapGenerator trapGenerator = new LinkTrapGenerator(nbTraps);
        server.registerMBean(trapGenerator, trapGeneratorObjName);
        println("\n>> Press <Enter> if you want to start sending traps.");
        println("   -or-");
        println(">> Press <Ctrl-C> if you want to stop this agent.");
        System.in.read();
        trapGenerator.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MalformedObjectNameException(javax.management.MalformedObjectNameException) JMException(javax.management.JMException) RuntimeOperationsException(javax.management.RuntimeOperationsException) SnmpStatusException(com.sun.management.snmp.SnmpStatusException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) JMRuntimeException(javax.management.JMRuntimeException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) FORGEROCK_OPENAM_SESSION_MIB(org.forgerock.openam.monitoring.session.FORGEROCK_OPENAM_SESSION_MIB) SnmpAdaptorServer(com.sun.management.comm.SnmpAdaptorServer) FORGEROCK_OPENAM_CTS_MIB(org.forgerock.openam.monitoring.cts.FORGEROCK_OPENAM_CTS_MIB) FORGEROCK_OPENAM_POLICY_MIB(org.forgerock.openam.monitoring.policy.FORGEROCK_OPENAM_POLICY_MIB) HtmlAdaptorServer(com.sun.jdmk.comm.HtmlAdaptorServer) MBeanServer(javax.management.MBeanServer)

Example 32 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.

the class Agent method startLocalManagementAgent.

// jcmd ManagementAgent.start_local entry point
// Also called due to command-line via startAgent()
private static synchronized void startLocalManagementAgent() {
    Properties agentProps = VMSupport.getAgentProperties();
    // start local connector if not started
    if (agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP) == null) {
        JMXConnectorServer cs = ConnectorBootstrap.startLocalConnectorServer();
        String address = cs.getAddress().toString();
        // Add the local connector address to the agent properties
        agentProps.put(LOCAL_CONNECTOR_ADDRESS_PROP, address);
        try {
            // export the address to the instrumentation buffer
            ConnectorAddressLink.export(address);
        } catch (Exception x) {
            // Connector server started but unable to export address
            // to instrumentation buffer - non-fatal error.
            warning(EXPORT_ADDRESS_FAILED, x.getMessage());
        }
    }
}
Also used : Properties(java.util.Properties) JdpException(sun.management.jdp.JdpException) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 33 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.

the class ConnectorBootstrap method exportMBeanServer.

private static JMXConnectorServerData exportMBeanServer(MBeanServer mbs, int port, int rmiPort, boolean useSsl, boolean useRegistrySsl, String sslConfigFileName, String[] enabledCipherSuites, String[] enabledProtocols, boolean sslNeedClientAuth, boolean useAuthentication, String loginConfigName, String passwordFileName, String accessFileName, String bindAddress) throws IOException, MalformedURLException {
    /* Make sure we use non-guessable RMI object IDs.  Otherwise
         * attackers could hijack open connections by guessing their
         * IDs.  */
    System.setProperty("java.rmi.server.randomIDs", "true");
    JMXServiceURL url = new JMXServiceURL("rmi", bindAddress, rmiPort);
    Map<String, Object> env = new HashMap<>();
    PermanentExporter exporter = new PermanentExporter();
    env.put(RMIExporter.EXPORTER_ATTRIBUTE, exporter);
    env.put(EnvHelp.CREDENTIAL_TYPES, new String[] { String[].class.getName(), String.class.getName() });
    boolean useSocketFactory = bindAddress != null && !useSsl;
    if (useAuthentication) {
        if (loginConfigName != null) {
            env.put("jmx.remote.x.login.config", loginConfigName);
        }
        if (passwordFileName != null) {
            env.put("jmx.remote.x.password.file", passwordFileName);
        }
        env.put("jmx.remote.x.access.file", accessFileName);
        if (env.get("jmx.remote.x.password.file") != null || env.get("jmx.remote.x.login.config") != null) {
            env.put(JMXConnectorServer.AUTHENTICATOR, new AccessFileCheckerAuthenticator(env));
        }
    }
    RMIClientSocketFactory csf = null;
    RMIServerSocketFactory ssf = null;
    if (useSsl || useRegistrySsl) {
        csf = new SslRMIClientSocketFactory();
        ssf = createSslRMIServerSocketFactory(sslConfigFileName, enabledCipherSuites, enabledProtocols, sslNeedClientAuth, bindAddress);
    }
    if (useSsl) {
        env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf);
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);
    }
    if (useSocketFactory) {
        ssf = new HostAwareSocketFactory(bindAddress);
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);
    }
    JMXConnectorServer connServer = null;
    try {
        connServer = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        connServer.start();
    } catch (IOException e) {
        if (connServer == null || connServer.getAddress() == null) {
            throw new AgentConfigurationError(CONNECTOR_SERVER_IO_ERROR, e, url.toString());
        } else {
            throw new AgentConfigurationError(CONNECTOR_SERVER_IO_ERROR, e, connServer.getAddress().toString());
        }
    }
    if (useRegistrySsl) {
        registry = new SingleEntryRegistry(port, csf, ssf, "jmxrmi", exporter.firstExported);
    } else if (useSocketFactory) {
        registry = new SingleEntryRegistry(port, csf, ssf, "jmxrmi", exporter.firstExported);
    } else {
        registry = new SingleEntryRegistry(port, "jmxrmi", exporter.firstExported);
    }
    int registryPort = ((UnicastRef) ((RemoteObject) registry).getRef()).getLiveRef().getPort();
    String jmxUrlStr = String.format("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", url.getHost(), registryPort);
    JMXServiceURL remoteURL = new JMXServiceURL(jmxUrlStr);
    return new JMXConnectorServerData(connServer, remoteURL);
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) IOException(java.io.IOException) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory) JMXConnectorServer(javax.management.remote.JMXConnectorServer) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) RemoteObject(java.rmi.server.RemoteObject) SslRMIServerSocketFactory(javax.rmi.ssl.SslRMIServerSocketFactory) RMIServerSocketFactory(java.rmi.server.RMIServerSocketFactory) AgentConfigurationError(sun.management.AgentConfigurationError) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) RemoteObject(java.rmi.server.RemoteObject)

Example 34 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.

the class ConnectorBootstrap method startRemoteConnectorServer.

/**
     * Initializes and starts a JMX Connector Server for remote
     * monitoring and management.
     **/
public static synchronized JMXConnectorServer startRemoteConnectorServer(String portStr, Properties props) {
    // Get port number
    final int port;
    try {
        port = Integer.parseInt(portStr);
    } catch (NumberFormatException x) {
        throw new AgentConfigurationError(INVALID_JMXREMOTE_PORT, x, portStr);
    }
    if (port < 0) {
        throw new AgentConfigurationError(INVALID_JMXREMOTE_PORT, portStr);
    }
    // User can specify a port to be used to export rmi object,
    // in order to simplify firewall rules
    // if port is not specified random one will be allocated.
    int rmiPort = 0;
    String rmiPortStr = props.getProperty(PropertyNames.RMI_PORT);
    try {
        if (rmiPortStr != null) {
            rmiPort = Integer.parseInt(rmiPortStr);
        }
    } catch (NumberFormatException x) {
        throw new AgentConfigurationError(INVALID_JMXREMOTE_RMI_PORT, x, rmiPortStr);
    }
    if (rmiPort < 0) {
        throw new AgentConfigurationError(INVALID_JMXREMOTE_RMI_PORT, rmiPortStr);
    }
    // Do we use authentication?
    final String useAuthenticationStr = props.getProperty(PropertyNames.USE_AUTHENTICATION, DefaultValues.USE_AUTHENTICATION);
    final boolean useAuthentication = Boolean.valueOf(useAuthenticationStr).booleanValue();
    // Do we use SSL?
    final String useSslStr = props.getProperty(PropertyNames.USE_SSL, DefaultValues.USE_SSL);
    final boolean useSsl = Boolean.valueOf(useSslStr).booleanValue();
    // Do we use RMI Registry SSL?
    final String useRegistrySslStr = props.getProperty(PropertyNames.USE_REGISTRY_SSL, DefaultValues.USE_REGISTRY_SSL);
    final boolean useRegistrySsl = Boolean.valueOf(useRegistrySslStr).booleanValue();
    final String enabledCipherSuites = props.getProperty(PropertyNames.SSL_ENABLED_CIPHER_SUITES);
    String[] enabledCipherSuitesList = null;
    if (enabledCipherSuites != null) {
        StringTokenizer st = new StringTokenizer(enabledCipherSuites, ",");
        int tokens = st.countTokens();
        enabledCipherSuitesList = new String[tokens];
        for (int i = 0; i < tokens; i++) {
            enabledCipherSuitesList[i] = st.nextToken();
        }
    }
    final String enabledProtocols = props.getProperty(PropertyNames.SSL_ENABLED_PROTOCOLS);
    String[] enabledProtocolsList = null;
    if (enabledProtocols != null) {
        StringTokenizer st = new StringTokenizer(enabledProtocols, ",");
        int tokens = st.countTokens();
        enabledProtocolsList = new String[tokens];
        for (int i = 0; i < tokens; i++) {
            enabledProtocolsList[i] = st.nextToken();
        }
    }
    final String sslNeedClientAuthStr = props.getProperty(PropertyNames.SSL_NEED_CLIENT_AUTH, DefaultValues.SSL_NEED_CLIENT_AUTH);
    final boolean sslNeedClientAuth = Boolean.valueOf(sslNeedClientAuthStr).booleanValue();
    // Read SSL config file name
    final String sslConfigFileName = props.getProperty(PropertyNames.SSL_CONFIG_FILE_NAME);
    String loginConfigName = null;
    String passwordFileName = null;
    String accessFileName = null;
    // Initialize settings when authentication is active
    if (useAuthentication) {
        // Get non-default login configuration
        loginConfigName = props.getProperty(PropertyNames.LOGIN_CONFIG_NAME);
        if (loginConfigName == null) {
            // Get password file
            passwordFileName = props.getProperty(PropertyNames.PASSWORD_FILE_NAME, getDefaultFileName(DefaultValues.PASSWORD_FILE_NAME));
            checkPasswordFile(passwordFileName);
        }
        // Get access file
        accessFileName = props.getProperty(PropertyNames.ACCESS_FILE_NAME, getDefaultFileName(DefaultValues.ACCESS_FILE_NAME));
        checkAccessFile(accessFileName);
    }
    final String bindAddress = props.getProperty(PropertyNames.HOST);
    if (log.debugOn()) {
        log.debug("startRemoteConnectorServer", Agent.getText("jmxremote.ConnectorBootstrap.starting") + "\n\t" + PropertyNames.PORT + "=" + port + (bindAddress == null ? "" : "\n\t" + PropertyNames.HOST + "=" + bindAddress) + "\n\t" + PropertyNames.RMI_PORT + "=" + rmiPort + "\n\t" + PropertyNames.USE_SSL + "=" + useSsl + "\n\t" + PropertyNames.USE_REGISTRY_SSL + "=" + useRegistrySsl + "\n\t" + PropertyNames.SSL_CONFIG_FILE_NAME + "=" + sslConfigFileName + "\n\t" + PropertyNames.SSL_ENABLED_CIPHER_SUITES + "=" + enabledCipherSuites + "\n\t" + PropertyNames.SSL_ENABLED_PROTOCOLS + "=" + enabledProtocols + "\n\t" + PropertyNames.SSL_NEED_CLIENT_AUTH + "=" + sslNeedClientAuth + "\n\t" + PropertyNames.USE_AUTHENTICATION + "=" + useAuthentication + (useAuthentication ? (loginConfigName == null ? ("\n\t" + PropertyNames.PASSWORD_FILE_NAME + "=" + passwordFileName) : ("\n\t" + PropertyNames.LOGIN_CONFIG_NAME + "=" + loginConfigName)) : "\n\t" + Agent.getText("jmxremote.ConnectorBootstrap.noAuthentication")) + (useAuthentication ? ("\n\t" + PropertyNames.ACCESS_FILE_NAME + "=" + accessFileName) : "") + "");
    }
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    JMXConnectorServer cs = null;
    JMXServiceURL url = null;
    try {
        final JMXConnectorServerData data = exportMBeanServer(mbs, port, rmiPort, useSsl, useRegistrySsl, sslConfigFileName, enabledCipherSuitesList, enabledProtocolsList, sslNeedClientAuth, useAuthentication, loginConfigName, passwordFileName, accessFileName, bindAddress);
        cs = data.jmxConnectorServer;
        url = data.jmxRemoteURL;
        log.config("startRemoteConnectorServer", Agent.getText("jmxremote.ConnectorBootstrap.ready", url.toString()));
    } catch (Exception e) {
        throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.toString());
    }
    try {
        // Export remote connector address and associated configuration
        // properties to the instrumentation buffer.
        Map<String, String> properties = new HashMap<>();
        properties.put("remoteAddress", url.toString());
        properties.put("authenticate", useAuthenticationStr);
        properties.put("ssl", useSslStr);
        properties.put("sslRegistry", useRegistrySslStr);
        properties.put("sslNeedClientAuth", sslNeedClientAuthStr);
        ConnectorAddressLink.exportRemote(properties);
    } catch (Exception e) {
        // Remote connector server started but unable to export remote
        // connector address and associated configuration properties to
        // the instrumentation buffer - non-fatal error.
        log.debug("startRemoteConnectorServer", e);
    }
    return cs;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) RemoteException(java.rmi.RemoteException) NoSuchObjectException(java.rmi.NoSuchObjectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) StringTokenizer(java.util.StringTokenizer) AgentConfigurationError(sun.management.AgentConfigurationError) MBeanServer(javax.management.MBeanServer)

Example 35 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.

the class Server method start.

public static String start() throws Exception {
    int serverPort = 12345;
    ObjectName name = new ObjectName("test", "foo", "bar");
    MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
    SteMBean bean = new Ste();
    jmxServer.registerMBean(bean, name);
    boolean exported = false;
    Random rnd = new Random(System.currentTimeMillis());
    do {
        try {
            LocateRegistry.createRegistry(serverPort);
            exported = true;
        } catch (ExportException ee) {
            if (ee.getCause() instanceof BindException) {
                serverPort = rnd.nextInt(10000) + 4096;
            } else {
                throw ee;
            }
        }
    } while (!exported);
    JMXServiceURL serverUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + serverPort + "/test");
    JMXConnectorServer jmxConnector = JMXConnectorServerFactory.newJMXConnectorServer(serverUrl, null, jmxServer);
    jmxConnector.start();
    return serverUrl.toString();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) Random(java.util.Random) BindException(java.net.BindException) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer) ExportException(java.rmi.server.ExportException) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Aggregations

JMXConnectorServer (javax.management.remote.JMXConnectorServer)63 JMXServiceURL (javax.management.remote.JMXServiceURL)57 JMXConnector (javax.management.remote.JMXConnector)47 MBeanServer (javax.management.MBeanServer)44 MBeanServerConnection (javax.management.MBeanServerConnection)38 ObjectName (javax.management.ObjectName)31 HashMap (java.util.HashMap)24 IOException (java.io.IOException)13 MalformedURLException (java.net.MalformedURLException)13 RemoteException (java.rmi.RemoteException)13 NotificationListener (javax.management.NotificationListener)12 Notification (javax.management.Notification)11 Attribute (javax.management.Attribute)9 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7 Map (java.util.Map)7 Properties (java.util.Properties)7 JMXPluggableAuthenticator (com.sun.jmx.remote.security.JMXPluggableAuthenticator)5 UnknownHostException (java.net.UnknownHostException)5 Test (org.junit.Test)5