public AS7CommandLine(ProcessInfo process) { this(process.getCommandLine()); this.process = process; }
/** * Returns a {@link ProcessInfo} instance for the parent of this process. * * This method uses the parent process id which is not static (it can change if the parent process dies before its * child). So in theory it should be moved to the {@link ProcessInfoSnapshot} type. * * In practice, it stays here because the parent {@link ProcessInfo} instance is cached after creation. * * @since 4.4 */ public ProcessInfo getParentProcess() throws SystemInfoException { if (this.parentProcess == null) { this.parentProcess = new ProcessInfo(priorSnaphot().getParentPid(), sigar); } else { this.parentProcess.refresh(); } return this.parentProcess; }
@Override public String toString() { StringBuilder s = new StringBuilder("process: "); s.append("pid=["); s.append(getPid()); s.append("], name=["); s.append((!getName().equals(UNKNOWN_PROCESS_NAME)) ? getName() : getBaseName()); s.append("], ppid=["); try { s.append(priorSnaphot().getParentPid()); } catch (Exception e) { s.append(e); } s.append("]"); return s.toString(); }
protected String getJavaVersion(ProcessInfo process, JMXServiceURL jmxServiceURL) { JMXConnector jmxConnector = null; try { jmxConnector = connect(jmxServiceURL); return getJavaVersion(jmxConnector); } catch (SecurityException e) { log.warn("Unable to to authenticate to JMX service URL [" + jmxServiceURL + "]: " + e.getMessage()); } catch (IOException e) { log.error("Failed to connect to JMX service URL [" + jmxServiceURL + "].", e); } catch (Exception e) { log.error("Failed to determine JVM version for process [" + process.getPid() + "] with command line [" + Arrays.asList(process.getCommandLine()) + "].", e); } finally { close(jmxConnector); } // TODO: We could exec "java -version" here. return null; }
/** * @deprecated as of 4.6. For similar purpose, call {@link #priorSnaphot()} and then corresponding method * from the returned {@link ProcessInfoSnapshot}. */ @Deprecated public ProcExe getExecutable() throws SystemInfoException { return priorSnaphot().getExecutable(); }
String[] serverParentProcessCommandLine = serverParentProcess.getCommandLine(); Integer startScriptIndex = getStartScriptIndex(serverParentProcessCommandLine); startScriptFile = new File(startScript); if (!startScriptFile.isAbsolute()) { ProcExe parentProcessExe = serverParentProcess.getExecutable(); if (parentProcessExe == null) {
operand1 = (processToMatch != null) ? processToMatch.getName() : ""; operand2 = criteria.getValue(); } else if (attribute.getAttributeValue().equals(Attribute.ProcessCategoryAttributes.basename.toString())) { operand1 = (processToMatch != null) ? processToMatch.getBaseName() : ""; operand2 = criteria.getValue(); } else if (attribute.getAttributeValue().equals(Attribute.ProcessCategoryAttributes.pid.toString())) { operand1 = (processToMatch != null) ? Long.toString(processToMatch.getPid()) : ""; operand2 = criteria.getValue(); } else if (attribute.getAttributeValue().equals(Attribute.ProcessCategoryAttributes.pidfile.toString())) { operand1 = (processToMatch != null) ? String.valueOf(processToMatch.getPid()) : null; operand2 = pidfileContentsCache; } else { matches.put(process.getPid(), process);
private static VirtualMachine attachToVirtualMachine(ProcessInfo process) throws AttachNotSupportedException, IOException { VirtualMachine vm = null; List<VirtualMachineDescriptor> vmDescriptors = VirtualMachine.list(); for (VirtualMachineDescriptor vmDescriptor : vmDescriptors) { if (Long.valueOf(vmDescriptor.id()) == process.getPid()) { String vmUserName = process.getCredentialsName().getUser(); String agentUserName = System.getProperty("user.name"); if (vmUserName.equals(agentUserName)) { LOG.debug("Attaching to JVM for java process with PID [" + process.getPid() + "]..."); vm = VirtualMachine.attach(vmDescriptor); LOG.debug("Attached to JVM [" + vm + "]."); } else { LOG.debug("Cannot attach to JVM for java process with PID [" + process.getPid() + "], because it is running as a different user (" + vmUserName + ") than the user the Agent is running as (" + agentUserName + ")."); } break; } } return vm; }
// caution: code is not tested but shows the general approach List<ProcessInformation> processInfo = new ArrayList<>(); String pInfoStr = new String[3]; int processInfoIndex = 0; String[] processInfoHolder = new String[3]; String processInfo = ""; int c; while( (c = reader.read()) != -1 ) { if (Character.isWhitespace(c)) { processInfoHolder[processInfoIndex++] = processInfo; processInfoStr = ""; } else if (c == 10) { // not sure if correct codepoint for whitespace processInfo.add(new ProcessInfo(processInfoHolder)); processInfoIndex = 0; } else { processInfoStr += c; } }
/** * Takes a fresh snapshot of non static properties of the underlying process. This method internally serializes * calls so that it maintains a consistent view of the various Sigar call results. * * @throws SystemInfoException */ public void refresh() throws SystemInfoException { // Serializing is also important as in somes cases, the process could be reported up while being down. // See this thread on VMWare forum: http://communities.vmware.com/message/2187972#2187972 boolean acquiredLock = false; try { acquiredLock = refreshLock.tryLock(REFRESH_LOCK_ACQUIRE_TIMEOUT_SECONDS, TimeUnit.SECONDS); } catch (InterruptedException e) { LOG.error("Thread interrupted while trying to acquire ProcessInfo[" + this.pid + "] refresh lock", e); } if (!acquiredLock) { throw new RuntimeException("Could not acquire ProcessInfo[" + this.pid + "] refresh lock"); } try { // No need to update if the process has already been reported down, Sigar will only throw exceptions... if (priorSnaphot().isRunning()) { update(this.pid); } } finally { refreshLock.unlock(); } }
if (sudo.canExecute() && (serverProcess.getCredentials() != null) && (thisProcess.getCredentials() != null)) { long processUid = serverProcess.getCredentials().getUid(); long processGid = serverProcess.getCredentials().getGid(); long agentProcessUid = thisProcess.getCredentials().getUid(); long agentProcessGid = thisProcess.getCredentials().getGid(); boolean sudoNeededForUser = (processUid != agentProcessUid); boolean sudoNeededForGroup = (processGid != agentProcessGid); if (sudoNeededForUser) { buffer.append(" -u "); if (serverProcess.getCredentialsName() != null) { buffer.append(serverProcess.getCredentialsName().getUser()); } else { buffer.append(serverProcess.getCredentials().getUid()); if (serverProcess.getCredentialsName() != null) { buffer.append(serverProcess.getCredentialsName().getGroup()); } else { buffer.append(serverProcess.getCredentials().getGid());
/** * Constructor for {@link ProcessInfoQuery} given an collection of process information that represents the processes * currently running. Think of the <code>processes</code> data as coming from part of the output you see in the * typical UNIX "ps" command. * * @param processes * * @see NativeSystemInfo#getAllProcesses() */ public ProcessInfoQuery(List<ProcessInfo> processes) { this.allProcesses = new HashMap<Long, ProcessInfo>(processes.size()); for (ProcessInfo process : processes) { this.allProcesses.put(process.getPid(), process); } }
/** * Similar to {@link #getName()}, this is a convenience method that returns the first command line argument, which * is the name of the program that the process is executing. However, this is only the relative filename of the * program, which does not include the full path to the program (e.g. this would return "sh" if the name of the * process is "/usr/bin/sh"). * * @return filename of program that is executing * * @see #getName() * @see #getCommandLine() */ public String getBaseName() { if (baseName == null) { baseName = (getName() != null) ? new File(getName()).getName() : UNKNOWN_PROCESS_NAME; } return baseName; }
/** * Retrieves a specific environment property if it exists, <code>null</code> otherwise. * * @param name the name of the property to find * * @return the environment value */ @Nullable public String getEnvironmentVariable(@NotNull String name) { if (this.procEnv == null) { return null; } SystemInfo systemInfo = SystemInfoFactory.createJavaSystemInfo(); boolean isWindows = systemInfo.getOperatingSystemType() == OperatingSystemType.WINDOWS; // Windows env names are case insensitive, so convert the specified name to all-caps before doing the lookup. return getEnvironmentVariables().get((isWindows) ? name.toUpperCase() : name); }
private File getAbsoluteFile(File file) { File absoluteFile; if (!file.isAbsolute()) { if ((this.process != null) && (this.process.getExecutable() != null)) { String cwd = this.process.getExecutable().getCwd(); absoluteFile = new File(cwd, file.getPath()); } else { String homeDir = getSystemProperties().get(HOME_DIR_SYSPROP); if (homeDir != null) { File binDir = new File(homeDir, "bin"); absoluteFile = new File(binDir, file.getPath()); } else { LOG.error("Failed to resolve relative properties file path [" + file + "]."); return null; } } } else { absoluteFile = file; } return absoluteFile; }
private JvmResourceKey buildResourceKey(ProcessInfo process, Integer jmxRemotingPort) { JvmResourceKey key; String mainClassName = getJavaMainClassName(process); String keyString = getSystemPropertyValue(process, SYSPROP_RHQ_RESOURCE_KEY); if (keyString != null && !keyString.equals("")) { log.debug("Using explicitly specified Resource key: [" + keyString + "]..."); key = JvmResourceKey.fromExplicitValue(mainClassName, keyString); } else { if (jmxRemotingPort != null) { log.debug("Using JMX remoting port [" + jmxRemotingPort + "] as Resource key..."); key = JvmResourceKey.fromJmxRemotingPort(mainClassName, jmxRemotingPort); } else { log.debug("Process [" + process.getPid() + "] with command line [" + Arrays.asList(process.getCommandLine()) + "] cannot be discovered, because it does not specify either of the following system properties: " + "-D" + SYSPROP_JMXREMOTE_PORT + "=12345, -D" + SYSPROP_RHQ_RESOURCE_KEY + "=UNIQUE_KEY"); key = null; } } return key; }
/** * @deprecated as of 4.6. For similar purpose, call {@link #priorSnaphot()} and then corresponding method * from the returned {@link ProcessInfoSnapshot}. */ @Deprecated public ProcState getState() throws SystemInfoException { return priorSnaphot().getState(); }
operand1 = (processToMatch != null) ? processToMatch.getName() : ""; operand2 = criteria.getValue(); } else if (attribute.getAttributeValue().equals(Attribute.ProcessCategoryAttributes.basename.toString())) { operand1 = (processToMatch != null) ? processToMatch.getBaseName() : ""; operand2 = criteria.getValue(); } else if (attribute.getAttributeValue().equals(Attribute.ProcessCategoryAttributes.pid.toString())) { operand1 = (processToMatch != null) ? Long.toString(processToMatch.getPid()) : ""; operand2 = criteria.getValue(); } else if (attribute.getAttributeValue().equals(Attribute.ProcessCategoryAttributes.pidfile.toString())) { operand1 = (processToMatch != null) ? String.valueOf(processToMatch.getPid()) : null; operand2 = pidfileContentsCache; } else { matches.put(process.getPid(), process);
public ProcessInfo getThisProcess() { long self = sigar.getPid(); ProcessInfo info = new ProcessInfo(self); return info; }
/** * Takes a fresh snapshot of non static properties of the underlying process. This method internally serializes * calls so that it maintains a consistent view of the various Sigar call results. * * @throws SystemInfoException */ public void refresh() throws SystemInfoException { // Serializing is also important as in somes cases, the process could be reported up while being down. // See this thread on VMWare forum: http://communities.vmware.com/message/2187972#2187972 boolean acquiredLock = false; try { acquiredLock = refreshLock.tryLock(REFRESH_LOCK_ACQUIRE_TIMEOUT_SECONDS, TimeUnit.SECONDS); } catch (InterruptedException e) { LOG.error("Thread interrupted while trying to acquire ProcessInfo[" + this.pid + "] refresh lock", e); } if (!acquiredLock) { throw new RuntimeException("Could not acquire ProcessInfo[" + this.pid + "] refresh lock"); } try { // No need to update if the process has already been reported down, Sigar will only throw exceptions... if (priorSnaphot().isRunning()) { update(this.pid); } } finally { refreshLock.unlock(); } }