public DiscoveredResourceDetails discoverResource(
      Configuration pluginConfig, ResourceDiscoveryContext resourceDiscoveryContext)
      throws InvalidPluginConfigurationException {
    ProcessInfo processInfo;
    try {
      processInfo =
          ProcessComponent.getProcessForConfiguration(
              pluginConfig, resourceDiscoveryContext.getSystemInformation());
    } catch (Exception e) {
      throw new RuntimeException(
          "Failed to manually add process Resource based on plugin config: "
              + pluginConfig.toString(true),
          e);
    }

    String type = pluginConfig.getSimpleValue("type", "pidFile");
    String resourceKey = pluginConfig.getSimpleValue(type, null);
    if (resourceKey == null || resourceKey.length() == 0) {
      throw new InvalidPluginConfigurationException(
          "Invalid type [" + type + "] value: [" + resourceKey + "]");
    }

    ResourceType resourceType = resourceDiscoveryContext.getResourceType();
    String resourceName = processInfo.getBaseName();
    String resourceVersion = null;
    String resourceDescription =
        processInfo.getBaseName()
            + " process with "
            + (type.equals("pidFile") ? "PID file" : "PIQL expression")
            + " ["
            + resourceKey
            + "]";

    DiscoveredResourceDetails detail =
        new DiscoveredResourceDetails(
            resourceType,
            resourceKey,
            resourceName,
            resourceVersion,
            resourceDescription,
            pluginConfig,
            processInfo);
    return detail;
  }
Exemplo n.º 2
0
  private void processArguments(AgentMain agent, String[] args) {
    PrintWriter out = agent.getOut();

    String sopts = "deop::sv";
    LongOpt[] lopts = {
      new LongOpt("disable", LongOpt.NO_ARGUMENT, null, 'd'),
      new LongOpt("enable", LongOpt.NO_ARGUMENT, null, 'e'),
      new LongOpt("os", LongOpt.NO_ARGUMENT, null, 'o'),
      new LongOpt("ps", LongOpt.OPTIONAL_ARGUMENT, null, 'p'),
      new LongOpt("shutdown", LongOpt.NO_ARGUMENT, null, 's'),
      new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'v')
    };

    Getopt getopt = new Getopt("native", args, sopts, lopts);
    int code;

    while ((code = getopt.getopt()) != -1) {
      switch (code) {
        case ':':
        case '?':
        case 1:
          {
            out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
            break;
          }

        case 'd':
          {
            SystemInfoFactory.disableNativeSystemInfo();
            out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_DISABLE_DONE));
            break;
          }

        case 'e':
          {
            SystemInfoFactory.enableNativeSystemInfo();
            out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_ENABLE_DONE));
            break;
          }

        case 'o':
          {
            SystemInfo sysInfo = SystemInfoFactory.createSystemInfo();

            // careful - I chose to only output things that I know the non-native Java sysinfo can
            // support
            out.println(
                MSG.getMsg(
                    AgentI18NResourceKeys.NATIVE_OS_OUTPUT,
                    sysInfo.getOperatingSystemName(),
                    sysInfo.getOperatingSystemVersion(),
                    sysInfo.getHostname()));

            break;
          }

        case 'p':
          {
            SystemInfo sysInfo = SystemInfoFactory.createSystemInfo();
            String verboseOpt = getopt.getOptarg();
            boolean verbose =
                (verboseOpt != null)
                    && verboseOpt.equals(MSG.getMsg(AgentI18NResourceKeys.NATIVE_VERBOSE));

            try {
              List<ProcessInfo> processes = sysInfo.getAllProcesses();
              if (verbose) {
                out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_PS_OUTPUT_VERBOSE_HEADER));
              } else {
                out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_PS_OUTPUT_SHORT_HEADER));
              }

              for (ProcessInfo p : processes) {
                if (verbose) {
                  out.println(
                      MSG.getMsg(
                          AgentI18NResourceKeys.NATIVE_PS_OUTPUT_VERBOSE,
                          p.getPid(),
                          p.getParentPid(),
                          p.getBaseName(),
                          Arrays.toString(p.getCommandLine())));
                } else {
                  out.println(
                      MSG.getMsg(
                          AgentI18NResourceKeys.NATIVE_PS_OUTPUT_SHORT, p.getPid(), p.getName()));
                }
              }
            } catch (Exception e) {
              out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_NOT_SUPPORTED));
            }

            break;
          }

        case 's':
          {
            if (!agent.isStarted()) {
              SystemInfoFactory.shutdown();
              SystemInfoFactory.disableNativeSystemInfo();
              out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_SHUTDOWN_DONE));
            } else {
              out.println(MSG.getMsg(AgentI18NResourceKeys.NATIVE_SHUTDOWN_FAILED_AGENT_STARTED));
            }

            break;
          }

        case 'v':
          {
            out.println(SystemInfoFactory.getNativeSystemInfoVersion());
            break;
          }
      }
    }

    if ((getopt.getOptind() + 1) < args.length) {
      out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
    }

    return;
  }