<commit_before>/*

    Copyright 2016 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.github.terma.m.node;

import com.github.terma.m.shared.Event;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Jvm extends HostAwareChecker {

    private final Sigar sigar = new Sigar();
    private final Pattern processPattern;
    private final Pattern jmxPortPattern = Pattern.compile("-Dcom.sun.management.jmxremote.port=(\\d+)");

    public Jvm(String host, String processPattern) {
        super(host);
        this.processPattern = Pattern.compile(processPattern);
    }

    private Map<String, String> findJmxPorts() throws SigarException {
        Map<String, String> jmxPorts = new HashMap<String, String>();
        for (long pid : sigar.getProcList()) {
            String app = null;

            String[] args;
            try {
                args = sigar.getProcArgs(pid);
            } catch (SigarException e) {
                // nothing we don't have access to that PID
                continue;
            }

            for (String arg : args) {
                Matcher matcher = processPattern.matcher(arg);
                if (matcher.find()) {
                    app = matcher.group(1);
                    break;
                }
            }

            if (app != null) {
                for (String arg : args) {
                    Matcher matcher = jmxPortPattern.matcher(arg);
                    if (matcher.find()) {
                        jmxPorts.put(app, matcher.group(1));
                        break;
                    }
                }
            }
        }

        return jmxPorts;
    }

    public List<Event> get() throws Exception {
        List<Event> events = new ArrayList<Event>();

        for (Map.Entry<String, String> appAndJmxPort : findJmxPorts().entrySet()) {
            String host = "localhost";
            JMXServiceURL url = new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + appAndJmxPort.getValue() + "/jmxrmi");
            JMXConnector mConnector = JMXConnectorFactory.connect(url);<commit_msg>#1 show correct host for JVM metrics<commit_after>/*

    Copyright 2016 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.github.terma.m.node;

import com.github.terma.m.shared.Event;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Jvm extends HostAwareChecker {

    private final Sigar sigar = new Sigar();
    private final Pattern processPattern;
    private final Pattern jmxPortPattern = Pattern.compile("-Dcom.sun.management.jmxremote.port=(\\d+)");

    public Jvm(String host, String processPattern) {
        super(host);
        this.processPattern = Pattern.compile(processPattern);
    }

    private Map<String, String> findJmxPorts() throws SigarException {
        Map<String, String> jmxPorts = new HashMap<String, String>();
        for (long pid : sigar.getProcList()) {
            String app = null;

            String[] args;
            try {
                args = sigar.getProcArgs(pid);
            } catch (SigarException e) {
                // nothing we don't have access to that PID
                continue;
            }

            for (String arg : args) {
                Matcher matcher = processPattern.matcher(arg);
                if (matcher.find()) {
                    app = matcher.group(1);
                    break;
                }
            }

            if (app != null) {
                for (String arg : args) {
                    Matcher matcher = jmxPortPattern.matcher(arg);
                    if (matcher.find()) {
                        jmxPorts.put(app, matcher.group(1));
                        break;
                    }
                }
            }
        }

        return jmxPorts;
    }

    public List<Event> get() throws Exception {
        List<Event> events = new ArrayList<Event>();

        for (Map.Entry<String, String> appAndJmxPort : findJmxPorts().entrySet()) {
            JMXServiceURL url = new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + appAndJmxPort.getValue() + "/jmxrmi");
            JMXConnector mConnector = JMXConnectorFactory.connect(url);
{'fraction_non_alphanumeric': 0.09773324572930354, 'fraction_numerical': 0.0037779237844940868, 'mean_word_length': 2.3548209366391184, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '672301', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1636, 'n_words': 472}