/**
  * Gets java path via sigar from the current process.
  *
  * @return java path
  */
 private String getJavaPath() {
   final long pid = SigarHolder.getSigar().getPid();
   try {
     return SigarHolder.getSigar().getProcExe(pid).getName();
   } catch (final SigarException e) {
     throw new IllegalStateException(
         "Failed to read java path via sigar from current process (" + pid + ")", e);
   }
 }
Exemple #2
0
/**
 * *********** Monitor implementation that collects operating system metrics using SIGAR.
 *
 * @author barakme
 * @since 2.1.0
 */
public class ProcessMonitor implements Monitor {

  private final Sigar sigar = SigarHolder.getSigar();

  @Override
  public Map<String, Number> getMonitorValues(
      final UniversalServiceManagerBean usm, final ServiceConfiguration config)
      throws MonitorException {

    final List<Long> pids = usm.getServiceProcessesList();

    if (pids.isEmpty()) {
      return new HashMap<String, Number>();
    } else if (pids.size() == 1) {
      final MonitorData data = new MonitorData(sigar, pids.get(0));
      final Map<String, Number> map = data.getDataMap();
      return map;
    } else {
      // Collect data for all processes, add PID to key name, and return all in one map.
      final Map<String, Number> allProcessesMap = new HashMap<String, Number>();
      for (final Long pid : pids) {
        final MonitorData data = new MonitorData(sigar, pid);
        final Map<String, Number> map = data.getDataMap();
        final Set<Entry<String, Number>> entries = map.entrySet();
        final String postfix = "-" + pid;
        for (final Entry<String, Number> entry : entries) {
          allProcessesMap.put(entry.getKey() + postfix, entry.getValue());
        }
      }

      return allProcessesMap;
    }
  }
}
  /**
   * Gets the java home folder through the process ID of this process.
   *
   * @return The path to the java folder
   * @throws CLIException Reporting a failure to retrieve the java home directory
   */
  private String getJavaHomeDirectory() throws CLIException {
    try {
      final Sigar sigar = SigarHolder.getSigar();
      final long thisProcessPid = sigar.getPid();
      // get the java path of the current running process.
      final String javaFilePath = sigar.getProcExe(thisProcessPid).getName();
      final File javaFile = new File(javaFilePath);

      // Locate the java folder.
      final File javaFolder = javaFile.getParentFile().getParentFile();
      final String javaFolderPath = javaFolder.getAbsolutePath();
      return javaFolderPath;
    } catch (final SigarException e) {
      throw new CLIException("Failed to set the JAVA_HOME environment variable.", e);
    }
  }