Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
using commons-cli to handle argument parsing duties. Adding --help
adding the ability to change the host and port of the the management api to collect from
fixing a windows superuser error and http auth error in the eap sos plugin
fixing NPE in standalone mode
adding os x support and prompts for username and password in external mode
  • Loading branch information
jhjaggars authored and bstansberry committed Dec 8, 2011
1 parent 1db2e44 commit 44ada82
Show file tree
Hide file tree
Showing 16 changed files with 474 additions and 321 deletions.
4 changes: 4 additions & 0 deletions build/build.xml
Expand Up @@ -441,6 +441,10 @@
<maven-resource group="commons-beanutils" artifact="commons-beanutils"/>
</module-def>

<module-def name="org.apache.commons.cli">
<maven-resource group="commons-cli" artifact="commons-cli"/>
</module-def>

<module-def name="org.apache.commons.codec">
<maven-resource group="commons-codec" artifact="commons-codec"/>
</module-def>
Expand Down
6 changes: 5 additions & 1 deletion build/pom.xml
Expand Up @@ -164,7 +164,6 @@
</activation>

<dependencies>

<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
Expand Down Expand Up @@ -215,6 +214,11 @@
<artifactId>commons-beanutils</artifactId>
</dependency>

<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
Expand Down
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ JBoss, Home of Professional Open Source.
~ Copyright 2011, Red Hat, Inc., and individual contributors
~ as indicated by the @author tags. See the copyright.txt file in the
~ distribution for a full listing of individual contributors.
~
~ This is free software; you can redistribute it and/or modify it
~ under the terms of the GNU Lesser General Public License as
~ published by the Free Software Foundation; either version 2.1 of
~ the License, or (at your option) any later version.
~
~ This software is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
~ Lesser General Public License for more details.
~
~ You should have received a copy of the GNU Lesser General Public
~ License along with this software; if not, write to the Free
~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->

<module xmlns="urn:jboss:module:1.1" name="org.apache.commons.cli">
<resources>
<!-- Insert resources here -->
</resources>

<dependencies>
</dependencies>
</module>
Expand Up @@ -31,6 +31,7 @@

<dependencies>
<module name="javax.api"/>
<module name="org.apache.commons.cli"/>
<module name="org.jboss.staxmapper"/>
<module name="org.jboss.as.controller"/>
<module name="org.jboss.as.server"/>
Expand Down
5 changes: 5 additions & 0 deletions jdr/jboss-as-jdr/pom.xml
Expand Up @@ -39,6 +39,11 @@
<name>JBoss Application Server: JDR</name>

<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.jboss.msc</groupId>
<artifactId>jboss-msc</artifactId>
Expand Down
Expand Up @@ -22,27 +22,68 @@

package org.jboss.as.jdr;

import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.OperationFailedException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.HelpFormatter;

/**
* Provides a main for collecting a JDR report from the command line.
*
* @author Mike M. Clark
* @author Jesse Jaggars
*/
public class CommandLineMain {

private static CommandLineParser parser = new GnuParser();
private static Options options = new Options();
private static HelpFormatter formatter = new HelpFormatter();
private static final String usage = "jdr.{sh,bat} [options]";

static {
options.addOption("H", "help", false, "prints help and exits");
options.addOption("h", "host", true, "hostname that the management api is bound to. (default: localhost)");
options.addOption("p", "port", true, "port that the management api is bound to. (default: 9990)");
}

/**
* Creates a JBoss Diagnostic Reporter (JDR) Report. A JDR report response
* is printed to <code>System.out</code>.
*
* @param args ignored
*/
public static void main(String[] args) {
String port = "9990";
String host = "localhost";

try {
CommandLine line = parser.parse(options, args);

if (line.hasOption("help")) {
formatter.printHelp(usage, options);
return;
}
if (line.hasOption("host")) {
host = line.getOptionValue("host");
}

if (line.hasOption("port")) {
port = line.getOptionValue("port");
}
} catch (Exception e) {
formatter.printHelp(usage, options);
return;
}

System.out.println("Initializing JBoss Diagnostic Reporter...");

JdrReportService reportService = new JdrReportService();

JdrReport response = null;
try {
response = reportService.standaloneCollect();
response = reportService.standaloneCollect(host, port);
} catch (OperationFailedException e) {
System.out.println("Failed to complete the JDR report: " + e.getMessage());
}
Expand Down
Expand Up @@ -39,10 +39,13 @@
import org.jboss.msc.value.InjectedValue;
import org.jboss.threads.JBossThreadFactory;

import java.io.Console;
import java.security.AccessController;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.net.URL;
import java.net.HttpURLConnection;

/**
* Service that provides a {@link JdrReportCollector}.
Expand Down Expand Up @@ -75,9 +78,41 @@ public static ServiceController<JdrReportCollector> addService(final ServiceTarg
/**
* Collect a JDR report when run outside the Application Server.
*/
public JdrReport standaloneCollect() throws OperationFailedException {
public JdrReport standaloneCollect(String host, String port) throws OperationFailedException {
Console cons = System.console();
String username = null;
String password = null;

if (host == null) {
host = "localhost";
}
if (port == null) {
port = "9990";
}

// Let's go ahead and see if we need to auth before prompting the user
// for a username and password
boolean must_auth = false;

try {
URL managementApi = new URL("http://" + host + ":" + port + "/management");
HttpURLConnection conn = (HttpURLConnection) managementApi.openConnection();
int code = conn.getResponseCode();
if (code != 200) {
must_auth = true;
}
}
catch(Exception e) {
}

if (must_auth) {
if (cons != null) {
username = cons.readLine("Management username: ");
password = String.valueOf(cons.readPassword("Management password: "));
}
}
SosInterpreter interpreter = new SosInterpreter();
return interpreter.collect();
return interpreter.collect(username, password, host, port);
}

/**
Expand Down
Expand Up @@ -55,6 +55,20 @@ public SosInterpreter() {
}

public JdrReport collect() throws OperationFailedException {
return collect(null, null, null, null);
}

/**
* Sets up a String global variable for sosreport
*/
private void setSosVariable(PythonInterpreter interpreter, String name, String value) {
if (value != null) {
interpreter.set(name, value);
interpreter.exec("sos." + name + " = " + name);
}
}

public JdrReport collect(String username, String password, String host, String port) throws OperationFailedException {
ROOT_LOGGER.startingCollection();
Date startTime = new Date();

Expand All @@ -76,19 +90,24 @@ public JdrReport collect() throws OperationFailedException {
ROOT_LOGGER.debug("locationDir = " + locationDir);
ROOT_LOGGER.debug("homeDir = " + SosInterpreter.cleanPath(homeDir));

PyObject report = null;
PyObject report = new PyObject();
try {
interpreter.exec("sys.path.append(\"" + pyLocation + "\")");
interpreter.exec("import sos");

// If we have a controller client, use it to
// get runtime information.
if (controllerClient != null) {
interpreter.exec("import sos");
interpreter.set("controller_client_proxy",
new ModelControllerClientProxy(controllerClient));
interpreter.exec("sos.controllerClient = controller_client_proxy");
}

setSosVariable(interpreter, "as7_user", username);
setSosVariable(interpreter, "as7_pass", password);
setSosVariable(interpreter, "as7_host", host);
setSosVariable(interpreter, "as7_port", port);

interpreter.exec("from sos.sosreport import main");
interpreter.exec("args = shlex.split('-k eap6.home=\"" + homeDir + "\" --tmp-dir=\"" + locationDir + "\" -o eap6 --batch --report --compression-type=zip --silent')");
interpreter.exec("reportLocation = main(args)");
Expand Down

0 comments on commit 44ada82

Please sign in to comment.