Search in sources :

Example 1 with OSProcess

use of com.creditease.agent.helpers.osproc.OSProcess in project uavstack by uavorg.

the class JVMContainerOSDetector method scanWithProcDetection.

/**
 * scanWithProcDetection
 */
@SuppressWarnings("unchecked")
private void scanWithProcDetection() {
    AgentFeatureComponent afc = (AgentFeatureComponent) this.getConfigManager().getComponent("procscan", "ProcDetectAgent");
    if (afc == null) {
        return;
    }
    Collection<OSProcess> procs = (Collection<OSProcess>) afc.exchange("procscan.query.allprocs");
    if (procs == null) {
        return;
    }
    if (log.isDebugEnable()) {
        log.debug(this, "ContainerOSDetector scan START...");
    }
    for (OSProcess proc : procs) {
        String name = proc.getName();
        /**
         * NOTE: support docker-proxy,docker,docker-current; support java http scanning for dev work more
         */
        if (name.indexOf("docker") == -1 && name.indexOf("java") == -1) {
            continue;
        }
        Map<String, String> tags = proc.getTags();
        String jargs = StringHelper.isEmpty(tags.get("jargs")) ? "" : tags.get("jargs");
        /**
         * NOTE: must install uavmof
         */
        if (!(jargs.contains("-javaagent:") && jargs.contains("monitorframework"))) {
            continue;
        }
        List<String> needDectectUrls = getDetectUrls(proc);
        for (String url : needDectectUrls) {
            client.doAsyncHttpGet(url + UAV_MOF_ROOT + "jvm?action=ping", new ScanPingCallback(proc, url));
        }
    }
}
Also used : Collection(java.util.Collection) AgentFeatureComponent(com.creditease.agent.spi.AgentFeatureComponent) OSProcess(com.creditease.agent.helpers.osproc.OSProcess)

Example 2 with OSProcess

use of com.creditease.agent.helpers.osproc.OSProcess in project uavstack by uavorg.

the class OSProcessScanner method collectProcState.

@SuppressWarnings("unchecked")
private void collectProcState(Map<String, OSProcess> procs) {
    if (procs == null || procs.isEmpty()) {
        return;
    }
    /**
     * detect if the process is watching
     */
    AgentFeatureComponent procwatch = (AgentFeatureComponent) this.getConfigManager().getComponent("procwatch", "ProcWatchAgent");
    Set<String> pids = new HashSet<>();
    for (String pid : procs.keySet()) {
        pids.add(pid);
        if (procwatch != null) {
            Object res = procwatch.exchange("agent.procwatch.iswatch", pid);
            if (res != null) {
                procs.get(pid).addTag("watch", String.valueOf(res));
            }
        }
    }
    /**
     * collect proc state metrics
     */
    Map<String, Map<String, String>> procStates = null;
    if (JVMToolHelper.isWindows()) {
        // do not include windows
        try {
            procStates = OSProcessHelper.getWinProcessInfo(shellPath, pids);
        } catch (Exception e) {
            log.err(this, "Collect Proc State Fail.", e);
            return;
        }
    } else {
        try {
            procStates = OSProcessHelper.getProcessInfo(shellPath, pids);
        } catch (Exception e) {
            log.err(this, "Collect Proc State Fail.", e);
            return;
        }
    }
    if (procStates == null) {
        return;
    }
    for (Map.Entry<String, Map<String, String>> en : procStates.entrySet()) {
        String pid = en.getKey();
        if (procs.containsKey(pid)) {
            OSProcess process = procs.get(pid);
            Map<String, String> states = en.getValue();
            for (Map.Entry<String, String> state : states.entrySet()) {
                process.addTag(state.getKey(), state.getValue());
            }
        }
    }
    /**
     * collect disk read/write speed for each process
     */
    pdioc.collect(procs);
    /**
     * collect in/out network stream for each process
     */
    if (null == portFlux) {
        return;
    }
    String networkDetectIntervalStr = this.getConfigManager().getFeatureConfiguration(this.feature, "networkDetect.interval");
    int networkDetectInterval = (StringHelper.isEmpty(networkDetectIntervalStr)) ? 60000 : Integer.parseInt(networkDetectIntervalStr);
    if (System.currentTimeMillis() - portFluxTimestamp > (2 * networkDetectInterval)) {
        if (log.isDebugEnable()) {
            log.debug(this, "portFlux is out of date ");
        }
        return;
    }
    Map<String, String> portFluxMap = null;
    try {
        portFluxMap = JSONHelper.toObject(portFlux, Map.class);
    } catch (Exception e) {
        log.err(this, "portFlux" + portFlux + " Json2Map error:\n " + e.toString());
        return;
    }
    if (null == portFluxMap) {
        return;
    }
    for (String pid : procs.keySet()) {
        OSProcess proc = procs.get(pid);
        float in_proc = 0;
        float out_proc = 0;
        for (String port : proc.getPorts()) {
            if (portFluxMap.containsKey("in_" + port)) {
                proc.addTag("in_" + port, portFluxMap.get("in_" + port));
                proc.addTag("out_" + port, portFluxMap.get("out_" + port));
                in_proc += Float.parseFloat(portFluxMap.get("in_" + port));
                out_proc += Float.parseFloat(portFluxMap.get("out_" + port));
            }
        }
        proc.addTag("in", String.valueOf(in_proc));
        proc.addTag("out", String.valueOf(out_proc));
    }
}
Also used : OSProcess(com.creditease.agent.helpers.osproc.OSProcess) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AgentFeatureComponent(com.creditease.agent.spi.AgentFeatureComponent) HashSet(java.util.HashSet)

Example 3 with OSProcess

use of com.creditease.agent.helpers.osproc.OSProcess in project uavstack by uavorg.

the class OSProcessScanner method scanLinuxProcesses.

/**
 * scan linux processes
 *
 * @param procs
 * @param portList
 * @param shellPath
 */
public void scanLinuxProcesses(Map<String, OSProcess> procs, StringBuffer portList, String shellPath) {
    try {
        String output = RuntimeHelper.exeShell("netstat -nlp -t", shellPath);
        if (log.isDebugEnable()) {
            log.debug(this, "Linux Process Scanning Shell Result: " + output);
        }
        if (!StringHelper.isEmpty(output)) {
            String[] lines = output.split("\n");
            int i = 0;
            for (; i < lines.length; i++) {
                String line = lines[i].trim();
                int tIndex = line.indexOf("tcp");
                if (tIndex != 0) {
                    continue;
                }
                OSProcess proc;
                line = line.replaceAll("\\s{2,}", " ");
                String[] fields = line.split(" ");
                // only need listening service port
                boolean findListenTag = false;
                int indexPidPro = -1;
                for (int fi = 0; fi < fields.length; fi++) {
                    if (fields[fi].indexOf("LISTEN") > -1) {
                        findListenTag = true;
                        indexPidPro = fi + 1;
                    }
                }
                if (findListenTag == false) {
                    continue;
                }
                // pid
                String pidPro = fields[indexPidPro];
                String[] pids = pidPro.split("/");
                String pid = pids[0];
                String proName = pids[pids.length - 1];
                if (!procs.containsKey(pid)) {
                    proc = new OSProcess();
                    proc.setPid(pid);
                    proc.setName(proName);
                    procs.put(pid, proc);
                } else {
                    proc = procs.get(pid);
                }
                // port
                String addr = fields[indexPidPro - 3];
                String[] addrLs = addr.split(":");
                String port = addrLs[addrLs.length - 1];
                proc.addPort(port);
                portList = portList.append(port).append(" ");
            }
        }
    } catch (Exception e) {
        log.err(this, "Linux Process Scanning Fail.", e);
    }
}
Also used : OSProcess(com.creditease.agent.helpers.osproc.OSProcess)

Example 4 with OSProcess

use of com.creditease.agent.helpers.osproc.OSProcess in project uavstack by uavorg.

the class OSProcessScanner method scanWindowsProcesses.

/**
 * scan windows processes
 *
 * @param portList
 */
public void scanWindowsProcesses(Map<String, OSProcess> procs, StringBuffer portList, String shellPath) {
    try {
        String output = RuntimeHelper.exeShell("netstat -anob -p TCP", shellPath);
        if (!StringHelper.isEmpty(output)) {
            String[] lines = output.split("\n");
            int i = 0;
            for (; i < lines.length; i++) {
                String line = lines[i].trim();
                int tIndex = line.indexOf("TCP");
                if (tIndex != 0) {
                    continue;
                }
                OSProcess proc;
                line = line.replaceAll("\\s{2,}", " ");
                String[] fields = line.split(" ");
                // only need listening service port
                if (!"listening".equalsIgnoreCase(fields[fields.length - 2])) {
                    continue;
                }
                // pid
                String pid = fields[fields.length - 1];
                if (!procs.containsKey(pid)) {
                    proc = new OSProcess();
                    proc.setPid(pid);
                    procs.put(pid, proc);
                } else {
                    proc = procs.get(pid);
                }
                // port
                String addr = fields[fields.length - 4];
                String port = addr.split(":")[1];
                proc.addPort(port);
                portList = portList.append(port).append(" ");
                if (!"UNKNOWN".equals(proc.getName())) {
                    continue;
                }
                // name
                String procNameLine = "";
                for (int j = i + 1; j < lines.length; j++) {
                    procNameLine = lines[j].trim();
                    if (procNameLine.indexOf("TCP") == 0) {
                        // i=j;
                        break;
                    }
                    if (procNameLine.indexOf("[") > -1) {
                        // i=j+1;
                        String procName = procNameLine.substring(1, procNameLine.length() - 1);
                        proc.setName(procName.trim());
                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        log.err(this, "Windows Process Scanning Fail.", e);
    }
}
Also used : OSProcess(com.creditease.agent.helpers.osproc.OSProcess)

Example 5 with OSProcess

use of com.creditease.agent.helpers.osproc.OSProcess in project uavstack by uavorg.

the class ResourceLimitationAuditor method check.

public void check(ResourceCheckRule... rules) {
    if (null == rules) {
        return;
    }
    AgentFeatureComponent afc = (AgentFeatureComponent) getConfigManager().getComponent("procscan", "ProcDetectAgent");
    if (afc != null) {
        String pid = JVMToolHelper.getCurrentProcId();
        OSProcess procState = (OSProcess) afc.exchange("procscan.query.procstate", pid);
        if (procState == null) {
            log.warn(this, "This component can't work as its dependent Feature[procscan] out of running");
            return;
        }
        for (ResourceCheckRule rt : rules) {
            AuditChecker ac = auditCheckerMap.get(rt.getResType());
            ac.setRule(rt);
            ac.setProcState(procState);
            try {
                boolean checkResult = ac.isOverLimit();
                if (checkResult) {
                    ac.doLimitation();
                }
            } catch (Exception e) {
                log.err(this, "RUN AuditChecker[" + ac.getResourceType() + "] FAIL:", e);
            }
        }
    }
}
Also used : OSProcess(com.creditease.agent.helpers.osproc.OSProcess)

Aggregations

OSProcess (com.creditease.agent.helpers.osproc.OSProcess)6 AgentFeatureComponent (com.creditease.agent.spi.AgentFeatureComponent)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1